Skip to content

Instantly share code, notes, and snippets.

@jacob-herr
Created March 10, 2023 02:26
Show Gist options
  • Save jacob-herr/2a63f20036405fb068321337b4bfea7b to your computer and use it in GitHub Desktop.
Save jacob-herr/2a63f20036405fb068321337b4bfea7b to your computer and use it in GitHub Desktop.
import random
import time
row1 = [" ", " ", " "]
row2 = [" ", " ", " "]
row3 = [" ", " ", " "]
#draws the board to the screen
def display(row1, row2, row3):
print("\n")
print(" " + "|".join(row1))
print(" ------- ")
print(" " + "|".join(row2))
print(" ------- ")
print(" " + "|".join(row3))
print("\n")
time.sleep(0.5)
#gets valid input from the user
def getinput():
i = 0
while i < 1:
global row
global collumn
row = int(input("select a row 1-3 : "))
collumn = int(input("select a collumn 1-3 : "))
if int(row) and int(collumn):
if row < 1 or row > 3 or collumn < 1 or collumn > 3:
print("1-3 please!")
continue
#checking if square is occupied
if row == 1:
if row1[collumn-1] != " ":
print("\n pick an empty sqaure! \n")
continue
if row == 2:
if row2[collumn-1] != " ":
print("\n pick an empty sqaure! \n")
continue
if row == 3:
if row3[collumn-1] != " ":
print("\n pick an empty sqaure! \n")
continue
i += 1
def drawplayer(ex,why):
y = int(why)
x = int(ex)
#puts point at y,x-1
if y == 1:
row1[x-1] = "x"
display(row1, row2, row3)
elif y == 2:
row2[x-1] = "x"
display(row1, row2, row3)
elif y == 3:
row3[x-1] = "x"
display(row1, row2, row3)
#draws randomly placed point
def drawcomputer():
i = 0
while i < 1:
x = random.randint(1,3)
y = random.randint(1,3)
# will only draw on blank square
if y == 1:
if row1[x-1] != " ":
continue
elif y == 2:
if row2[x-1] != " ":
continue
elif y == 3:
if row3[x-1] != " ":
continue
if y == 1:
row1[x-1] = "0"
display(row1, row2, row3)
elif y == 2:
row2[x-1] = "0"
display(row1, row2, row3)
elif y == 3:
row3[x-1] = "0"
display(row1, row2, row3)
i += 1
display(row1, row2, row3)
def winconditions():
global tie
global gameover
global gamelost
global win
gameover = False
gamelost = False
tie = False
joined = row1 + row2 + row3
b = 9
#cycles through every square and subtracts 1 for every occupied one
for i in joined:
if i == "x" or i == "0":
b -= 1
if b == 0:
gameover = True
tie = True
for i in range(0,3):
if row1[i] == "x" and row2[i] == "x" and row3[i] == "x":
gameover = True
win = True
if row1[i] == "0" and row2[i] == "0" and row3[i] == "0":
gameover = True
gamelost = True
if row1[0] == "x" and row1[1] == "x" and row1[2] == "x":
gameover = True
win = True
elif row2[0] == "x" and row2[1] == "x" and row2[2] == "x":
gameover = True
win = True
if row1[0] == "0" and row1[1] == "0" and row1[2] == "0":
gameover = True
gamelost = True
elif row2[0] == "0" and row2[1] == "0" and row2[2] == "0":
gameover = True
gamelost = True
if row3[0] == "x" and row3[1] == "x" and row3[2] == "x":
gameover = True
win = True
elif row1[0] == "x" and row2[1] == "x" and row3[2] == "x":
gameover = True
win = True
elif row3[0] == "x" and row2[1] == "x" and row1[2] == "x":
gameover = True
win = True
if row3[0] == "x" and row3[1] == "x" and row3[2] == "x":
gameover = True
win = True
elif row1[0] == "x" and row2[1] == "x" and row3[2] == "x":
gameover = True
win = True
elif row3[0] == "x" and row2[1] == "x" and row1[2] == "x":
gameover = True
win = True
return gameover
while winconditions() == False:
getinput()
drawplayer(collumn, row)
if winconditions() == True:
break
drawcomputer()
if tie:
print("Gameover !\n\n you tied!\n\n\n\n\n\n")
elif win:
print("Gameover !\n\n you won!\n\n\n\n\n\n")
elif gamelost:
print("Gameover !\n\n you lost :(\n\n\n\n\n\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment