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 shuheiktgw #48

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions kadai3-1/shuheiktgw/typing/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
typing
11 changes: 11 additions & 0 deletions kadai3-1/shuheiktgw/typing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Gopher道場 課題3-1
====

Gopher道場 課題3-1,タイピングゲームです。

## Usage

```
go build
./typing
```
64 changes: 64 additions & 0 deletions kadai3-1/shuheiktgw/typing/game.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

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

const (
ExitCodeOK = iota
)

var questions = []string{"Golang", "Ruby", "Scala", "Java", "PHP", "JavaScript", "C", "C++"}

func init() {
rand.Seed(time.Now().UnixNano())
}

func Run() int {
score := 0
done := time.After(5 * time.Second)

for flag := true; flag; {
result := make(chan bool)
go run(result)

select {
case <-done:
flag = false
case r := <-result:
if r {
score++
}
}
}

fmt.Println("Time up!")
fmt.Printf("Your score is %d\n", score)

return ExitCodeOK
}

func run(channel chan<- bool) {
defer close(channel)

q := questions[rand.Intn(7)]
fmt.Printf("Type it: %s\n", q)

in := bufio.NewScanner(os.Stdin)
in.Scan()
a := in.Text()

isCorrect := q == a

if isCorrect {
fmt.Println("Correct")
} else {
fmt.Println("Wrong")
}

channel <- isCorrect
}
7 changes: 7 additions & 0 deletions kadai3-1/shuheiktgw/typing/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "os"

func main() {
os.Exit(Run())
}