-
Notifications
You must be signed in to change notification settings - Fork 7
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
Kadai3-1: nagaa052 #25
base: master
Are you sure you want to change the base?
Conversation
flag.Usage = usage | ||
flag.Parse() | ||
|
||
g, err := game.New(game.Options{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
基本ポインタにしておく。
}, os.Stdin, os.Stdout, os.Stderr) | ||
|
||
if err != nil { | ||
log.Fatal("Failed to start the game") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
log.Fatalは使わない
) | ||
|
||
const ( | ||
ExitOK = iota |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
プログラムの外部に出る情報にはiota
を使わない。
例:DBに保存されるもの、ネットワークを介して送られるもの、コマンドライツールの出力になるもの
// Game is manages game information | ||
type Game struct { | ||
qs *questions.Questions | ||
*Result |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
埋め込む必要性は?
// DefaultOptions is the default value of Options. | ||
var DefaultOptions = Options{ | ||
TimeUpSecond: 30, | ||
IsColor: false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
false
なら別に初期化する必要ないのでは?
// Options is a specifiable option. | ||
type Options struct { | ||
TimeUpSecond int | ||
IsColor bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
?
|
||
// Options is a specifiable option. | ||
type Options struct { | ||
TimeUpSecond int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
time.Duration
を使う
} | ||
|
||
// Print is Output the result. | ||
func (r *Result) Print(out io.Writer) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
io.Writer
の変数はw
と書くことが多い
|
||
return &Game{ | ||
qs: qs, | ||
Result: &Result{}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
埋め込むならポインタじゃなくてもいい。そしたらゼロ値で使える。
scanner := bufio.NewScanner(g.inStream) | ||
defer close(dst) | ||
|
||
for scanner.Scan() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
エラー処理
|
||
func (g *Game) getQuestion() (*questions.Question, error) { | ||
|
||
rand.Seed(time.Now().UnixNano()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
呼び出しの度にSeedをセットしない
func (g *Game) getQuestion() (*questions.Question, error) { | ||
|
||
rand.Seed(time.Now().UnixNano()) | ||
index := rand.Intn(g.qs.GetSize()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同じ問題が出る
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これはt.Paralell
しない?
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
inStream := &bytes.Buffer{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ゼロ値でよい
_, err := game.New(tt.opt, inStream, outStream, errStream) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr) | ||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
returnする必要性ある?
got, err := game.ExportGetQuestion(g) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Game.getQuestion() error = %v, wantErr %v", err, tt.wantErr) | ||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
returnするならt.Fatalf
を使う
|
||
type inMemQ struct{} | ||
|
||
var words = []string{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
外部ファイルからもらうようにした方がよいのでは?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あ、なるほど、テスト用の実装?
want *qs.Questions | ||
wantErr bool | ||
}{ | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
テストケースが少なすぎる
want bool | ||
}{ | ||
{ | ||
name: "Is correct test", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
もっと名前をシンプルにして、パット見で分かるようにする。
タイピングゲーム
要件
コマンド/オプション
パッケージ構成
main
: 起動/コマンドオプションpkg/game
: タイピングゲームのメインロジックpkg/questions
: 問題の管理/取得やってみたこと
できなかったこと