コード例 #1
0
ファイル: pvp.py プロジェクト: ganatrat/tic-tac-toe-py
    def test_player_1_winner(self):
        game = Game(False)

        @patch('builtins.input')
        def run(m_input):
            m_input.side_effect = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
            game.start()

        run()
        game.declare_winner()
        state = game.get_state()
        self.assertTrue(state.get('is_player_1_winner'))
        self.assertEqual(['❌', '⭕', '❌', '⭕', '❌', '⭕', '❌', '8️⃣', '9️⃣'],
                         state.get('board'))
コード例 #2
0
 def test_mutates_game_state(self):
     game = Game()
     moves = Moves(game)
     assert moves.game is game
     assert moves.game.state is game.state
     moves.perform({"position": [1, 1], "player": "X"})
     self.assertEqual(len(game.state["moves"]), 1)
コード例 #3
0
 def test_move_must_be_within_board(self):
     game = Game()
     moves = Moves(game)
     move = {"position": [-1, 2], "player": "X"}
     self.assertRaises(
         Exception,
         moves.valid,
     )
コード例 #4
0
 def test_empty_positions_list(self):
     game = Game()
     moves = Moves(game)
     self.assertEqual(len(moves.get_empty_positions()), 9)
     move = {"position": [1, 1], "player": "X"}
     assert move["position"] not in moves.get_occupied_positions()
     moves.perform(move)
     assert move["position"] not in moves.get_empty_positions()
     self.assertEqual(len(moves.get_empty_positions()), 8)
コード例 #5
0
 def test_player_from_symbol(self):
    game = Game()
    player = Player(game)
    Moves(game).perform({
      "player": "X",
      "position": [1,1]
    })
    self.assertEqual(player.player_from_symbol("X"),
                     player.get_players()[0])
コード例 #6
0
ファイル: test_game.py プロジェクト: joshcampbell/tic-tac-toe
 def test_empty_constructor(self):
     """
   Game's constructor takes a single, optional parameter: a dict containing
   a complete or partial game state, which is merged with a copy of the 
   default state to produce the instance's starting state.
   """
     subject = Game()
     modified_default = default_state
     modified_default.update({"active_player": "X"})
     self.assertEqual(subject.state, modified_default)
コード例 #7
0
 def test_second_row_victory(self):
     game = Game()
     victory = Victory(game)
     moves = Moves(game)
     winning_line = [[2, 1], [2, 2], [2, 3]]
     winning_moves = [{
         "player": "X",
         "position": position
     } for position in winning_line]
     moves.perform_all(winning_moves)
     self.assertEquals(victory.winning_symbol(), "X")
コード例 #8
0
 def test_diagonal_victory(self):
     game = Game()
     victory = Victory(game)
     moves = Moves(game)
     winning_line = [[1, 1], [2, 2], [3, 3]]
     winning_moves = [{
         "player": "X",
         "position": position
     } for position in winning_line]
     moves.perform_all(winning_moves)
     assert victory.is_present()
コード例 #9
0
 def test_victory_by_filled_row(self):
     game = Game()
     victory = Victory(game)
     moves = Moves(game)
     # create a winning line consisting of the top row
     winning_line = [[1, 1], [1, 2], [1, 3]]
     winning_moves = [{
         "player": "X",
         "position": position
     } for position in winning_line]
     moves.perform_all(winning_moves)
     assert victory.is_present()
コード例 #10
0
 def test_victory_has_winner(self):
     game = Game()
     victory = Victory(game)
     moves = Moves(game)
     # create a winning line consisting of the top row
     winning_line = ThroughLine(game).rows()[0]
     winning_moves = [{
         "player": "X",
         "position": position
     } for position in winning_line]
     moves.perform_all(winning_moves)
     self.assertEquals(victory.winning_symbol(), "X")
コード例 #11
0
 def test_next_states_on_nearly_full_board(self):
         game = Game()
         subject = GameAIPresenter(game)
         subject.move(1,1,"O")
         self.assertEqual(len(subject.potential_next_states()),8)
         subject.move(2,2,"X")
         self.assertEqual(len(subject.potential_next_states()),7)
         subject.move(3,3,"O")
         self.assertEqual(len(subject.potential_next_states()),6)
         subject.move(1,2,"X")
         self.assertEqual(len(subject.potential_next_states()),5)
         subject.move(1,3,"O")
         self.assertEqual(len(subject.potential_next_states()),4)
コード例 #12
0
 def test_three_moves_required(self):
     game = Game()
     moves = Moves(game)
     victory = Victory(game)
     # create a winning line consisting of the top row
     winning_line = ThroughLine(game).rows()[0]
     winning_moves = [{
         "player": "X",
         "position": position
     } for position in winning_line]
     for move in winning_moves:
         moves.perform(move)
         assert not victory.is_present() or move["position"] == [1, 3]
コード例 #13
0
 def test_four_corners(self):
     corners = Board(Game()).corners()
     self.assertEqual(len(corners), 4)
コード例 #14
0
 def test_board_positions_are_lists(self):
     # they're not tuples because I have a dream of sending game states
     # back and forth as JSON
     positions = Board(Game()).positions()
     for position in positions:
         self.assertIsInstance(position, list)
コード例 #15
0
 def setUp(self):
   self.game = Game()
   self.subject = ThroughLine(self.game)
   self.board_size = Board(self.game).get_size()
コード例 #16
0
 def test_default_game_state_is_not_victorious(self):
     game = Game()
     victory = Victory(game)
     assert not victory.is_present()
コード例 #17
0
 def test_nondefault_valid_symbols(self):
   game = Game({ "players": [ { "symbol": "B", "name": "B"}, \
               {"symbol": "A", "name": "A"}] })
   player = Player(game)
   self.assertEqual(set(player.valid_symbols()),{"A","B"})
コード例 #18
0
ファイル: test_game.py プロジェクト: joshcampbell/tic-tac-toe
 def test_unknown_key_exception(self):
     with self.assertRaises(Exception):
         Game({"badkey": []})
コード例 #19
0
 def test_switching_active_player(self):
   game = Game()
   player = Player(game)
   self.assertEqual(player.get_active_symbol(),"X")
   player.switch_active()
   self.assertEqual(player.get_active_symbol(),"O")
コード例 #20
0
 def test_default_game_state_has_no_winner(self):
     game = Game()
     victory = Victory(game)
     self.assertEqual(victory.winning_symbol(), None)
コード例 #21
0
ファイル: play.py プロジェクト: ave19/t3
#! /usr/bin/env python

from t3.game import Game

g = Game()
g.play()

コード例 #22
0
 def setUp(self):
     self.g = Game()
コード例 #23
0
 def test_next_states_on_empty_board(self):
         game = Game()
         subject = GameAIPresenter(game)
         self.assertEqual(len(subject.potential_next_states()),9)
コード例 #24
0
 def test_nine_board_positions_by_default(self):
     game = Game()
     board = Board(game)
     self.assertEqual(len(board.positions()), 9)
コード例 #25
0
ファイル: test_game.py プロジェクト: joshcampbell/tic-tac-toe
 def test_update_board_size(self):
     subject = Game({"board": {"size": 5}})
     self.assertEqual(subject.state["board"]["size"], 5)
コード例 #26
0
 def test_get_opponent_symbol(self):
   game = Game()
   player = Player(game)
   symbol = player.get_opponent_symbol("O")
   self.assertEqual(symbol,"X")
コード例 #27
0
ファイル: test_game.py プロジェクト: joshcampbell/tic-tac-toe
 def test_clone_returns_copy(self):
     subject = Game()
     clone = subject.clone()
     assert subject is not clone
     assert subject.state is not clone.state
コード例 #28
0
 def test_starting_player_is_valid(self):
   game = Game({ "players": [ { "symbol": "B", "name": "B"}, \
               {"symbol": "A", "name": "A"}] })
   player = Player(game)
   self.assertIn(player.get_active_symbol(),player.valid_symbols())
コード例 #29
0
 def setUp(self):
     self.game = Game()
コード例 #30
0
 def test_update_symbol(self):
   game = Game({ "players": [ { "symbol": "X", "name": "X"},
               {"symbol": "Y", "name": "Y"}] })
   player = Player(game)