Created
March 11, 2023 00:24
-
-
Save jacob-herr/a117882f793337ec162b9da69dd6b3d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import time | |
#print board | |
board = [["-", "-", "-"], | |
["-", "-", "-"], | |
["-", "-", "-"]] | |
def print_board(): | |
print("\n") | |
print(" " + " | ".join(board[0])) | |
print(" ---+---+--- ") | |
print(" " + " | ".join(board[1])) | |
print(" ---+---+--- ") | |
print(" " + " | ".join(board[2])) | |
print("\n") | |
print_board() | |
#take player input | |
def take_input(): | |
while True: | |
row = int(input("enter a row 1-3: ")) | |
column = int(input("enter a column 1-3: ")) | |
if not (0 < row < 4 and 0 < column < 4): | |
print("numbers 1- 3 only") | |
continue | |
if board[row-1][column - 1] != "-": | |
print("empty squares only :") | |
continue | |
else: | |
return row, column | |
# draw player input | |
def draw_player(func): | |
(row, column) = func | |
board[row-1][column-1] = "x" | |
print_board() | |
#draw random computer input | |
def draw_AI(): | |
while True: | |
y = random.randint(0,2) | |
x = random.randint(0,2) | |
if board[y][x] != "-": | |
continue | |
else: | |
board[y][x] = "o" | |
print_board() | |
return | |
#checks and ends game if win, lose, tie | |
def winstates (): | |
game_over = False | |
c = 0 | |
b = 0 | |
while True: | |
for i in range(0,3): | |
if board[i][i] == "x": | |
c += 1 | |
if c == 3: | |
win_animation(True) | |
print('you won!\n\n\n\n') | |
exit() | |
if board[i][i] == "o": | |
b += 1 | |
if b == 3: | |
game_over = True | |
win_animation(True) | |
print('you lost!\n\n\n\n') | |
exit() | |
if board[i][0] == "x" and board[i][1] == "x" and board[i][2] == "x": | |
win_animation(True) | |
game_over = True | |
print('you won!\n\n\n\n') | |
elif board[i][0] == "o" and board[i][1] == "o" and board[i][2] == "o": | |
game_over = True | |
win_animation(False) | |
print('you lost!\n\n\n\n') | |
exit() | |
elif board[0][i] == "x" and board[1][i] == "x" and board[2][i] == "x": | |
game_over = True | |
win_animation(True) | |
print('you won!\n\n\n\n') | |
exit() | |
elif board[0][i] == "o" and board[1][i] == "o" and board[2][i] == "o": | |
game_over = True | |
win_animation(False) | |
print('you lost!\n\n\n\n') | |
exit() | |
if board[2][0] == "x" and board[1][1] == "x" and board[0][2] == "x": | |
game_over = True | |
win_animation(True) | |
print('you won!\n\n\n\n') | |
exit() | |
if board[2][0] == "o" and board[1][1] == "o" and board[0][2] == "o": | |
game_over = True | |
win_animation(False) | |
print('you lost!\n\n\n\n') | |
exit() | |
available = 9 | |
if not game_over: | |
for i in board: | |
for square in i: | |
if square not in "-": | |
available -=1 | |
if available == 0: | |
print("you tied !\n\n\n\n\n") | |
exit() | |
return | |
#silly little animation | |
def win_animation(boolian): | |
if boolian: | |
for i in range(0,3): | |
board[i][0] = 'x' | |
time.sleep(0.1) | |
board[i][1] = 'x' | |
time.sleep(0.1) | |
board[i][2] = 'x' | |
print_board() | |
else: | |
for i in range(0,3): | |
board[i][0] = 'o' | |
time.sleep(0.1) | |
board[i][1] = 'o' | |
time.sleep(0.1) | |
board[i][2] = 'o' | |
print_board() | |
time.sleep(0.5) | |
while True: | |
winstates() | |
draw_player(take_input()) | |
time.sleep(0.7) | |
winstates() | |
draw_AI() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment