Skip to content

Kadai3 1 gosagawa #52

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

Open
wants to merge 4 commits into
base: master
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
2 changes: 2 additions & 0 deletions kadai3-1/gosagawa/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
!.gitkeep
bin/
13 changes: 13 additions & 0 deletions kadai3-1/gosagawa/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# kadai3-1

##タイピングゲームを作ろう
- 標準出力に英単語を出す(出すものは自由)
- 標準入力から1行受け取る
- 制限時間内に何問解けたか表示する

##usage

```
go run main.go
```

8 changes: 8 additions & 0 deletions kadai3-1/gosagawa/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import "github.com/gopherdojo/dojo3/kadai3-1/gosagawa/typingGame"

func main() {

typingGame.Start()
}
109 changes: 109 additions & 0 deletions kadai3-1/gosagawa/typingGame/typingGame.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package typingGame

import (
"bufio"
"fmt"
"io"
"math/rand"
"os"
"time"
)

var questions []string

const playTime int = 10

func init() {
questions = getQuestions()
}

func Start() {
var num int
var endGame bool
inputCh := input(os.Stdin)

startDisplay()
<-inputCh

timeCh := time.After(time.Duration(playTime) * time.Second)

for {
word := chooseWord()
displayWord(word)

select {
case input := <-inputCh:
isCorrect := check(word, input)
displayResult(isCorrect)
if isCorrect {
num++
}

case <-timeCh:
endDisplay(num)
endGame = true
}

if endGame {
break
}
}
}

func getQuestions() []string {
var List = []string{"white", "yellow", "orange", "red", "pink", "purple", "blue", "green", "brown", "grey", "black"}
return List
}

func input(r io.Reader) <-chan string {
ch := make(chan string)
go func() {
s := bufio.NewScanner(r)
for s.Scan() {
ch <- s.Text()
}
close(ch)
}()
return ch
}

func startDisplay() {
fmt.Println("Please type same words as much as possible ...")
fmt.Println("Hit any key to start..")
fmt.Println("")

}
func chooseWord() string {
rand.Seed(time.Now().UnixNano())
q := questions[rand.Intn(len(questions))]
return q

return "test"
}

func displayWord(word string) {
fmt.Println(word)
fmt.Printf(">")
}

func check(word string, input string) bool {
if word == input {
return true
}
return false
}

func displayResult(isCorrect bool) {
if isCorrect {
fmt.Println("Correct!")
} else {
fmt.Println("Miss...")
}
fmt.Println("")
}

func endDisplay(correctNumber int) {
fmt.Println("")
fmt.Println("Time's up !!!")
fmt.Printf("Your score is %v\n", correctNumber)
}