go-tetris/ai_test.go

495 lines
9.1 KiB
Go
Raw Normal View History

2017-03-27 15:07:29 -05:00
package main
import (
"gopkg.in/inconshreveable/log15.v2"
"os"
"testing"
)
func TestMain(m *testing.M) {
setupForTesting()
retCode := m.Run()
os.Exit(retCode)
}
func setupForTesting() {
logger = log15.New()
logger.SetHandler(log15.StreamHandler(os.Stdout, log15.LogfmtFormat()))
engine = NewEngine()
}
func TestBoardStatsFullLines1(t *testing.T) {
board = NewBoard()
// minoO
mino1 := &Mino{
minoRotation: minoBag[3],
}
mino1.length = len(mino1.minoRotation[0])
mino1.x = 0
mino1.y = 18
mino1.SetOnBoard()
// minoI
mino1 = &Mino{
minoRotation: minoBag[0],
}
mino1.length = len(mino1.minoRotation[0])
mino1.x = 2
mino1.y = 18
// minoI
mino2 := &Mino{
minoRotation: minoBag[0],
}
mino2.length = len(mino2.minoRotation[0])
mino2.x = 6
mino2.y = 18
2017-03-28 14:52:09 -05:00
fullLines, holes, bumpy := board.boardStatsWithMinos(mino1, mino2)
2017-03-27 15:07:29 -05:00
expected := 1
if fullLines != expected {
t.Error("fullLines expected", expected, "got", fullLines)
}
expected = 0
2017-03-28 14:52:09 -05:00
if holes != expected {
t.Error("holes expected", expected, "got", holes)
2017-03-27 15:07:29 -05:00
}
expected = 1
if bumpy != expected {
t.Error("bumpy expected", expected, "got", bumpy)
}
// for debuging
// mino1.SetOnBoard()
// mino2.SetOnBoard()
// board.drawDebugBoard()
}
func TestBoardStatsFullLines2(t *testing.T) {
board = NewBoard()
// minoO
mino1 := &Mino{
minoRotation: minoBag[3],
}
mino1.length = len(mino1.minoRotation[0])
mino1.x = 0
mino1.y = 18
mino1.SetOnBoard()
// minoO
mino1 = &Mino{
minoRotation: minoBag[3],
}
mino1.length = len(mino1.minoRotation[0])
mino1.x = 2
mino1.y = 18
mino1.SetOnBoard()
// minoO
mino1 = &Mino{
minoRotation: minoBag[3],
}
mino1.length = len(mino1.minoRotation[0])
mino1.x = 4
mino1.y = 18
mino1.SetOnBoard()
// minoO
mino1 = &Mino{
minoRotation: minoBag[3],
}
mino1.length = len(mino1.minoRotation[0])
mino1.x = 6
mino1.y = 18
mino1.SetOnBoard()
// minoO
mino1 = &Mino{
minoRotation: minoBag[3],
}
mino1.length = len(mino1.minoRotation[0])
mino1.x = 8
mino1.y = 18
mino1.SetOnBoard()
// minoO
mino1 = &Mino{
minoRotation: minoBag[3],
}
mino1.length = len(mino1.minoRotation[0])
mino1.x = 0
mino1.y = 16
mino1.SetOnBoard()
// minoI
mino1 = &Mino{
minoRotation: minoBag[0],
}
mino1.length = len(mino1.minoRotation[0])
mino1.x = 2
mino1.y = 16
// minoI
mino2 := &Mino{
minoRotation: minoBag[0],
}
mino2.length = len(mino2.minoRotation[0])
mino2.x = 6
mino2.y = 16
2017-03-28 14:52:09 -05:00
fullLines, holes, bumpy := board.boardStatsWithMinos(mino1, mino2)
2017-03-27 15:07:29 -05:00
expected := 3
if fullLines != expected {
t.Error("fullLines expected", expected, "got", fullLines)
}
expected = 0
2017-03-28 14:52:09 -05:00
if holes != expected {
t.Error("holes expected", expected, "got", holes)
2017-03-27 15:07:29 -05:00
}
expected = 1
if bumpy != expected {
t.Error("bumpy expected", expected, "got", bumpy)
}
// for debuging
// mino1.SetOnBoard()
// mino2.SetOnBoard()
// board.drawDebugBoard()
}
func TestBoardStatsFullLines3(t *testing.T) {
board = NewBoard()
// minoO
mino1 := &Mino{
minoRotation: minoBag[3],
}
mino1.length = len(mino1.minoRotation[0])
mino1.x = 0
mino1.y = 18
mino1.SetOnBoard()
// minoI
mino1 = &Mino{
minoRotation: minoBag[0],
}
mino1.length = len(mino1.minoRotation[0])
mino1.x = 0
mino1.y = 16
mino1.SetOnBoard()
// minoO
mino1 = &Mino{
minoRotation: minoBag[3],
}
mino1.length = len(mino1.minoRotation[0])
mino1.x = 4
mino1.y = 18
mino1.SetOnBoard()
// minoI
mino1 = &Mino{
minoRotation: minoBag[0],
}
mino1.length = len(mino1.minoRotation[0])
mino1.x = 4
mino1.y = 16
mino1.SetOnBoard()
// minoO
mino1 = &Mino{
minoRotation: minoBag[3],
}
mino1.length = len(mino1.minoRotation[0])
mino1.x = 8
mino1.y = 18
// minoO
mino2 := &Mino{
minoRotation: minoBag[3],
}
mino2.length = len(mino2.minoRotation[0])
mino2.x = 8
mino2.y = 16
2017-03-28 14:52:09 -05:00
fullLines, holes, bumpy := board.boardStatsWithMinos(mino1, mino2)
2017-03-27 15:07:29 -05:00
expected := 1
if fullLines != expected {
t.Error("fullLines expected", expected, "got", fullLines)
}
expected = 0
2017-03-28 14:52:09 -05:00
if holes != expected {
t.Error("holes expected", expected, "got", holes)
2017-03-27 15:07:29 -05:00
}
expected = 9
if bumpy != expected {
t.Error("bumpy expected", expected, "got", bumpy)
}
// for debuging
// mino1.SetOnBoard()
// mino2.SetOnBoard()
// board.drawDebugBoard()
}
func TestBoardStatsBumpy1(t *testing.T) {
board = NewBoard()
// minoO
mino1 := &Mino{
minoRotation: minoBag[3],
}
mino1.length = len(mino1.minoRotation[0])
mino1.x = 0
mino1.y = 18
// minoO
mino2 := &Mino{
minoRotation: minoBag[3],
}
mino2.length = len(mino2.minoRotation[0])
mino2.x = 0
mino2.y = 16
2017-03-28 14:52:09 -05:00
fullLines, holes, bumpy := board.boardStatsWithMinos(mino1, mino2)
2017-03-27 15:07:29 -05:00
expected := 0
if fullLines != expected {
t.Error("fullLines expected", expected, "got", fullLines)
}
expected = 0
2017-03-28 14:52:09 -05:00
if holes != expected {
t.Error("holes expected", expected, "got", holes)
2017-03-27 15:07:29 -05:00
}
expected = 4
if bumpy != expected {
t.Error("bumpy expected", expected, "got", bumpy)
}
// for debuging
// mino1.SetOnBoard()
// mino2.SetOnBoard()
// board.drawDebugBoard()
}
func TestBoardStatsBumpy2(t *testing.T) {
board = NewBoard()
// minoO
mino1 := &Mino{
minoRotation: minoBag[3],
}
mino1.length = len(mino1.minoRotation[0])
mino1.x = 0
mino1.y = 18
mino1.SetOnBoard()
// minoI
mino1 = &Mino{
minoRotation: minoBag[0],
}
mino1.length = len(mino1.minoRotation[0])
mino1.x = 0
mino1.y = 16
// minoI
mino2 := &Mino{
minoRotation: minoBag[0],
}
mino2.length = len(mino2.minoRotation[0])
mino2.x = 0
mino2.y = 15
2017-03-28 14:52:09 -05:00
fullLines, holes, bumpy := board.boardStatsWithMinos(mino1, mino2)
2017-03-27 15:07:29 -05:00
expected := 0
if fullLines != expected {
t.Error("fullLines expected", expected, "got", fullLines)
}
2017-03-28 14:52:09 -05:00
expected = 4
if holes != expected {
t.Error("holes expected", expected, "got", holes)
2017-03-27 15:07:29 -05:00
}
expected = 4
if bumpy != expected {
t.Error("bumpy expected", expected, "got", bumpy)
}
// for debuging
2017-03-28 14:52:09 -05:00
// mino1.SetOnBoard()
// mino2.SetOnBoard()
// board.drawDebugBoard()
2017-03-27 15:07:29 -05:00
}
func TestBoardStatsBumpy3(t *testing.T) {
board = NewBoard()
// minoO
mino1 := &Mino{
minoRotation: minoBag[3],
}
mino1.length = len(mino1.minoRotation[0])
mino1.x = 0
mino1.y = 18
mino1.SetOnBoard()
// minoO
mino1 = &Mino{
minoRotation: minoBag[3],
}
mino1.length = len(mino1.minoRotation[0])
mino1.x = 1
mino1.y = 16
// minoO
mino2 := &Mino{
minoRotation: minoBag[3],
}
mino2.length = len(mino2.minoRotation[0])
mino2.x = 2
mino2.y = 14
2017-03-28 14:52:09 -05:00
fullLines, holes, bumpy := board.boardStatsWithMinos(mino1, mino2)
2017-03-27 15:07:29 -05:00
expected := 0
if fullLines != expected {
t.Error("fullLines expected", expected, "got", fullLines)
}
2017-03-28 14:52:09 -05:00
expected = 6
if holes != expected {
t.Error("holes expected", expected, "got", holes)
2017-03-27 15:07:29 -05:00
}
expected = 10
if bumpy != expected {
t.Error("bumpy expected", expected, "got", bumpy)
}
// for debuging
2017-03-28 14:52:09 -05:00
// mino1.SetOnBoard()
// mino2.SetOnBoard()
// board.drawDebugBoard()
2017-03-27 15:07:29 -05:00
}
func TestBoardStatsBumpy4(t *testing.T) {
board = NewBoard()
// minoI
mino1 := &Mino{
minoRotation: minoBag[0],
}
mino1.rotation = 1
mino1.length = len(mino1.minoRotation[0])
mino1.x = 6
mino1.y = 16
// minoI
mino2 := &Mino{
minoRotation: minoBag[0],
}
mino2.rotation = 1
mino2.length = len(mino2.minoRotation[0])
mino2.x = 6
mino2.y = 12
2017-03-28 14:52:09 -05:00
fullLines, holes, bumpy := board.boardStatsWithMinos(mino1, mino2)
2017-03-27 15:07:29 -05:00
expected := 0
if fullLines != expected {
t.Error("fullLines expected", expected, "got", fullLines)
}
expected = 0
2017-03-28 14:52:09 -05:00
if holes != expected {
t.Error("holes expected", expected, "got", holes)
2017-03-27 15:07:29 -05:00
}
expected = 16
if bumpy != expected {
t.Error("bumpy expected", expected, "got", bumpy)
}
// for debuging
// mino1.SetOnBoard()
// mino2.SetOnBoard()
// board.drawDebugBoard()
}
2017-03-28 14:52:09 -05:00
func TestBoardStatsholes1(t *testing.T) {
2017-03-27 15:07:29 -05:00
board = NewBoard()
// minoJ
mino1 := &Mino{
minoRotation: minoBag[1],
}
mino1.rotation = 1
mino1.length = len(mino1.minoRotation[0])
mino1.x = -1
mino1.y = 17
// minoJ
mino2 := &Mino{
minoRotation: minoBag[1],
}
mino2.rotation = 1
mino2.length = len(mino2.minoRotation[0])
mino2.x = 1
mino2.y = 17
2017-03-28 14:52:09 -05:00
fullLines, holes, bumpy := board.boardStatsWithMinos(mino1, mino2)
2017-03-27 15:07:29 -05:00
expected := 0
if fullLines != expected {
t.Error("fullLines expected", expected, "got", fullLines)
}
2017-03-28 14:52:09 -05:00
expected = 4
if holes != expected {
t.Error("holes expected", expected, "got", holes)
2017-03-27 15:07:29 -05:00
}
expected = 3
if bumpy != expected {
t.Error("bumpy expected", expected, "got", bumpy)
}
// for debuging
// mino1.SetOnBoard()
// mino2.SetOnBoard()
// board.drawDebugBoard()
}
2017-03-28 14:52:09 -05:00
func TestBoardStatsholes2(t *testing.T) {
2017-03-27 15:07:29 -05:00
board = NewBoard()
// minoJ
mino1 := &Mino{
minoRotation: minoBag[1],
}
mino1.rotation = 1
mino1.length = len(mino1.minoRotation[0])
mino1.x = -1
mino1.y = 17
// minoJ
mino2 := &Mino{
minoRotation: minoBag[1],
}
mino2.rotation = 1
mino2.length = len(mino2.minoRotation[0])
mino2.x = -1
mino2.y = 14
2017-03-28 14:52:09 -05:00
fullLines, holes, bumpy := board.boardStatsWithMinos(mino1, mino2)
2017-03-27 15:07:29 -05:00
expected := 0
if fullLines != expected {
t.Error("fullLines expected", expected, "got", fullLines)
}
2017-03-28 14:52:09 -05:00
expected = 4
if holes != expected {
t.Error("holes expected", expected, "got", holes)
2017-03-27 15:07:29 -05:00
}
expected = 6
if bumpy != expected {
t.Error("bumpy expected", expected, "got", bumpy)
}
// for debuging
// mino1.SetOnBoard()
// mino2.SetOnBoard()
// board.drawDebugBoard()
}