import os,random
theBoard = {'top-L': ' ', 'top-M': ' ', 'top-R': ' ',
'mid-L': ' ', 'mid-M': ' ', 'mid-R': ' ',
'low-L': ' ', 'low-M': ' ', 'low-R': ' '}
def printBoard(board):
print(f"""
{board['top-L']} | {board['top-M']} | {board['top-R']}
-----------
{board['mid-L']} | {board['mid-M']} | {board['mid-R']}
-----------
{board['low-L']} | {board['low-M']} | {board['low-R']}
""")
def computerMove():
while True:
c_move = random.choice(list(theBoard.keys()))
if theBoard[c_move] != ' ': continue
else:
theBoard[c_move] = 'O'
break
def userMove():
while True:
u_move = input('you are X.. enter your move! (eg: mid-M): ')
if u_move not in theBoard.keys(): print("invalid input!"); continue
if theBoard[u_move] != ' ':
print('Position already filled!')
continue
else:
theBoard[u_move] = 'X'
break
def checkResult(board):
global userWin, computerWin, noSpace
userWin, computerWin, noSpace = False, False, False
arr = [list(board.values())[i:i+3] for i in range(0,9,3)]
if ' ' not in list(board.values()): noSpace = True
#check Rows
for i in arr:
if i[0] == i[1] == i[2] != ' ':
if i[0] == 'X': userWin = True
else: computerWin = True
#check columns
for i in range(0,3):
if arr[0][i] == arr[1][i] == arr[2][i] != ' ':
if arr[0][i] == 'X': userWin = True
else: computerWin = True
#check diagonals
if (arr[0][0] == arr[1][1] == arr[2][2]) or (arr[0][2] == arr[1][1] == arr[2][0]):
if arr[1][1] == 'X': userWin = True
else: computerWin = True
def resetBoard():
for i in theBoard.keys():
theBoard[i] = ' '
os.system('clear')
printBoard(theBoard)
print("instructions:")
print(f"""
top-L | top-M' | top-R
------------------------
mid-L | mid-M | mid-R
------------------------
low-L | low-M | low-R
""")
while True:
userMove()
computerMove()
checkResult(theBoard)
os.system('clear')
printBoard(theBoard)
if userWin:
print('You won!')
elif computerWin:
print('lol noob')
elif noSpace:
print('you used up all the damn space you troglodytes')
if noSpace or computerWin or userWin:
print('again? (y/n): ')
again = input()
if again == 'y':
resetBoard()
else: break
So after some testing, it seems to do oopsies and i can’t tell why for the life of me.
I really appreciate any help
edit: I have
tried changing if ‘ ‘ not in board.values(): noSpace = True
to a different method involving nested for loops, noSpace doesn’t seem to have an issue. atleast not in this line.
tried changing #check rows, #check columns and diagonal, doesn’t seem to have an issue there.
Although the problem really seems like it is userWin, computerWin or noSpace retaining it’s True value over iterations. My brain is short circuiting on me and i can’t tell where the issue lies for the life of me <3
tried changing if ‘ ‘ not in board.values(): noSpace = True
to a different method involving nested for loops, noSpace doesn’t seem to have an issue. atleast not in this line.
tried changing #check rows, #check columns and diagonal, doesn’t seem to have an issue there.
Although the problem really seems like it is userWin, computerWin or noSpace retaining it’s True value over iterations. My brain is short circuiting on me and i can’t tell where the issue lies for the life of me <3
Ram Jenab Zadeh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.