-
Notifications
You must be signed in to change notification settings - Fork 2
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 yashiken #25
base: master
Are you sure you want to change the base?
Conversation
@@ -0,0 +1,23 @@ | |||
# 課題1 回答 |
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.
課題ごとに分けてほしいですー
"github.com/gopherdojo/dojo6/kadai3-1/yashiken/typegame" | ||
) | ||
|
||
var t 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.
パッケージ変数は短くしない。
1文字にするのはブロックスコープの変数(引数やレシーバも含む)とunexportedなフィールドくらい。
func main() { | ||
var ( | ||
tm = time.After(time.Duration(t) * time.Minute) | ||
score = 0 |
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.
ゼロ値がすでに0
なのであえて初期化は不要
// 単語リストの取得 | ||
words, err := typegame.Words(s) | ||
if err != nil { | ||
fmt.Println(err) |
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.Stderrに
// 単語リストの要素をランダムに入れ替え | ||
words = typegame.Shuffle(words) | ||
|
||
fmt.Printf("タイピングゲームを始めます。制限時間は%d分です。\n", 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.
出力先が固定
words = typegame.Shuffle(words) | ||
|
||
fmt.Printf("タイピングゲームを始めます。制限時間は%d分です。\n", t) | ||
for i := true; i && score < len(words); { |
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.
i
は通常添字(int
型の)に使うのでbool型の値に使わない。
フラグを使わずにbreak
すればよい。
// 標準入力の内容を受け取ったあとチャネルに格納して返します。 | ||
func Input(r io.Reader) <-chan string { | ||
ch := make(chan string) | ||
go func() { |
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.
タイムアウトしたときにこのゴールーチンが終了しない
// shuffleはスライスの要素をランダムに入れ替え、 | ||
// 要素を入れ替えたスライスを返します。 | ||
func Shuffle(s []string) []string { | ||
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.
乱数の種の初期化は1回で十分
|
||
// shuffleはスライスの要素をランダムに入れ替え、 | ||
// 要素を入れ替えたスライスを返します。 | ||
func Shuffle(s []string) []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.
rand.Permで良さそう
input string | ||
output 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.
ケースが不十分。
}) | ||
} | ||
} | ||
func TestShuffle(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.
これはテストの必要性があるのか?rand.Perm使えば別にこの関数ごといらなさそう。
success bool | ||
) | ||
// ランダムにスライスの要素を入れ替えるため、十分な回数shuffleを実行 | ||
for i := 1; i < 1000; i++ { |
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.
1000回やれば十分という根拠がない。
) | ||
// ランダムにスライスの要素を入れ替えるため、十分な回数shuffleを実行 | ||
for i := 1; i < 1000; i++ { | ||
if result = typegame.Shuffle(c.input); reflect.DeepEqual(result, c.output) { |
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.Run(c.name, func(t *testing.T) { | ||
path := filepath.Join("testdata/" + c.file) | ||
sl, err := typegame.Words(path) | ||
if !reflect.DeepEqual(sl, c.output) && err != c.expectErr { |
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.
slとc.outputの比較とエラーの比較がなぜ&&で繋げられてるんでしょうか?
エラーは値で比較しない。
課題3−1
タイピングゲームをつくる
TODO
Usage
感想