コード例 #1
0
class TestPlayer(unittest.TestCase):
    def setUp(self):
        self.player = Player('X')

    def test_init(self):
        self.assertEqual(self.player.symbol, 'X')

    def test_input_mark(self):
        with mock.patch('builtins.input', return_value='2'):
            marker_tuple = self.player.input_mark()
        self.assertEqual(marker_tuple, ('2', 'X'))

    def test_place_mark(self):
        with self.subTest():
            mark_indices = self.player.place_mark(board=[['X', 'X', 'X'],
                                                         ['4', '5', '6'],
                                                         ['7', '8', '9']],
                                                  player_input=('8', 'X'))
            self.assertEqual(mark_indices, (2, 1))

        with self.subTest():
            failed_placement = self.player.place_mark(board=[['X', 'X', 'X'],
                                                             ['4', '5', '6'],
                                                             ['7', 'X', '9']],
                                                      player_input=('8', 'X'))
            self.assertFalse(failed_placement)

    def tearDown(self):
        del self.player
コード例 #2
0
ファイル: player_test.py プロジェクト: Jer-Sch/Tic-Tac-Toe
def test_human_player_can_make_a_move():
    new_board = Board()
    player = Player(token=["X", "O"])

    new_board.move(1, new_board.board, player.token[0])

    assert new_board.board[1] == "X"
コード例 #3
0
def test_turn_count_keeps_track_of_number_of_turns(board_state, turn_count):
    new_board = Board()

    new_board.board = board_state

    player = Player(token=["X", "O"])

    assert new_board.turn_count(player) == turn_count
コード例 #4
0
def test_board_raises_error_when_positions_are_taken():
    new_board = Board()
    player = Player(token=["X", "O"])

    new_board.board = ["X"]

    with pytest.raises(PositionAlreadyTakenError):
        new_board.turn(0, player)
コード例 #5
0
def test_current_player_returns_current_player_token(board_state,
                                                     current_player):
    new_board = Board()

    new_board.board = board_state

    player = Player(token=["X", "O"])

    assert new_board.current_player(player) == current_player
コード例 #6
0
def run():
    board = Board()
    player = Player(token=["X", "O"])
    view = CommandLineBoardPresenter()
    printer = Printer()
    user_messages = UserMessages(printer)
    errors = Errors(printer)

    user_messages.countdown()

    user_messages.logo()

    user_messages.instructions_option()

    if player.get_input() == 0:
        user_messages.display_instructions()
        view.display_board(board, printer)
    else:
        view.display_board(board, printer)
        user_messages.whos_turn(player, board)

    while not board.game_over():
        try:
            selection = player.get_input()
            board.turn(selection, player)
        except InputNotNumericError:
            errors.input_not_numeric_error_message()
        except InvalidBoardIndexError:
            errors.invalid_board_index_error_message()
        except PositionAlreadyTakenError:
            errors.position_already_taken_error_message()
        finally:
            view.display_board(board, printer)
            if not board.win() and not board.tie():
                user_messages.whos_turn(player, board)

    if board.win():
        user_messages.who_won(board)

    if board.tie():
        user_messages.its_a_tie()
コード例 #7
0
def player_obj():
    return Player('Player1'), Player('Player2')
コード例 #8
0
A command-line game of tic-tac-toe written in python 2.7.

Main objects are Game for game control, Player for player moves
(including AIPlayer for computer play), and io to handle
everything related to display and input.
"""

from tic_tac_toe.game import Game, GameTied, InvalidMove
from tic_tac_toe.player import Player
from tic_tac_toe.ai_player import AIPlayer
from tic_tac_toe import io
import sys

# Initialize objects
game = Game(3)
players = [Player('X'), AIPlayer('O')]
playing = True  # Main loop control needed to break out of multiple levels
io.init()

# Main game loop
while playing:
    io.print_board(game)
    try:
        for player in players:
            player.make_move(game)
            if game.check_winner(player.symbol):
                io.print_board(game)
                if players.index(player) == 0:
                    io.winner()
                else:
                    io.loser()
コード例 #9
0
def test_board_raises_error_if_index_not_on_board():
    new_board = Board()
    player = Player(token=["X", "O"])

    with pytest.raises(InvalidBoardIndexError):
        new_board.turn(9, player)
コード例 #10
0
def test_user_input_raises_input_not_numeric_error(mocked_input):
    mocked_input.return_value = "a"

    with pytest.raises(InputNotNumericError):
        Player(token=["X"]).get_input()
コード例 #11
0
ファイル: player_test.py プロジェクト: Jer-Sch/Tic-Tac-Toe
def test_player_initializes_with_a_player_token():
    player = Player(token="X")

    assert player.token == "X"
コード例 #12
0
print('q or "enter" key to exit')

try:
    action = str(input('---> '))
except (EOFError, KeyboardInterrupt):
    print('Bye!')
    sys.exit(0)

if action == 'q':
    sys.exit(0)

if action == 's':
    player1 = input('Enter player1 name: ')
    player2 = input('Enter player2 name: ')

    p1 = Player(str(player1))
    p2 = Player(str(player2))

    # Game session loop
    game_session_flag = True

    while game_session_flag:
        # Init game
        game = Game(p1, p2)
        game.start_game()

        # Current game data
        last_move = ['', '']
        winner = False
        messages = []
        current_game_flag = True
コード例 #13
0
 def setUp(self):
     self.player = Player('X')
コード例 #14
0
            "To play, the first player must first choose their playing symbol('X' or 'O') .\n"
            "They must then proceed to enter a number position as indicated on the game board \n"
            "in order to place their symbol .\n"
            "The game shall then turn to the next player after such a move.Welcome Mates!\n"
            "\n ************************************************************************************\n"
        )

        the_game = Game()  # Initialize the Game.

        player_on_turn = initial_player(
        )  # Allow the first user to choose their symbol.
        while player_on_turn[0]:

            the_game.show_board()  # Show the game board.

            player = Player(symbol=player_on_turn[1])
            # Allow a player to place their mark in a board position.
            player_input = player.input_mark()
            mark_indices = player.place_mark(player_input,
                                             board=the_game.board)

            while not mark_indices:
                # In case a player  enters an already taken position,then allow them to take another position.
                player_input = player.input_mark()
                mark_indices = player.place_mark(player_input,
                                                 board=the_game.board)

            if the_game.determine_win(mark_indices):
                # In case of a win, end game.
                print(Fore.LIGHTGREEN_EX,
                      Style.BRIGHT + "\nGet in there! You Win!")