def lambda_handler(event, context): id_game = _get_id_game(event) is_spymaster = _is_spymaster(event) query_results = get_game_words(conn, id_game) if not query_results: return { 'statusCode': 404, 'headers': {"Content-Type": "application/json", "Access-Control-Allow-Origin": "*"}, 'body': json.dumps({'message': 'Game not found'}), } game_words = map_game_words(query_results, is_spymaster) team_1_count, team_2_count = get_remaining_words(game_words) all_players = map_players(get_game_players(conn, id_game)) team_1, team_2 = get_each_teams_players(all_players) current_turn = get_current_turn(conn, id_game) body = { 'id_game': id_game, "current_turn": current_turn, "winner": check_winner(team_1_count, team_2_count), 'words': game_words, 'team_1_remaining_words': team_1_count, 'team_2_remaining_words': team_2_count, 'team_1': team_1, 'team_2': team_2, } return { 'statusCode': 200, 'headers': {"Content-Type": "application/json", "Access-Control-Allow-Origin": "*"}, 'body': json.dumps(body), }
def make_move(self, request): """Make a move. Returns current game state""" game = get_by_urlsafe(request.urlsafe_game_key, Game) if not game: raise endpoints.NotFoundException('Game not found') if game.gameOver: raise endpoints.NotFoundException('Game is over') player = Player.get_player_by_name(request.name) # check if player has next move if player.key != game.nextMove: raise endpoints.BadRequestException('It is not your turn') # check if input for move numeric if request.move.isnumeric() == False: raise endpoints.BadRequestException( 'Please input a number for move') move = int(float(request.move)) # check if input for move within 9 if move in range(9): if game.board[move] != '': raise endpoints.BadRequestException( 'The block has been filled') # x is true when is player one's turn; false when player two's turn x = True if player.key == game.playerOne else False game.board[move] = 'X' if x else 'O' game.nextMove = game.playerTwo if x else game.playerOne # check if there is a winner winner = check_winner(game.board) if winner: game.endGame(player.key) winner = game.winner.get().name else: if check_full(game.board): # tie game.endGame() winner = 'None' game.history.append(('X' if x else 'O', 'Move: ' + str(move), 'Played by: ' + request.name, 'Next Move: ' + game.nextMove.get().name, 'Game Over: ' + str(game.gameOver), 'Tie: ' + str(game.tie), 'Winner: ' + winner, )) game.put() else: raise endpoints.BadRequestException( 'Input for move can only be 0~8') return game.copy_game_to_form()
def wound_i_win(board, row, column): if board[row][column] != ' ': return False board = [list(b) for b in board] board[row][column] = 'O' board = tuple([tuple(b) for b in board]) answer = utils.check_winner(board) return answer == 'O'
def make_move(self, request): """Makes a move. Returns a game state with message""" game = get_by_urlsafe(request.urlsafe_game_key, Game) if not game: raise endpoints.NotFoundException('Game not found') if game.game_over: return game.to_form('Game already over!') # prevent a user from playing twice in a row user = User.get_current_user(request.user_name) if user.key != game.nextMove: raise endpoints.BadRequestException('It\'s not your turn!') x = True if user.key == game.player_x else False # Make a move, and verify if the move is a valid move move = request.move size = game.boardSize * game.boardSize - 1 if move < 0 or move > size: raise endpoints.BadRequestException('Bad Move!') if game.board[move] != '': raise endpoints.BadRequestException('Invalid Move') # Make a move, and send the move to game history game.board[move] = 'X' if x else 'O' game.game_history.append(('X' if x else 'O', move)) game.nextMove = game.player_o if x else game.player_x #Check if there is a winner in the game and end the game winner = check_winner(game.board, game.boardSize) if winner: game.end_game(user.key) else: #If no winner, game ends in a draw if check_full(game.board): game.end_game() else: # Send email reminder to player if game still in progress taskqueue.add(url='/tasks/send_move_email', params={ 'user_key': game.nextMove.urlsafe(), 'game_key': game.key.urlsafe() }) game.put() # Upde the Memcache if the game is over if game.game_over: taskqueue.add(url='/task/cache_games_finished') return game.to_form()
def make_move(self, request): """Makes a move. Returns a game state with message""" game = get_by_urlsafe(request.urlsafe_game_key, Game) if not game: raise endpoints.NotFoundException('Game not found') if game.game_over: raise endpoints.NotFoundException('Game already over') user = User.get_user_by_name(request.user_name) if user.key != game.next_move: raise endpoints.BadRequestException('It\'s not your turn!') # Just a dummy signifier, what type of symbol is going down x = True if user.key == game.user_x else False move = request.move # Verify move is valid size = game.board_size * game.board_size - 1 if move < 0 or move > size: raise endpoints.BadRequestException('Invalid move! Must be between' '0 and %s ' % size) if game.board[move] != '': raise endpoints.BadRequestException('Invalid move!') game.board[move] = 'X' if x else 'O' # Append a move to the history game.history.append(('X' if x else 'O', move)) game.next_move = game.user_o if x else game.user_x # Check if there's a winner winner = check_winner(game.board, game.board_size) # If there's winner end game if winner: game.end_game(user.key) else: # If there's no winner and game board is full end game with tie if check_full(game.board): # Game tied game.end_game() else: # If game is still ongoing, send remainder email to player taskqueue.add(url='/tasks/send_move_email', params={'user_key': game.next_move.urlsafe(), 'game_key': game.key.urlsafe()}) game.put() # If game is over, update memcache if game.game_over: taskqueue.add(url='/tasks/update_finished_games') return game.to_form()
def make_move(self, request): """Makes a move. Returns a game state with message""" game = get_by_urlsafe(request.urlsafe_game_key, Game) if not game: raise endpoints.NotFoundException('Game not found') if game.game_over: return game.to_form('Game already over!') # prevent a user from playing twice in a row user = User.get_current_user(request.user_name) if user.key != game.nextMove: raise endpoints.BadRequestException('It\'s not your turn!') x = True if user.key == game.player_x else False # Make a move, and verify if the move is a valid move move = request.move size = game.boardSize * game.boardSize - 1 if move < 0 or move > size: raise endpoints.BadRequestException('Bad Move!') if game.board[move] != '': raise endpoints.BadRequestException('Invalid Move') # Make a move, and send the move to game history game.board[move] = 'X' if x else 'O' game.game_history.append(('X' if x else 'O', move)) game.nextMove = game.player_o if x else game.player_x #Check if there is a winner in the game and end the game winner = check_winner(game.board, game.boardSize) if winner: game.end_game(user.key) else: #If no winner, game ends in a draw if check_full(game.board): game.end_game() else: # Send email reminder to player if game still in progress taskqueue.add(url='/tasks/send_move_email', params={'user_key': game.nextMove.urlsafe(), 'game_key': game.key.urlsafe()}) game.put() # Upde the Memcache if the game is over if game.game_over: taskqueue.add(url='/task/cache_games_finished') return game.to_form()
def _generate_body(query_results, id_game, all_players, is_spymaster=False): game_words = map_game_words(query_results, is_spymaster) team_1_count, team_2_count = get_remaining_words(game_words) team_1, team_2 = get_each_teams_players(all_players) current_turn = get_current_turn(conn, id_game) return { 'type': "GET_GAME_DATA", 'id_game': id_game, "current_turn": current_turn, "winner": check_winner(team_1_count, team_2_count), 'words': game_words, 'team_1_remaining_words': team_1_count, 'team_2_remaining_words': team_2_count, 'team_1': team_1, 'team_2': team_2, }
def minimax(state, actions, player): # minimax(state, actions, player = 'O') """heiristic algorithm to improve the winning ratio""" winner = check_winner(state) if winner: return (None, None), winner else: state = (list([list(row) for row in state])) draw_move = None # print("I am going to evaluate", actions) for action in actions: # print("Evaluated Action: ", action) newBoard = deepcopy(state) row = action[0] column = action[1] newBoard[row][column] = player # insert 'O' # updates the empty_cells list and calls the function itself with the board as the newBoard _newBoard = (tuple([tuple(row) for row in newBoard])) newEmptyCells = findEmptyCells(_newBoard) # print("Who is the winner for?", newBoard) move, winner = minimax(_newBoard, newEmptyCells, get_opponent(player)) # print("The winner is", winner) # output: who is going to win? # ----> 'O', 'X', DRAW if player == 'O': if winner == 'O': # print("The winner is 'O'") return (row, column), winner if winner == "It's a Draw": draw_move = action if player == 'X': if winner == 'X': return (row, column), winner output = pick_random_move(state) # TODO: BUG HERE! vvvv return output, winner
def make_move(self, request): """Makes a move. Returns a game state with message""" game = get_by_urlsafe(request.urlsafe_game_key, Game) if not game: raise endpoints.NotFoundException("Game not found") if game.game_over: raise endpoints.NotFoundException("Game already over") user = User.query(User.name == request.user_name).get() if user.key != game.next_move: raise endpoints.BadRequestException("It's not your turn!") # Just a dummy signifier, what type of symbol is going down x = True if user.key == game.user_x else False move = request.move # Verify move is valid if move < 0 or move > 8: raise endpoints.BadRequestException("Invalid move! Must be between" "0 and 8") if game.board[move] != "": raise endpoints.BadRequestException("Invalid move!") game.board[move] = "X" if x else "O" # Append a move to the history game.history.append(("X" if x else "O", move)) game.next_move = game.user_o if x else game.user_x winner = check_winner(game.board) if not winner and check_full(game.board): # Just delete the game game.key.delete() raise endpoints.NotFoundException("Tie game, game deleted!") if winner: game.end_game(user.key) else: # Send reminder email taskqueue.add( url="/tasks/send_move_email", params={"user_key": game.next_move.urlsafe(), "game_key": game.key.urlsafe()}, ) game.put() return game.to_form()
def make_move(self, request): """Makes a move. Returns a game state with message""" game = get_by_urlsafe(request.urlsafe_game_key, Game) if not game: raise endpoints.NotFoundException('Game not found!') if game.game_over: raise endpoints.NotFoundException('Game is already over!') user = User.query(User.name == request.user_name).get() if user.key != game.next_move: raise endpoints.BadRequestException('It\'s not your turn!') move = request.move if not IsValidMove(move, game.board): raise endpoints.BadRequestException('Invalid move!') # Game history is a list of which player put pieces into which column. # Since the list is chronological, a game can be recreated using only # this information. if user.key == game.player_1: piece = '1' game.next_move = game.player_2 game.history.append('%s placed piece in column %s' % (str(user.name), move)) else: piece = '2' game.next_move = game.player_1 game.history.append('%s placed piece in column %s' % (str(user.name), move)) makeMove(piece, move, game.board) winner = check_winner(game.board) if not winner and check_full(game.board): game.tie_game() game.history.append('game ended in a tie') if winner: game.end_game(user.key) game.history.append('%s wins the game!' % str(user.name)) game.put() return game.to_form('board updated!')
def main(): policy = {} boards = itertools.product(' XO', repeat=9) for board_string in tqdm.tqdm(boards): if ' ' not in board_string: continue board = (tuple(board_string[:3]), tuple(board_string[3:6]), tuple(board_string[6:])) if utils.check_winner(board): continue n_x = board_string.count('X') n_o = board_string.count('O') if n_x - n_o not in (0, 1): continue policy[''.join(board_string)] = choose_move(board) with open('manu_policy.txt', 'w') as fout: for k, v in policy.items(): fout.write(str(k) + ":" + str(v) + "\n")
def play_games( self, nr_games_to_play, player1, player2, gameShow=0, ): leaderboard = {'Lia': 0, 'Manu': 0, 'Draw': 0} for game_nr in tqdm.tqdm(range(nr_games_to_play)): if game_nr < gameShow: print("======= GAME nr {} STARTED =======".format(game_nr)) who_plays = random.randint(0, 1) turn = 0 board = [ [ ' ', ' ', ' ', ], [ ' ', ' ', ' ', ], [ ' ', ' ', ' ', ], ] board_seq = [] frozen_board = (tuple([tuple(row) for row in board])) while True: if game_nr < gameShow: print('turn', turn) if who_plays == 0: row, column = player1(frozen_board) if game_nr < gameShow: print('Lia picked', row, column) if board[row][column] != ' ': print('Lia cheated!') break board[row][column] = 'O' who_plays = 1 elif who_plays == 1: if self.args is not None and self.args.manu_is_drunk: row, column = utils.pick_random_move(frozen_board) else: row, column = player2(frozen_board) if game_nr < gameShow: print('Manu picked', row, column) # this check if a new place is being overwritten if board[row][column] != ' ': if self.crash_on_cheats: print('Manu cheated!') break else: board[0] = ['O', 'O', 'O'] board[row][column] = 'X' who_plays = 0 # end of the turn if game_nr < gameShow: for row in board: print(row) print() turn += 1 frozen_board = (tuple([tuple(row) for row in board])) board_seq.append(frozen_board) # checking who is the winner now winner = utils.check_winner(frozen_board) if winner == 'X': # for idx, b in enumerate(board_seq): # print(idx) # for row in b: # print(row) # print() if game_nr < gameShow: print("Manu wins a kiss from Lia") leaderboard['Manu'] += 1 break elif winner == 'O': if game_nr < gameShow: print("Lia wins ten kisses from Manu") leaderboard['Lia'] += 1 break # check if game is draw, and exit loop elif winner == "It's a Draw": if game_nr < gameShow: print("It's a Draw") leaderboard['Draw'] += 1 break print(leaderboard) total_wins = float(leaderboard['Lia'] + leaderboard['Manu']) winningRatio = float(leaderboard['Lia']) / total_wins return winningRatio
def make_move(self, request): """Makes a move. Returns a game state with message""" game = get_by_urlsafe(request.urlsafe_game_key, Game) if not game: raise endpoints.NotFoundException('Game not found') if game.game_over: raise endpoints.NotFoundException('Game already over') # Get user name user = User.query(User.name == request.user_name).get() # Check turn print user print game.turn if user.key != game.turn and game.attempts_remaining_a != 0 and \ game.attempts_remaining_b != 0: raise endpoints.BadRequestException('Not your turn!') # Check user is valid if user == None : raise endpoints.NotFoundException('User not found!') # Signifiers for which user is guessing a = True if (user.key == game.user_a) else False word_x = game.word_a if a == True else game.word_b word_x_guess = game.word_a_guess if a == True else game.word_b_guess guess = request.guess game.turn = game.user_b if a == True else game.user_a # Verify user has moves left if a == True : if game.attempts_remaining_a == 0: raise endpoints.BadRequestException('You have been hanged!') else: if game.attempts_remaining_b == 0: raise endpoints.BadRequestException('You have been hanged!') validMove = re.compile('[a-zA-Z]') if not validMove.match(guess): raise endpoints.BadRequestException('Invalid charachter!') if len(list(guess)) > 1: raise endpoints.BadRequestException('You can only enter 1 character!') # Verify in history that guess has not been guessed before if a == True: for (usr , gss, opt) in game.history: if usr == 'A' and gss == guess: raise endpoints.BadRequestException('You already guessed that letter!') else: for (usr , gss, opt) in game.history: if usr == 'B' and gss == guess: raise endpoints.BadRequestException('You already guessed that letter!') # Get guess and place it in word_x_guess if correct for num in range(0, len(word_x)): if guess in str(word_x[num]): word_x_guess = replaceCharacterAtIndexInString(word_x_guess,num,guess) message = "Right" # If incorrect down one counter on attempts_remaining if guess not in str(word_x): print a if a == True : if game.attempts_remaining_a == 1 : game.attempts_remaining_a -= 1 message = "user A was hanged" else: game.attempts_remaining_a -= 1 message = "Wrong" else: if game.attempts_remaining_b == 1 : game.attempts_remaining_b -= 1 message = "user B was hanged" else: game.attempts_remaining_b -= 1 message = "Wrong" if game.attempts_remaining_a == 0 and game.attempts_remaining_b ==0 : game.key.delete() raise endpoints.NotFoundException('Both have been hanged, losers!') # Append a move to the history game.history.append(('A' if a else 'B', guess, message)) #Check winner winner = check_winner(word_x, word_x_guess) if winner: game.end_game(user.key) game.message = "User {} wins".format(request.user_name) game.history.append(game.message) return game.to_form() game.put() taskqueue.add(url='/tasks/cache_average_attempts') taskqueue.add(url='/tasks/send_move', params={'user_key': game.turn.urlsafe(), 'game_key': game.key.urlsafe()}) return game.to_form()
def main() -> None: user_input = (utils.welcome()).lower().lstrip().rstrip() # Display Menu while user_input != "q": if user_input == "a": # Single Player against Computer player1_name = utils.get_player_name( num=1).lower().lstrip().rstrip() player1_choice = utils.get_player_choice(name=player1_name) # input validation while player1_choice.lower() not in ["r", "s", "p"]: player1_choice = utils.get_player_choice(name=player1_name) computer_choice = utils.get_computer_choice() player1 = tuple((player1_name, player1_choice)) computer = tuple(("Computer", computer_choice)) print(utils.print_choices(player1=player1, player2=computer)) winner = utils.check_winner(player1=player1, player2=computer).lower() if winner == "tie": print(utils.print_winner(winner=winner, tie=True)) else: print(utils.print_winner(winner=winner, tie=False)) elif user_input == "b": # Two Player player1_name = utils.get_player_name( num=1).lower().lstrip().rstrip() player2_name = utils.get_player_name(num=2) player1_choice = utils.get_player_choice(name=player1_name) # input validation while player1_choice.lower() not in ["r", "s", "p"]: player1_choice = utils.get_player_choice(name=player1_name) player2_choice = utils.get_player_choice(name=player2_name) # input validation while player1_choice.lower() not in ["r", "s", "p"]: player2_choice = utils.get_player_choice(name=player2_name) player1 = (player1_name, player1_choice) player2 = (player2_name, player2_choice) winner = utils.check_winner(player1=player1, player2=player2) if winner.lower() == "tie": print(utils.print_winner(winner=winner, tie=True)) else: print(utils.print_winner(winner=winner, tie=False)) elif user_input == "q": # Quit print("THANK YOU FOR PLAYING, GOODBYE!") return else: print("IMPROPER INPUT! Please choose one of the options in menu.") user_input = ( utils.welcome()).lower().lstrip().rstrip() # Display Menu if user_input == "q": print("THANK YOU FOR PLAYING, GOODBYE!") return
def make_move(self, request): """Makes a move. Returns a game state with message""" game = get_by_urlsafe(request.urlsafe_game_key, Game) player = Player.get_player_by_name(request.player_name) if not game: raise endpoints.NotFoundException('Game not found!') if game.status == 1: raise endpoints.NotFoundException('Game not in session!') if game.check_player(player): if game.check_player_turn(player): if 1 <= move <= 9: if move in game.setup: move = request.move game.history.append([player_name, move]) if game.is_host(player): game.host_moves.append(move) if check_winner(game.host_moves): game.end_game(player) taskqueue.add(url='/tasks/send_congrats_email', params={ 'user_key': game.host.urlsafe(), 'game_key': game.key.urlsafe() }) else: game.next_turn = game.oppoent if game.is_oppoent(player): game.oppoent_moves.append(move) if check_winner(game.oppoent_moves): game.end_game(player) taskqueue.add(url='/tasks/send_congrats_email', params={ 'user_key': game.oppoent.urlsafe(), 'game_key': game.key.urlsafe() }) else: game.next_turn = game.host else: msg = 'Your oppoent has taken this spot. Please try again' else: msg = 'Invalid move! Please choose a number (1-9).' else: raise endpoints.ConflictException( 'Please wait until your turn to make a move!') else: raise endpoints.NotFoundException( 'Player not current game session!') position = game.setup.index(move) del game.setup[position] game.put() if check_full(game.setup): game.end_game() msg = 'It\'s a tie! Thank you for playing!' taskqueue.add(url='/tasks/send_finish_email', params={ 'user_key': game.next_turn.urlsafe(), 'game_key': game.key.urlsafe() }) taskqueue.add(url='/tasks/send_finish_email', params={ 'user_key': player.urlsafe(), 'game_key': game.key.urlsafe() }) else: # If game is still ongoing, send remainder email to player taskqueue.add(url='/tasks/send_congrats_email', params={ 'user_key': game.next_turn.urlsafe(), 'game_key': game.key.urlsafe() }) return game.to_form(msg)
def play_games(nr_games_to_play, gameShow=0, args=None, crash_on_cheat=True): if args.manu_policy == "qlearning": q_learning = QLearning() previous_state = None # nr_games_to_play = [10, 100, 1000, 10000] leaderboard = {'Lia': 0, 'Manu': 0, 'Draw': 0} # for game_nr in range(nr_games_to_play): for game_nr in tqdm.tqdm(range(nr_games_to_play)): if game_nr < gameShow: print("======= GAME nr {} STARTED =======".format(game_nr)) who_plays = random.randint(0, 1) turn = 0 board = [ [ ' ', ' ', ' ', ], [ ' ', ' ', ' ', ], [ ' ', ' ', ' ', ], ] board_seq = [] frozen_board = (tuple([tuple(row) for row in board])) while True: if game_nr < gameShow: print('turn', turn) if who_plays == 0: row, column = lia_s_agent(frozen_board) if game_nr < gameShow: print('Lia picked', row, column) if board[row][column] != ' ': print('Lia cheated!') break board[row][column] = 'O' who_plays = 1 elif who_plays == 1: if args.manu_policy == "random": row, column = pick_random_move(frozen_board) elif args.manu_policy == "not_drunk": row, column = manu_s_agent(frozen_board) elif args.manu_policy == "qlearning": previous_state = frozen_board row, column = q_learning.choose(frozen_board, game_nr) if game_nr < gameShow: print('Manu picked', row, column) # this check if a new place is being overwritten if board[row][column] != ' ': if crash_on_cheat: print('Manu cheated!') break else: board[0] = ['O', 'O', 'O'] board[row][column] = 'X' who_plays = 0 # end of the turn if game_nr < gameShow: for row in board: print(row) print() turn += 1 frozen_board = (tuple([tuple(row) for row in board])) board_seq.append(frozen_board) # checking who is the winner now winner = check_winner(frozen_board) if winner: name = who_is_the_winner(winner, game_nr, gameShow) leaderboard[name] += 1 if args.manu_policy == "qlearning": reward = 0 if name == "Draw" else 1 if name == "Manu" else -1 current_state = None q_learning.update(current_state, previous_state, (row, column), reward) break else: if args.manu_policy == "qlearning": reward = 0 q_learning.update(frozen_board, previous_state, (row, column), reward) print(leaderboard) total_wins = float(leaderboard['Lia'] + leaderboard['Manu']) winning_ratio = float(leaderboard['Lia']) / total_wins return winning_ratio