-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay_game.go
73 lines (56 loc) · 1.26 KB
/
play_game.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"github.com/wrutkowski/go1010/drawer"
"github.com/wrutkowski/go1010/game"
)
type boardElement int
func main() {
g := game.New()
drawer.PrepareTerminal()
drawer.DrawGame(g)
for {
block, x, y, exit := nextMoveInteractive()
if exit {
break
}
g.Move(block, x, y)
drawer.PrepareTerminal()
drawer.DrawGame(g)
if g.GameOver {
fmt.Println("GAME OVER")
break
}
}
}
func nextMoveInteractive() (block game.BlockType, x int, y int, exit bool) {
fmt.Print("Next move: ")
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
text = text[:len(text)-1]
if text == "exit" || text == "e" {
return 0, 0, 0, true
}
components := strings.Split(text, " ")
if len(components) != 3 {
fmt.Println("Provide 3 components: 0-2 for block number and X and Y position of block placement...")
return nextMoveInteractive()
}
blockNumber, _ := strconv.Atoi(components[0])
positionX, _ := strconv.Atoi(components[1])
positionY, _ := strconv.Atoi(components[2])
var selectedBlock game.BlockType
switch blockNumber {
case 0:
selectedBlock = game.A
case 1:
selectedBlock = game.B
case 2:
selectedBlock = game.C
}
return selectedBlock, positionX, positionY, false
}