From 84b1169e14413643075218e93569aceae4f09c8a Mon Sep 17 00:00:00 2001 From: MichaelS11 Date: Tue, 28 Mar 2017 12:52:09 -0700 Subject: [PATCH] Improved AI Correction for key input --- ai.go | 18 ++++++------ ai_test.go | 82 +++++++++++++++++++++++++--------------------------- key_input.go | 11 +++---- 3 files changed, 54 insertions(+), 57 deletions(-) diff --git a/ai.go b/ai.go index 8453fde..587cb5b 100644 --- a/ai.go +++ b/ai.go @@ -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 } diff --git a/ai_test.go b/ai_test.go index 9903a3b..054d91c 100644 --- a/ai_test.go +++ b/ai_test.go @@ -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 { diff --git a/key_input.go b/key_input.go index 821bb2b..7a09dc4 100644 --- a/key_input.go +++ b/key_input.go @@ -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() } }