Skip to content

Instantly share code, notes, and snippets.

@Eclairemoy
Last active August 4, 2019 23:05
Show Gist options
  • Save Eclairemoy/63acdae568204aec32558255a4f0eaf9 to your computer and use it in GitHub Desktop.
Save Eclairemoy/63acdae568204aec32558255a4f0eaf9 to your computer and use it in GitHub Desktop.
# tic tac toe
# TODO: adding support for a computer player to your game.
# You can start with random moves and make the AI smarter if you have time.
player_dict = {0: "*",
1: "",
2: ""}
board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
turn = 1
class TicTacToe:
#START GAME
@classmethod
def start_game(cls):
global player_dict
global board
global turn
winner = False
print("Let's play a game of Tic Tac Toe!")
player_name = input("Player one, please choose X or O: ")
is_letter_valid = cls.check_letter(player_name)
while not is_letter_valid:
player_name = input("Please choose only X or O: ")
is_letter_valid = cls.check_letter(player_name)
player_dict[1] = player_name.lower()
if player_dict[1] == "x":
player_dict[2] = "o"
else:
player_dict[2] = "x"
print("Okay, player one you are " + player_dict[1] + "'s. Player two, you are " + player_dict[2] + "'s.")
cls.draw_board()
while turn < 10 and winner is False:
print("Player " + str(cls.get_current_player()) + "'s turn.")
player_one_move = input("Please choose which spot on the board you want, between 1-9 where the top left corner is 1, the top right corner is 3, the bottom left corner is 7 and the bottom right corner is 9 ")
player_choice = cls.get_user_input(player_one_move)
is_space_empty = cls.is_space_empty(player_choice)
while not is_space_empty:
player_choice = input("That space is taken. Please choose another space: ")
is_space_empty = cls.is_space_empty(int(player_choice))
board = cls.update_board(player_choice, cls.get_current_player())
winner = cls.check_for_winner(cls.get_current_player())
if winner:
print("Player " + str(cls.get_current_player()) + " wins!")
turn += 1
if turn == 10:
print("The game is a draw! Try again.")
cls.draw_board()
@classmethod
#GET CURRENT PLAYER
def get_current_player(cls):
global turn
global player_dict
player_number = 1
if turn % 2 == 0:
player_number = 2
return player_number
#DRAW BOARD
def draw_board():
global board
global player_dict
i=0
print('\n')
for row in board:
print("|" + player_dict[row[0]] + "|" + player_dict[row[1]] + "|" + player_dict[row[2]] + "|")
i+=1
if i < 3:
print("-------")
print('\n')
return board
@classmethod
#PROMPT USER INPUT
def get_user_input(cls, player_choice):
if player_choice != " ":
is_number_valid = cls.check_number_input(player_choice)
while not is_number_valid:
player_choice = input("Please choose a number between 1 and 9: ")
is_number_valid = cls.check_number_input(player_choice)
return player_choice
#CHECK Xs or Os
def check_letter(input):
if input.lower() == "x" or input.lower() == "o":
return True
return False
#CHECK Xs or Os
def check_number_input(num_input):
if num_input != " ":
num_input = int(num_input)
if 0 < num_input < 10:
return True
return False
#CHECK BOARD SPACE
def is_space_empty(choice):
#if space is taken return true otherwise return false
global board
k=0
for i in range(len(board)):
for j in range(len(board)):
if int(k) == int(choice) - 1 and board[i][j] != 0:
return False
k+=1
return True
#UPDATE BOARD SPACE
def update_board(choice, current_player):
#Update the space on the board with current player's choice
global board
k=0
for i in range(len(board)):
for j in range(len(board)):
if int(k) == int(choice) - 1:
board[i][j] = current_player
k+=1
return board
#CHECK IF WINNER
@classmethod
def check_for_winner(cls, current_player):
global board
if cls.horizontal_check(int(current_player)):
return True
elif cls.vertical_check(int(current_player)):
return True
elif cls.diagonal_check(int(current_player)):
return True
return False
@classmethod
def horizontal_check(cls, player_num):
for i in board:
if i[0] == i[1] == i[2] == player_num:
return True
return False
@classmethod
def vertical_check(cls, player_num):
for j in range(3):
if board[0][j] == board[1][j] == board[2][j] == player_num:
return True
return False
@classmethod
def diagonal_check(cls, player_num):
if board[2][0] == board[1][1] == board[0][2] == player_num:
return True
elif board[0][0] == board[1][1] == board[2][2] == player_num:
return True
return False
if __name__== "__main__":
TicTacToe.start_game()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment