diff --git a/kadai3/marltake/data/words.txt b/kadai3/marltake/data/words.txt new file mode 100644 index 0000000..7e3b1b3 --- /dev/null +++ b/kadai3/marltake/data/words.txt @@ -0,0 +1,425 @@ +Access +Agreement +Animation +Argument +Ascending +August +Bitmap +Button +Calendar +Candidate +Charlie +CheckBox +Clicked +Clicking +Clipboard +Column +Columns +ComboBox +Command +Community +Concept +Connection +Control +Current +DELETE +DataGrid +DataRow +DataSet +DataSource +DataTable +Database +DateTime +December +Default +Descending +Dialog +Document +February +Fields +Findobject +Friday +Fucking +General +Google +GroupBox +HAVING +INSERT +IPAddress +Identifier +ImageList +Initial +Intensity +January +Kubernetes +Laughing +LinkLabel +ListBox +ListView +Literal +Manual +Memory +MenuStrip +Mercurial +Monday +NotifyIcon +November +October +OnClick +Original +Parameter +PictureBox +Preview +Primitive +Program +Progress +Random +Reference +Regular +Release +Report +Resistance +Result +Return +Rolling +SELECT +Saturday +September +Service +Snapshot +Software +Splitter +Standard +String +Sunday +Symbol +TabControl +TabPage +Technology +TextBox +Thursday +ToolStrip +ToolTip +TrackBar +TreeView +Tuesday +UPDATE +Ubuntu +UpDown +VALUES +Vector +WebBrowser +Wednesday +ability +abnormal +accept +access +acronym +action +activate +address +adjust +allocate +amount +ampersand +ancestors +apostrophe +append +applicable +argument +asterisk +attach +attribute +available +average +awesome +background +backslash +backup +backward +bacronym +before +behavior +binary +boolean +bottom +bounds +brackets +calculate +canClose +canSave +canary +canceledus +caption +category +caution +center +change +channel +chapter +character +cheatsheet +children +classify +client +collapse +collect +comment +commit +common +compare +complement +complete +compute +config +configure +connect +connection +contact +contains +contents +controller +convert +correct +country +create +crypto +current +customer +darkness +deactivate +decimal +declare +decrypt +define +degrade +delegate +delete +delimiter +dependency +dequeue +descriptor +destroy +detach +diagram +difficulty +directory +disable +disconnect +dispatch +diversity +document +dollar +double +download +dynamic +ecosystem +electric +element +employee +employer +enable +encrypt +engineer +enqueue +enroll +equals +evaluate +exception +exclude +execute +existence +expand +export +expression +extension +failure +figure +finish +folder +footer +foreground +formula +forward +fractional +frequency +function +gender +general +generate +global +greater +growth +handle +hasChanged +hasSaved +header +height +hidden +hogehoge +hogera +identifier +identify +identity +ignore +immutable +impedance +import +indices +individual +initialize +insert +inside +instance +integer +invalid +invoke +irregular +isEmpty +isNull +isOpened +isSmall +isValid +iterator +length +letter +listen +manager +maximum +memory +middle +minimum +minute +modified +narrow +natural +navigation +nightly +normal +normalize +notation +notice +number +numeric +object +offset +operator +optimize +ordinal +output +overlapped +overload +override +paragraph +parameter +parent +patient +percent +period +permit +personal +picture +player +pointer +position +postfix +preference +prefix +prepend +previous +private +problem +procedure +process +property +provider +public +quantity +question +quotation +radius +random +reality +receive +recommend +recover +refresh +refuse +register +regression +regular +relation +relative +release +remark +remote +remove +repair +replace +repository +request +required +reserved +resistance +respond +response +restore +resume +retrieve +return +revised +search +second +section +selector +semicolon +sentence +separator +sequential +serialize +server +service +setting +signature +silence +single +something +source +special +square +stable +standard +statement +static +status +string +subject +subsection +subtitle +success +suffix +supplement +suspend +syntax +target +temporary +terminate +thickness +timestamp +toggle +underbar +underline +underscore +unknown +unsigned +update +upgrade +upload +validate +verbose +verify +version +vertical +visible +warning +within \ No newline at end of file diff --git a/kadai3/marltake/docs/typing_game.md b/kadai3/marltake/docs/typing_game.md new file mode 100644 index 0000000..82040fc --- /dev/null +++ b/kadai3/marltake/docs/typing_game.md @@ -0,0 +1,11 @@ +# 課題3-1 タイピングゲーム +## ルール +* 標準出力に英単語を出す(出すものは自由) +* 標準入力から1行受け取る +* 制限時間内に何問解けたか表示する + +## ヒント +* 制限時間にはtime.After関数を用いる + * context.WithTimeoutでもよい +* select構文を用いる + * 制限時間と入力を同時に待つ diff --git a/kadai3/marltake/go.mod b/kadai3/marltake/go.mod new file mode 100644 index 0000000..f3e50eb --- /dev/null +++ b/kadai3/marltake/go.mod @@ -0,0 +1,3 @@ +module marltake + +go 1.12 diff --git a/kadai3/marltake/main.go b/kadai3/marltake/main.go new file mode 100644 index 0000000..61b9a11 --- /dev/null +++ b/kadai3/marltake/main.go @@ -0,0 +1,13 @@ +package main + +import ( + "fmt" + "marltake/typing" + "time" +) + +func main() { + gameTime := 30 + passCount, totalCount := typing.Game(time.Duration(gameTime) * time.Second) + fmt.Printf("\nPass / Total = %d / %d\n", passCount, totalCount) +} diff --git a/kadai3/marltake/typing/game.go b/kadai3/marltake/typing/game.go new file mode 100644 index 0000000..cc45e32 --- /dev/null +++ b/kadai3/marltake/typing/game.go @@ -0,0 +1,65 @@ +package typing + +import ( + "bufio" + "fmt" + "log" + "math/rand" + "os" + "time" +) + +// Game is main control flow of typing game +func Game(gameTime time.Duration) (pass, total int) { + pass, total = 0, 0 + timeout := time.After(gameTime) + rand.Seed(time.Now().UnixNano()) + words := loadFile("data/words.txt") + fetchInput := make(chan string) + go func() { + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + fetchInput <- scanner.Text() + } + if err := scanner.Err(); err != nil { + log.Fatal("Error while reading answer") + } + }() + fmt.Println("GAME starts. Type a word as shown.") +GAMEMAIN: + for { + goodWord := words[rand.Intn(len(words))] + fmt.Printf("\n%s\n", goodWord) + select { + case typedWord := <-fetchInput: + total++ + if typedWord == goodWord { + pass++ + fmt.Println("OK") + } else { + fmt.Println("Booo") + } + case <-timeout: + break GAMEMAIN + } + } + return +} + +func loadFile(filepath string) []string { + file, err := os.Open(filepath) + if err != nil { + log.Fatal("No training word to load.") + } + defer file.Close() + scanner := bufio.NewScanner(file) + words := []string{} + for scanner.Scan() { + words = append(words, scanner.Text()) + } + if err := scanner.Err(); err != nil { + log.Fatal("Error while loading words.txt") + } + return words + +}