Skip to content

Instantly share code, notes, and snippets.

@cerisier
Last active March 23, 2016 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cerisier/a1700429e831f89deb97 to your computer and use it in GitHub Desktop.
Save cerisier/a1700429e831f89deb97 to your computer and use it in GitHub Desktop.
package main
import "fmt"
type Game struct {
Board [][]int
}
func (g *Game) semiDiagonalScore(rowDirection int, colDirection int, row int, col int) int {
fmt.Printf("checking direcetion %d,%d\n", rowDirection, colDirection)
score := 0
for g.Board[row][col] == 1 {
fmt.Printf("checking [%d][%d] == %d\n", row, col, g.Board[row][col])
score += 1
row += rowDirection
col += colDirection
if row < 0 || row == len(g.Board) || col < 0 || col == len(g.Board[0]) {
break
}
}
fmt.Printf("score = %d\n", score)
return score
}
func (g *Game) checkVictory(row int, col int) bool {
score := g.semiDiagonalScore(-1, -1, row, col) + g.semiDiagonalScore(1, 1, row, col)
fmt.Println(score)
if score > 4 {
return true
}
score = g.semiDiagonalScore(-1, 1, row, col) + g.semiDiagonalScore(1, -1, row, col)
fmt.Println(score)
if score > 4 {
return true
}
return false
}
func NewGame() *Game {
g := &Game{
Board: [][]int{
[]int{0, 0, 0, 0, 0, 0},
[]int{0, 0, 0, 0, 0, 0},
[]int{0, 0, 1, 0, 0, 0},
[]int{0, 0, 0, 1, 0, 0},
[]int{0, 0, 0, 0, 1, 0},
[]int{0, 0, 0, 0, 0, 1},
[]int{0, 0, 0, 0, 0, 0},
},
}
return g
}
func main() {
game := NewGame()
victory := game.checkVictory(3, 3)
fmt.Printf("game is won: %+v", victory)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment