def mainGame(): game=Game(WIN) run=True clock=pygame.time.Clock() while run: clock.tick(FPS) if w:=game.winner(): pygame.time.delay(1000) confirm=messagebox.askyesnocancel('Game OVER',w+" IS THE WINNER\nWant to restart??") if confirm: game.reset() else: run=False if game.turn==WHITE: game.thinking=True game.update() _, new_board=minimax(game.get_board(),AI_LEVEL,visualize=VISUALISE,game=game) game.thinking=False pygame.time.delay(200) game.ai_move(new_board) for event in pygame.event.get(): if event.type==pygame.QUIT: run=False break if event.type==pygame.MOUSEBUTTONDOWN: row,col=get_click_pos(pygame.mouse.get_pos()) game.select(row,col) game.update()
def main(): pygame.mixer.init() mixer.music.load('background.wav') mixer.music.play(-1) run = True clock = pygame.time.Clock() game = Game(WIN) while True: clock.tick(FPS) winner = game.winner() if winner != None: messagebox.showinfo( "Winner", "{0}! You won smarty pants!".format("Red" if winner == ( 255, 0, 0) else "Blue")) break if game.turn == BLUE: value, new_board = minimax(game.get_board(), 3, BLUE, game) game.ai_move(new_board) for event in pygame.event.get(): if event.type == pygame.QUIT: break if event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = pygame.mouse.get_pos() row, col = get_row_col_from_mouse(mouse_pos) game.select(row, col) game.update() pygame.quit()
def main(): #print(evaluation([['X', 'O', 'X'], ['O', 'X', 'O'], ['X', '', '']])) clock = pygame.time.Clock() game = Game(WIN) run = True while run: z = game.check_if_end() # print(game.copy_board()) if z: print(game.score) pygame.time.wait(500) if game.turn == "O": x = minimax(game.copy_board(), True, 1)[1] game.set_board(x) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False if event.type == pygame.MOUSEBUTTONDOWN: game.select(get_row_col(pygame.mouse.get_pos())) game.draw()
def main(): run = True clock = pygame.time.Clock() game = Game(WIN) while run: clock.tick(FPS) if game.turn == WHITE: value, new_board = minimax(game.get_board(), 3, WHITE, game) game.ai_move(new_board) if game.winner() != None: if (game.winner() != WHITE): print("WHITE WON!") else: print("GRAY WON!") #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) #if game.turn == GRAY: game.select(row, col) game.update() pygame.quit()
def main(self): logics = Logics() while logics.running: logics.curr_menu.display_menu() logics.game_loop() while self.RUN: self.CLOCK.tick(FPS) if self.GAME.turn == WHITE: value, new_board = minimax(self.GAME.get_board(), logics.options.difficulty, WHITE, self.GAME) if new_board is None: print("TIE") break self.GAME.ai_move(new_board) if self.GAME.winner() is not None: print(self.GAME.winner()) self.RUN = False for event in pygame.event.get(): if event.type == pygame.QUIT: self.RUN = False if event.type == pygame.MOUSEBUTTONDOWN: pos = pygame.mouse.get_pos() row, col = self.get_row_col_from_mouse(pos) self.GAME.select(row, col) self.GAME.update() pygame.quit()
def main(): run = True clock = pygame.time.Clock() game = Game(WIN) # piece = board.get_piece(0, 1) # board.move(piece, 4, 3) while run: clock.tick(FPS) if game.turn == BLACK: new_board = minimax(game.get_board(), 3, True, game)[1] game.ai_move(new_board) if game.winner() != None: print(game.winner()) game.reset() 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, WIN) game.update() pygame.quit()
def main(): run = True clock = pygame.time.Clock() game = Game(WIN) while run: clock.tick(60) if game.turn == WHITE: value, newBoard = minimax(game.getBoard(), 3, WHITE, game) game.aiMove(newBoard) 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 = getRowColFromMouse(pos) game.select(row, col) game.update() pygame.quit()
def start_the_game_ai(self): run = True clock = pygame.time.Clock() game = Game(self.win) while run: clock.tick(self.fps) if game.turn == BLACK: value, new_board = minimax(game.get_board(), self.ai_difficulty, BLACK, game) game.ai_move(new_board) if game.winner() != None: self.print_winner(game.winner()) if game.draw(): print("draw") 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() game.select(pos) game.update()
def main(): run = True # Make sure game runs at constant speed clock = pygame.time.Clock() game = Game(WIN) while run: clock.tick(FPS) if game.turn == WHITE: value, new_board = minimax(game.get_board(), 4, WHITE, game) game.ai_move(new_board) 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() pygame.quit()
def main(): run = True clock = pg.time.Clock() game = Game(WIN) while run: clock.tick(FPS) if game.turn == WHITE: value, new_board = minimax(game.get_board(),3, WHITE, game) game.ai_move(new_board) if game.winner != None: print(game.winner()) for event in pg.event.get(): if event.type == pg.QUIT: run = False if event.type == pg.MOUSEBUTTONDOWN: pos = pg.mouse.get_pos() row, col = get_row_col_from_mouse(pos) game.select(row, col) game.update() pg.quit()
def main(): run = True clock = pygame.time.Clock() game = Game(WIN) while run: clock.tick(FPS) if game.turn == WHITE: value, new_board = minimax(game.get_board(), 6, True, game, float('-inf'), float('inf')) game.ai_move(new_board) print(value) 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() pygame.quit()
def main(): run = True new_game = Game(WIN) while run: if not new_game.human_player: if new_game.turn == WHITE: value, new_board = minimax(new_game.get_board(), 4, WHITE, new_game) new_game.algorithm_move(new_board) if new_game.winner() is not None: print(new_game.winner() + '\n Press new game or exit the application') 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_mouse_coords(pos) new_game.select(row, col) root.update() new_game.update() pygame.quit()
def main(): start_chess_engine = True runtime = pygame.time.Clock() game_rules = Rules(window) while start_chess_engine: runtime.tick(FPS) if game_rules.turn_taken == white_piece: value, new_board = minimax(game_rules.get_board(), 3, white_piece, game_rules) game_rules.algorithm_move(new_board) if game_rules.winner() is not None: print(game_rules.winner()) start_chess_engine = False for event in pygame.event.get(): if event.type == pygame.QUIT: start_chess_engine = False if event.type == pygame.MOUSEBUTTONDOWN: position = pygame.mouse.get_pos() board_row, board_column = get_board_row_col_from_mouse( position) game_rules.select(board_row, board_column) game_rules.update() pygame.quit()
def main(): run = True clock = pygame.time.Clock() game = Game(WIN) DEPTH = 3 while run: clock.tick(FPS) if game.turn == BLACK: start = time.time() value, new_board = minimax(game.board.get_board(), DEPTH, True, game) game.ai_move(new_board) print("TIME: ", time.time() - start) 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) if col <= 7: game.select((8 * row + col)) keys = pygame.key.get_pressed() if keys[pygame.K_r]: game.reset() game.update() pygame.quit()
def main(): clock = pygame.time.Clock() game = Game(WINDOW) play_with_ai = True ai_vs_ai = False ai_color = WHITE player_color = RED while True: clock.tick(FPS) game.update() if play_with_ai and game.turn == ai_color: new_board = minimax(game.get_board(), DEPTH, True, ai_color, player_color)[1] if new_board is None: is_player_winner = True break game.ai_move(new_board) if ai_vs_ai: time.sleep(1) if ai_vs_ai and game.turn != ai_color: new_board = minimax(game.get_board(), DEPTH, True, player_color, ai_color)[1] if new_board is None: is_player_winner = False break game.ai_move(new_board) time.sleep(1) if game.winner() is not None: is_player_winner = game.winner() != ai_color break for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: pos = pygame.mouse.get_pos() row, col = get_row_col_from_mouse(pos) game.select(row, col) if event.type == pygame.QUIT: pygame.quit() print_end_message(is_player_winner) pygame.quit()
def run(self): while self.active: self.clock.tick(self.FPS) if not self.new_game: if self.game.turn == WHITE: value, new_board = minimax(self.game.get_board(), 3, True, self.game) self.game.ai_move(new_board) self._check_winner() self.game.update() else: self._start_new_game() self._check_events()
def main(): playing = True frames = pygame.time.Clock() game = Game(DISPLAY) while playing: # run game at 60fps frames.tick(FPS) if game.winner() != None: print(game.winner() + " HAS WON THE GAME") break # checks if player can't make a move if game.no_valid_moves() == None: if game.turn == WHITE: print("WHITE HAS NO POSSIBLE MOVES") elif game.turn == RED: print("RED HAS NO POSSIBLE MOVES") break # white turn is ai's turn if game.turn == WHITE: value, new_board = minimax(game.get_board(), 4, float('-inf'), float('inf'), WHITE, game) game.ai_move(new_board) # default pygame actions for action in pygame.event.get(): # check if player quit game if action.type == pygame.QUIT: playing = False # gets mouse click position if action.type == pygame.MOUSEBUTTONDOWN: pos = pygame.mouse.get_pos() row, col = mouse_pos(pos) game.select(row, col) # keeps updating game class game.update() pygame.quit()
def main(): run = True # Make sure game doesn't run too fast or too slow clock = pygame.time.Clock() # Create a Game object which will control the board for us game = Game(WIN) # Create an event loop while the game is running while run: clock.tick(FPS) if game.turn == WHITE: value, new_board = minimax(game.get_board(), 3, WHITE, game) game.ai_move(new_board) # Get the new board after the ai has moved if game.winner() != None: print(game.winner()) run = False # Check for any events happening at current time for event in pygame.event.get(): # End the game and get rid of window by clicking the cross if event.type == pygame.QUIT: run = False # If we press any button on mouse, we first get the row, column we are in # Then we will select the piece on that location and move that to wherever we want to move if event.type == pygame.MOUSEBUTTONDOWN: pos = pygame.mouse.get_pos() row, col = get_row_col_from_mouse(pos) game.select(row, col) game.update() pygame.quit()
def main(): clock = pygame.time.Clock() game = Game(WIN) while game.is_active: clock.tick(FPS) if game.turn == RED: _, new_board = minimax(game.get_board(), 3, False) game.ai_move(new_board) for event in pygame.event.get(): if event.type == pygame.QUIT: game.is_active = False elif event.type == pygame.MOUSEBUTTONDOWN: x, y = pygame.mouse.get_pos() row, col = get_row_col_from_mouse(x, y) game.is_active = game.play(row, col) game.update() pygame.quit()
def main(opponent): ''' main loop of the game ''' game_over = False clock = pg.time.Clock() #define fps game = Game(SCREEN) while not game_over: clock.tick(30) if game.winner() != None: print(game.winner()) game_over = True if opponent == 'AI' or opponent == 'ai': if game.turn == WHITE: score, new_board = minimax(game.get_board(), DEPTH, WHITE, game, DRAW_MOVES) game.ai_move(new_board) for event in pg.event.get(): if event.type == pg.KEYDOWN: if event.key == pg.K_r: game.restart() if event.type == pg.QUIT: game_over = True if event.type == pg.MOUSEBUTTONDOWN: row, col = get_piece_position_from_mouse(pg.mouse.get_pos()) game.select(row, col) game.update() pg.quit()
pygame.display.update() return run board = Board((102, 51, 0)) board.create_board() chess_game = Chess(board) RUN = True DEPTH = 2 # mainloop if __name__ == "__main__": while RUN: RUN = redraw_game_window(WIN, chess_game) if chess_game.turn_color == chess_game.ai_color: if chess_game.check_if_ai_checkmated_or_draw(WIN): RUN = chess_game.end_game(WIN) else: value, new_game = minimax(chess_game, DEPTH, True) chess_game.ai_move(WIN, new_game) for event in pygame.event.get(): if event.type == pygame.QUIT: RUN = False pygame.quit()