本文整理汇总了Python中book.Book.check_grid方法的典型用法代码示例。如果您正苦于以下问题:Python Book.check_grid方法的具体用法?Python Book.check_grid怎么用?Python Book.check_grid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类book.Book
的用法示例。
在下文中一共展示了Book.check_grid方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestPlayerOneLogic
# 需要导入模块: from book import Book [as 别名]
# 或者: from book.Book import check_grid [as 别名]
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='O', 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')
示例2: start_game
# 需要导入模块: from book import Book [as 别名]
# 或者: from book.Book import check_grid [as 别名]
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
示例3: TestPlayerTwoLogic
# 需要导入模块: from book import Book [as 别名]
# 或者: from book.Book import check_grid [as 别名]
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='X', 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')