Improved AI

Correction for key input
This commit is contained in:
MichaelS11 2017-03-28 12:52:09 -07:00
parent 646d20ae17
commit 84b1169e14
3 changed files with 54 additions and 57 deletions

18
ai.go
View File

@ -81,8 +81,8 @@ func (ai *Ai) getBestQueue() []rune {
continue
}
fullLines, holeDepth, bumpy := board.boardStatsWithMinos(mino1, mino2)
score := ai.getScoreFromBoardStats(fullLines, holeDepth, bumpy)
fullLines, holes, bumpy := board.boardStatsWithMinos(mino1, mino2)
score := ai.getScoreFromBoardStats(fullLines, holes, bumpy)
if slide1 < 3 {
slideScore = slide1
@ -192,7 +192,7 @@ func (mino *Mino) minoOverlap(mino1 *Mino) bool {
return false
}
func (board *Board) boardStatsWithMinos(mino1 *Mino, mino2 *Mino) (fullLines int, holeDepth int, bumpy int) {
func (board *Board) boardStatsWithMinos(mino1 *Mino, mino2 *Mino) (fullLines int, holes int, bumpy int) {
// fullLines
fullLinesY := make(map[int]bool, 2)
for j := 0; j < boardHeight; j++ {
@ -209,7 +209,7 @@ func (board *Board) boardStatsWithMinos(mino1 *Mino, mino2 *Mino) (fullLines int
}
}
// holeDepth and bumpy
// holes and bumpy
indexLast := 0
for i := 0; i < boardWidth; i++ {
index := boardHeight
@ -238,7 +238,7 @@ func (board *Board) boardStatsWithMinos(mino1 *Mino, mino2 *Mino) (fullLines int
index++
for j := index; j < boardHeight; j++ {
if board.colors[i][j] == blankColor && !mino1.isMinoAtLocation(i, j) && !mino2.isMinoAtLocation(i, j) {
holeDepth += 3 + j - index
holes++
}
}
}
@ -260,11 +260,11 @@ func (mino *Mino) isMinoAtLocation(x int, y int) bool {
return false
}
func (ai *Ai) getScoreFromBoardStats(fullLines int, holeDepth int, bumpy int) (score int) {
func (ai *Ai) getScoreFromBoardStats(fullLines int, holes int, bumpy int) (score int) {
if fullLines == 4 {
score += 16
score += 256
}
score -= holeDepth
score -= bumpy
score -= 75 * holes
score -= 25 * bumpy
return score
}

View File

@ -47,14 +47,14 @@ func TestBoardStatsFullLines1(t *testing.T) {
mino2.x = 6
mino2.y = 18
fullLines, holeDepth, bumpy := board.boardStatsWithMinos(mino1, mino2)
fullLines, holes, bumpy := board.boardStatsWithMinos(mino1, mino2)
expected := 1
if fullLines != expected {
t.Error("fullLines expected", expected, "got", fullLines)
}
expected = 0
if holeDepth != expected {
t.Error("holeDepth expected", expected, "got", holeDepth)
if holes != expected {
t.Error("holes expected", expected, "got", holes)
}
expected = 1
if bumpy != expected {
@ -140,14 +140,14 @@ func TestBoardStatsFullLines2(t *testing.T) {
mino2.x = 6
mino2.y = 16
fullLines, holeDepth, bumpy := board.boardStatsWithMinos(mino1, mino2)
fullLines, holes, bumpy := board.boardStatsWithMinos(mino1, mino2)
expected := 3
if fullLines != expected {
t.Error("fullLines expected", expected, "got", fullLines)
}
expected = 0
if holeDepth != expected {
t.Error("holeDepth expected", expected, "got", holeDepth)
if holes != expected {
t.Error("holes expected", expected, "got", holes)
}
expected = 1
if bumpy != expected {
@ -215,14 +215,14 @@ func TestBoardStatsFullLines3(t *testing.T) {
mino2.x = 8
mino2.y = 16
fullLines, holeDepth, bumpy := board.boardStatsWithMinos(mino1, mino2)
fullLines, holes, bumpy := board.boardStatsWithMinos(mino1, mino2)
expected := 1
if fullLines != expected {
t.Error("fullLines expected", expected, "got", fullLines)
}
expected = 0
if holeDepth != expected {
t.Error("holeDepth expected", expected, "got", holeDepth)
if holes != expected {
t.Error("holes expected", expected, "got", holes)
}
expected = 9
if bumpy != expected {
@ -254,14 +254,14 @@ func TestBoardStatsBumpy1(t *testing.T) {
mino2.x = 0
mino2.y = 16
fullLines, holeDepth, bumpy := board.boardStatsWithMinos(mino1, mino2)
fullLines, holes, bumpy := board.boardStatsWithMinos(mino1, mino2)
expected := 0
if fullLines != expected {
t.Error("fullLines expected", expected, "got", fullLines)
}
expected = 0
if holeDepth != expected {
t.Error("holeDepth expected", expected, "got", holeDepth)
if holes != expected {
t.Error("holes expected", expected, "got", holes)
}
expected = 4
if bumpy != expected {
@ -302,15 +302,14 @@ func TestBoardStatsBumpy2(t *testing.T) {
mino2.x = 0
mino2.y = 15
fullLines, holeDepth, bumpy := board.boardStatsWithMinos(mino1, mino2)
fullLines, holes, bumpy := board.boardStatsWithMinos(mino1, mino2)
expected := 0
if fullLines != expected {
t.Error("fullLines expected", expected, "got", fullLines)
}
// 9 + 9 = 18
expected = 18
if holeDepth != expected {
t.Error("holeDepth expected", expected, "got", holeDepth)
expected = 4
if holes != expected {
t.Error("holes expected", expected, "got", holes)
}
expected = 4
if bumpy != expected {
@ -318,9 +317,9 @@ func TestBoardStatsBumpy2(t *testing.T) {
}
// for debuging
// mino1.SetOnBoard()
// mino2.SetOnBoard()
// board.drawDebugBoard()
// mino1.SetOnBoard()
// mino2.SetOnBoard()
// board.drawDebugBoard()
}
func TestBoardStatsBumpy3(t *testing.T) {
@ -351,15 +350,14 @@ func TestBoardStatsBumpy3(t *testing.T) {
mino2.x = 2
mino2.y = 14
fullLines, holeDepth, bumpy := board.boardStatsWithMinos(mino1, mino2)
fullLines, holes, bumpy := board.boardStatsWithMinos(mino1, mino2)
expected := 0
if fullLines != expected {
t.Error("fullLines expected", expected, "got", fullLines)
}
// 6 + 7 + 4 + 5 + 6 + 7 = 35
expected = 35
if holeDepth != expected {
t.Error("holeDepth expected", expected, "got", holeDepth)
expected = 6
if holes != expected {
t.Error("holes expected", expected, "got", holes)
}
expected = 10
if bumpy != expected {
@ -367,9 +365,9 @@ func TestBoardStatsBumpy3(t *testing.T) {
}
// for debuging
// mino1.SetOnBoard()
// mino2.SetOnBoard()
// board.drawDebugBoard()
// mino1.SetOnBoard()
// mino2.SetOnBoard()
// board.drawDebugBoard()
}
func TestBoardStatsBumpy4(t *testing.T) {
@ -393,14 +391,14 @@ func TestBoardStatsBumpy4(t *testing.T) {
mino2.x = 6
mino2.y = 12
fullLines, holeDepth, bumpy := board.boardStatsWithMinos(mino1, mino2)
fullLines, holes, bumpy := board.boardStatsWithMinos(mino1, mino2)
expected := 0
if fullLines != expected {
t.Error("fullLines expected", expected, "got", fullLines)
}
expected = 0
if holeDepth != expected {
t.Error("holeDepth expected", expected, "got", holeDepth)
if holes != expected {
t.Error("holes expected", expected, "got", holes)
}
expected = 16
if bumpy != expected {
@ -413,7 +411,7 @@ func TestBoardStatsBumpy4(t *testing.T) {
// board.drawDebugBoard()
}
func TestBoardStatsHoleDepth1(t *testing.T) {
func TestBoardStatsholes1(t *testing.T) {
board = NewBoard()
// minoJ
@ -434,15 +432,14 @@ func TestBoardStatsHoleDepth1(t *testing.T) {
mino2.x = 1
mino2.y = 17
fullLines, holeDepth, bumpy := board.boardStatsWithMinos(mino1, mino2)
fullLines, holes, bumpy := board.boardStatsWithMinos(mino1, mino2)
expected := 0
if fullLines != expected {
t.Error("fullLines expected", expected, "got", fullLines)
}
// 3 + 4 + 3 + 4 = 14
expected = 14
if holeDepth != expected {
t.Error("holeDepth expected", expected, "got", holeDepth)
expected = 4
if holes != expected {
t.Error("holes expected", expected, "got", holes)
}
expected = 3
if bumpy != expected {
@ -455,7 +452,7 @@ func TestBoardStatsHoleDepth1(t *testing.T) {
// board.drawDebugBoard()
}
func TestBoardStatsHoleDepth2(t *testing.T) {
func TestBoardStatsholes2(t *testing.T) {
board = NewBoard()
// minoJ
@ -476,15 +473,14 @@ func TestBoardStatsHoleDepth2(t *testing.T) {
mino2.x = -1
mino2.y = 14
fullLines, holeDepth, bumpy := board.boardStatsWithMinos(mino1, mino2)
fullLines, holes, bumpy := board.boardStatsWithMinos(mino1, mino2)
expected := 0
if fullLines != expected {
t.Error("fullLines expected", expected, "got", fullLines)
}
// 3 + 4 + 6 + 7 = 20
expected = 20
if holeDepth != expected {
t.Error("holeDepth expected", expected, "got", holeDepth)
expected = 4
if holes != expected {
t.Error("holes expected", expected, "got", holes)
}
expected = 6
if bumpy != expected {

View File

@ -79,16 +79,13 @@ func (keyInput *KeyInput) ProcessEvent(event *termbox.Event) {
return
}
if event.Ch != 0 {
if engine.aiEnabled {
switch event.Ch {
case 'p':
engine.Pause()
case 'i':
engine.EnabledAi()
engine.DisableAi()
}
}
if engine.aiEnabled {
return
}
@ -111,6 +108,10 @@ func (keyInput *KeyInput) ProcessEvent(event *termbox.Event) {
board.MinoRotateLeft()
case 'x':
board.MinoRotateRight()
case 'p':
engine.Pause()
case 'i':
engine.EnabledAi()
}
}