~dax/con44

ref: ccdf4927cf90f3780f03dbfc82ad8e4dde732b2a con44/con44.go -rw-r--r-- 4.1 KiB
ccdf4927bonbon Added checkWin, changed Move to account for checkWin 1 year, 9 months ago
                                                                                
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package con44

import "fmt"

//Config stores a configuration for a game
//This is also used to query a running game's config
type Config struct {
	Players, Blockers, Rows, Cols int
}

//Game stores all relevant information to a game state
type Game struct {
	config   Config
	board    [][][]int
	blockers []int
	turn     int
	won      int
}

//Validate returns any errors with the Config
func (c Config) Validate() error {
	if c.Players <= 0 {
		err := fmt.Errorf("invalid player count: %d", c.Players)
		return err
	}

	if c.Blockers < 0 {
		err := fmt.Errorf("invalid blocker count: %d", c.Blockers)
		return err
	}

	if c.Rows <= 0 {
		err := fmt.Errorf("invalid row count: %d", c.Rows)
		return err
	}

	if c.Cols <= 0 {
		err := fmt.Errorf("invalid col count: %d", c.Cols)
		return err
	}

	return nil
}

//NewGame initializes a new Game state using the given Config
//if the Config is invalid, it will return a nil state and an error
func NewGame(cfg Config) (*Game, error) {
	// check for a valid config
	if err := cfg.Validate(); err != nil {
		return nil, err
	}
	game := &Game{config: cfg}

	// initialize the board
	game.board = make([][][]int, cfg.Rows)
	for i := range game.board {
		game.board[i] = make([][]int, cfg.Cols)
		for j := range game.board[i] {
			game.board[i][j] = make([]int, 2)
		}
	}

	// initialize blocker counts
	game.blockers = make([]int, cfg.Players)
	for i := range game.blockers {
		game.blockers[i] = cfg.Blockers
	}

	return game, nil
}

//NewGameDefault creates a game with default Config options
//4 players, 2 blockers, 7 rows, and 6 columns
func NewGameDefault() *Game {
	game, _ := NewGame(Config{
		Players:  4,
		Blockers: 2,
		Rows:     6,
		Cols:     7,
	})
	return game
}

func (g Game) Config() Config {
	return g.config
}

func (g Game) Board() [][][]int {
	return g.board
}

func (g Game) Blockers() []int {
	return g.blockers
}

func (g Game) Turn() int {
	return g.turn
}

func (g Game) Won() int {
	return g.won
}

func (g Game) IsValidMove(col, depth int) bool {
	topRow := g.config.Rows - 1

	switch {
	case depth < 0 || col < 0 || col > g.config.Cols-1:
		//  invalid input
		return false
	case depth >= 2:
		// blocker
		return g.board[topRow][col][0] == 0 && g.board[topRow][col][1] == 0
	default:
		// non-blocker
		return g.board[topRow][col][depth] == 0
	}
}

//Move will attempt to place the next player's token in the given column and depth
//An error is returned on an invalid move, nil otherwise
func (g *Game) Move(col, depth int) error {
	if g.won != 0 {
		err := fmt.Errorf("invalid move: game finished")
		return err
	}

	if !g.IsValidMove(col, depth) {
		err := fmt.Errorf("invalid move: col %d, depth %d", col, depth)
		return err
	}

	curPlayer := g.turn%g.config.Players + 1
	var rowPlaced int
	switch {
	case depth >= 2:
		// blocker
		if g.blockers[curPlayer-1] <= 0 {
			err := fmt.Errorf("invalid move: no blockers")
			return err
		}
		for row := range g.board {
			if g.board[row][col][0] == 0 && g.board[row][col][1] == 0 {
				g.board[row][col][0] = curPlayer
				g.board[row][col][1] = curPlayer
				rowPlaced = row
				break
			}
		}
		g.blockers[curPlayer-1]--
	default:
		// non-blocker
		row := len(g.board) - 1
		for row > 0 && g.board[row-1][col][depth] == 0 {
			row--
		}
		g.board[row][col][depth] = curPlayer
		rowPlaced = row
	}

	// check win condition
	if w := g.checkWin(rowPlaced, col, curPlayer); w != 0 {
		g.won = w
	} else {
		g.turn++
	}

	// no errors
	return nil
}

//TODO: write win check
func (g Game) checkWin(row, col, curPlayer int) int {
	var directions [4][2]int
	directions[0] = [2]int{0, 1}
	directions[1] = [2]int{1, 0}
	directions[2] = [2]int{1, 1}
	directions[3] = [2]int{-1, 1}

	for _, dir := range directions {
		r, c := row+dir[0], col+dir[1]
		slotsFilled := 1
		for r >= 0 && r < g.config.Rows && c < g.config.Cols &&
			(g.board[r][c][0] == curPlayer || g.board[r][c][1] == curPlayer) {
			slotsFilled++
			r += dir[0]
			c += dir[1]
		}
		r, c = row-dir[0], col-dir[1]
		for r >= 0 && r < g.config.Rows && c >= 0 &&
			(g.board[r][c][0] == curPlayer || g.board[r][c][1] == curPlayer) {
			slotsFilled++
			r -= dir[0]
			c -= dir[1]
		}
		if slotsFilled >= 4 {
			return curPlayer
		}
	}
	return 0
}