Example #1
0
func TestMove(t *testing.T) {
	testCases := []struct {
		dir      game.Direction
		expected game.Position
	}{
		{
			dir:      game.UP,
			expected: game.NewPosition(2, 1),
		},
		{
			dir:      game.DOWN,
			expected: game.NewPosition(0, 1),
		},
		{
			dir:      game.RIGHT,
			expected: game.NewPosition(1, 2),
		},
		{
			dir:      game.LEFT,
			expected: game.NewPosition(1, 0),
		},
	}

	for n, tc := range testCases {
		p := game.NewPosition(1, 1)
		actual := p.Move(tc.dir)
		if actual != tc.expected {
			t.Errorf("(%v) Expected moving %v from %v to go to %v, got %v", n, tc.dir, p, tc.expected, actual)
		}
	}
}
Example #2
0
func TestValid(t *testing.T) {
	testCases := []struct {
		position game.Position
		expected bool
	}{
		{
			position: game.NewPosition(0, 0),
			expected: true,
		},
		{
			position: game.NewPosition(0, -1),
			expected: false,
		},
		{
			position: game.NewPosition(1, 0),
			expected: false,
		},
		{
			position: game.NewPosition(0, 1),
			expected: false,
		},
		{
			position: game.NewPosition(-1, 0),
			expected: false,
		},
	}

	for n, tc := range testCases {
		if actual := tc.position.Valid(1); actual != tc.expected {
			t.Errorf("(%v) Expected position %v to have validity %v", n, tc.position, tc.expected)
		}
	}
}
Example #3
0
func TestNewBoard(t *testing.T) {
	board := game.NewBoard(2)
	if !board.Adjacent(game.NewPosition(0, 0), game.NewPosition(0, 1)) {
		t.Errorf("Missing expected adjacency: %v", board)
	}
	if board.Adjacent(game.NewPosition(0, 0), game.NewPosition(1, 1)) {
		t.Errorf("Unexpected adjacency: %v", board)
	}
}
Example #4
0
func TestMoves(t *testing.T) {
	testCases := []struct {
		curPos            game.Position
		expectedDirection game.Direction
	}{
		{
			curPos:            game.NewPosition(5, 5),
			expectedDirection: game.UP,
		},
	}

	for n, tc := range testCases {
		g := game.NewGame()
		g.ActivePlayer.PawnPos = tc.curPos
		cpy := g.Copy()
		intel := intelligence.NewSimpleIntelligence(g.ActivePlayer.Color)
		move := intel.NextMove(cpy)
		movePawn, ok := move.(*game.MovePawn)
		if !ok {
			t.Errorf("(%d) Expected move pawn, got %v", n, move)
		}
		if movePawn.Direction != tc.expectedDirection {
			t.Errorf("(%d) Expected direction %v, got %v", n, tc.expectedDirection, movePawn.Direction)
		}
	}
}
Example #5
0
func TestMovePawn(t *testing.T) {
	testCases := []struct {
		direction        game.Direction
		expectedResult   bool
		expectedPosition game.Position
	}{
		{
			direction:        game.UP,
			expectedResult:   true,
			expectedPosition: game.NewPosition(1, 4),
		},
		{
			direction:        game.DOWN,
			expectedResult:   false,
			expectedPosition: game.NewPosition(0, 4),
		},
		{
			direction:        game.LEFT,
			expectedResult:   true,
			expectedPosition: game.NewPosition(0, 3),
		},
		{
			direction:        game.RIGHT,
			expectedResult:   true,
			expectedPosition: game.NewPosition(0, 5),
		},
	}

	for n, tc := range testCases {
		g := game.NewGame()
		move := game.MovePawn{
			Direction: tc.direction,
		}
		if tc.expectedResult != g.Move(move) {
			t.Errorf("(%v) Expected move %v to have result %v", n, move, tc.expectedResult)
		}
		var player *game.Player
		if tc.expectedResult {
			player = g.WaitingPlayer
		} else {
			player = g.ActivePlayer
		}
		if actualPosition := player.PawnPos; tc.expectedPosition != actualPosition {
			t.Errorf("(%v) Expected move %v to end at %v, not %v", n, move, tc.expectedPosition, actualPosition)
		}
	}
}
Example #6
0
func newAdjacency(sr, sc, er, ec int) []game.Position {
	return []game.Position{
		game.NewPosition(sr, sc),
		game.NewPosition(er, ec),
	}
}