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

Kadai3-1 yashiken #25

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open

Kadai3-1 yashiken #25

wants to merge 23 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Aug 6, 2019

課題3−1

タイピングゲームをつくる

TODO

  • 標準出力に英単語を出力する
  • 標準入力から1行受け取る
  • 制限時間内に何問解けたか表示する
  • テストを書く

Usage

$ go build -o typinggame
$ ./typinggame

感想

  • goroutineを用いたデータのやりとりのイメージを持つことができた。
  • 単語リストの取得をもう少しいい感じにできないか(Wikipediaのページから単語リストを取得し、表示など)と考えたが、タイムアップしてしまい残念。
  • テーブル駆動テストでerrorsパッケージを使うとエラーを生成できて便利だと感じた。

@ghost ghost added the kadai3-1 label Aug 6, 2019
@@ -0,0 +1,23 @@
# 課題1 回答
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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)
Copy link
Member

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)
Copy link
Member

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); {
Copy link
Member

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() {
Copy link
Member

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())
Copy link
Member

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 {
Copy link
Member

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
}{
{
Copy link
Member

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) {
Copy link
Member

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++ {
Copy link
Member

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) {
Copy link
Member

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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slとc.outputの比較とエラーの比較がなぜ&&で繋げられてるんでしょうか?
エラーは値で比較しない。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants