forked from gophercises/quiz
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added siredmar's solution (gophercises#48)
- Loading branch information
1 parent
2a121d4
commit 314ad91
Showing
2 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"encoding/csv" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"strings" | ||
"time" | ||
) | ||
|
||
var ( | ||
file = flag.String("csv", "problems.csv", "a csv file in the format 'question,answer'") | ||
limit = flag.Int("limit", 30, "the time limit for the quiz in seconds") | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
if *limit < 0 { | ||
fmt.Println("Error: enter positive time limit") | ||
os.Exit(1) | ||
} | ||
|
||
records, err := read(*file) | ||
if err != nil { | ||
fmt.Println("Error: ", err) | ||
os.Exit(1) | ||
} | ||
|
||
fmt.Println("Press enter to start. Time limit is", *limit, "seconds") | ||
in := bufio.NewReader(os.Stdin) | ||
in.ReadString('\n') | ||
|
||
input := make(chan string) | ||
|
||
quizdone := make(chan bool) | ||
timeout := time.NewTicker(time.Duration(*limit) * time.Second) | ||
|
||
go getInput(input) | ||
|
||
var score int | ||
var maxscore int | ||
|
||
go func() { | ||
maxscore = len(records) | ||
for i, v := range records { | ||
fmt.Print("Problem #", i+1, ": ", v[0], " = ") | ||
text := <-input | ||
if text == v[1] { | ||
score++ | ||
} | ||
} | ||
quizdone <- true | ||
}() | ||
|
||
select { | ||
case <-quizdone: | ||
case <-timeout.C: | ||
fmt.Println("\nThe time is up! Game over!") | ||
|
||
} | ||
|
||
fmt.Println("You scored", score, "out of", maxscore) | ||
} | ||
|
||
func read(filename string) ([][]string, error) { | ||
dat, err := ioutil.ReadFile(filename) | ||
r := csv.NewReader(strings.NewReader(string(dat))) | ||
if err == nil { | ||
var records [][]string | ||
for { | ||
record, err := r.Read() | ||
if err == io.EOF { | ||
break | ||
} | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
records = append(records, record) | ||
} | ||
return records, nil | ||
} | ||
return nil, err | ||
} | ||
|
||
func trim(s string) string { | ||
t := strings.Trim(s, "\n") | ||
t = strings.Trim(t, " ") | ||
return t | ||
} | ||
|
||
func getInput(input chan<- string) { | ||
for { | ||
in := bufio.NewReader(os.Stdin) | ||
result, err := in.ReadString('\n') | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
result = trim(result) | ||
input <- result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"What is the favourite food?",pizza | ||
5+5,10 | ||
7+3,10 | ||
1+1,2 | ||
8+3,11 | ||
1+2,3 | ||
8+6,14 | ||
3+1,4 | ||
1+4,5 | ||
5+1,6 | ||
2+3,5 | ||
3+3,6 | ||
2+4,6 | ||
5+2,7 |