Ejemplo n.º 1
0
class TestPlayerOneLogic(unittest.TestCase):
    ''' Unittests for the logic patterns if the book is the first player
    '''
    def setUp(self):
        self.grid = Grid()
        self.book = Book('X')
        
    def run_game(self, moves):
        '''Actually runs the game until someone wins, or there are no more squares available
        '''
        while not self.grid.test_win() and self.grid.get_available():
            self.grid = self.book.check_grid(self.grid)
            try:
                self.grid = self.grid.fill_square(user='******', square=moves.pop())
            except:
                break
            
        
    def test_first_center_corner(self):
        ''' Game for the second player taking the center, then a corner
        '''
        moves = ['5', '7', '2']
        moves.reverse() # reversing so I can pop them off
        self.run_game(moves)
        self.assertEquals(self.grid.test_win(), 'X')
        
    def test_first_center_edge(self):
        ''' Game for the second player taking the center, then an edge
        '''
        moves = ['5', '8', '3', '4']
        moves.reverse()
        self.run_game(moves)
        self.assertFalse(self.grid.get_available())
        
    def test_first_corner(self):
        ''' Game for the second player taking a corner
        '''
        moves = ['3', '5', '4']
        moves.reverse()
        self.run_game(moves)
        self.assertEquals(self.grid.test_win(), 'X')
    
    def test_first_edge_nothreat(self):
        ''' Game where the second player takes an edge, followed by a non-threat move
        '''
        moves = ['4', '9', '7']
        moves.reverse()
        self.run_game(moves)
        self.assertEquals(self.grid.test_win(), 'X')
        
    def test_first_edge_threat(self):
        ''' Game where the second player takes an edge, followed by a threat
        '''
        moves = ['6', '9', '7']
        moves.reverse()
        self.run_game(moves)
        self.assertEquals(self.grid.test_win(), 'X')
Ejemplo n.º 2
0
def start_game(grid, human):
    ''' Starts the actual tic-tac-toe game
    '''
    if human == 'O':
        ai = Book('X')        
    else:
        ai = Book('O')
        while 1==1:
            # A turn for the human if they want to go first.
            grid.print_grid()
            square = raw_input("What square would you like to fill? (1-9) ")
            if square[0] in grid.get_available():
                grid = grid.fill_square(user=human, square=square[0])
                break
            else:
                print "I'm sorry, %s isn't available." % square  
    
    # Main game loop
    while 1 == 1:
        grid = ai.check_grid(grid)
        if grid.test_win():
            grid.print_grid()
            print "Computer won!"
            break
        while 1==1:
            grid.print_grid()
            square = raw_input("What square would you like to fill? (1-9) ")
            available = grid.get_available()
            if square[0] not in available:
                print "I'm sorry, %s isn't available." % square
            else:
                grid = grid.fill_square(user=human, square=square[0])
                grid.print_grid()
                break
        if grid.test_win() == 'Draw':
            print "Draw! No one won!"
            break
        if grid.test_win():
            print "You won!"
            break        
Ejemplo n.º 3
0
class TestPlayerTwoLogic(unittest.TestCase):
    ''' Unittests for when the book goes second
    '''
    
    def setUp(self):
        self.grid = Grid()
        self.book = Book('O')
        
    def run_game(self, moves):
        '''Actually runs the game until someone wins, or there are no more squares available
        '''
        while not self.grid.test_win() and self.grid.get_available():
            try:
                self.grid = self.grid.fill_square(user='******', square=moves.pop())
            except:
                break
            self.grid = self.book.check_grid(self.grid)
        
    def test_noncenter_threat_threat(self):
        ''' Game where the first player does not fill the center, then follows with two threats
        '''
        moves = ['1', '4', '3', '8', '9']
        moves.reverse()
        self.run_game(moves)
        self.assertEquals(self.grid.test_win(), 'Draw')
            
    def test_noncenter_threat_nothreat(self):
        ''' Game where the first player does not fill the center, then follows with one threat
        '''
        moves = ['7', '9', '2', '6', '1']
        moves.reverse()
        self.run_game(moves)
        self.assertEquals(self.grid.test_win(), 'Draw')
        
    def test_corner_edge(self):
        ''' Game where the first player does not fill the center, and has filled a corner and an edge
        '''
        moves = ['1', '6', '7', '2', '8']
        moves.reverse()
        self.run_game(moves)
        self.assertEquals(self.grid.test_win(), 'Draw')
        
    def test_two_edges_on_corner(self):
        ''' Game where the first player does not fill the center, and has filled two edges that boarder the same corner
        '''
        moves = ['4', '2','9','7', '6']
        moves.reverse()
        self.run_game(moves)
        self.assertEquals(self.grid.test_win(), 'Draw')
        
    def test_two_edges_no_corner(self):
        ''' Game where the first player does not fill the center, and has filled two edges that do not share a corner
        '''
        moves = ['4', '6', '8', '9']
        moves.reverse()
        self.run_game(moves)
        self.assertEquals(self.grid.test_win(), 'O')
        
    def test_caddy_corners(self):
        ''' Game where the first player does not fill the center, and has filled caddy corners.
        '''
        moves = ['1', '9', '8',  '3', '4']
        moves.reverse()
        self.run_game(moves)
        self.assertEqual(self.grid.test_win(), 'Draw')
    
    def test_center_threat(self):
        ''' game where the first player takes the center, then threatens a win
        '''
        moves = ['5', '7', '2', '6', '9']
        moves.reverse()
        self.run_game(moves)
        self.assertEqual(self.grid.test_win(), 'Draw')
        
    def test_center_nothreat(self):
        ''' Game where the first player takes center, and doesn't threaten a win on their next move
        '''
        moves = ['5', '9', '2', '4', '7']
        moves.reverse()
        self.run_game(moves)
        self.assertEqual(self.grid.test_win(), 'Draw')