Skip to content

Instantly share code, notes, and snippets.

@Spyder-0
Last active April 23, 2024 14:24
Show Gist options
  • Save Spyder-0/2c7d8dbb77275bef3d8d54330901249e to your computer and use it in GitHub Desktop.
Save Spyder-0/2c7d8dbb77275bef3d8d54330901249e to your computer and use it in GitHub Desktop.
A Simple Array Game where a User Adds O's to X's
import time
# The Aim of this Code is to Help Anyone Understand how Arrays, User Input, Validation, and Custom Errors work.
# Take a Look Around and Try Understanding What Each Part Does.
# Vars
grid = [
["X", "X", "X", "X"],
["X", "X", "X", "X"],
["X", "X", "X", "X"],
["X", "X", "X", "X"],
["X", "X", "X", "X"],
["X", "X", "X", "X"]
]
# Functions
def GridPrint():
for row in range(6):
print(grid[row])
def ts(SleepingTime):
time.sleep(SleepingTime)
# Main Program
print("[SYS] Welcome to the Grid Game!")
print("[SYS] To Quit, Press Ctrl+C.")
ts(3)
while True:
# Display
print("\n[SYS] This is your Grid currently:")
ts(0.5)
GridPrint()
ts(2)
# User Input and Check if it is Valids
while True:
try:
row = int(input("\n[CHOICE 1-6] Enter a Row Number: "))
ts(0.3)
column = int(input("[CHOICE 1-4] Enter a Column Nummber: "))
ts(0.3)
break
except:
print("[ERROR] Bad Input. Should be an Integer.")
ts(2)
# Turn User Input into Array Values
row = row - 1
column = column - 1
# Give Error if User Input Index is Out of Range
try:
grid[row][column] = "O"
except:
print("[ERROR] Coordinates Cannot be Found. List Index Out of Range?")
ts(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment