def generate_lifeforms(dirname): # load board from file board = Board(filename=config.life, rows=config.rows, cols=config.cols) # generate image to add to the images print "Generating life images. Please be patient, this may take a while.\n" while board.step < config.steps: print ".", board.execute(config.sample_rate) array = board.get_array() for manipulate in config.array_manipulators: array = manipulate[0](array, **manipulate[1]) if config.array_manipulators == (): img = scipy.misc.toimage(array) else: img = Image.fromarray(array) for manipulate in config.image_manipulators: img = manipulate[0](img, **manipulate[1]) filepath = os.path.join(dirname, "%d.png"%board.step) img.save(filepath)
def endscreen(message, Board): pygame.display.set_caption("Game Over! Result: %s" % message) head = big.render(message, True, (255, 0, 0) if message[0].lower() == "r" else ((0, 255, 0) if message[0].lower() == "g" else (0, 0, 0))) foot = med.render("Click to play again!", True, (0, 0, 0)) b = pygame.Surface(s) Board.draw(b) b.set_alpha(40) while True: screen.fill((0, 0, 255)) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: return pygame.draw.rect(screen, (255, 255, 255), ((50, 50), [i - 100 for i in s]), 0) screen.blit(head, head.get_rect(center=[s[0] / 2, s[1] / 2 - 50])) screen.blit(foot, foot.get_rect(center=[s[0] / 2, s[1] / 2 + 20])) screen.blit(b, (0, 0)) pygame.display.flip()
def main(stdscr): # Clear screen stdscr.clear() stdscr.addstr("test") stdscr.addstr(3, 3, "x") stdscr.addstr(6, 6, "x") stdscr.clear() n = 0 board = Board() while True: stdscr.clear() board.cursesDisplay(stdscr) p = stdscr.getyx() c = stdscr.getch() stdscr.move(2, 0) print(p) stdscr.move(1, 3) # if c == curses.KEY_UP: # stdscr.move(p[0] + 1, p[1]) # elif c == curses.KEY_DOWN: # stdscr.move(p[0] - 1, p[1]) # elif c == curses.KEY_LEFT: # stdscr.move(p[0], p[1] - 1) # elif c == curses.KEY_RIGHT: # stdscr.move(p[0], p[1] + 1) # elif c == curses.KEY_ENTER or c == 10 or c == 13: # stdscr.move(p[0] + 1, p[1]) n += 1
def main(): b = Board(4, debug=False, cubes=14) s = Screen(500, 500, b) running = True pygame.event.set_allowed(None) pygame.event.set_allowed([pygame.KEYDOWN, pygame.QUIT]) move = 'stay' while (not b.lost and running): for event in pygame.event.get(): if event.type == pygame.QUIT: # change the value to False, to exit the main loop running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_DOWN: move = 'DOWN' elif event.key == pygame.K_UP: move = 'UP' elif event.key == pygame.K_LEFT: move = 'LEFT' elif event.key == pygame.K_RIGHT: move = 'RIGHT' #move = input() #move = move.upper() b.move_board(move) move = 'stay' s.update_screen(b) return 0
def test_make_move(self): board = Board() figure = board.getFigure(WHITE, PAWN, 0) self.assertFalse(figure.moved) with self.assertRaises(errors.WrongMoveError): figure.move(2, 3) figure.move(1, 3) self.assertTrue(figure.moved)
def test_castle_1(self): board = Board('Ke1,Rh1,ke8') king = board.getFigure(WHITE, KING) rook = board.getFigure(WHITE, ROOK) self.assertEqual(str(king), 'Ke1') self.assertEqual(str(rook), 'Rh1') king.castle() self.assertEqual(str(king), 'Kg1') self.assertEqual(str(rook), 'Rf1')
def test_get_piece_at_position(self): """Fetch a piece we add to the board, fetch an empty square and make sure we get None.""" board, white = Board(), Player(Color.W) pos_white = [4, 6] # e2 pawn_white = Pawn(white, pos_white) board.add_to_board(pawn_white) self.assertTrue(board.get_piece_at_position(pos_white) is pawn_white) self.assertTrue(board.get_piece_at_position([1, 1]) is None)
def setUp(self): self.chips = [ ("black", 1, 0), ("black", 1, 1), ("black", 1, 2), ("white", 0, 2), ("white", 6, 2), ("white", 7, 2), ] self.board = Board()
def test_make_move_pawn_double_jump(self): """Verifies that making a double jump opens up the piece for en passant.""" board, white = Board(), Player(Color.W) pawn_pos_white = [5, 6] # f2 pawn_white = Pawn(white, pawn_pos_white) board.add_to_board(pawn_white) board.make_move(pawn_white, [5, 4]) self.assertTrue(board.en_passant is pawn_white)
def test_castle_2(self): board = Board('Ke1,Rh1,Ng1,ke8') king = board.getFigure(WHITE, KING) rook = board.getFigure(WHITE, ROOK) self.assertEqual(str(king), 'Ke1') self.assertEqual(str(rook), 'Rh1') with self.assertRaises(errors.WrongMoveError): king.castle() self.assertEqual(str(king), 'Ke1') self.assertEqual(str(rook), 'Rh1')
def test_check_if_empty(self): """Make sure check_if_empty returns True at first, and then False when we add a piece.""" board, white = Board(), Player(Color.W) pos_white = [4, 6] # e2 pawn_white = Pawn(white, pos_white) self.assertTrue(board.check_if_empty(pos_white)) board.add_to_board(pawn_white) self.assertFalse(board.check_if_empty(pos_white))
def main(): startscreen() P1 = Player1() P2 = Player2() while True: board = Board(P1, P2, s) pygame.display.set_caption( "Connect 4 by NIP |||| Red: %s |||| Green: %s" % (P1.wins, P2.wins)) while True: board.events() board.draw(screen) pygame.draw.circle(screen, (255, 0, 0) if board.turn else (0, 255, 0), pygame.mouse.get_pos(), 10, 0) pygame.draw.circle(screen, (255, 0, 0) if board.turn else (0, 255, 0), pygame.mouse.get_pos(), 30, 2) if board.check(): break pygame.display.flip() if board.check() == 1: if not board.turn: P1.wins += 1 else: P2.wins += 1 endscreen(("Red Wins!" if not board.turn else "Green Wins!") if board.check() == 1 else "Draw", board)
def play(self): self.game = Board() self.game.start() self.round = 1 pygame.init() pygame.display.set_caption('Chess') screen = pygame.display.set_mode(Settings.screen_size) screen.fill(Settings.bg_color) while True: self.update(screen) while not self.turn(screen): pass
def test_pawn_white_simple(self): """Tests that a simple forward move is in the pawn's move list.""" board, white = Board(), Player(Color.W) pawn_white = Pawn(white, [3, 4]) # d4 pawn_white.first_move = False board.add_to_board(pawn_white) correct_move = [3, 3] # d5 returned_moves = pawn_white.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 1) self.assertTrue(correct_move in returned_moves)
def test_pawn_black_simple(self): """Tests that a simple forward move is in the pawn's move list.""" board, black = Board(), Player(Color.B) pawn_black = Pawn(black, [3, 4]) # d4 pawn_black.first_move = False board.add_to_board(pawn_black) correct_move = [3, 5] # d3 returned_moves = pawn_black.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 1) self.assertTrue(correct_move in returned_moves)
def test_pawn_black_first_move(self): """Tests that two moves are available to a new unimpeded pawn.""" board, black = Board(), Player(Color.B) pawn_black = Pawn(black, [3, 1]) # d7 board.add_to_board(pawn_black) correct_move1 = [3, 2] # d6 correct_move2 = [3, 3] # d5 returned_moves = pawn_black.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 2) self.assertTrue(correct_move1 in returned_moves) self.assertTrue(correct_move2 in returned_moves)
def test_pawn_white_first_move(self): """Tests that two moves are available to a new unimpeded pawn.""" board, white = Board(), Player(Color.W) pawn_white = Pawn(white, [3, 6]) # d2 board.add_to_board(pawn_white) correct_move1 = [3, 5] # d3 correct_move2 = [3, 4] # d4 returned_moves = pawn_white.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 2) self.assertTrue(correct_move1 in returned_moves) self.assertTrue(correct_move2 in returned_moves)
def test_add_to_board(self): """Add a piece, verify the square is taken and that the board's pieces list has the piece in it.""" board, white = Board(), Player(Color.W) pos_white = [4, 6] # e2 pawn_white = Pawn(white, pos_white) board.add_to_board(pawn_white) # fetch the piece using xy_conversion self.assertTrue(board.board[xy_to_num(pos_white)] is pawn_white) # assure that the piece is in the board's pieces list. self.assertTrue(pawn_white in board.pieces)
def test_castling_normal(self): """Ensures that castling is properly returned in the King's moved list.""" board, white = Board(), Player(Color.W) king_white = King(white, [4, 7]) # e1 board.add_to_board(king_white) rook_white = Rook(white, [7, 7]) # h1 board.add_to_board(rook_white) castle_move = [6, 7] # g1 returned_moves = king_white.get_legal_moves(board, True) self.assertTrue(castle_move in returned_moves)
def test_simple(self): """Verifies that a knight's returned movelist has the correct number of moves and that the boundaries of it's movelist is correct.""" board, white = Board(), Player(Color.W) knight_white = Knight(white, [3, 4]) # d4 board.add_to_board(knight_white) # c6, e6, f5, f3, e2, c2, b3, b5 correct_move_list = [[2, 2], [4, 2], [5, 3], [5, 5], [4, 6], [2, 6], [1, 5], [1, 3]] returned_moves = knight_white.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 8) for move in correct_move_list: self.assertTrue(move in returned_moves)
def generate_board_and_save(board_name: str, config: Config, path_to_save: str = None, archive: bool = False) -> NoReturn: board = Board(board_name).setup(project_name=config.project_name, mips_type=config.mips_type, flt=config.configs, conf=config.functions_params, func=config.functions).generate() if archive: _ = board.archive(path=path_to_save) else: _ = board.dump(path=path_to_save)
def __init__(self, view_type): self.board = Board(10, 20) self.board.generate_piece() self.view_type = view_type self.game_over = False if view_type == TextView: def cls(): os.system('cls') self.show_action = cls self.max_fps = 5 else: self.show_action = None self.max_fps = 50
def test_path_impeded(self): """Verifies that a bishop's returned moves stops when encountering another of the bishop's owner's pieces.""" board, white = Board(), Player(Color.W) king_white = King(white, [3, 4]) # d4 board.add_to_board(king_white) pawn_white = Pawn(white, [2, 4]) # c4 board.add_to_board(pawn_white) incorrect_move1 = [2, 4] # c4 returned_moves = king_white.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 7) self.assertFalse(incorrect_move1 in returned_moves)
def test_castling_already_moved(self): """Ensures that castling is not returned in the King's moved list if the King has already moved.""" board, white = Board(), Player(Color.W) king_white = King(white, [4, 7]) # e1 king_white.first_move = False board.add_to_board(king_white) rook_white = Rook(white, [7, 7]) # h1 board.add_to_board(rook_white) castle_move = [6, 7] # g1 returned_moves = king_white.get_legal_moves(board, True) self.assertFalse(castle_move in returned_moves)
def test_path_impeded(self): """Verifies that a rook's returned moves stops when encountering another of the rook's owner's pieces.""" board, white = Board(), Player(Color.W) rook_white = Rook(white, [3, 4]) # d4 board.add_to_board(rook_white) pawn_white = Pawn(white, [2, 4]) # c4 board.add_to_board(pawn_white) incorrect_move1 = [2, 4] # c4 returned_moves = rook_white.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 11) self.assertTrue(incorrect_move1 not in returned_moves)
def board(board: str) -> Response: if board not in BOARDS: return create_error_response( ErrorCode.UNSUPPORTED_BOARD, description=f"There is no '{board}' in supported list: {BOARDS}" ) return jsonify({'board': board, 'params': Board(board).params})
def build(self): # Instantiate screen manager self.sm = ScreenManager() global sm sm = self.sm # Instantiate game engine self.game = Board() global game game = self.game # Set window size on desktop if platform.system() == 'Windows': Window.size = 540, 960 # Derive font sizes from window height self.window_height = Window.height self.largest = Window.height / 10 self.large = Window.height / 15 self.medium = Window.height / 20 self.small = Window.height / 25 self.smallest = Window.height / 30 self.tiny = Window.height / 40 # Declare screen manager and add screens sm.add_widget(MenuScreen(name='menu_screen')) sm.add_widget(InstructionScreen(name='instruct_screen')) sm.add_widget(GameScreen(name='game_screen')) sm.add_widget(EndScreen(name='end_screen')) # return screen manager as root widget return sm
def get_configured_board(config: Config) -> Board: return Board(config.board).setup( project_name=config.project_name, mips_type=config.mips_type, flt=config.configs, conf=config.functions_params, func=config.functions ).generate()
def test_pawn_black_promotion(self): """Tests that the four promotion moves are available to a promoting pawn.""" board, black = Board(), Player(Color.B) pawn_black = Pawn(black, [3, 6]) # d2 board.add_to_board(pawn_black) correct_move1 = [3, "Q"] # d1 correct_move2 = [3, "N"] # d1 correct_move3 = [3, "B"] # d1 correct_move4 = [3, "R"] # d1 returned_moves = pawn_black.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 4) self.assertTrue(correct_move1 in returned_moves) self.assertTrue(correct_move2 in returned_moves) self.assertTrue(correct_move3 in returned_moves) self.assertTrue(correct_move4 in returned_moves)
def test_pawn_white_promotion(self): """Tests that the four promotion moves are available to a promoting pawn.""" board, white = Board(), Player(Color.W) pawn_white = Pawn(white, [3, 1]) # d7 board.add_to_board(pawn_white) correct_move1 = [3, "Q"] # d8 correct_move2 = [3, "N"] # d8 correct_move3 = [3, "B"] # d8 correct_move4 = [3, "R"] # d8 returned_moves = pawn_white.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 4) self.assertTrue(correct_move1 in returned_moves) self.assertTrue(correct_move2 in returned_moves) self.assertTrue(correct_move3 in returned_moves) self.assertTrue(correct_move4 in returned_moves)
def test_simple(self): """Verifies that a rook's returned move list has the correct number of moves and that the boundaries of it's movelist is correct.""" board, white = Board(), Player(Color.W) rook_white = Rook(white, [3, 4]) # d4 board.add_to_board(rook_white) correct_move1 = [0, 4] # a4 correct_move2 = [7, 4] # h4 correct_move3 = [3, 0] # d8 correct_move4 = [3, 7] # d1 returned_moves = rook_white.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 14) self.assertTrue(correct_move1 in returned_moves) self.assertTrue(correct_move2 in returned_moves) self.assertTrue(correct_move3 in returned_moves) self.assertTrue(correct_move4 in returned_moves)
def test_simple(self): """Verifies that a bishops's returned movelist has the correct number of moves and that the boundaries of it's movelist is correct.""" board, white = Board(), Player(Color.W) bishop_white = Bishop(white, [3, 4]) # d4 board.add_to_board(bishop_white) correct_move1 = [7, 0] # h8 correct_move2 = [6, 7] # g1 correct_move3 = [0, 1] # a7 correct_move4 = [0, 7] # a1 returned_moves = bishop_white.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 13) self.assertTrue(correct_move1 in returned_moves) self.assertTrue(correct_move2 in returned_moves) self.assertTrue(correct_move3 in returned_moves) self.assertTrue(correct_move4 in returned_moves)
def test_figure(self): with self.app.test_request_context(): figure = Board('Ke1').getFigure(WHITE, KING) data = serializers.FigureSerializer(figure).calc() self.assertEqual(data['kind'], 'king') self.assertEqual(data['color'], 'white') self.assertEqual(data['position'], 'e1') self.assertEqual(sorted(data['moves']), sorted(['d1', 'd2', 'e2', 'f2', 'f1']))
def test_remove_a_piece(self): """Add a piece, then remove it, and verify space is empty""" board, white = Board(), Player(Color.W) pos_white = [4, 6] # e2 pawn_white = Pawn(white, pos_white) board.add_to_board(pawn_white) # emptySpace self.assertFalse(board.check_if_empty(pos_white)) board.remove_from_board(pos_white) self.assertTrue(board.check_if_empty(pos_white))
def test_try_to_castle_black(self): # short castle king = Board('Ke1,rh8,ke8').getFigure(BLACK, KING) self.assertFalse(king.try_to_castle(6, 8)) self.assertFalse(king.try_to_castle(8, 8)) self.assertEqual(king.try_to_castle(7, 8), '0-0') # long castle king = Board('Ke1,ra8,ke8').getFigure(BLACK, KING) self.assertFalse(king.try_to_castle(1, 8)) self.assertFalse(king.try_to_castle(2, 8)) self.assertFalse(king.try_to_castle(4, 8)) self.assertEqual(king.try_to_castle(3, 8), '0-0-0')
def test_board_with_passed_in_board(self): """Tests the creation of a board using a previous board's array. Each peice should have a copy created.""" board, white = Board(), Player(Color.W) pawn_white = Pawn(white, [3, 2]) # d6 board.add_to_board(pawn_white) new_board = Board(board.board) self.assertEqual(len(new_board.pieces), 1) self.assertFalse(new_board.check_if_empty([3, 2])) self.assertFalse(new_board.get_piece_at_position([3, 2]) is pawn_white)
def test_try_to_castle_white(self): # short castle king = Board('Ke1,Rh1,ke8').getFigure(WHITE, KING) self.assertFalse(king.try_to_castle(6, 1)) self.assertFalse(king.try_to_castle(8, 1)) self.assertEqual(king.try_to_castle(7, 1), '0-0') # long castle king = Board('Ke1,Ra1,ke8').getFigure(WHITE, KING) self.assertFalse(king.try_to_castle(1, 1)) self.assertFalse(king.try_to_castle(2, 1)) self.assertFalse(king.try_to_castle(4, 1)) self.assertEqual(king.try_to_castle(3, 1), '0-0-0')
def test_make_move_castle_queenside(self): """Ensures queenside castling moving works correctly.""" board, white = Board(), Player(Color.W) king_pos_white = [4, 7] # e1 rook_pos_white = [0, 7] # a1 king_white = King(white, king_pos_white) rook_white = Rook(white, rook_pos_white) board.add_to_board(king_white) board.add_to_board(rook_white) board.make_move(king_white, [2, 7]) # castle to c1 self.assertTrue(board.get_piece_at_position([2, 7]) is king_white) self.assertTrue(board.get_piece_at_position([3, 7]) is rook_white) self.assertTrue(board.get_piece_at_position([4, 7]) is None) self.assertTrue(board.get_piece_at_position([0, 7]) is None) self.assertTrue(board.get_piece_at_position([1, 7]) is None)
def test_simple(self): """Verifies that a king's returned movelist has the correct number of moves and that the boundaries of it's movelist is correct.""" board, white = Board(), Player(Color.W) king_white = King(white, [3, 4]) # d4 board.add_to_board(king_white) correct_move_list = [[3, 5]] # d3 correct_move_list += [2, 5], # c3 correct_move_list += [4, 5], # e3 correct_move_list += [4, 4], # e4 correct_move_list += [2, 4], # c4 correct_move_list += [4, 3], # e5 correct_move_list += [3, 3], # d5 correct_move_list += [2, 3], # c5 returned_moves = king_white.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 8) for move in correct_move_list: self.assertTrue(move in returned_moves)
def test_simple(self): """Verifies that a queen's returned movelist has the correct number of moves and that the boundaries of it's movelist is correct.""" board, white = Board(), Player(Color.W) queen_white = Queen(white, [3, 4]) # d4 board.add_to_board(queen_white) correct_move_list = [[7, 0]] # h8 correct_move_list += [6, 7], # g1 correct_move_list += [0, 1], # a7 correct_move_list += [0, 7], # a1 correct_move_list += [0, 4], # a4 correct_move_list += [7, 4], # h4 correct_move_list += [3, 0], # d8 correct_move_list += [3, 7], # d1 returned_moves = queen_white.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 27) for move in correct_move_list: self.assertTrue(move in returned_moves)
def test_make_move_pawn_promotion(self): """Verifies that making promition moves work appropriately.""" board, white = Board(), Player(Color.W) pawn_white = Pawn(white, [3, 1]) # d7 board.add_to_board(pawn_white) board.make_move(pawn_white, [3, 'Q']) # promotion at d8 square # ensure the new piece is a queen. self.assertTrue(isinstance(board.get_piece_at_position([3, 0]), Queen))
def test_make_move_simple(self): """Makes a simple move, verifies values are appropriately updated.""" board, white = Board(), Player(Color.W) rook_white = Rook(white, [3, 2]) # d6 board.add_to_board(rook_white) board.make_move(rook_white, [5, 2]) # f6 # ensure make_move updated piece position self.assertTrue(rook_white.position == [5, 2]) # ensure make_move updated board position self.assertTrue(board.get_piece_at_position([5, 2]) is rook_white) self.assertTrue(board.get_piece_at_position([3, 2]) is None)
def test_make_move_pawn_simple(self): """Makes a simple pawn move, verifies values are appropriately updated.""" board, white = Board(), Player(Color.W) pawn_white = Pawn(white, [3, 6]) # d2 board.add_to_board(pawn_white) board.make_move(pawn_white, [3, 4]) # d4 # ensure make_move updated piece position self.assertTrue(pawn_white.position == [3, 4]) # ensure make_move updated board position self.assertTrue(board.get_piece_at_position([3, 4]) is pawn_white) self.assertTrue(board.get_piece_at_position([3, 6]) is None)
def test_castling_to_check(self): """Ensures that castling is not returned in the King's moved list if the King would be castling into check.""" board, white, black = Board(), Player(Color.W), Player(Color.B) king_white = King(white, [4, 7]) # e1 board.add_to_board(king_white) rook_white = Rook(white, [7, 7]) # h1 board.add_to_board(rook_white) castle_move = [6, 7] # g1 returned_moves = king_white.get_legal_moves(board, True) self.assertTrue(castle_move in returned_moves) rook_black = Rook(black, [6, 0]) # g7 board.add_to_board(rook_black) returned_moves = king_white.get_legal_moves(board, True) self.assertFalse(castle_move in returned_moves)
def test_path_impeded(self): """Verifies that a bishop's returned moves stops when encountering another of the bishop's owner's pieces.""" board, white = Board(), Player(Color.W) queen_white = Queen(white, [3, 4]) # d4 board.add_to_board(queen_white) pawn_white1 = Pawn(white, [2, 3]) # c5 board.add_to_board(pawn_white1) incorrect_move1 = [2, 3] # c6 pawn_white2 = Pawn(white, [2, 4]) # c4 board.add_to_board(pawn_white2) incorrect_move2 = [2, 4] # c4 returned_moves = queen_white.get_legal_moves(board, True) self.assertTrue(len(returned_moves) == 21) self.assertTrue(incorrect_move1 not in returned_moves) self.assertTrue(incorrect_move2 not in returned_moves)
def test_get_all_legal_moves_simple(self): """Simply verify that getting the legal moves of two pieces sum to the returned value of get_all_legal_moves.""" board, white = Board(), Player(Color.W) pos_white = [4, 6] # e2 pawn_white = Pawn(white, pos_white) board.add_to_board(pawn_white) pos_white2 = [3, 3] # d5 rook_white = Rook(white, pos_white2) board.add_to_board(rook_white) combined_positions = [] for move in pawn_white.get_legal_moves(board, True): combined_positions += [pawn_white, move], for move in rook_white.get_legal_moves(board, True): combined_positions += [rook_white, move], self.assertEqual(board.get_all_legal_moves(white), combined_positions)