| 42 | } |
| 43 | |
| 44 | func loop(in *bufio.Scanner) { |
| 45 | g := newGame() |
| 46 | |
| 47 | for { |
| 48 | g.prompt() |
| 49 | |
| 50 | if !in.Scan() { |
| 51 | break |
| 52 | } |
| 53 | |
| 54 | // Atoi already return 0 on error; no need to check |
| 55 | // it for the following switch to work |
| 56 | pos, _ := strconv.Atoi(in.Text()) |
| 57 | |
| 58 | msg := g.play(pos) |
| 59 | if msg != "" { |
| 60 | fmt.Println("\n>>>", msg) |
| 61 | continue |
| 62 | } |
| 63 | |
| 64 | g.turn++ |
| 65 | if msg := g.finito(); msg != "" { |
| 66 | g.print() |
| 67 | fmt.Printf("\n%s\n", msg) |
| 68 | break |
| 69 | } |
| 70 | |
| 71 | g.player = switchTo(g.player) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func newGame() game { |
| 76 | return game{ |