本文整理汇总了Python中board.Board.check方法的典型用法代码示例。如果您正苦于以下问题:Python Board.check方法的具体用法?Python Board.check怎么用?Python Board.check使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类board.Board
的用法示例。
在下文中一共展示了Board.check方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import check [as 别名]
class Game:
def __init__(self):
self.board = Board(6)
self.next_player = None
def setPlayer1(self, player1):
self.player1 = player1
def setPlayer2(self, player2):
self.player2 = player2
def setFirstMover(self, player):
self.next_player = player
def getNextPlayer(self):
if self.next_player == self.player1:
self.next_player = self.player2
else:
self.next_player = self.player1
return self.next_player
def getBoard(self):
return str(self.board.board)
def gameover(self):
ifwinner = self.board.check()
return (ifwinner != "NOT_FULL")
def getWinner(self):
ifwinner = self.board.check()
if ifwinner != "NOT_FULL" and len(ifwinner) == 1:
print(ifwinner + " wins")
return ifwinner
else:
print(ifwinner);
return "";
def makeMove(self, player, move):
if self.gameover():
print "Game Over"
return;
if player != self.next_player:
print "Illegal move, NOT_YOUR_TURN";
return;
if not self.board.validateMove(move):
print "Illegal move, make your move again"
return;
self.board.write(self.next_player.symbol, move)
self.next_player = self.getNextPlayer()
示例2: __init__
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import check [as 别名]
class Game:
def __init__(self):
self.board = Board(6)
def setPlayer1(self, player1):
self.player1 = player1
def setPlayer2(self, player2):
self.player2 = player2
def setFirstPlayer(self, player):
self.firstPlayer = player
def run(self):
next_player = self.firstPlayer
while 1:
self.board.show_board()
ifwinner = self.board.check()
if ifwinner != "NOT_FULL":
if ifwinner == "FULL":
print("Nobody wins, game is draw")
else:
print(ifwinner + " wins")
break
print(next_player.symbol + " turn")
time.sleep(1)
coord = next_player.getNextMove(self.board.makeCopy())
if self.board.validateMove(coord):
self.board.write(next_player.symbol, coord)
else:
print("Invalid move: " + str(coord))
continue
if next_player == self.player1:
next_player = self.player2
else:
next_player = self.player1
示例3: Board
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import check [as 别名]
if __name__ == '__main__':
board = Board(screen)
player = 2
moves = 9
while True:
screen.fill(BLACK)
board.draw()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
row, line = pygame.mouse.get_pos()
line //= 200
row //= 200
if board.check(row, line) == 'can be placed here':
if player == 1:
player = 2
elif player == 2:
player = 1
board.color(player, line, row)
moves -= 1
if board.check_win(1) == 'win':
screen.fill(BLUE)
pygame.display.flip()
pygame.time.wait(1000)
board.reset()
elif board.check_win(2) == 'win':
screen.fill(GREEN)
pygame.display.flip()
pygame.time.wait(1000)
示例4: Game
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import check [as 别名]
class Game():
def __init__(self):
pygame.init()
self.surface = pygame.display.set_mode(screen_size, DOUBLEBUF)
pygame.display.set_caption('Tetris by wukat')
self.startScreen = StartScreen(self.surface)
self.gamestate = self.startScreen.loop() # 1 - run, 0 - exit, -1 - pause
self.overScreen = GameOverScreen(self.surface)
self.board = Board(self.surface) # games board
self.quit = Button(self.surface, "QUIT", screen_size[0] - 60, screen_size[1] - 30, 25)
self.pause = Button(self.surface, "PAUSE", screen_size[0] - 76, screen_size[1] - 65, 25)
self.gameOverText = Text(self.surface, "GAME OVER", True, 70, WHITE, backgroundcolor, screen_size[0] // 2, screen_size[1] * 3 / 8)
if self.gamestate == 1:
while self.loop(): # loop returns 1 - play again, 0 - quit
pass
self.game_exit()
def game_exit(self):
pygame.quit()
def loop(self):
self.board.reset() # initialization of everything
self.pause.draw()
self.quit.draw()
self.acttime = 0
self.checktime = 0
self.hideMouse()
pygame.key.set_repeat(80, 80)
while self.gamestate: # main loop
if not self.readEvents() and self.gamestate == 1:
temp = pygame.time.get_ticks() # get time
if -(self.acttime - temp) > self.board.time: # time
self.board.block.moveDown()
self.checktime = temp + 100
self.acttime = temp
if -(self.checktime - temp) > 150: # time
self.checktime = temp
self.board.checkBlock() # check if blocks lays
self.board.draw()
if self.board.isGameOver(): # when the game is over gameoverscreen runs
self.gameOverText.show()
pygame.display.update()
pygame.time.wait(1000)
self.overScreen.show(self.board.point)
return self.overScreen.loop() # loop return 1 or 0 - restart or quit
return 0
def hideMouse(self): # if mouse under the board, hide
x, y = pygame.mouse.get_pos()
if x >= 0 and x <= NCOLS * size + 5 and y >= 0 and y <= NROWS * size + 5:
pygame.mouse.set_visible(False)
else:
pygame.mouse.set_visible(True)
def readEvents(self): # events
flag = 0
for event in pygame.event.get():
if (event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE)) or (event.type == MOUSEBUTTONDOWN and self.quit.checkIfHover(pygame.mouse.get_pos())):
self.gamestate = 0
flag = 1
elif (event.type == KEYDOWN and event.key == K_SPACE) or (event.type == MOUSEBUTTONDOWN and self.pause.checkIfHover(pygame.mouse.get_pos())):
self.gamestate *= -1
flag = 1
elif (event.type == KEYDOWN and (event.key == K_w or event.key == K_UP)) and self.gamestate == 1:
self.board.block.rotate()
flag = 1
elif (event.type == KEYDOWN and (event.key == K_s or event.key == K_DOWN)) and self.gamestate == 1:
self.board.block.moveDown()
flag = 1
elif (event.type == KEYDOWN and (event.key == K_d or event.key == K_RIGHT)) and self.gamestate == 1:
self.board.block.move(1, 0)
flag = 1
elif (event.type == KEYDOWN and (event.key == K_a or event.key == K_LEFT)) and self.gamestate == 1:
self.board.block.move(-1, 0)
flag = 1
elif event.type == MOUSEMOTION: # check if buttons are howered
pos = pygame.mouse.get_pos()
if self.quit.checkIfHover(pos):
self.quit.hover = 1
else:
self.quit.hover = 0
if self.pause.checkIfHover(pos):
self.pause.hover = 1
else:
self.pause.hover = 0
self.pause.drawBorder()
self.quit.drawBorder()
self.hideMouse()
self.board.actualize(self.board.check()) # actualization of the game (check if any rows are full)
if flag:
self.checktime += 150
return flag
示例5: TestBoard
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import check [as 别名]
class TestBoard(unittest.TestCase):
def setUp(self):
"""Set up an empty 10x10 board."""
self.board = Board(10)
def tearDown(self):
"""Clean up."""
self.board = None
def testRandomBoardNoShips(self):
"""Test making a random board with no ships."""
self.board.random_board(0, 0, 0, 0)
self.assertEqual(self.board.board, {}, "Board should be empty not %s."\
% (str(self.board.board)))
def testRandomBoardOneShip(self):
"""Test making a random board with a single ship."""
self.board.random_board(1, 0, 0, 0)
self.assertTrue("DEST1" in self.board.board.values(), \
"DEST1 should be on the board, not %s."\
% (str(self.board.board.values())))
self.assertEqual(len(self.board.board), 2, \
"Board should have 2 co-ord, not %d." \
% (len(self.board.board)))
def testRandomBoardTwoCarrier(self):
"""Test making a random 6x6 board with 2 carriers."""
self.board = Board(6)
self.board.random_board(0, 0, 0, 2)
self.assertTrue("CARR1" in self.board.board.values(), \
"CARR1 should be on the board, not %s."\
% (str(self.board.board.values())))
self.assertEqual(len(self.board.board), 10, \
"Board should have 5 co-ord, not %d." \
% (len(self.board.board)))
def testRandomMultipleSameShips(self):
"""Test making a random board with multiples of the same ship."""
self.board.random_board(2, 0, 0, 0)
self.assertTrue("DEST1" in self.board.board.values(), \
"DEST1 should be on the board, not %s."\
% (str(self.board.board.values())))
self.assertTrue("DEST2" in self.board.board.values(), \
"DEST2 should be on the board, not %s."\
% (str(self.board.board.values())))
self.assertEqual(len(self.board.board), 4, \
"Board should have 4 co-ord, not %d." \
% (len(self.board.board)))
def testRandomDifferentShips(self):
"""Test making a random board with all different types of ships."""
self.board.random_board(1, 1, 1, 1)
self.assertTrue("DEST1" in self.board.board.values(), \
"DEST1 should be on the board, not %s."\
% (str(self.board.board.values())))
self.assertTrue("SUBM1" in self.board.board.values(), \
"DEST2 should be on the board, not %s."\
% (str(self.board.board.values())))
self.assertTrue("BATT1" in self.board.board.values(), \
"BATT1 should be on the board, not %s."\
% (str(self.board.board.values())))
self.assertTrue("CARR1" in self.board.board.values(), \
"CARR1 should be on the board, not %s."\
% (str(self.board.board.values())))
self.assertEqual(len(self.board.board), 14, \
"Board should have 14 different co-ord, not %d." \
% (len(self.board.board)))
def testRandomOnBoard(self):
"""Test making a random board with multiple different types of ship."""
self.board.random_board(2, 2, 2, 2)
for coord in self.board.board:
self.assertTrue(0 <= coord[0] <= 9,\
"Coord %s is outside the board." % (str(coord)))
self.assertTrue(0 <= coord[1] <= 9, \
"Co-ord %s is outside the board." % (str(coord)))
def testCheckValidSpace(self):
"""Test to see if a ship can be placed at an empty cordinate."""
self.assertTrue(self.board.check(5, 5, 2, "U"), "Co-ord is free, \
should have returned True.")
def testCheckFirstCoordOverlap(self):
"""Test a ship starting from another ship, moving to a free space."""
self.board.board = {(0, 0): "DEST1", (0, 1): "DEST1"}
self.assertFalse(self.board.check(0, 0, 2, "D"), \
"Co-ord is taken, should have returned False.")
def testCheckSecondCoordOverlap(self):
#.........这里部分代码省略.........