I am trying to make a tic tac toe game and I am very new to python so I was wondering why my board was not getting reset despite my functions explicitly resetting the board.
I tried getting rid of the function and just implementing a reset where each value was replaced with an underscore like the original Board list but I want to make this more efficient.
CODE
import random
score=0
AIscore=0
PlayerSym="X"
AIsymbol="O"
Win=False
ListTF=[True,False]
Board=[["_","_","_"],
["_","_","_"],
["_","_","_"]]
def resetGame():
Board=[["_","_","_"],
["_","_","_"],
["_","_","_"]]
print("resetting board ///////////////////////////////////////////")
def initiateGame(Win,AIsymbol):
print("Welcome to tic tac toe")
print("If you score 3 points you win")
print("pick a row and column to place your symbol:")
def Symbols():
TF=random.choice(ListTF)
if Win==False:
PlayerSym="X"
print("player, you are 'X' so you go first")
AIsymbol="O"
else:
if TF==True:
PlayerSym="X"
AIsymbol="O"
print("player, you are 'X' so you go first")
if TF==False:
PlayerSym="O"
AIsymbol="X"
print("player, you are 'O' so you go second")
def GameWinner(score,Board,AIscore):
for row in range(0,3): #checks all columns for a win
if(Board[row][0]==Board[row][1] == Board[row][2] == PlayerSym):
Win=True
print("congrats, you have earned a point")
print("the score is:")
score=score+1
print("Player:",score)
print("AI:",AIscore)
resetGame()
elif(Board[row][0]==Board[row][1] == Board[row][2] == AIsymbol):
Win=False
print("The AI has earned a point")
print("the score is:")
print("Player:",score)
AIscore=AIscore+1
print("AI:",AIscore)
resetGame()
if(Board[0][0]==Board[1][1] == Board[2][2] == PlayerSym):
Win=True
print("congrats, you have earned a point")
print("the score is:")
score=score+1
print("Player:",score)
print("AI:",AIscore)
resetGame()
elif(Board[0][0]==Board[1][1] == Board[2][2] == AIsymbol):
Win=False
print("The AI has earned a point")
print("the score is:")
print("Player:",score)
AIscore=AIscore+1
print("AI:",AIscore)
resetGame()
if(Board[0][2]==Board[2][0] == Board[1][1] == PlayerSym):
Win=True
print("congrats, you have earned a point")
print("the score is:")
score=score+1
print("Player:",score)
print("AI:",AIscore)
resetGame()
elif(Board[0][2]==Board[2][0] == Board[1][1] == AIsymbol):
Win=False
print("The AI has earned a point")
print("the score is:")
print("Player:",score)
AIscore=AIscore+1
print("AI:",AIscore)
resetGame()
def Space(Board,PlayerSym,AIsymbol):
print(Board[0])
print(Board[1])
print(Board[2])
row=int(input("pick a row from (0-2):"))
column=int(input("pick a column from (0-2)"))
while (row>2 or row<0) or (column>2 or column<0):
print("you entered a value that is not within the parameters, pick again")
row=int(input("pick a row from (0-2):"))
column=int(input("pick a column from (0-2)"))
while (Board[row][column]==PlayerSym or Board[row][column]==AIsymbol):
print("your row and column already has an 'X' or 'O', pick again")
row=int(input("pick a row from (0-2):"))
column=int(input("pick a column from (0-2)"))
Board[row][column]=PlayerSym
initiateGame(Win,AIsymbol)
def main():
Symbols()
GameWinner(score,Board,AIscore)
Space(Board,PlayerSym,AIsymbol)
while True:
main()
As @quamrana stated, your
resetGame()function is spawning a newBoard = [[ ... ]], but that's all it is doing here. You are not referencing the previously spawnedBoardat all, so this newBoardthat exists within theresetGame()scope and does not interact with the global version that you are trying to reset. A simple fix for this would be to return your new board, and assign that value whenever you'd like the board reset. E.g.:Alternatively, you can target the global variable directly with the
globalkeyword, which adds the variable in question to the method scope:Here's a good resource on how scope works in python to help you: https://www.w3schools.com/python/python_scope.asp
Cheers, best of luck!