def test_board(): board = Board(15) left = Direction(-1, 0) right = Direction(1, 0) stay = Direction(0, 0) up = Direction(0, -1) down = Direction(0, 1) assert isinstance(board, object) assert isinstance(board.GRID_SIZE, int) assert board.GRID_SIZE == 15 assert isinstance(board.pacman, object) assert isinstance(board.ghosts, list) assert isinstance(board.score, int) row = board.pacman.get_row() col = board.pacman.get_col() board.move(up) assert board.score == 10 assert board.is_game_over() == False assert board.pacman.get_row() == row - 1 assert board.pacman.get_col() == col
def __init__(self, size=10): self.size = 0 while True: try: self.size = int(input("Please enter a size for the board:")) break except: print("Invalid number, please try again.") if self.size < 10 or self.size > 100: self.size = size self.board = Board(self.size)
def play_game(): """Activate the game logic.""" board = Board() print_color("Game starts!", fg=UI_C) # select order while True: first = input_color("Who starts first? (ai/me)\n", fg=UI_C) if first == "ai" or first == "me": break print_color("Try again!", fg=UI_C) if first == "me": turn_flag = True else: turn_flag = False # start main loop print(f"{'=' * 5}\n{board}\n{'=' * 5}") while True: if turn_flag: # start player input loop while True: try: turn = input_color( "Your turn (two comma-separated " f"integers in range(0, " f"{board.DIM_HINT})): ", fg=UI_C) turn = tuple(map(int, turn.split(","))) assert len(turn) == 2, "Please provide only two ints!" board.make_move(*turn, symbol=board.HUMAN) except ValueError: print_color("Input should be of form: 2,1", fg=UI_C) except (AssertionError, BeyondBoardError, OccupiedCellError) as e: print_color(e.args[0], fg=UI_C) else: print_color("Successful turn!", fg=board.COLOR_MAP[board.HUMAN]) break print(f"{'='*5}\n{board}\n{'='*5}") else: # make ai move board.make_move(*board.get_ai_move(verbose=True), symbol=board.AI) print_color("this one!", fg=board.COLOR_MAP[board.AI]) print(f"{'='*5}\n{board}\n{'='*5}") # check game state; exit if over state = board.game_state() if state == board.HUMAN: print_color("\nYou win!", fg=board.COLOR_MAP[board.HUMAN]) break elif state == board.AI: print_color("\nAI wins!", fg=board.COLOR_MAP[board.AI]) break elif state == board.EMPTY: print_color("\nIt's a draw!", fg=board.COLOR_MAP[board.EMPTY]) break else: turn_flag = not turn_flag print_color("Thank you for the game!", fg=UI_C)
def main(): size = c.SCREEN_SIZE board = Board(*size) screen = pygame.display.set_mode(size) # load bg block_bg = pygame.transform.scale( pygame.image.load(os.path.join('static', 'block_bg.png')), board.get_block_size() ) myfont = pygame.font.SysFont("monospace", 16) end_game = EndGame(myfont, c.WHITE, size, screen, board) game = Game(board, pygame, screen, end_game, block_bg, myfont) screen.fill(c.GRAY) while True: game.flow()
def main(): p1 = Player("X") p2 = Player("Y") players = [p1, p2] p1.has_turned = False b = Board() b.draw() game = True while game: for i, player in enumerate(players): if player.has_turned == False: val = input(f"{player.char}s Turn. Specify the Field on the Board you want to change: ") if int(val) <= 8: if b.check_val(val) == "0": b.set_val(player, val) player.has_turned = True if i == 0: players[1].has_turned = False elif i == 1: players[0].has_turned = False b.draw() ret = b.check_if_won(player) if ret == player.char: print(f"Player {player.char} has won!") game = False break elif ret == "Tie": print("Tie!") game = False break else: val = input(f"{player.char}s Turn. Specify the Field on the Board you want to change: ") else: print("[-] Please pick a Value between 0-8")
def play_game(): """ Starts tetris game in terminal. """ board_instance = Board(20) current_piece = Piece(board_instance) board_instance.print(current_piece) game = Game() player_move = input() while (game.status): msg = move.make_move(player_move, board_instance, current_piece) board_instance.print(current_piece, msg) game.is_over(board_instance, current_piece) player_move = input() print("GAME OVER!")
9: ("Outpost", 3), 10: ("Outpost", 3), 11: ("Monastery", 2), 12: ("Monastery", 3), 13: ("Harbour", 4), 14: ("Harbour", 3), 15: ("Harbour", 4), 16: ("Outpost", 4), 17: ("Outpost", 3), 18: ("Harbour", 3), 19: ("Harbour", 4), 20: ("Harbour", 4), 21: ("Harbour", 4), 22: ("Harbour", 3), 23: ("Harbour", 4) } if fieldOfFame: location_pool[24] = ("Township", 4) location_pool[25] = ("Township", 3) location_pool[26] = ("Township", 4) return pool, location_pool pool, location_pool = raiders_config_menu() bag = Bag(pool) board = Board(location_pool, bag) board.report()
def main(): ## Get file name filename = input('Enter the puzzles filename: ') ## Reading input file ## with open(filename) as f: puzzles = f.readlines() ## Setup ## for puzzle in puzzles: # Boards Setup puzzleIndex = puzzles.index(puzzle) print("Puzzle #: ", puzzleIndex) print(puzzle) elements = puzzle.split() board_size = int(elements[0]) max_depth = int(elements[1]) max_search_length = int(elements[2]) boardValues = list(elements[3]) counter = 0 board = np.zeros(shape=(board_size, board_size)) for i in range(board_size): for j in range(board_size): board[i][j] = boardValues[counter] counter += 1 # Board Setup myBoard = Board(board_size) myBoard.setBoard(board) myBoard.initializeBoardOnes() myBoard.initializeBoardZeros() # Get search algorithm from user algo = input('What algorithm do you wish to use ? ') isAlgoSupported = 0 if (algo == 'DFS'): # DFS setup isAlgoSupported = 1 game = DFSearch(max_depth, puzzleIndex) elif (algo == 'BFS'): # BFS setup isAlgoSupported = 1 game = BFSearch(max_search_length, puzzleIndex) elif (algo == 'A*'): # A* setup isAlgoSupported = 1 game = AStarSearch(max_search_length, puzzleIndex) elif (algo == 'All'): # run all algorithms print("Initial Board") print(myBoard.getBoard()) board1 = deepcopy(myBoard) board2 = deepcopy(myBoard) board3 = deepcopy(myBoard) DFSearch(max_depth, puzzleIndex).run(board1) BFSearch(max_search_length, puzzleIndex).run(board2) AStarSearch(max_search_length, puzzleIndex).run(board3) else: print('Algorithm not supported') if isAlgoSupported == 1: print("Initial Board") print(myBoard.getBoard()) game.run(myBoard)
class playGame: #initializes the game def __init__(self, size=10): self.size = 0 while True: try: self.size = int(input("Please enter a size for the board:")) break except: print("Invalid number, please try again.") if self.size < 10 or self.size > 100: self.size = size self.board = Board(self.size) #starts the game def start_game(self): self.board.print_board() while True: if self.board.is_game_over(): print("Game Over!") return self.move_characters() #moves the pacman and the ghost def move_characters(self): movedir = input("Please enter a direction for movement:") #moving up if movedir == 'U': if self.board.can_move(up): self.board.move(up) self.board.print_board() #moving down elif movedir == 'D': if self.board.can_move(down): self.board.move(down) self.board.print_board() #moving left elif movedir == 'L': if self.board.can_move(left): self.board.move(left) self.board.print_board() #moving right elif movedir == 'R': if self.board.can_move(right): self.board.move(right) self.board.print_board() #stay elif movedir == 'S': if self.board.can_move(stay): self.board.move(stay) self.board.print_board() else: print("Invalid moves, please try again.")
def play(): alpha = float("-inf") beta = float("inf") #init board board = Board() board = Tree(board) print(board.value) # stage 1 for i in range(9): while True: position = position_input("Postavite 'W'") if board.value.dict[position].middle is "O": break print("Odabrano polje je već zauzeto.") board.value.dict[position].middle = "W" if is_in_mill(position, "W", board.value): print(board.value) while True: mill = position_input("Uklonite 'B'") if board.value.dict[mill].middle is "B": if is_in_mill(mill, "B", board.value): print("Odabrana figura je u mici.") continue board.value.dict[mill].middle = "O" break print("Niste odabrali polje na kojem je 'B'.") start = time.time() best_move = minimaxAB(board, 3, True, alpha, beta, 1) end = time.time() board = best_move.board print(board.value) print("Vreme izvršavanja: " + str(end - start)) _, _, win, human, ai = diff_pieces_blocked("W", board.value) if win is 1: print("Pobedili ste!") exit() if win is -1: print("Izgubili ste!") exit() # stage 2 and 3 stage3 = False human, ai = count_pieces(board, "W") while True: while True: cords = move_position_input() if board.value.dict[cords[0]].middle is "W": if human is 3 or adjacent(cords): if board.value.dict[cords[1]].middle is "O": break else: print("Odabrano polje je već zauzeto.") else: print("Unešene koordinate nisu susedne.") else: print("Niste odabrali polje na kojem je 'W'.") board.value.dict[cords[0]].middle = "O" board.value.dict[cords[1]].middle = "W" if is_in_mill(cords[1], "W", board.value): print(board.value) while True: mill = position_input("Uklonite 'B'") if board.value.dict[mill].middle is "B": if is_in_mill(mill, "B", board.value): print("Odabrana figura je u mici.") continue board.value.dict[mill].middle = "O" break print("Niste odabrali polje na kojem je 'B'.") _, _, win, human, ai = diff_pieces_blocked("W", board.value) if win is 1: print("Pobedili ste!") exit() if ai is 3: stage3 = True if stage3: if human is ai: print("Nerešeno") exit() if stage3: start = time.time() best_move = minimaxAB(board, 1, True, alpha, beta, 3) else: start = time.time() best_move = minimaxAB(board, 4, True, alpha, beta, 2) end = time.time() board = best_move.board print(board.value) print("Vreme izvršavanja: " + str(end - start)) _, _, win, human, ai = diff_pieces_blocked("W", board.value) if win is -1: print("Izgubili ste!") exit() if stage3: if human is ai: print("Nerešeno") exit()
player, board.value) _, three_diff = diff_double_three(player, board.value) return 18 * closed + 26 * mill_diff + blocked_diff + 9 * piece_diff + 10 * two_diff + 7 * three_diff + 10 * blocked_mill_diff elif stage is 2: mill_diff, _, blocked_mill_diff = diff_mills_and_two( player, board.value) piece_diff, blocked_diff, win, _, _ = diff_pieces_blocked( player, board.value) double_diff, _ = diff_double_three(player, board.value) return 14 * closed + 43 * mill_diff + 10 * blocked_diff + 8 * piece_diff + 42 * double_diff + 1086 * win + 15 * blocked_mill_diff else: playa, opponent = count_pieces(board, player) _, two_diff, blocked_mill_diff = diff_mills_and_two( player, board.value) _, three_diff = diff_double_three(player, board.value) if playa is 2: win = -1 elif opponent is 2: win = 1 else: win = 0 return 16 * closed + 10 * two_diff + three_diff + 1190 * win + 20 * blocked_mill_diff if __name__ == '__main__': board = Board() _, _, win, human, ai = diff_pieces_blocked("W", board) if win is -1: print("Izgubili ste!") exit() print("no.")