Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #another tic tac toe game
- import random
- from random import randint
- #gameboard interface
- gameboard = [[" "] * 3, [" "] * 3, [" "] * 3] # 2d Array
- aigameboard = [[" "] * 3, [" "] * 3, [" "] * 3] # 2d Array for ai minmax sim
- input_var1 = [["a1", "b1", "c1"], ["a2", "b2", "c2"], ["a3", "b3", "c3"]]
- available_moves = ["a1", "b1", "c1", "a2", "b2", "c2", "a3", "b3", "c3"]
- potential_moves = ["a1", "b1", "c1", "a2", "b2", "c2", "a3", "b3", "c3"] #for minmax use
- rows = 3
- columns = 3
- player_symbols = ["_", "X", "O"]
- current_player = 1
- def end_game():
- global gameboard
- global aigameboard
- global potential_moves
- global available_moves
- global current_player
- print("We have a winner ", "Player ", current_player, " has won!")
- draw_gameboard()
- replay = input("Do you want to play again? Y/N: ")
- if replay == "Y" or replay == "y":
- gameboard = [[" "] * 3, [" "] * 3, [" "] * 3] # 2d Array
- aigameboard = [[" "] * 3, [" "] * 3, [" "] * 3]
- available_moves = ["a1", "b1", "c1", "a2", "b2", "c2", "a3", "b3", "c3"]
- potential_moves = ["a1", "b1", "c1", "a2", "b2", "c2", "a3", "b3", "c3"]
- current_player = 1
- main_game_loop(False)
- else:
- quit()
- def draw_gameboard(): #this is our game output
- print(" A B C")
- for i in range(len(gameboard)):
- print(i+1, (gameboard[i]))
- def draw_fake_gameboard(board): #this is debug tool
- print(" A B C <- fake ai gameboard")
- for i in range(len(board)):
- print(i+1, (board[i]))
- def get_player_input():
- print("It's your turn, proper input example is 'a3', or 'b1'. Enter 'x' to quit")
- pos = input("Enter position you want to take:")
- if pos == "x":
- quit()
- return pos
- def get_ai_input():
- global aigameboard
- global gameboard
- global potential_moves
- global available_moves
- score = []
- for i in range(len(available_moves)):
- score.append(mini_max(available_moves[i], aigameboard, True))
- aigameboard = gameboard
- print(score)
- pos = available_moves[score.index(max(score))]
- #pos = available_moves[randint(0, (len(available_moves))-1)]
- return pos
- def mini_max(position, board, isMaximizing):
- global input_var1
- global potential_moves
- score = 0
- scores = []
- winner = 3
- probed_position_x = 0
- probed_position_y = 0
- player = 1
- value = 0
- position_index = 0
- internal_board = board
- next_player = not isMaximizing
- #draw_fake_gameboard(board)
- #print(position)
- if isMaximizing == True:
- player = 2
- value = 1
- else:
- player = 1
- value = -1 #we wil multiply check for winners by value to get us -1 in case of minimizing
- for x in range(len(input_var1)):
- for y in range(len(input_var1[x])):
- if input_var1[x][y] == position:
- board[x][y] = player_symbols[player]
- probed_position_x = x
- probed_position_y = y
- position_index = potential_moves.index(position)
- potential_moves.remove(position)
- winner = check_for_winners(board, potential_moves)
- if winner == 1 or winner == 0:
- #there is a winner here
- score = winner*value
- board[probed_position_x][probed_position_y] = " "
- potential_moves.insert(position_index, position)
- return score
- else:
- for i in range(len(potential_moves)):
- scores.append(mini_max(potential_moves[i], internal_board, next_player))
- internal_board = board
- if isMaximizing:
- score = min(scores)
- else:
- score = max(scores)
- board[probed_position_x][probed_position_y] = " "
- potential_moves.insert(position_index, position)
- return score
- def update_game(player_number):
- global gameboard
- global input_var1
- global current_player
- if player_number == 1:
- pos = get_player_input()
- else:
- pos = get_ai_input()
- if pos in available_moves:
- available_moves.remove(pos)
- potential_moves.remove(pos)
- for x in range(len(input_var1)):
- for y in range(len(input_var1[x])):
- if input_var1[x][y] == pos:
- gameboard[x][y] = player_symbols[player_number]
- print(" ")
- print("Player", player_number, "move: ")
- else:
- print("this position is invalid or already taken")
- update_game(player_number)
- def check_for_winners(gameboard_state, moves):
- column_check = []
- #horizontal
- for x in range(len(gameboard_state[0])):
- if gameboard_state[x].count(gameboard_state[x][0]) == len(gameboard_state[x]) and gameboard_state[x][0] != " ":
- #there is horizontal winner
- return 1
- #vertical
- for i in range(len(gameboard_state)):
- for row in gameboard_state:
- column_check.append(row[i])
- if column_check.count(column_check[0]) == len(column_check) and column_check[0] != " ":
- #there is vertical winner
- return 1
- else:
- column_check.clear()
- #diagonal right
- for counter, row in enumerate(gameboard_state):
- column_check.append(row[counter])
- if column_check.count(column_check[0]) == len(column_check) and column_check[0] != " ":
- #there is diagonal winner
- return 1
- else:
- column_check.clear()
- #diagonal left
- for counter, row in enumerate(gameboard_state):
- column_check.append(row[-counter-1])
- if column_check.count(column_check[0]) == len(column_check) and column_check[0] != " ":
- #there is diagonal winner
- return 1
- else:
- column_check.clear()
- #check for TIE
- if len(moves) == 0:
- return 0
- return -3
- def change_player():
- global current_player
- if current_player == 1:
- current_player = 2
- else:
- current_player = 1
- def main_game_loop(game_ended):
- global aigameboard
- while game_ended == False:
- draw_gameboard()
- update_game(current_player)
- if check_for_winners(gameboard, available_moves) != -3:
- game_ended = True
- else:
- aigameboard = gameboard
- change_player()
- else:
- end_game()
- main_game_loop(False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement