Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with classes
#3
Your code doesn't run as presented:
Error:
What level would you like the computer to play at? (1/2/3) 1 Would you like to be noughts or crosses? (O/X) X Traceback (most recent call last): File "/media/larz60/Data-2TB/Projects/T-Z/T/TryStuff/src/classesFeb6-2019Orig.py", line 396, in <module> computer_player() File "/media/larz60/Data-2TB/Projects/T-Z/T/TryStuff/src/classesFeb6-2019Orig.py", line 42, in computer_player c_print_symbols() File "/media/larz60/Data-2TB/Projects/T-Z/T/TryStuff/src/classesFeb6-2019Orig.py", line 112, in c_print_symbols │Computer is " + PLAYER_SYMBOLS[computer_symbol] + " " + "(" + computer_symbol + ")" + "│\n\ NameError: name 'PLAYER_SYMBOLS' is not defined
So you will have to fix errors in class as well.
** Note ** I have not attempted to make code any more efficient, but there is room for improvement.
Again you will need to fix errors:
import time
import random


class NoughtsAndCrosses:
    def __init__(self):
        self.Board = None
        self.draw_count = None
        self.INPUTS = None
        self.PLAYER_SYMBOLS = None
        self.computer_level = None
        self.MAIN_TEXT = None
        self.computer_symbol = None
        self.player_symbol = None
        self.reset_values()
        self.computer_player()

    def reset_values(self):
        self.BOARD = [["1", "2", "3"],
                ["4", "5", "6"],
                ["7", "8", "9"]]
    
        self.INPUTS = [{"row": 0, "column": 0},
                {"row": 0, "column": 1},
                {"row": 0, "column": 2},
                {"row": 1, "column": 0},
                {"row": 1, "column": 1},
                {"row": 1, "column": 2},
                {"row": 2, "column": 0},
                {"row": 2, "column": 1},
                {"row": 2, "column": 2}]
    
        self.PLAYER_SYMBOLS = {"O": "noughts", "X": "crosses"}
    
        self.draw_count = 0
    
        self.computer_level = 0
    
        self.MAIN_TEXT = """
        ┌───────────────────────────────┐
        │Welcome to noughts and crosses!│
        │Press Q in any prompt to quit. │
        └───────────────────────────────┘"""
    
    def computer_player(self):
        """
        Runs the player vs computer game. Calls other functions in a while True loop.
        """
        
        while True:
            self.c_get_level()
            self.c_get_symbol()
            self.c_print_symbols()
            self.c_get_start()
            self.number_board()
            self.print_board()
            self.reset_board()
    
            while True:
                self.c_get_square()
                self.check_end()
                self.c_computer_move()
                self.check_end()
    
    
    def c_get_level(self):
        while True:
            self.wait()
            level_input = input("\nWhat level would you like the computer to play at? (1/2/3) ")
            self.wait()
    
            if level_input in ("1", "2", "3"):
                self.computer_level = int(level_input)
                break
    
            elif level_input in ("H", "h"):
                print("You need to enter either 1, 2 or 3 here. This will decide the level that the computer will")
            
            else:
                self.invalid_input()
    
    
    def c_get_symbol(self):
        """
        Gets player's input for their character
        """
        while True:
            self.wait()
            symbol_input = input("\nWould you like to be noughts or crosses? (O/X) ")
            self.wait()
            
            if symbol_input in ("O", "o", "0"):
                self.player_symbol = "O"
                self.computer_symbol = "X"
                
                self.number_board()
                break
    
            elif symbol_input in ("X", "x"):
                player_symbol = "X"
                computer_symbol = "O"
                
                self.number_board()
                break
            
            elif symbol_input in ("Q", "q"):
                self.quit_program()
            
            else:
                self.invalid_input()
    
    
    def c_print_symbols(self):
        print("\n\
    ┌───────────────────────┐\n\
    │Player 1 is " + self.PLAYER_SYMBOLS[self.player_symbol] + " " + "(" + self.player_symbol + ")" + "│\n\
    │Computer is " + self.PLAYER_SYMBOLS[self.computer_symbol] + " " + "(" + self.computer_symbol + ")" + "│\n\
    └───────────────────────┘")
    
    
    def c_get_start(self):
        """
        Ask the user if they want to start the game. Either restarts or quits if they don't.
        """
    
        while True:
            self.wait()
            start_input = input("\nWould you like to start the game? (Y/N) ")
            self.wait()
    
            if start_input in ("Y", "y"):
                break
    
            elif start_input in ("N", "n"):
                self.quit_program()
    
            elif start_input in ("H", "h"):
                print("You need to enter either Y or N here. Y will start the game and N will quit it.")
    
            else:
                self.invalid_input()
    
    
    def reset_board(self):
        """
        Initialise the board to empty spaces.
        ┌───┬───┬───┐
        │   │   │   │ 
        ├───┼───┼───┤
        │   │   │   │ 
        ├───┼───┼───┤
        │   │   │   │ 
        └───┴───┴───┘
        """
        
        self.BOARD = [[" ", " ", " "],
                [" ", " ", " "],
                [" ", " ", " "]]
    
    
    def number_board(self):
        """
        Initialise the board to numbers.
        ┌───┬───┬───┐
        │ 1 │ 2 │ 3 │ 
        ├───┼───┼───┤
        │ 4 │ 5 │ 6 │ 
        ├───┼───┼───┤
        │ 7 │ 8 │ 9 │ 
        └───┴───┴───┘
        """
                
        self.BOARD = [["1", "2", "3"],
                ["4", "5", "6"],
                ["7", "8", "9"]]
    
    
    def print_board(self):
        """
        Draw the board with the characters in the BOARD list.
        """
    
        self.wait()
        row_count = 0
        
        print("\n┌───┬───┬───┐") # print first line (row_count = 0)
        
        for line in self.BOARD:
            print("│ ", end = "") # print first vertical separator and space
            row_count += 1 # 1, 2, 3
            
            for item in line:
                print(item, end = " │ ") # print symbol, a space, a vertical separator and a space
            
            if row_count in (1, 2):
                print("\n├───┼───┼───┤") # print middle lines (row_count = 1, 2)
            
            elif row_count == 3:
                print("\n└───┴───┴───┘\n") # print last line (row_count = 3)
    
        self.wait()
    
    
    def c_get_square(self):
        """
        Get player's square and check if it's legal.
        """
        
        while True:
            square_input = input("What square would you like to choose? (1-9) ")
    
            if square_input in ("1", "2", "3", "4", "5", "6", "7", "8", "9"):
                pass
            
            else:
                self.invalid_input()
                continue
    
            if self.BOARD[self.INPUTS[int(square_input) - 1]["row"]][self.INPUTS[int(square_input) - 1]["column"]] in ("X", "O"):
                print("\nSorry, you have put a symbol in the same place as another!\n")
    
            else:
                self.BOARD[self.INPUTS[int(square_input) - 1]["row"]][self.INPUTS[int(square_input) - 1]["column"]] = self.player_symbol
                break
    
        self.print_board()
    
    
    def c_computer_move(self):
        if self.computer_level == 1:
    
            while True:
                computer_move = random.randint(1, 9)
                computer_row = self.INPUTS[computer_move - 1]["row"]
                computer_col = self.INPUTS[computer_move - 1]["column"]
    
                if self.BOARD[computer_row][computer_col] in ("X", "O"):
                    continue
    
                else:
                    self.BOARD[computer_row][computer_col] = self.computer_symbol
                    break
    
            self.print_board()
    
    ##    if computer_level == 2:
    ##        
    ##        while True:
    ##            if 
            
    
    
    
    def check_end(self):
        draw_count = self.draw_count
        
        while True:
            if self.BOARD[0][0] == self.BOARD[0][1] == self.BOARD[0][2] != " ":
                self.state_win()
    
            elif self.BOARD[1][0] == self.BOARD[1][1] == self.BOARD[1][2] != " ":
                self.state_win()
    
            elif self.BOARD[2][0] == self.BOARD[2][1] == self.BOARD[2][2] != " ":
                self.state_win()
    
            elif self.BOARD[0][0] == self.BOARD[1][0] == self.BOARD[2][0] != " ":
                self.state_win()
    
            elif self.BOARD[0][1] == self.BOARD[1][1] == self.BOARD[2][1] != " ":
                self.state_win()
    
            elif self.BOARD[0][2] == self.BOARD[1][2] == self.BOARD[2][2] != " ":
                self.state_win()
    
            elif self.BOARD[0][0] == self.BOARD[1][1] == self.BOARD[2][2] != " ":
                self.state_win()
    
            elif self.BOARD[0][2] == self.BOARD[1][1] == self.BOARD[2][0] != " ":
                self.state_win()
    
            else:
                break
    
        while True:
            for row in self.BOARD:
                for symbol in row:
                    if symbol != " ":
                        draw_count += 1
                        if draw_count == 9:
                            self.state_draw()
                        else:
                            pass
                    
                    else:
                        break
                    break
                break
            break
    
    
    def state_win(self):
        print("\n\
    ┌────────┐\n\
    │YOU WON!│\n\
    └────────┘\n")
    
        while True:
            restart_input = input("Do you want to play again? (Y/N) ")
    
            if restart_input in ("Y", "y"):
                self.wait()
                print("Restarting the game...")
                self.wait()
                self.computer_player()
    
            elif restart_input in ("N", "n"):
                self.quit_program()
    
            else:
                self.wait()
                self.invalid_input()
    
    
    def state_draw(self):
        print("\n\
    ┌────────────┐\n\
    │It's a draw!│\n\
    └────────────┘\n")
    
        while True:
            restart_input = input("Do you want to play again? (Y/N) ")
    
            if restart_input in ("Y", "y"):
                self.restart_calculator()
    
            elif restart_input in ("N", "n"):
                self.quit_program()
    
            else:
                self.wait()
                self.invalid_input()
    
    
    def quit_program(self):
        while True:
            quit_input = input("Do you want to quit the game (Y/N)? ")
            
            if quit_input in ("Y", "y"):
                self.wait()
                print("\nNow quitting the game", end="")
                self.print_dots()
                self.wait()
                raise SystemExit
    
            elif quit_input in ("N", "n"):
                self.wait()
                print("Restarting the game...")
                self.wait()
                
                self.computer_player()
    
            else:
                self.wait()
                self.invalid_input()
    
    
    def restart_calculator(self):
        self.wait()
        print("\nRestarting the game", end="")
        self.print_dots()
        self.wait()
        self.computer_player()
    
    
    def print_dots(self):
        self.wait()
        for _ in range(3):
            print(".", end="", flush=True)
            self.wait()
    
    
    def wait(self):
        time.sleep(0.25)
    
    
    def invalid_input(self):
        self.wait()
        print("\nYou have entered an invalid response. Enter H for help.\n")
 
if __name__ == '__main__':
    NoughtsAndCrosses()
Reply


Messages In This Thread
Help with classes - by lucaswinnerton - Feb-06-2019, 06:33 PM
RE: Help with classes - by woooee - Feb-06-2019, 08:22 PM
RE: Help with classes - by lucaswinnerton - Feb-06-2019, 08:26 PM
RE: Help with classes - by Larz60+ - Feb-06-2019, 08:23 PM
RE: Help with classes - by woooee - Feb-06-2019, 09:40 PM
RE: Help with classes - by lucaswinnerton - Feb-06-2019, 10:01 PM
RE: Help with classes - by Larz60+ - Feb-07-2019, 01:23 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Using classes? Can I just use classes to structure code? muteboy 5 5,107 Nov-01-2017, 04:20 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020