def computer_move(self): if self.turn == "computer": move = game.getComputerMove(main_app.board, "O") game.makeMove(main_app.board, "O", move) a = { 1: self.o_1, 2: self.o_2, 3: self.o_3, 4: self.o_4, 5: self.o_5, 6: self.o_6, 7: self.o_7, 8: self.o_8, 9: self.o_9 } Clock.schedule_once(partial(self.set_opacity, a[move], 1, 0.1), 1) if game.isWinner(main_app.board, "O"): print('The computer has beaten you! You lose.') self.gameIsPlaying = False main_app.lose.play() self.manager.current = "lose" self.clear_screen() main_app.board = [" "] * 10 else: if game.isBoardFull(main_app.board): print('The game is a tie!') self.gameIsPlaying = False self.clear_screen() main_app.lose.play() self.manager.current = "tie" main_app.board = [" "] * 10 else: self.turn = "player" self.turn = "player"
def game_start(self): self.turn = game.whoGoesFirst() #self.turn = "player" print(self.turn) self.gameIsPlaying = True if self.turn == "computer": move = game.getComputerMove(main_app.board, "O") game.makeMove(main_app.board, "O", move) self.turn = "player" elif self.turn == "player": pass
def moveScore(game, move): # check for invalid move if not game.validMove(move): return MOVE_INVALID, MOVE_INVALID # evaluate the actual game for reference score player_base_score = boardEvaluation(game.board, game.token()) opponent_base_score = boardEvaluation(game.board, game.tokenOther()) # copy the actual game for opponent check game_opponent = deepcopy(game) # generate move for the current player winner_move, _ = game.makeMove(move) # check for a winning move if winner_move: return MOVE_WIN, MOVE_LOOSE # mock an opponent move for the current game winner_move, _ = game_opponent.makeMoveOther(move) if winner_move: return MOVE_LOOSE, MOVE_WIN # evaluate the current move player_score = boardEvaluation(game.board, game.tokenOther()) # evaluate the mock opponent move opponent_score = boardEvaluation(game_opponent.board, game_opponent.token()) return player_score - player_base_score, opponent_score - opponent_base_score
def makeMove(request, x, y): board = request.session['board'] result = game.makeMove(board, int(x), int(y)) if result: request.session['board'] = board status = game.isGameOver(board) response = { 'success': True, 'gameover': status } else: response = { 'success': False, 'message': 'Invalid move: spot already taken.' } return HttpResponse(json.dumps(response), mimetype="application/json")
def on_touch_up(self, touch): super().on_touch_up(touch) a_delay = 0.05 if touch.spos[1] < 0.118: # Bottom Button if main_app.select: main_app.select.stop() main_app.select.play() if self.collide_point(*touch.pos): self.clear_screen() return False # 1st button if 0.15 < touch.spos[0] < 0.355 and 0.576 < touch.spos[1] < 0.696: if main_app.board[1] != "O" and main_app.board[ 1] != "X" and self.turn == "player": if main_app.select: main_app.select.stop() main_app.select.play() main_app.board[1] = "X" Clock.schedule_once( partial(self.set_opacity, self.x_1, 1, a_delay), 0) self.o_1.opacity = 0 game.makeMove(main_app.board, "X", 1) if game.isWinner(main_app.board, "X"): print('Hooray! You have won the game!') self.gameIsPlaying = False main_app.win.play() self.manager.current = "win" self.clear_screen() main_app.board = [" "] * 10 else: if game.isBoardFull(main_app.board): print('The game is a tie!') self.gameIsPlaying = False main_app.lose.play() self.manager.current = "tie" self.clear_screen() main_app.board = [" "] * 10 else: self.turn = 'computer' self.computer_move() return False # 2nd button if 0.380 < touch.spos[0] < 0.611 and 0.576 < touch.spos[1] < 0.696: if main_app.board[2] != "O" and main_app.board[ 2] != "X" and self.turn == "player": if main_app.select: main_app.select.stop() main_app.select.play() main_app.board[2] = "X" Clock.schedule_once( partial(self.set_opacity, self.x_2, 1, a_delay), 0) self.o_2.opacity = 0 game.makeMove(main_app.board, "X", 2) if game.isWinner(main_app.board, "X"): print('Hooray! You have won the game!') self.gameIsPlaying = False main_app.win.play() self.manager.current = "win" self.clear_screen() main_app.board = [" "] * 10 else: if game.isBoardFull(main_app.board): print('The game is a tie!') self.gameIsPlaying = False main_app.lose.play() self.manager.current = "tie" self.clear_screen() main_app.board = [" "] * 10 else: self.turn = 'computer' self.computer_move() return False # 3rd button if 0.633 < touch.spos[0] < 0.847 and 0.576 < touch.spos[1] < 0.696: if main_app.board[3] != "O" and main_app.board[ 3] != "X" and self.turn == "player": if main_app.select: main_app.select.stop() main_app.select.play() main_app.board[3] = "X" Clock.schedule_once( partial(self.set_opacity, self.x_3, 1, a_delay), 0) self.o_3.opacity = 0 game.makeMove(main_app.board, "X", 3) if game.isWinner(main_app.board, "X"): print('Hooray! You have won the game!') self.gameIsPlaying = False main_app.win.play() self.manager.current = "win" self.clear_screen() main_app.board = [" "] * 10 else: if game.isBoardFull(main_app.board): print('The game is a tie!') self.gameIsPlaying = False main_app.lose.play() self.manager.current = "tie" self.clear_screen() main_app.board = [" "] * 10 else: self.turn = 'computer' self.computer_move() return False # 4th button if 0.15 < touch.spos[0] < 0.355 and 0.437 < touch.spos[1] < 0.567: if main_app.board[4] != "O" and main_app.board[ 4] != "X" and self.turn == "player": if main_app.select: main_app.select.stop() main_app.select.play() main_app.board[4] = "X" Clock.schedule_once( partial(self.set_opacity, self.x_4, 1, a_delay), 0) self.o_4.opacity = 0 game.makeMove(main_app.board, "X", 4) if game.isWinner(main_app.board, "X"): print('Hooray! You have won the game!') self.gameIsPlaying = False main_app.win.play() self.manager.current = "win" self.clear_screen() main_app.board = [" "] * 10 else: if game.isBoardFull(main_app.board): print('The game is a tie!') self.gameIsPlaying = False main_app.lose.play() self.manager.current = "tie" self.clear_screen() main_app.board = [" "] * 10 else: self.turn = 'computer' self.computer_move() return False # 5th button if 0.380 < touch.spos[0] < 0.611 and 0.437 < touch.spos[1] < 0.567: if main_app.board[5] != "O" and main_app.board[ 5] != "X" and self.turn == "player": if main_app.select: main_app.select.stop() main_app.select.play() main_app.board[5] = "X" Clock.schedule_once( partial(self.set_opacity, self.x_5, 1, a_delay), 0) self.o_5.opacity = 0 game.makeMove(main_app.board, "X", 5) if game.isWinner(main_app.board, "X"): print('Hooray! You have won the game!') self.gameIsPlaying = False main_app.win.play() self.manager.current = "win" self.clear_screen() main_app.board = [" "] * 10 else: if game.isBoardFull(main_app.board): print('The game is a tie!') self.gameIsPlaying = False main_app.lose.play() self.manager.current = "tie" self.clear_screen() main_app.board = [" "] * 10 else: self.turn = 'computer' self.computer_move() return False # 6th button if 0.633 < touch.spos[0] < 0.847 and 0.437 < touch.spos[1] < 0.567: if main_app.board[6] != "O" and main_app.board[ 6] != "X" and self.turn == "player": if main_app.select: main_app.select.stop() main_app.select.play() main_app.board[6] = "X" Clock.schedule_once( partial(self.set_opacity, self.x_6, 1, a_delay), 0) self.o_6.opacity = 0 game.makeMove(main_app.board, "X", 6) if game.isWinner(main_app.board, "X"): print('Hooray! You have won the game!') self.gameIsPlaying = False main_app.win.play() self.manager.current = "win" self.clear_screen() main_app.board = [" "] * 10 else: if game.isBoardFull(main_app.board): print('The game is a tie!') self.gameIsPlaying = False main_app.lose.play() self.manager.current = "tie" self.clear_screen() main_app.board = [" "] * 10 else: self.turn = 'computer' self.computer_move() return False # 7th button if 0.15 < touch.spos[0] < 0.355 and 0.304 < touch.spos[1] < 0.423: if main_app.board[7] != "O" and main_app.board[ 7] != "X" and self.turn == "player": if main_app.select: main_app.select.stop() main_app.select.play() main_app.board[7] = "X" Clock.schedule_once( partial(self.set_opacity, self.x_7, 1, a_delay), 0) self.o_7.opacity = 0 game.makeMove(main_app.board, "X", 7) if game.isWinner(main_app.board, "X"): print('Hooray! You have won the game!') self.gameIsPlaying = False main_app.win.play() self.manager.current = "win" self.clear_screen() main_app.board = [" "] * 10 else: if game.isBoardFull(main_app.board): print('The game is a tie!') self.gameIsPlaying = False main_app.lose.play() self.manager.current = "tie" self.clear_screen() main_app.board = [" "] * 10 else: self.turn = 'computer' self.computer_move() return False # 8th button if 0.380 < touch.spos[0] < 0.611 and 0.304 < touch.spos[1] < 0.423: if main_app.board[8] != "O" and main_app.board[ 8] != "X" and self.turn == "player": if main_app.select: main_app.select.stop() main_app.select.play() main_app.board[8] = "X" Clock.schedule_once( partial(self.set_opacity, self.x_8, 1, a_delay), 0) self.o_8.opacity = 0 game.makeMove(main_app.board, "X", 8) if game.isWinner(main_app.board, "X"): print('Hooray! You have won the game!') self.gameIsPlaying = False main_app.win.play() self.manager.current = "win" self.clear_screen() main_app.board = [" "] * 10 else: if game.isBoardFull(main_app.board): print('The game is a tie!') self.gameIsPlaying = False main_app.lose.play() self.manager.current = "tie" self.clear_screen() main_app.board = [" "] * 10 else: self.turn = 'computer' self.computer_move() return False # 9th button if 0.633 < touch.spos[0] < 0.847 and 0.304 < touch.spos[1] < 0.423: if main_app.board[9] != "O" and main_app.board[ 9] != "X" and self.turn == "player": if main_app.select: main_app.select.stop() main_app.select.play() main_app.board[9] = "X" Clock.schedule_once( partial(self.set_opacity, self.x_9, 1, a_delay), 0) self.o_9.opacity = 0 game.makeMove(main_app.board, "X", 9) if game.isWinner(main_app.board, "X"): print('Hooray! You have won the game!') self.gameIsPlaying = False main_app.win.play() self.manager.current = "win" self.clear_screen() main_app.board = [" "] * 10 else: if game.isBoardFull(main_app.board): print('The game is a tie!') self.gameIsPlaying = False main_app.lose.play() self.manager.current = "tie" self.clear_screen() main_app.board = [" "] * 10 else: self.turn = 'computer' self.computer_move() return False
ns = game.getNext(gm) print("next moves:", len(ns), " possible moves ") bestMove = 0 for st in ns: tmp = abmax(st, d - 1, a, b) if tmp[0] < v: v = tmp[0] bestMove = st if v <= a: return [v, st] if v < b: b = v return [v, bestMove] ''' s=game.create() game.makeMove(s,1,1) print(s) game.makeMove(s,0,0) game.makeMove(s,0,1) game.makeMove(s,0,2) game.makeMove(s,1,0) game.makeMove(s,1,1) game.makeMove(s,1,2) game.makeMove(s,2,1) game.makeMove(s,2,0) game.printState(s) print(go(s)) '''
# values = [0, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 0, 4, 2, 2] # while(True): # inp = input("What? \n") # if (inp == "y"): # board.print_new_grid(values) # random.shuffle(values) grid = game.startGame() board.print_new_grid(grid) try: while (game.endGame(grid) == False): high_score = max(grid) inp = input("Move? \n") if inp == "a": if (game.moveLeft(grid)): game.makeMove(grid) else: print("Make a valid move!!") print("") elif inp == "d": if (game.moveRight(grid)): game.makeMove(grid) else: print("Make a valid move!!") print("") elif inp == "w": if (game.moveUp(grid)): game.makeMove(grid) else: