My attemp at the Tic Tac Toe - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Code sharing (https://python-forum.io/forum-5.html) +--- Thread: My attemp at the Tic Tac Toe (/thread-27455.html) |
My attemp at the Tic Tac Toe - menator01 - Jun-08-2020 As always, I welcome input. It's easy to win. I need to work on the logic, if there ar two x's in a row. Give it a random choice picking the slot or another. #! /usr/bin/env python3 '''Tic - Tac - Toe''' # Do the imports from subprocess import call import os import random as rnd from time import sleep # Check if a *nix or windoas os if os.name == 'nt': import colorama colorama.init() else: pass # Define some basic colors bright_red = '\033[91m' bright_yellow = '\033[33;1m' bright_cyan = '\033[36;1m' bright_blue = '\033[34;1m' bright_magenta = '\033[35;1m' bright_green = '\033[32;1m' # Other ansi sequences underline = '\033[4m' bold = '\033[1m' # Resets all ansi to system default reset = '\033[0m' class Game: def __init__(self): # Set the numbers for our game grid self.grid = [i for i in range(1, 10)] # Set what we are looking for in the grid for a win self.x3 = [f'{bright_cyan}X{reset}', f'{bright_cyan}X{reset}', f'{bright_cyan}X{reset}'] self.o3 = [f'{bright_magenta}O{reset}', f'{bright_magenta}O{reset}', f'{bright_magenta}O{reset}'] self.turn = 0 self.clear() print('This game uses terminal colors. If you are on windows, you will', flush=True) print('need to do python3 -m pip install colorama for correct game view.', flush=True) sleep(3) self.clear() while True: print(f'{bright_yellow}-------------{reset}',flush=True) print(f'{bright_yellow}|{reset}{underline}{bold}{bright_blue}Tic Tac Toe{reset}{bright_yellow}|{reset}', flush=True) print(f'{bright_yellow}-------------{reset}',flush=True) self.turn += 1 try: if self.turn % 2 == True: self.board() print(f'{bright_cyan}Please enter a number from the board.{reset}',flush=True) number = int(input('>>> ')) self.player = 'human' if number > 0 and number < 10: # Checks to see what available numbers if number in self.pick(): # Set the slot to players choice self.letters(number) # Number has already been picked. Set message for player to try again else: self.clear() print(f'\n{bright_red}Error!{reset} {bright_yellow}That number has already been chosen. Please try again.{reset}', flush=True) self.turn = 0 continue else: self.clear() print(f'{bright_red}Error!{reset} {bright_green}Please use whole numbers between 1 - 9.{reset}', flush=True) self.turn = 0 continue else: self.board() print(f'{bright_magenta}Computer choosing{reset} ...', flush=True) sleep(3) self.player = 'computer' try: self.letters(rnd.choice(self.pick())) except IndexError as error: print(f'{bright_yellow}Looks like a tie game.', flush=True) self.play_again() # Check for any wins. Ask to play again for check in self.possible(): if self.x3 == check: print(f'{bright_yellow}Congradulations! You Won{reset}', flush=True) self.play_again() if self.o3 == check: print(f'{bright_yellow}Sorry, you lost the game.{reset}', flush=True) self.play_again() except ValueError as error: self.clear() print(f'{bright_red}Error!{reset}{bright_yellow}Please use whole number between 1 and 9.{reset}', flush=True) self.turn = 0 continue # Function for asking to play or exit game def play_again(self): try: ask = input(f'{bright_yellow}Would you like to play again? y/n:{reset} ').lower() if ask.strip() == 'y': self.clear() self.grid = '' self.grid = [i for i in range(1, 10)] self.turn = 0 else: self.clear() print(f'{bright_cyan}Thankyou for playing!{reset}', flush=True) os.sys.exit() except ValueError as error: print(f'{bright_red}error{reset}') # Function for determing possible wins def possible(self): # Rows self.row1 = [self.grid[0], self.grid[1], self.grid[2]] self.row2 = [self.grid[3], self.grid[4], self.grid[5]] self.row3 = [self.grid[6], self.grid[7], self.grid[8]] # Columns self.col1 = [self.grid[0], self.grid[3], self.grid[6]] self.col2 = [self.grid[1], self.grid[4], self.grid[7]] self.col3 = [self.grid[2], self.grid[5], self.grid[8]] # Diagonals self.diag1 = [self.grid[0], self.grid[4], self.grid[8]] self.diag2 = [self.grid[2], self.grid[4], self.grid[6]] wins = [self.row1, self.row2, self.row3, self.col1, self.col2, self.col3, self.diag1, self.diag2] self.clear() return wins # Inserts player or computers choice. X or O def letters(self, number): if number in self.grid: index = self.grid.index(number) if self.player == 'human': self.grid[index] = f'{bright_cyan}X{reset}' else: self.grid[index] = f'{bright_magenta}O{reset}' self.grid = self.grid else: pass # Checks for available slots and retuns to players def pick(self): # Get all the slots choices = self.grid # Define empty list player_choices = [] # Go through the list and add any integers to players choice. Return for choice in choices: if type(choice) == int: player_choices.append(choice) else: pass return player_choices # Create our grid def board(self): j = 0 print('-------------') for i in self.grid: print(f'| {i} ', end='') if j == 2: print('|') print('-------------') j = 0 else: j += 1 # Function for clearing screen of cluttered text def clear(self): _ = call('clear' if os.name == 'posix' else 'cls') def main(): Game() if __name__ == '__main__': main() |