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

View File

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

View File

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