def game_engine(): count = 0 flag = 1 while True: avatar = str(input(' Which avatar(X,O) would you like?\n\n\t\t ')) if avatar in ('x','X'): avatar = 'X' ai_avatar = 'O' break elif avatar in ('o','O','0'): avatar = 'O' ai_avatar = 'X' break else: print('Invalid avatar!') continue clrscr() print_board() while flag == 1: if seed == 1: #(2*n)th game, n = 0,1,2,... User gets first turn move = user_play() board[str(move)] = avatar count += 1 if count >= 5 and win_check(board,avatar) == 0: break move = ai.ai_play(board,avatar) board[str(move)] = ai_avatar count += 1 if count >= 5 and win_check(board,avatar) == 0: break elif seed == -1: #((2*n) + 1)th game, n = 0,1,2,... Program gets first turn move = ai.ai_play(board,avatar) board[str(move)] = ai_avatar count += 1 clrscr() print_board() if count >= 5 and win_check(board,avatar) == 0: break move = user_play() board[str(move)] = avatar count += 1 if count >= 5 and win_check(board,avatar) == 0: break clrscr() print_board()
def load_p_vs_ai(screen): """ Load the gamemode against an AI. """ run = True board = init_board() player_color = set_color_player() if player_color is None: return True ref = Referee() Referee.played = 0 ai_turn = True if player_color == 'w' else False while run: display_board(screen, board) if ai_turn: Referee.played += 2 if board == init_board(): pos = [9, 9] else: pos = ai.ai_play(board, ('b' if player_color == 'w' else 'w')) rboard, score, msg = ref.set_stone( board, ('b' if player_color == 'w' else 'w'), pos) if rboard is not None: board = rboard win = ref.check5(board, pos, ('b' if player_color == 'w' else 'w')) ai_turn = False run = ref.display_score(score, ('b' if player_color == 'w' else 'w'), win, msg) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: run = False elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and not ai_turn: coord_x, coord_y = pygame.mouse.get_pos() coord_x = int((coord_x - 5) / 40) coord_y = int((coord_y - 5) / 40) rboard, score, msg = ref.set_stone(board, player_color, [coord_y, coord_x]) if rboard is not None: board = rboard win = ref.check5(board, [coord_y, coord_x], player_color) ai_turn = True run = ref.display_score(score, player_color, win, msg) display_board(screen, board) update_stone_player(screen, player_color) pygame.display.update() return True
def newgame(self): #Clears board and game variables for new game app = App.get_running_app() app.board = { '7' : ' ', '8' : ' ', '9' : ' ', '4' : ' ', '5' : ' ', '6' : ' ', '1' : ' ', '2' : ' ', '3' : ' '} app.flag = 1 app.count = 0 app.avatar = 'X' if app.avatar == 'X': ai_avatar = 'O' else: ai_avatar = 'X' app.seed *= -1 self.ids.lbl7.text = ' ' self.ids.lbl8.text = ' ' self.ids.lbl9.text = ' ' self.ids.lbl4.text = ' ' self.ids.lbl5.text = ' ' self.ids.lbl6.text = ' ' self.ids.lbl1.text = ' ' self.ids.lbl2.text = ' ' self.ids.lbl3.text = ' ' if app.seed == -1: #block executes in every alternate game so that ai_play gets first turn move = ai.ai_play(app.board,app.avatar) print('ai move is: ') print(move) app.board[str(move)] = ai_avatar if str(move) == '7': self.ids.lbl7.text = app.board[str(move)] elif str(move) == '8': self.ids.lbl8.text = app.board[str(move)] elif str(move) == '9': self.ids.lbl9.text = app.board[str(move)] elif str(move) == '4': self.ids.lbl4.text = app.board[str(move)] elif str(move) == '5': self.ids.lbl5.text = app.board[str(move)] elif str(move) == '6': self.ids.lbl6.text = app.board[str(move)] elif str(move) == '1': self.ids.lbl1.text = app.board[str(move)] elif str(move) == '2': self.ids.lbl2.text = app.board[str(move)] elif str(move) == '3': self.ids.lbl3.text = app.board[str(move)] app.count += 1
def press(self,pos): #executed when any of the 9 game buttons are touched/pressed app = App.get_running_app() if app.flag == 1 : #checking that game has not ended app.board[pos] = app.avatar print('clicked pos: ' + pos) print('Value at clicked pos is: ' + app.board[pos]) if app.avatar == 'X': ai_avatar = 'O' else: ai_avatar = 'X' if pos == '7': if self.ids.lbl7.text == ' ': self.ids.lbl7.text = app.board[pos] else: return elif pos == '8': if self.ids.lbl8.text == ' ': self.ids.lbl8.text = app.board[pos] else: return elif pos == '9': if self.ids.lbl9.text == ' ': self.ids.lbl9.text = app.board[pos] else: return elif pos == '4': if self.ids.lbl4.text == ' ': self.ids.lbl4.text = app.board[pos] else: return elif pos == '5': if self.ids.lbl5.text == ' ': self.ids.lbl5.text = app.board[pos] else: return elif pos == '6': if self.ids.lbl6.text == ' ': self.ids.lbl6.text = app.board[pos] else: return elif pos == '1': if self.ids.lbl1.text == ' ': self.ids.lbl1.text = app.board[pos] else: return elif pos == '2': if self.ids.lbl2.text == ' ': self.ids.lbl2.text = app.board[pos] else: return elif pos == '3': if self.ids.lbl3.text == ' ': self.ids.lbl3.text = app.board[pos] else: return app.count += 1 if app.count >= 5 and self.win_check(app.board,app.avatar) == 0: print('User won') show_win_popup() app.flag = 0 return elif self.tie_check(app.board): print('Tie') show_tie_popup() app.flag = 0 return move = ai.ai_play(app.board,app.avatar) print('ai move is: ') print(move) app.board[str(move)] = ai_avatar if str(move) == '7': self.ids.lbl7.text = app.board[str(move)] elif str(move) == '8': self.ids.lbl8.text = app.board[str(move)] elif str(move) == '9': self.ids.lbl9.text = app.board[str(move)] elif str(move) == '4': self.ids.lbl4.text = app.board[str(move)] elif str(move) == '5': self.ids.lbl5.text = app.board[str(move)] elif str(move) == '6': self.ids.lbl6.text = app.board[str(move)] elif str(move) == '1': self.ids.lbl1.text = app.board[str(move)] elif str(move) == '2': self.ids.lbl2.text = app.board[str(move)] elif str(move) == '3': self.ids.lbl3.text = app.board[str(move)] app.count += 1 if app.count >= 5 and self.win_check(app.board,app.avatar) == 0: show_lost_popup() app.flag = 0 return elif self.tie_check(app.board): show_tie_popup() app.flag = 0 return else: return