~dax/con44

ccdf4927cf90f3780f03dbfc82ad8e4dde732b2a — bonbon 1 year, 10 months ago 04e12fd
Added checkWin, changed Move to account for checkWin
1 files changed, 31 insertions(+), 3 deletions(-)

M con44.go
M con44.go => con44.go +31 -3
@@ 131,7 131,7 @@ func (g *Game) Move(col, depth int) error {
	}

	curPlayer := g.turn%g.config.Players + 1

	var rowPlaced int
	switch {
	case depth >= 2:
		// blocker


@@ 143,6 143,7 @@ func (g *Game) Move(col, depth int) error {
			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
			}
		}


@@ 154,10 155,11 @@ func (g *Game) Move(col, depth int) error {
			row--
		}
		g.board[row][col][depth] = curPlayer
		rowPlaced = row
	}

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


@@ 168,6 170,32 @@ func (g *Game) Move(col, depth int) error {
}

//TODO: write win check
func (g Game) checkWin() int {
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
}