Skip to content

Instantly share code, notes, and snippets.

@nawarazpokhrel
Created December 11, 2022 13:05
Show Gist options
  • Save nawarazpokhrel/871416cacc8caee64d0f3717794bd602 to your computer and use it in GitHub Desktop.
Save nawarazpokhrel/871416cacc8caee64d0f3717794bd602 to your computer and use it in GitHub Desktop.
Python Tic Tac Toe Game
class TicTacToe:
player1 = input("What’s player #1 name? ")
player2 = input("What’s player #2 name? ")
def __init__(self):
# initialize the game board
self.board = ["1", "2", "3",
"4", "5", "6",
"7", "8", "9"]
# initialize player names and turn
self.player_turn = self.player1
def print_board(self):
# function to print the current state of the game board
print("| " + self.board[0] + " | " + self.board[1] + " | " + self.board[2] + " |")
print("| " + self.board[3] + " | " + self.board[4] + " | " + self.board[5] + " |")
print("| " + self.board[6] + " | " + self.board[7] + " | " + self.board[8] + " |")
def play_game(self):
# main game loop
game_over = False
while not game_over:
# print current game state
self.print_board()
# Check if all space has been fulfilled by O and X if it is fulfilled then game is tied
result = all(self.check_values(value) for value in self.board)
if not result:
# prompt player for their next move
space = int(input(f"{self.player_turn}, please choose your number to play: "))
# check if user input is between 1 and 9 else we raise prompt
if 1 <= space <= 9:
# check if the chosen space is empty
if self.board[space - 1] != "X" and self.board[space - 1] != "O":
# update the game board with the player's move
self.board[space - 1] = "X" if self.player_turn == self.player1 else "O"
# check for a win and all possible cases where the game rule is fulfilled.
if (self.board[0] == self.board[1] == self.board[2]) or \
(self.board[3] == self.board[4] == self.board[5]) or \
(self.board[6] == self.board[7] == self.board[8]) or \
(self.board[0] == self.board[3] == self.board[6]) or \
(self.board[1] == self.board[4] == self.board[7]) or \
(self.board[2] == self.board[5] == self.board[8]) or \
(self.board[0] == self.board[4] == self.board[8]) or \
(self.board[2] == self.board[4] == self.board[6]):
# if a win is detected, print the winner and end the game
print(f"Congratulations! {self.player_turn}! you Won.")
self.print_board()
again = input("Do you want to play another round? (Y/N) ")
if again.lower() == "y":
print()
print()
print("Next Round Started.")
print("********************************")
self.__init__() # If yes, start a new game
self.play_game() # Play the new game
else:
print("Thank You for playing the game!")
game_over = True
# if there is no winner, switch to the other player's turn
else:
if self.player_turn == self.player1:
self.player_turn = self.player2
else:
self.player_turn = self.player1
else:
# checking if space is already occupied
print(f"{self.player_turn}!! that space is already occupied please try again on new space")
else:# promoting that value must be between 1 and 9
print(f"{self.player_turn}!! {space} is not a valid number!.")
else:
# case when game gets draw
print("Game was Tied!!")
# asking if user wants another game.
again = input("Do you want to play another round? (Y/N) ")
if again.lower() == "y":
print("Next Round Started.")
print("********************************")
self.__init__() # If yes, start a new game
self.play_game() # Play the new game
else:
print("Thank You for playing the game!")
game_over = True
@staticmethod
# a method that return true or false if value space is O or X
def check_values(value):
return value == 'X' or value == 'O'
def main():
# initialize class instance
game = TicTacToe()
# call play game method
game.play_game()
if __name__ == "__main__":
# call main function
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment