Example #1
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,
	}
}
Example #2
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,
	}
}
Example #3
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,
	}
}
Example #4
0
// NewState creates a State with the given parent State and Information.
func NewState(parent *State, information Information) *State {
	parentBoardHistory := parent.referee.BoardHistory()
	referee := mancala.NewInitializedReferee(
		parentBoardHistory,
		information.Board(),
	)
	return &State{
		parent:      parent,
		referee:     referee,
		information: information,
	}
}
Example #5
0
// TestRefereeBoardHistory tests that mancala.Referee::BoardHistory returns the
// mancala.Referee's mancala.Board history.
func TestRefereeBoardHistory(t *testing.T) {
	t.Parallel()
	actual := mancala.NewInitializedReferee(
		[]mancala.Board{mancala.NewBoard()},
		mancala.NewBoard(),
	).BoardHistory()
	expected := []mancala.Board{mancala.NewBoard()}
	if !reflect.DeepEqual(actual, expected) {
		t.Errorf(
			"referee.BoardHistory() = %v, want %v",
			actual,
			expected,
		)
	}
}
Example #6
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,
	}
}
Example #7
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,
	}
}
Example #8
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,
	}
}
Example #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,
		),
	}
}
Example #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,
		),
	}
}
Example #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,
		)
	}
}
Example #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,
		)
	}
}