Skip to content

Commit

Permalink
Testing - caching won't work unless we make more compressed way to st…
Browse files Browse the repository at this point in the history
…ore []Move (OOM)
  • Loading branch information
Vadman97 committed Mar 21, 2019
1 parent 549333b commit 29c46f4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
2 changes: 2 additions & 0 deletions chessai/game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ type Game struct {
}

func (g *Game) PlayTurn() {
start := time.Now()
g.Players[g.CurrentTurnColor].MakeMove(g.CurrentBoard)
g.PlayTime[g.CurrentTurnColor] += time.Now().Sub(start)
g.CurrentTurnColor = (g.CurrentTurnColor + 1) % color.NumColors
g.MovesPlayed++
}
Expand Down
16 changes: 10 additions & 6 deletions chessai/player/ai/ai_player.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,13 @@ func (p *Player) GetBestMove(b *board.Board) *board.Move {
} else {
var m *ScoredMove
if p.Algorithm == AlgorithmMiniMax {
m = p.MiniMax(b, 4, p.PlayerColor)
m = p.MiniMax(b, 2, p.PlayerColor)
} else if p.Algorithm == AlgorithmAlphaBetaWithMemory {
m = p.AlphaBetaWithMemory(b, 4, NegInf, PosInf, p.PlayerColor)
} else {
panic("invalid ai algorithm")
}
c := "Black"
if p.PlayerColor == color.White {
c = "White"
}
fmt.Printf("AI (%s - %s) best move leads to score %d\n", p.Algorithm, c, m.Score)
fmt.Printf("%s best move leads to score %d\n", p.Repr(), m.Score)
debugBoard := b.Copy()
//for i := 0; i < len(m.MoveSequence); i++ {
for i := len(m.MoveSequence) - 1; i >= 0; i-- {
Expand Down Expand Up @@ -207,3 +203,11 @@ func (p *Player) EvaluateBoard(b *board.Board) *board.Evaluation {
p.evaluationMap.Store(&hash, int32(eval.TotalScore))
return eval
}

func (p *Player) Repr() string {
c := "Black"
if p.PlayerColor == color.White {
c = "White"
}
return fmt.Sprintf("AI (%s - %s)", p.Algorithm, c)
}
1 change: 1 addition & 0 deletions chessai/player/ai/minimax.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (p *Player) MiniMax(b *board.Board, depth int, currentPlayer byte) *ScoredM
}

var best ScoredMove
// TODO(Vadim) if depth is odd, flip these?
if currentPlayer == p.PlayerColor {
// maximizing player
best.Score = NegInf
Expand Down
8 changes: 5 additions & 3 deletions chessai/test/ai_basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func TestBoardAI(t *testing.T) {
const MovesToPlay = 100
const TimeToPlay = 60 * time.Second
const TimeToPlay = 300 * time.Second

aiPlayerSmart := ai.NewAIPlayer(color.Black)
aiPlayerSmart.Algorithm = ai.AlgorithmMiniMax
Expand All @@ -30,13 +30,15 @@ func TestBoardAI(t *testing.T) {
}
g.PlayTurn()
fmt.Printf("Move %d\n", g.MovesPlayed)
fmt.Println(g.CurrentBoard.Print())
fmt.Print(g.CurrentBoard.Print())
fmt.Printf("White %s has thought for %s\n", g.Players[color.White].Repr(), g.PlayTime[color.White])
fmt.Printf("Black %s has thought for %s\n", g.Players[color.Black].Repr(), g.PlayTime[color.Black])
}

fmt.Println("After moves")
fmt.Println(g.CurrentBoard.Print())
// comment out printing inside loop for accurate timing
fmt.Printf("Played %d moves in %d ms.\n", MovesToPlay, time.Now().Sub(start)/time.Millisecond)
fmt.Printf("Played %d moves in %d ms.\n", g.MovesPlayed, time.Now().Sub(start)/time.Millisecond)

smartScore := aiPlayerSmart.EvaluateBoard(g.CurrentBoard).TotalScore
dumbScore := aiPlayerDumb.EvaluateBoard(g.CurrentBoard).TotalScore
Expand Down

0 comments on commit 29c46f4

Please sign in to comment.