Feb-06-2019, 06:33 PM
Hi everyone!
I've created a noughts and crosses game in Python, and I can't figure out the best way to eliminate global variables.
I'm not sure what variables to instantiate or methods to have in my class.
Any help would be great!
I am aware that my code doesn't follow PEP 8, I'm just trying to get rid of the global variables right now.
I've created a noughts and crosses game in Python, and I can't figure out the best way to eliminate global variables.
I'm not sure what variables to instantiate or methods to have in my class.
Any help would be great!
I am aware that my code doesn't follow PEP 8, I'm just trying to get rid of the global variables right now.
import time import random def reset_values(): global BOARD, INPUTS, PLAYER_SYMBOLS, draw_count, computer_level, MAIN_TEXT BOARD = [["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"]] 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}] PLAYER_SYMBOLS = {"O": "noughts", "X": "crosses"} draw_count = 0 computer_level = 0 MAIN_TEXT = """ ┌───────────────────────────────┐ │Welcome to noughts and crosses!│ │Press Q in any prompt to quit. │ └───────────────────────────────┘""" def computer_player(): """ Runs the player vs computer game. Calls other functions in a while True loop. """ while True: c_get_level() c_get_symbol() c_print_symbols() c_get_start() number_board() print_board() reset_board() while True: c_get_square() check_end() c_computer_move() check_end() def c_get_level(): global computer_level while True: wait() level_input = input("\nWhat level would you like the computer to play at? (1/2/3) ") wait() if level_input in ("1", "2", "3"): 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: invalid_input() def c_get_symbol(): """ Gets player's input for their character """ global player_symbol, computer_symbol while True: wait() symbol_input = input("\nWould you like to be noughts or crosses? (O/X) ") wait() if symbol_input in ("O", "o", "0"): player_symbol = "O" computer_symbol = "X" number_board() break elif symbol_input in ("X", "x"): player_symbol = "X" computer_symbol = "O" number_board() break elif symbol_input in ("Q", "q"): quit_program() else: invalid_input() def c_print_symbols(): print("\n\ ┌───────────────────────┐\n\ │Player 1 is " + PLAYER_SYMBOLS[player_symbol] + " " + "(" + player_symbol + ")" + "│\n\ │Computer is " + PLAYER_SYMBOLS[computer_symbol] + " " + "(" + computer_symbol + ")" + "│\n\ └───────────────────────┘") def c_get_start(): """ Ask the user if they want to start the game. Either restarts or quits if they don't. """ while True: wait() start_input = input("\nWould you like to start the game? (Y/N) ") wait() if start_input in ("Y", "y"): break elif start_input in ("N", "n"): 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: invalid_input() def reset_board(): """ Initialise the board to empty spaces. ┌───┬───┬───┐ │ │ │ │ ├───┼───┼───┤ │ │ │ │ ├───┼───┼───┤ │ │ │ │ └───┴───┴───┘ """ global BOARD BOARD = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]] def number_board(): """ Initialise the board to numbers. ┌───┬───┬───┐ │ 1 │ 2 │ 3 │ ├───┼───┼───┤ │ 4 │ 5 │ 6 │ ├───┼───┼───┤ │ 7 │ 8 │ 9 │ └───┴───┴───┘ """ global BOARD BOARD = [["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"]] def print_board(): """ Draw the board with the characters in the BOARD list. """ global BOARD wait() row_count = 0 print("\n┌───┬───┬───┐") # print first line (row_count = 0) for line in 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) wait() def c_get_square(): """ Get player's square and check if it's legal. """ global BOARD 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: invalid_input() continue if BOARD[INPUTS[int(square_input) - 1]["row"]][INPUTS[int(square_input) - 1]["column"]] in ("X", "O"): print("\nSorry, you have put a symbol in the same place as another!\n") else: BOARD[INPUTS[int(square_input) - 1]["row"]][INPUTS[int(square_input) - 1]["column"]] = player_symbol break print_board() def c_computer_move(): if computer_level == 1: while True: computer_move = random.randint(1, 9) computer_row = INPUTS[computer_move - 1]["row"] computer_col = INPUTS[computer_move - 1]["column"] if BOARD[computer_row][computer_col] in ("X", "O"): continue else: BOARD[computer_row][computer_col] = computer_symbol break print_board() ## if computer_level == 2: ## ## while True: ## if def check_end(): global BOARD global draw_count while True: if BOARD[0][0] == BOARD[0][1] == BOARD[0][2] != " ": state_win() elif BOARD[1][0] == BOARD[1][1] == BOARD[1][2] != " ": state_win() elif BOARD[2][0] == BOARD[2][1] == BOARD[2][2] != " ": state_win() elif BOARD[0][0] == BOARD[1][0] == BOARD[2][0] != " ": state_win() elif BOARD[0][1] == BOARD[1][1] == BOARD[2][1] != " ": state_win() elif BOARD[0][2] == BOARD[1][2] == BOARD[2][2] != " ": state_win() elif BOARD[0][0] == BOARD[1][1] == BOARD[2][2] != " ": state_win() elif BOARD[0][2] == BOARD[1][1] == BOARD[2][0] != " ": state_win() else: break while True: for row in BOARD: for symbol in row: if symbol != " ": draw_count += 1 if draw_count == 9: state_draw() else: pass else: break break break break def state_win(): 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"): wait() print("Restarting the game...") wait() computer_player() elif restart_input in ("N", "n"): quit_program() else: wait() invalid_input() def state_draw(): 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"): restart_calculator() elif restart_input in ("N", "n"): quit_program() else: wait() invalid_input() def quit_program(): while True: quit_input = input("Do you want to quit the game (Y/N)? ") if quit_input in ("Y", "y"): wait() print("\nNow quitting the game", end="") print_dots() wait() raise SystemExit elif quit_input in ("N", "n"): wait() print("Restarting the game...") wait() computer_player() else: wait() invalid_input() def restart_calculator(): wait() print("\nRestarting the game", end="") print_dots() wait() computer_player() def print_dots(): wait() for _ in range(3): print(".", end="", flush=True) wait() def wait(): time.sleep(0.25) def invalid_input(): wait() print("\nYou have entered an invalid response. Enter H for help.\n") computer_player()