Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix-section06 #10

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/gohandson/gacha-ja

go 1.18
10 changes: 8 additions & 2 deletions skeleton/section06/step01/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strconv"

"github.com/gohandson/gacha-ja/gacha"
"github.com/gohandson/gacha-ja/skeleton/section04/step02/gacha"
"github.com/gohandson/gacha-ja/skeleton/section05/step01/gacha"
)

var (
Expand All @@ -32,13 +34,15 @@ func run() error {

tickets, err := initialTickets()
// TODO: エラーが発生した場合はエラーをそのまま返す

if err != nil {
return err
}
p := gacha.NewPlayer(tickets, flagCoin)

n := inputN(p)
// TODO: gacha.DrawN関数を呼び出す
// 戻り値はresults, summary, errに代入する

results, summary, err := gacha.DrawN(p, n)
if err != nil {
return err
}
Expand All @@ -58,6 +62,7 @@ func initialTickets() (int, error) {
if flag.NArg() == 0 {
// TODO: 0とエラーを返す
// エラーは"ガチャチケットの枚数を入力してください"
return 0, errors.New("ガチャチケットの枚数を入力してください")
}

num, err := strconv.Atoi(flag.Arg(0))
Expand All @@ -66,6 +71,7 @@ func initialTickets() (int, error) {
}

// TODO: numとエラーがないことを表すnilを返す
return num, nil
}

func inputN(p *gacha.Player) int {
Expand Down
2 changes: 2 additions & 0 deletions skeleton/section06/step02/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,14 @@ func saveResults(results []*gacha.Card) (rerr error) {
f, err := os.Create("results.txt")
if err != nil {
// TODO: エラーを"result.txtの作成:"という文字列を付加してラップして返す
return fmt.Errorf("result.txt作成: %w", err)
}

defer func() {
if err := f.Close(); err != nil && rerr == nil {
// TODO: 関数saveResultsの戻り値になるようにエラーをrerrに代入する
// エラーは"result.txtのクローズ:"という文字列を付加してラップして返す
rerr = fmt.Errorf("result.txtのクローズ: %w", err)
}
}()

Expand Down
6 changes: 4 additions & 2 deletions skeleton/section06/step03/gacha/gacha.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ func (p *Play) Err() error {

func (p *Play) Draw() bool {
// TODO: エラーがすでに発生した場合はfalseを返す

if p.err != nil {
return false
}
if err := p.player.draw(1); err != nil {
// TODO: エラーをフィールドに代入する

p.err = err
return false
}

Expand Down
5 changes: 3 additions & 2 deletions skeleton/section06/step04/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
var (
regexpResults = regexp.MustCompile(`^results.*\.txt$`)
// TODO: `^summary.*\.txt$`を正規表現としてコンパイルし変数regexpSummaryに代入
regexpSummary = regexp.MustCompile(`^summary.*\.txt$`)
)

func init() {
Expand All @@ -39,8 +40,8 @@ func main() {

func run() error {
flag.Parse()

if /* 結果ファイルの名前が妥当か正規表現を使って調べる */ {
/* 結果ファイルの名前が妥当か正規表現を使って調べる */
if !regexpResults.MatchString(flagResults) {
return fmt.Errorf("結果ファイル名が不正(%s)", flagResults)
}

Expand Down