Esempio n. 1
0
// testCaptureLoop is a simulateTestCaseGenerator which generates a test case
// where the current mancala.Player captures after looping around the
// mancala.Board.
func testCaptureLoop(player mancala.PlayerIndex) simulateTestCase {
	grid := mancala.Grid{}
	grid[player] = mancala.Row{17, 0, 0, 0, 0, 0}
	grid[player.Other()] = mancala.Row{}
	board := mancala.NewInitializedBoard(0, 0, grid)
	expectedGrid := mancala.Grid{}
	expectedGrid[player] = mancala.Row{0, 2, 2, 2, 2, 2}
	expectedGrid[player.Other()] = mancala.Row{0, 1, 1, 1, 1, 1}
	player1Score := 0
	player2Score := 0
	if player == mancala.Player1 {
		player1Score += 2
	} else {
		player2Score += 2
	}
	expectedBoard := mancala.NewInitializedBoard(
		player1Score, player2Score,
		expectedGrid,
	)
	return simulateTestCase{
		referee: mancala.NewInitializedReferee(
			[]mancala.Board{},
			board,
		),
		move:          0,
		expectedBoard: expectedBoard,
		expectedErr:   nil,
	}
}
Esempio n. 2
0
// testAlreadySeenBoard is a simulateTestCaseGenerator which generates a test
// case where the current mancala.Board is in the mancala.Referee's
// mancala.Board history.
func testAlreadySeenBoard(player mancala.PlayerIndex) simulateTestCase {
	seenGrid := mancala.Grid{}
	seenGrid[player] = mancala.Row{0, 1, 1, 1, 1, 1}
	seenGrid[player.Other()] = mancala.Row{1, 0, 0, 0, 0, 0}
	grid := mancala.Grid{}
	grid[player] = mancala.Row{5, 0, 0, 0, 0, 0}
	grid[player.Other()] = mancala.Row{1, 0, 0, 0, 0, 0}
	referee := mancala.NewInitializedReferee(
		[]mancala.Board{mancala.NewInitializedBoard(0, 0, seenGrid)},
		mancala.NewInitializedBoard(0, 0, grid),
	)
	player1Score := 0
	player2Score := 0
	if player == mancala.Player1 {
		player1Score += 5
		player2Score++
	} else {
		player2Score += 5
		player1Score++
	}
	expectedBoard := mancala.NewInitializedBoard(
		player1Score, player2Score,
		mancala.Grid{},
	)
	return simulateTestCase{
		referee:       referee,
		move:          0,
		expectedBoard: expectedBoard,
		expectedErr:   nil,
	}
}
Esempio n. 3
0
// testNoSeedsCantReach is a simulateTestCaseGenerator which generates a test
// case where the other mancala.Player has no seeds and the current
// mancala.Player doesn't have a move which will give the other mancala.Player
// seeds.
func testNoSeedsCantReach(player mancala.PlayerIndex) simulateTestCase {
	grid := mancala.Grid{}
	grid[player] = mancala.Row{0, 0, 1, 1, 0, 0}
	board := mancala.NewInitializedBoard(0, 0, grid)
	player1Score := 0
	player2Score := 0
	if player == mancala.Player1 {
		player1Score += 2
	} else {
		player2Score += 2
	}
	advancedBoard := mancala.NewInitializedBoard(
		player1Score, player2Score,
		mancala.Grid{},
	)
	return simulateTestCase{
		referee: mancala.NewInitializedReferee(
			[]mancala.Board{},
			board,
		),
		move:          2,
		expectedBoard: advancedBoard,
		expectedErr:   nil,
	}
}
Esempio n. 4
0
// makePlayerBoard makes a mancala.Board with a mancala.Grid from
// makePlayerGrid, player 1's score being mancala.Player1, and player 2's score
// being mancala.Player2.
func makePlayerBoard() mancala.Board {
	grid := makePlayerGrid()
	return mancala.NewInitializedBoard(
		mancala.Player1, mancala.Player2,
		grid,
	)
}
Esempio n. 5
0
// testCaptureAllHouses is a simulateTestCaseGenerator which generates a test
// case where the current mancala.Player captures all of the other
// mancala.Player's houses from the end of the other mancala.Player's row to the
// beginning.
func testCaptureAllHouses(player mancala.PlayerIndex) simulateTestCase {
	grid := mancala.Grid{}
	grid[player] = mancala.Row{0, 0, 0, 0, 0, 6}
	grid[player.Other()] = mancala.Row{1, 1, 1, 1, 1, 1}
	board := mancala.NewInitializedBoard(0, 0, grid)
	expectedGrid := mancala.Grid{}
	expectedGrid[player] = mancala.Row{}
	expectedGrid[player.Other()] = mancala.Row{2, 2, 2, 2, 2, 2}
	expectedBoard := mancala.NewInitializedBoard(0, 0, expectedGrid)
	return simulateTestCase{
		referee: mancala.NewInitializedReferee(
			[]mancala.Board{}, board,
		),
		move:          5,
		expectedBoard: expectedBoard,
		expectedErr:   nil,
	}
}
Esempio n. 6
0
// testSowLoopToStart is a simulateTestCaseGenerator which generates a test case
// where the current mancala.Player sows in a loop around the mancala.Board just
// past the house the mancala.Player started sowing from.
func testSowLoopToStart(player mancala.PlayerIndex) simulateTestCase {
	grid := mancala.Grid{}
	grid[player] = mancala.Row{0, 0, 0, 0, 0, 45}
	grid[player.Other()] = mancala.Row{}
	board := mancala.NewInitializedBoard(0, 0, grid)
	expectedGrid := mancala.Grid{}
	expectedGrid[player] = mancala.Row{4, 4, 4, 4, 4, 0}
	expectedGrid[player.Other()] = mancala.Row{5, 4, 4, 4, 4, 4}
	expectedBoard := mancala.NewInitializedBoard(0, 0, expectedGrid)
	return simulateTestCase{
		referee: mancala.NewInitializedReferee(
			[]mancala.Board{}, board,
		),
		move:          5,
		expectedBoard: expectedBoard,
		expectedErr:   nil,
	}
}
Esempio n. 7
0
// testSowToOtherSide is a simulateTestCaseGenerator which generates a test case
// where the current mancala.Player sows to the middle of the other
// mancala.Player's mancala.Row.
func testSowToOtherSide(player mancala.PlayerIndex) simulateTestCase {
	grid := mancala.Grid{}
	grid[player] = mancala.Row{0, 0, 6, 0, 0, 0}
	grid[player.Other()] = mancala.Row{}
	board := mancala.NewInitializedBoard(0, 0, grid)
	expectedGrid := mancala.Grid{}
	expectedGrid[player] = mancala.Row{0, 0, 0, 1, 1, 1}
	expectedGrid[player.Other()] = mancala.Row{1, 1, 1, 0, 0, 0}
	expectedBoard := mancala.NewInitializedBoard(0, 0, expectedGrid)
	return simulateTestCase{
		referee: mancala.NewInitializedReferee(
			[]mancala.Board{}, board,
		),
		move:          2,
		expectedBoard: expectedBoard,
		expectedErr:   nil,
	}
}
Esempio n. 8
0
// expectedBoard returns a shortPlayer's mancala.Game's expected final
// mancala.Board.
func (short *shortPlayer) expectedBoard() mancala.Board {
	return mancala.NewInitializedBoard(
		2, 26,
		mancala.Grid{
			mancala.Row{1, 0, 0, 0, 0, 0},
			mancala.Row{1, 0, 5, 1, 2, 10},
		},
	)
}
Esempio n. 9
0
// testNoSeeds is a simulateTestCaseGenerator which generates a test case where
// the other mancala.Player has no seeds and the current mancala.Player has a
// move which will give the other mancala.Player seeds and doesn't make it.
func testNoSeedsCanReach(player mancala.PlayerIndex) simulateTestCase {
	grid := mancala.Grid{}
	grid[player] = mancala.Row{1, 0, 0, 0, 0, 1}
	board := mancala.NewInitializedBoard(0, 0, grid)
	return simulateTestCase{
		referee: mancala.NewInitializedReferee(
			[]mancala.Board{}, board,
		),
		move:          0,
		expectedBoard: board,
		expectedErr: fmt.Errorf(
			"moves exist which can give %v seeds but move at %d "+
				"doesn't",
			player.Other(),
			0,
		),
	}
}
Esempio n. 10
0
// testNoSeeds is a simulateTestCaseGenerator which generates a test case where
// the current mancala.Player tries to sow from a house with no seeds.
func testNoSeeds(player mancala.PlayerIndex) simulateTestCase {
	return simulateTestCase{
		referee: mancala.NewInitializedReferee(
			[]mancala.Board{},
			mancala.Board{},
		),
		move: 0,
		expectedBoard: mancala.NewInitializedBoard(
			0, 0,
			mancala.Grid{},
		),
		expectedErr: fmt.Errorf(
			"house at board.RowFor(%v)[%d] has 0 seeds",
			player,
			0,
		),
	}
}
Esempio n. 11
0
// TestFinishInitializedGame tests that mancala.FinishInitializedGame returns
// the correct mancala.Result from an already in progress mancala.Game described
// by a shortPlayer.
func TestFinishInitializedGame(t *testing.T) {
	t.Parallel()
	turnOffSet := 10
	player1 := newShortPlayer(mancala.Player1 + turnOffSet)
	player2 := newShortPlayer(mancala.Player2 + turnOffSet)
	referee := mancala.NewInitializedReferee(
		[]mancala.Board{},
		mancala.NewInitializedBoard(
			2, 15,
			mancala.Grid{
				mancala.Row{0, 2, 1, 1, 1, 1},
				mancala.Row{1, 10, 4, 0, 1, 9},
			},
		),
	)
	actual := mancala.FinishInitializedGame(
		referee,
		player1,
		player2,
		mancala.Player2,
		turnOffSet+1,
	)
	expected := mancala.NewResult(
		player1.expectedBoard(),
		player1.expectedWinner(),
		player1.expectedMoveCount(),
		player1.expectedLastMove(),
		nil,
	)
	if actual != expected {
		t.Errorf(
			"FinishInitializedGame(\n\t%v,\n\t%v,\n\t%v,\n\t%v,\n)"+
				" = \n%v, want \n%v",
			referee,
			player1,
			player2,
			mancala.Player2,
			actual,
			expected,
		)
	}
}
Esempio n. 12
0
// TestGameIsOver tests that mancala.Game::IsOver returns whether or not a
// mancala.Game is over for the possible ways an Oware game can end.
func TestGameIsOver(t *testing.T) {
	t.Parallel()
	actual := mancala.NewGame(errPlayer{}, errPlayer{}).IsOver()
	expected := false
	if actual != expected {
		t.Errorf(
			"game.IsOver() = %v, want %v with no win",
			actual,
			expected,
		)
	}
	actual = mancala.NewInitializedGame(
		mancala.NewInitializedReferee(
			[]mancala.Board{},
			mancala.NewInitializedBoard(
				(mancala.DefaultSeedCount/2)+1,
				(mancala.DefaultSeedCount/2)-1,
				mancala.Grid{},
			),
		),
		errPlayer{},
		errPlayer{},
		mancala.Player1,
		0,
	).IsOver()
	expected = true
	if actual != expected {
		t.Errorf(
			"game.IsOver() = %v, want %v with player 1 win",
			actual,
			expected,
		)
	}
	actual = mancala.NewInitializedGame(
		mancala.NewInitializedReferee(
			[]mancala.Board{},
			mancala.NewInitializedBoard(
				(mancala.DefaultSeedCount/2)-1,
				(mancala.DefaultSeedCount/2)+1,
				mancala.Grid{},
			),
		),
		errPlayer{},
		errPlayer{},
		mancala.Player2,
		0,
	).IsOver()
	expected = true
	if actual != expected {
		t.Errorf(
			"game.IsOver() = %v, want %v with player 2 win",
			actual,
			expected,
		)
	}
	actual = mancala.NewInitializedGame(
		mancala.NewInitializedReferee(
			[]mancala.Board{},
			mancala.NewInitializedBoard(
				mancala.DefaultSeedCount/2,
				mancala.DefaultSeedCount/2,
				mancala.Grid{},
			),
		),
		errPlayer{},
		errPlayer{},
		-1,
		0,
	).IsOver()
	expected = true
	if actual != expected {
		t.Errorf(
			"game.IsOver() = %v, want %v with tie",
			actual,
			expected,
		)
	}
	errGame := mancala.NewGame(errPlayer{}, errPlayer{})
	errGame.PlayMove()
	actual = errGame.IsOver()
	expected = true
	if actual != expected {
		t.Errorf(
			"game.IsOver() = %v, want %v with error",
			actual,
			expected,
		)
	}
}
Esempio n. 13
0
// TestGamePlayMove tests that mancala.Game::PlayMove returns the mancala.Result
// after a move in a shortPlayer mancala.Game and updates the mancala.Game
// state.
func TestGamePlayMove(t *testing.T) {
	t.Parallel()
	game := mancala.NewGame(badPlayer{}, errPlayer{})
	actual := game.PlayMove().Err()
	expected := fmt.Errorf(
		"move at house %d is out of range [%d, %d]",
		-1,
		0,
		mancala.RowWidth-1,
	)
	if actual.Error() != expected.Error() {
		t.Errorf(
			"game.PlayMove().Err().Error() = %s, want %s",
			actual.Error(),
			expected.Error(),
		)
	}
	game = mancala.NewGame(errPlayer{}, badPlayer{})
	actual = game.PlayMove().Err()
	expected = errors.New("error")
	if actual.Error() != expected.Error() {
		t.Errorf(
			"game.PlayMove().Err().Error() = %s, want %s",
			actual.Error(),
			expected.Error(),
		)
	}
	game = mancala.NewGame(
		newShortPlayer(mancala.Player1),
		newShortPlayer(mancala.Player2),
	)
	actualResult := game.PlayMove()
	expectedResult := mancala.NewResult(
		mancala.NewInitializedBoard(
			0, 0,
			mancala.Grid{
				mancala.Row{4, 4, 4, 0, 5, 5},
				mancala.Row{5, 5, 4, 4, 4, 4},
			},
		),
		-1,
		1,
		3,
		nil,
	)
	if actualResult != expectedResult {
		t.Errorf(
			"game.PlayMove() = \n%v, want \n%v",
			actualResult,
			expectedResult,
		)
	}
	actualPlayer := game.CurrentPlayer()
	expectedPlayer := mancala.PlayerIndex(mancala.Player2)
	if actualPlayer != expectedPlayer {
		t.Errorf(
			"game.CurrentPlayer() = %v, want %v",
			actualPlayer,
			expectedPlayer,
		)
	}
}