Beispiel #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,
		)
	}
}
Beispiel #2
0
// Play returns the string content of all states in a game between the ai.Agents
// represented by the two agent names.
func Play(agent1, agent2 ai.AgentConstructor) string {
	game := mancala.NewGame(agent1(), agent2())
	output := ""
	output += fmt.Sprintf("%v\n\n", game.Result().Board())
	for !game.IsOver() {
		output += fmt.Sprintf(
			"Current player is %v.\n\n",
			game.CurrentPlayer(),
		)
		game.PlayMove()
		output += fmt.Sprintf("%v\n\n", game.Result())
	}
	if game.Result().Winner() != -1 {
		output += fmt.Sprintf(
			"%v won.\n",
			game.Result().Winner(),
		)
	} else {
		output += "The game was a tie.\n"
	}
	return output
}
Beispiel #3
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,
		)
	}
}
Beispiel #4
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,
		)
	}
}