コード例 #1
0
class TestComputerPlayer(unittest.TestCase):
  def setUp(self):
    self.comp_player = ComputerPlayer()
    self.board = TestBoard(9)
    self.ttt_rules = TTTGameRules(WIN_SETS)
    self.X = self.board.valid_teams_list[0]
    self.O = self.board.valid_teams_list[1]
  
  def test_makes_a_winning_move_if_it_is_available(self):
    self.board.set_board({0: self.X, 2: self.O, 3: self.O, 4: self.O, 6: self.X, 8: self.X})
    self.assertEqual(7, self.comp_player.get_next_move(self.board, self.ttt_rules))
  
  def test_blocks_an_opponents_winning_move_if_a_winning_move_isnt_available(self):
    self.board.set_board({0: self.X, 4: self.O, 1: self.X})
    self.assertEqual(2, self.comp_player.get_next_move(self.board, self.ttt_rules))
  
  def test_wont_set_self_up_for_trap(self):
    self.board.set_board({0: self.X})
    move = self.comp_player.get_next_move(self.board, self.ttt_rules)
    self.assertNotEqual(1, move)
    self.assertNotEqual(3, move)
    self.assertNotEqual(5, move)
    self.assertNotEqual(7, move)
  
  def test_kiddie_corner_trap(self):
    self.board.set_board({0: self.X, 4: self.O, 8: self.X})
    move = self.comp_player.get_next_move(self.board, self.ttt_rules)
    self.assertNotEqual(2, move)
    self.assertNotEqual(6, move)
  
  def test_triangle_trap(self):
    self.board.set_board({0: self.O, 4: self.X, 8: self.X})
    move = self.comp_player.get_next_move(self.board, self.ttt_rules)
    self.assertNotEqual(1, move)
    self.assertNotEqual(3, move)
    self.assertNotEqual(5, move)
    self.assertNotEqual(7, move)
  
  def test_corner_trap(self):
    self.board.set_board({1: self.X, 4: self.O, 5: self.X})
    move = self.comp_player.get_next_move(self.board, self.ttt_rules)
    self.assertNotEqual(3, move)
    self.assertNotEqual(6, move)
    self.assertNotEqual(7, move)
  
  def test_disguised_corner_trap(self):
    self.board.set_board({5: self.X, 0: self.O, 7: self.X})
    move = self.comp_player.get_next_move(self.board, self.ttt_rules)
    self.assertNotEqual(1, move)
    self.assertNotEqual(3, move)
    self.assertNotEqual(4, move)
    self.assertNotEqual(8, move)
  
  def test_makes_a_random_move_only_if_there_are_no_other_moves_on_the_board(self):
    self.board.set_board({})
    self.comp_player.random_move = lambda low, high, rules, board: 100
    move = self.comp_player.get_next_move(self.board, self.ttt_rules)
    self.assertEqual(100, move)
コード例 #2
0
class TestComputerPlayer(unittest.TestCase):
    def setUp(self):
        self.comp_player = ComputerPlayer()
        self.board = TestBoard(9)
        self.ttt_rules = TTTGameRules(WIN_SETS)
        self.X = self.board.valid_teams_list[0]
        self.O = self.board.valid_teams_list[1]

    def test_makes_a_winning_move_if_it_is_available(self):
        self.board.set_board({0: self.X, 2: self.O, 3: self.O, 4: self.O, 6: self.X, 8: self.X})
        self.assertEqual(7, self.comp_player.get_next_move(self.board, self.ttt_rules))

    def test_blocks_an_opponents_winning_move_if_a_winning_move_isnt_available(self):
        self.board.set_board({0: self.X, 4: self.O, 1: self.X})
        self.assertEqual(2, self.comp_player.get_next_move(self.board, self.ttt_rules))

    def test_wont_set_self_up_for_trap(self):
        self.board.set_board({0: self.X})
        move = self.comp_player.get_next_move(self.board, self.ttt_rules)
        self.assertNotEqual(1, move)
        self.assertNotEqual(3, move)
        self.assertNotEqual(5, move)
        self.assertNotEqual(7, move)

    def test_kiddie_corner_trap(self):
        self.board.set_board({0: self.X, 4: self.O, 8: self.X})
        move = self.comp_player.get_next_move(self.board, self.ttt_rules)
        self.assertNotEqual(2, move)
        self.assertNotEqual(6, move)

    def test_triangle_trap(self):
        self.board.set_board({0: self.O, 4: self.X, 8: self.X})
        move = self.comp_player.get_next_move(self.board, self.ttt_rules)
        self.assertNotEqual(1, move)
        self.assertNotEqual(3, move)
        self.assertNotEqual(5, move)
        self.assertNotEqual(7, move)

    def test_corner_trap(self):
        self.board.set_board({1: self.X, 4: self.O, 5: self.X})
        move = self.comp_player.get_next_move(self.board, self.ttt_rules)
        self.assertNotEqual(3, move)
        self.assertNotEqual(6, move)
        self.assertNotEqual(7, move)

    def test_disguised_corner_trap(self):
        self.board.set_board({5: self.X, 0: self.O, 7: self.X})
        move = self.comp_player.get_next_move(self.board, self.ttt_rules)
        self.assertNotEqual(1, move)
        self.assertNotEqual(3, move)
        self.assertNotEqual(4, move)
        self.assertNotEqual(8, move)

    def test_makes_a_random_move_only_if_there_are_no_other_moves_on_the_board(self):
        self.board.set_board({})
        self.comp_player.random_move = lambda low, high, rules, board: 100
        move = self.comp_player.get_next_move(self.board, self.ttt_rules)
        self.assertEqual(100, move)
コード例 #3
0
class TTTGameRunner():
  def __init__(self):
    WIN_SETS = [[0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [2,4,6]]
    self.board = TTTBoard(9)
    self.rules = TTTGameRules(WIN_SETS)
    self.io = TerminalTTTIO()
    self.ai = ComputerPlayer()
    
  def _get_valid_human_team(self):
    selected_team = self.io.get_human_team(self.board)
    while not selected_team in self.board.valid_teams_list:
      self.io.invalid_team()
      selected_team = self.io.get_human_team(self.board)
    return selected_team
  
  def _make_valid_move(self):
    try:
      move_location = int(self.io.get_next_move())
      self.board.fill_space(move_location, self.rules.active_team(self.board))
    except Exception as e:
      self.io.invalid_move(e.args[0])
      self._make_valid_move()
  
  def _take_turn(self, current_team):
    if current_team == self.human_team:
      self._make_valid_move()
    if current_team != self.human_team:
      self.io.thinking()
      move_location = self.ai.get_next_move(self.board, self.rules)
      self.board.fill_space(move_location, current_team)

  def run_game(self):
    self.human_team = self._get_valid_human_team()
    
    while not self.rules.is_game_over(self.board):
      self.io.show_board(self.board)
      self._take_turn(self.rules.active_team(self.board))
    
    self.game_over()

  def game_over(self):
    self.io.show_board(self.board)
    winner = self.rules.get_winner(self.board)
    if winner == None: self.io.tie_game()
    if winner != None: self.io.show_winner(winner)
コード例 #4
0
 def setUp(self):
   self.comp_player = ComputerPlayer()
   self.board = TestBoard(9)
   self.ttt_rules = TTTGameRules(WIN_SETS)
   self.X = self.board.valid_teams_list[0]
   self.O = self.board.valid_teams_list[1]
コード例 #5
0
 def __init__(self):
   WIN_SETS = [[0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [2,4,6]]
   self.board = TTTBoard(9)
   self.rules = TTTGameRules(WIN_SETS)
   self.io = TerminalTTTIO()
   self.ai = ComputerPlayer()
コード例 #6
0
 def setUp(self):
     self.comp_player = ComputerPlayer()
     self.board = TestBoard(9)
     self.ttt_rules = TTTGameRules(WIN_SETS)
     self.X = self.board.valid_teams_list[0]
     self.O = self.board.valid_teams_list[1]