def main(): board = Board() while(True): while(True): m, n = map(int, input( 'Enter the position of x(row, column): ').split()) if (board.change(m, n, 'x')): break else: print('Wrong input') print(board) if board.win('x'): print('The x has won') break if board.fill_check(): print('We have a tie here') break ai_move(board) print(board) if board.win('o'): print('The o has won') break if board.fill_check(): print('We have a tie here') break
def ai_move(self, request): """Instruct the AI to make a move""" user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') game = get_by_urlsafe(request.game_key, Game) if not game: raise endpoints.NotFoundException("Game not found") player = Player.query(Player.email == user.email()).get() if not player or player.key != game.player: raise endpoints.UnauthorizedException( 'You are not the player for the game') if game.state == 0: raise endpoints.ForbiddenException( 'It is the player\'s turn') if game.state != 1: raise endpoints.ForbiddenException( 'Game already over') board = Board(values=game.board_values) ai_won, origin, destination, captures = ai_move(board=board) origin_value = game.add_move( board, False, ai_won, origin, destination, captures) return game.get_play_result( origin_value, origin, destination, captures, game.state)
def init_game(win, single_player): board = init_board() turn = get_turn(board) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit(0) if event.type == pygame.MOUSEBUTTONDOWN: x, y = pygame.mouse.get_pos() row, col = get_row_col(x, y) if board[row][col] == '': select(board, row, col) update(board, win) if has_won(board, turn): draw_end(f'{turn} WINS', win) init_game(win, single_player) if is_full(board): draw_end('TIE', win) init_game(win, single_player) turn = get_opposite_turn(turn) if single_player: pygame.time.delay(200) ai_move(board) update(board, win) if has_won(board, turn): draw_end(f'{turn} WINS', win) init_game(win, single_player) if is_full(board): draw_end('TIE', win) init_game(win, single_player) update(board, win)
def check_move(parent, answ): if answ[1] == 0: s_time = time.clock() answ = ai_move(answ) z = 0 for x in range(len(answ[0])): for y in range(len(answ[0])): z += 1 one_AI_Btn(parent, x, y, z, answ) end_text, check = check_end(answ[0]) if check: end_game(parent, answ[0], end_text) else: for x in range(len(answ[0])): for y in range(len(answ[0])): one_PLR_Btn(parent, x, y, answ)
def pvA(): name1 = input("Player1's name? ") play = 1 player = 1 while play != 0: board = Functions.start_board() os.system('clear') Functions.printboard(board) game = 1 while game == 1: if player == 1: cprint(name1 + " turn!", "red", attrs=["bold"]) coord = Functions.player_move(board) if coord == None: return None else: coord = ai.ai_move(board) board = Functions.move(board, coord, player) if player == 1: player = 2 else: player = 1 os.system('clear') Functions.printboard(board) actual_result = Functions.wincheck(board) if actual_result == 1: cprint(name1 + ' has won!', "red", attrs=["bold"]) game = 0 if actual_result == 2: cprint("Computer has won! :(", "yellow", attrs=["bold"]) game = 0 if game == 1: gamedraw = Functions.fullcheck(board) if gamedraw == 1: cprint("This game is a Draw!", "cyan", attrs=["bold"]) game = 0 answer = "z" while answer != "n": answer = input("Do you want to play again? y or n: ") if answer == "n": play = 0 break if answer == "y": break
def run(): pi = RaspberryPI() # Find corners of the game board and compute homography while True: # Wait for button press wait_for_button() # Wait 200ms, then take picture of board im = pi.take_picture() # Parse image to create board board = run_homography(im) # Determine AI move move = ai_move(board) # Announce AI move on the speakers, and print it to console pi.voice_position(move[0],move[1])
def main(): screen = pg.display.set_mode((WIDTH + BORDER, HEIGHT + BORDER)) clock = pg.time.Clock() #screen.fill(pg.Color("ghostwhite")) screen.fill(pg.Color("brown4")) #screen.fill(pg.Color("sienna")) gs = Game_state() load_images() running = True square_selected = () # x, y coordinate of selected square player_clicks = [] # list of appended square_selected valid_moves, first_click_turn = gs.get_valid_moves( ) # compute valid moves outside loop (for efficiency) game_over = False # signals end of game user_prompt = False # pauses gui rendering for user input AI_MODE = False # flag for activating AI mode delay = 0 # delay the speed of AI plays display_time = 0 # AI_MODE text display persistence timer while running: if not user_prompt: found = False if AI_MODE and display_time == 0 and not game_over: # pause a bit between AI plays if delay > 0 if delay == 0: move, gs = ai_move(gs) if move: # if AI made a move animate(move, screen, gs.board, clock) print(move.get_chess_notation()) delay = 4 # pause magnitude else: delay -= 1 for e in pg.event.get(): if e.type == pg.QUIT: running = False elif e.type == pg.KEYDOWN: if e.key == pg.K_u and not AI_MODE: # u key pressed (undo last move) gs.undo_move() valid_moves, first_click_turn = gs.get_valid_moves() elif e.key == pg.K_r and not AI_MODE: # r key pressed (reset game) gs = Game_state() valid_moves, turn = [], None square_selected = () player_clicks = [] print("Board reset!") valid_moves, first_click_turn = gs.get_valid_moves() elif e.key == pg.K_a: # a key pressed (toggle AI mode) #toggle = True display_time = 10 AI_MODE = not AI_MODE ai_reset() print("AI MODE ENABLED") if AI_MODE else print( "AI MODE DISABLED") elif e.type == pg.MOUSEBUTTONDOWN: if not game_over and not AI_MODE: location = pg.mouse.get_pos( ) # x, y location of mouse click location_col_transform = location[0] // SQ_SIZE - 1 location_row_transform = location[1] // SQ_SIZE - 1 col = (location_col_transform) if ( 0 <= location_col_transform < 8) else -1 row = (location_row_transform) if ( 0 <= location_row_transform < 8) else -1 if col >= 0 and row >= 0: if square_selected == ( row, col): # clicked same position twice square_selected = () player_clicks = [] else: # new position clicked (destination) square_selected = (row, col) player_clicks.append(square_selected) if len(player_clicks ) == 2: # 'from' and 'to' are available move = Move(player_clicks[0], player_clicks[1], gs.board) # create move object for obj in range(len(valid_moves)): if move == valid_moves[obj]: move = valid_moves[obj] found = True gs.make_move(move) if (move.end_row == 0 or move.end_row == 7) and (move.piece_moved[0] == "p"): user_prompt = True choice = ("q", "r", "b", "n") promotion = "" while promotion not in choice: promotion = input( "Promote to: q => Queen, r => Rook, b => Bishop, n => Knight\n" ) gs.board[move.end_row][ move. end_col] = promotion + move.piece_moved[ 1] user_prompt = False animate(move, screen, gs.board, clock) print(move.get_chess_notation()) square_selected = () player_clicks = [] valid_moves, first_click_turn = gs.get_valid_moves( ) break if not found: # move selected not a valid move current_turn = "l" if gs.light_to_move else "d" if current_turn == first_click_turn: player_clicks = [square_selected] square_selected = () else: player_clicks = [] square_selected = () display_game_state(screen, gs, valid_moves, player_clicks) # display text for switching AI mode if display_time > 0: display_text(screen, "AI MODE ENABLED", "Green") if AI_MODE else display_text( screen, "AI MODE DISABLED", "Red") display_time -= 1 # countdown for text to disappear if gs.check_mate: game_over = True if gs.light_to_move: display_text(screen, "Dark wins by checkmate") else: display_text(screen, "Light wins by checkmate") elif gs.stale_mate: game_over = True display_text(screen, "Stalemate") clock.tick(MAX_FPS) pg.display.flip()
def main(choice=False): screen = pg.display.set_mode((WIDTH + BORDER, HEIGHT + BORDER)) clock = pg.time.Clock() #screen.fill(pg.Color("ghostwhite")) screen.fill(pg.Color("Peru")) gs = Game_state() load_images() gs.light_to_move = not gs.light_to_move if FLIP else True running = True square_selected = () # x, y coordinate of selected square player_clicks = [] # list of appended square_selected valid_moves, first_click_turn = gs.get_valid_moves() # compute valid moves outside loop (for efficiency) game_over = False # signals end of game user_prompt = False # pauses gui rendering for user input AI_MODE = choice # flag for activating AI mode delay = 0 # delay the speed of AI plays display_time = 0 # AI_MODE text display persistence timer PLAYBACK_MODE=False playback_index=0 while running: if not user_prompt: found = False if AI_MODE and display_time == 0 and not game_over: # pause a bit between AI plays if delay > 0 if delay == 0: move, gs = ai_move(gs) if move: # if AI made a move animate(move, screen, gs.board, clock) print(move.get_chess_notation()) if Move.mute==True: engine.say(move.get_chess_notation()) engine.runAndWait() delay = 20 # pause magnitude else: delay -= 1 for e in pg.event.get(): if e.type == pg.QUIT: running = False elif e.type == pg.KEYDOWN: if e.key == pg.K_u and not AI_MODE and not PLAYBACK_MODE: # u key pressed (undo last move) gs.undo_move() valid_moves, first_click_turn = gs.get_valid_moves() elif e.key == pg.K_m: # m key pressed (mute commentary) Move.mute=not Move.mute elif e.key == pg.K_r and not AI_MODE and not PLAYBACK_MODE: # r key pressed (reset game) gs = Game_state() valid_moves, turn = [], None square_selected = () player_clicks = [] print("Board reset!") if Move.mute==True: engine.say("Board reset!") engine.runAndWait() valid_moves, first_click_turn = gs.get_valid_moves() # elif e.key == pg.K_a: # a key pressed (toggle AI mode) # #toggle = True # display_time = 10 # AI_MODE = not AI_MODE # ai_reset() # print("AI MODE ENABLED") if AI_MODE else print("AI MODE DISABLED") elif e.key == pg.K_p and not AI_MODE and game_over: PLAYBACK_MODE = not PLAYBACK_MODE playback_log = gs.move_log gs = Game_state() valid_moves, turn = [], None square_selected = () player_clicks = [] valid_moves, first_click_turn = gs.get_valid_moves() print("PLAYBACK MODE ENABLED \nNumber of Moves Available: " + str(len(playback_log))) if PLAYBACK_MODE else print( "PLAYBACK MODE DISABLED") playback_index = 0 elif e.key == pg.K_n and PLAYBACK_MODE and game_over: if len(playback_log) == 0: print("No Moves to Play") if Move.mute==True: engine.say("No Moves to Play") engine.runAndWait() break if playback_index == len(playback_log): # playback_index = len(playback_log) - 1 print("Max Playback Reached!") if Move.mute==True: engine.say("Max Playback Reached!") engine.runAndWait() else: str_sqr = str(playback_log[playback_index]) start_row = int(str_sqr[1]) start_col = int(str_sqr[4]) start_sqr = (start_row, start_col) end_row = int(str_sqr[8]) end_col = int(str_sqr[11]) end_sqr = (end_row, end_col) clicked_sqr = [start_sqr, end_sqr] # print(start_sqr) # print(end_sqr) # print(str_sqr) # print(clicked_sqr) playback_index += 1 move = Move( start_sqr, end_sqr, gs.board) gs.make_move(move) animate(move, screen, gs.board, clock) print("Move: "+str((playback_index))) if Move.mute==True: engine.say("Move: "+str((playback_index))) engine.runAndWait() print(move.get_chess_notation()) if Move.mute==True: engine.say(move.get_chess_notation()) engine.runAndWait() elif e.key == pg.K_b and PLAYBACK_MODE and game_over: if playback_index <= 0: playback_index = 0 print("Min Playback Reached!") if Move.mute==True: engine.say("Min Playback Reached!") engine.runAndWait() else: playback_index -= 1 print("Move: "+str((playback_index+1))) if Move.mute==True: engine.say("Move: "+str((playback_index+1))) engine.runAndWait() gs.undo_move() valid_moves, first_click_turn = gs.get_valid_moves() # gs.make_move(move) # animate(gs.undo_move, screen, gs.board, clock) # print(move.get_chess_notation()) elif e.type == pg.MOUSEBUTTONDOWN: if not game_over and not AI_MODE: location = pg.mouse.get_pos() # x, y location of mouse click location_col_transform = location[0] // SQ_SIZE - 1 location_row_transform = location[1] // SQ_SIZE - 1 col = (location_col_transform) if (0 <= location_col_transform < 8) else -1 row = (location_row_transform) if (0 <= location_row_transform < 8) else -1 if col >= 0 and row >= 0: if square_selected == (row, col): # clicked same position twice square_selected = () player_clicks = [] else: # new position clicked (destination) square_selected = (row, col) player_clicks.append(square_selected) if len(player_clicks) == 2: # 'from' and 'to' are available move = Move(player_clicks[0], player_clicks[1], gs.board) # create move object for obj in range(len(valid_moves)): if move == valid_moves[obj]: move = valid_moves[obj] found = True gs.make_move(move) if (move.end_row == 0 or move.end_row == 7) and (move.piece_moved[0] == "p"): user_prompt = True choice = ("q", "r", "b", "n") promotion = True while promotion: for event in pg.event.get(): if event.type == pg.MOUSEBUTTONDOWN: location = pg.mouse.get_pos() # x, y location of mouse click location_col_transform = location[0] // square- 1 # divided by sqaure size location_row_transform = location[1] // square - 1 row_comb= [1,2,15,16] # list for all the possible rows if location_row_transform in row_comb: #making sure the event meet requirements row = location_row_transform col =location_col_transform if ((row == 15) and (col%2 != 0)) or ((row == 1) and (col%2 != 0)): piece = "q" elif ((row == 15) and (col%2 == 0)) or ((row == 1) and (col%2 == 0)): piece = "r" elif ((row == 16) and (col%2 != 0)) or ((row == 2) and (col%2 != 0)): piece = "b" elif ((row == 16) and (col%2 == 0)) or ((row == 2) and (col%2 == 0)): piece = "n" print("Promoting Piece") if Move.mute==True: engine.say("Promoting Piece") engine.runAndWait() promotion = False display_board2(screen,move.end_row, move.end_col) pg.display.update() clock.tick(5) gs.board[move.end_row][move.end_col] = piece+ move.piece_moved[1] user_prompt = False animate(move, screen, gs.board, clock) print(move.get_chess_notation()) if Move.mute==True: engine.say(move.get_chess_notation()) engine.runAndWait() square_selected = () player_clicks = [] valid_moves, first_click_turn = gs.get_valid_moves() break if not found: # move selected not a valid move current_turn = "l" if gs.light_to_move else "d" if current_turn == first_click_turn: player_clicks = [square_selected] square_selected = () else: player_clicks = [] square_selected = () display_game_state(screen, gs, valid_moves, player_clicks) # display text for switching AI mode if display_time > 0: display_text(screen, "AI MODE ENABLED", "Green") if AI_MODE else display_text( screen, "AI MODE DISABLED", "Red") display_time -= 1 # countdown for text to disappear if AI_MODE and not game_over: if gs.light_to_move: display_Thinking_text(screen, gs, "Thinking....") else: display_Thinking_text(screen, gs, "Thinking....") if gs.check_mate: game_over = True AI_MODE = False engine.say("Checkmate!") engine.runAndWait() engine.stop() if gs.light_to_move: display_text(screen, "Dark wins by checkmate") else: display_text(screen, "Light wins by checkmate") elif gs.stale_mate: game_over = True AI_MODE = False engine.say("Stalemate!") engine.runAndWait() display_text(screen, "Stalemate") clock.tick(MAX_FPS) pg.display.flip()
selected_position = select_position(game_board) place_marker(game_board, "O", selected_position) if has_won(game_board, "O"): display_board(game_board) print("Player has won!") game_on = False else: if is_board_full(game_board): display_board(game_board) print("It's a tie!") game_on = False else: turn = "AI" else: ai_move(game_board) display_board(game_board) if has_won(game_board, "X"): display_board(game_board) print("AI has won!") game_on = False else: if is_board_full(game_board): display_board(game_board) print("It's a tie") game_on = False else: turn = "Player" if not replay(): break