예제 #1
0
// TestGameCurrentPlayer tests that mancala.Game::CurrentPlayer returns the
// current mancala.PlayerIndex.
func TestGameCurrentPlayer(t *testing.T) {
	t.Parallel()
	game := mancala.NewGame(
		newShortPlayer(mancala.Player1),
		newShortPlayer(mancala.Player2),
	)
	actual := game.CurrentPlayer()
	expected := mancala.PlayerIndex(mancala.Player1)
	if actual != expected {
		t.Errorf(
			"game.CurrentPlayer() = %v, want %v",
			actual,
			expected,
		)
	}
	game.PlayMove()
	actual = game.CurrentPlayer()
	expected = mancala.PlayerIndex(mancala.Player2)
	if actual != expected {
		t.Errorf(
			"game.CurrentPlayer() = %v, want %v",
			actual,
			expected,
		)
	}
}
예제 #2
0
// TestResultWinner tests that mancala.Result::Winner returns the winning
// mancala.Player's mancala.PlayerIndex.
func TestResultWinner(t *testing.T) {
	t.Parallel()
	expected := mancala.PlayerIndex(mancala.Player1)
	actual := mancala.NewResult(
		mancala.NewBoard(),
		expected,
		0,
		-1,
		nil,
	).Winner()
	if actual != expected {
		t.Errorf("result.Winner() = %v, want %v", actual, expected)
	}
}
예제 #3
0
// TestBoardRowFor tests that mancala.Board::RowFor returns the mancala.Row for
// the given mancala.PlayerIndex.
func TestBoardRowFor(t *testing.T) {
	t.Parallel()
	for _, index := range []mancala.PlayerIndex{
		mancala.Player1, mancala.Player2,
	} {
		actual := makePlayerBoard().RowFor(index)
		expected := makeRow(int(index))
		if actual != expected {
			t.Errorf(
				"makePlayerBoard.RowFor(%v) = %v, want %v",
				mancala.PlayerIndex(index),
				actual,
				expected,
			)
		}
	}
}
예제 #4
0
// TesBoardScoreFor tests that mancala.Board::ScoreFor returns the score for the
// mancala.Player at the given mancala.PlayerIndex.
func TestBoardScoreFor(t *testing.T) {
	t.Parallel()
	for _, index := range []mancala.PlayerIndex{
		mancala.Player1, mancala.Player2,
	} {
		actual := mancala.PlayerIndex(makePlayerBoard().ScoreFor(index))
		expected := index
		if actual != expected {
			t.Errorf(
				"makePlayerBoard().ScoreFor(%v) = %d, want %d",
				index,
				actual,
				expected,
			)
		}
	}
}
예제 #5
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,
		)
	}
}