def main(): run = True clock = pygame.time.Clock() game = Game(WIN) start_screen() while run: clock.tick(FPS) if game.winner() != None: print(game.winner()) run = False for event in pygame.event.get(): if event.type == pygame.QUIT: run = False if event.type == pygame.MOUSEBUTTONDOWN: pos = pygame.mouse.get_pos() row, col = get_row_col_from_mouse(pos) game.select(row, col) game.update() end_screen()
def handle(self): gameInstance = Game() isPlayFirst = random.choice([True, False]) if isPlayFirst: gameInstance.setNextTurn(callBot(gameInstance.getInfo())) while not gameInstance.checkGameOver(): self.request.sendall(bytes(gameInstance.getInfo(), "ASCII")) try: ret = str(self.request.recv(8), "ASCII") except: self.request.sendall( bytes("ERROR: INVALID ASCII STRING ~ " + repr(ret), "ASCII")) return if ret != "NULL" and re.fullmatch("\w\d", ret) is None: self.request.sendall( bytes("ERROR: INVALID INPUT ~ " + repr(ret), "ASCII")) return if not gameInstance.setNextTurn(ret): self.request.sendall( bytes("ERROR: INVALID MOVE ~ " + repr(ret), "ASCII")) return if not gameInstance.checkGameOver(): gameInstance.setNextTurn(callBot(gameInstance.getInfo())) self.request.sendall(bytes(gameInstance.getFinalResult(), "ASCII"))
def test_checkGameOver(self): x = Game() self.assertFalse(x.checkGameOver()) x = Game() for i in x.victory_cells: x.board.data[x.board.getRowId(i[1]), x.board.getColumnId(i[0])] = 'W' self.assertTrue(x.checkGameOver()) self.assertEqual(x.getWinner(), "WHITE") x = Game() for i in x.victory_cells: x.board.data[x.board.getRowId(i[1]), x.board.getColumnId(i[0])] = 'B' self.assertTrue(x.checkGameOver()) self.assertEqual(x.getWinner(), "BLACK") x = Game() for (i, j) in itertools.product(range(8), range(8)): if i in range(0, 3): x.board.data[i, j] = 'W' if i in range(3, 5): x.board.data[i, j] = 'E' if i in range(5, 8): x.board.data[i, j] = 'B' x.victory_cells = ["a4", "a5", "b4", "b5", "c4"] self.assertTrue(x.checkGameOver()) self.assertEqual(x.getWinner(), "DRAW") x.winner = None x.board.data[3, 7] = 'W' self.assertTrue(x.checkGameOver()) self.assertEqual(x.getWinner(), "WHITE") x.winner = None x.board.data[3, 6] = 'W' x.board.data[4, 1] = 'B' self.assertTrue(x.checkGameOver()) self.assertEqual(x.getWinner(), "WHITE") x.winner = None x.board.data[4, 7] = 'B' self.assertTrue(x.checkGameOver()) self.assertEqual(x.getWinner(), "BLACK")
def test_getNextTurn(self): x = Game() self.assertEqual(x.getNextTurn(), "BLACK") x.history.append("a1") self.assertEqual(x.getNextTurn(), "WHITE") x.history.append("b1") self.assertEqual(x.getNextTurn(), "BLACK") x.history.append("c1") self.assertEqual(x.getNextTurn(), "WHITE") x.history.append("d1") self.assertEqual(x.getNextTurn(), "BLACK")
def test_setNextTurn(self): x = Game() y = Board() x.winner = "DRAW" self.assertFalse(x.setNextTurn("d3")) x.winner = None self.assertTrue(x.setNextTurn("d3")) y.data = np.array([['E', 'E', 'E', 'E', 'E', 'E', 'E', 'E'], ['E', 'E', 'E', 'E', 'E', 'E', 'E', 'E'], ['E', 'E', 'E', 'B', 'E', 'E', 'E', 'E'], ['E', 'E', 'E', 'B', 'B', 'E', 'E', 'E'], ['E', 'E', 'E', 'B', 'W', 'E', 'E', 'E'], ['E', 'E', 'E', 'E', 'E', 'E', 'E', 'E'], ['E', 'E', 'E', 'E', 'E', 'E', 'E', 'E'], ['E', 'E', 'E', 'E', 'E', 'E', 'E', 'E']]) self.assertTrue(np.array_equal(x.board.data, y.data)) self.assertEqual(x.history, ["d3"]) self.assertFalse(x.setNextTurn("e6")) self.assertTrue(x.setNextTurn("NULL")) self.assertEqual(x.getWinner(), "BLACK")