Jul-11-2018, 06:00 AM
Sup the boys,
I would like to have rounds so that like if the player or AI gets to like maybe a score of like 5 or something it prints "you have won the game!".I tried a little bit by making a variable or something with the score and adding to it everytime a player wins however i also dont know how to get the board to reset and go to the next round... Can anyone tell me how i could do this please?
Thanks.
(code..:)
I would like to have rounds so that like if the player or AI gets to like maybe a score of like 5 or something it prints "you have won the game!".I tried a little bit by making a variable or something with the score and adding to it everytime a player wins however i also dont know how to get the board to reset and go to the next round... Can anyone tell me how i could do this please?
Thanks.
(code..:)
import random
def printGameBoard (boardValues): #prints out the game board layout
print(boardValues[0], boardValues[1], boardValues[2])
print(boardValues[3], boardValues[4], boardValues[5])
print(boardValues[6], boardValues[7], boardValues[8])
print()
def chooseMove (boardValues): ###This is the players choice of where to go. (the player starts first always in this game.)
validMove = False
while (not validMove):
printGameBoard(boardValues)
try:
squareChoice = int(input("Choose a square!")) -1
except ValueError:
print("\nAnswer is not a number! please enter a valid number!")
continue
if boardValues[squareChoice] != "X" and boardValues[squareChoice] != "O": ## checks to see that the chosen square is not already occupied.
boardValues[squareChoice] = "X"
validMove = True
elif boardValues[squareChoice] != range(0, 9):
print("\nAnswer is not a number! please enter a valid number!")
continue
else:
print("Please choose a free square!")
return checkForWin(boardValues)
def chooseRandom(boardValues): ##This is the AI's/ Computers turn function. (Quite similar to the players)
validMove = False
while (not validMove):
squareChoice = random.randint(0, 8)# chooses random integer between 0-8 on the board, this is done through import random at the top.
if boardValues[squareChoice] != "X" and boardValues[squareChoice] != "O":
boardValues[squareChoice] = "O"
validMove = True
return checkForWin(boardValues)
def checkHorizontal(boardValues): ##Checks the Horizontal axis/squares of the board to see if they form a row.
foundWin = False
if (boardValues [0] == boardValues [1] == boardValues [2]):
foundWin = True
elif (boardValues [3] == boardValues [4] == boardValues [5]):
foundWin = True
elif (boardValues [6] == boardValues [7] == boardValues [8]):
foundWin = True
return foundWin
def checkVerticle(boardValues):##Checks the Verticle axis/squares of the board to see if they form a row.
foundWin = False
if (boardValues [0] == boardValues [3] == boardValues [6]):
foundWin = True
elif (boardValues [1] == boardValues [4] == boardValues [7]):
foundWin = True
elif (boardValues [2] == boardValues [5] == boardValues [8]):
foundWin = True
return foundWin
def checkDiagnol(boardValues):##checks the Diagnol axis/squares of the board to see if they form a row.
foundWin = False
if (boardValues [0] == boardValues [4] == boardValues [8]):
foundWin = True
elif (boardValues [6] == boardValues [4] == boardValues [2]):
foundWin = True
return foundWin
def checkForWin(boardValues):##Checks to see if any of the horizontal,verticle or diagnol functions are now true, and if any are, gamewon will be returned.
gameWon = False
if (checkHorizontal(boardValues) == True):
gameWon = True
elif (checkVerticle(boardValues) == True):
gameWon = True
elif (checkDiagnol(boardValues) == True):
gameWon = True
return gameWon
boardList = [ "1", "2", "3", "4", "5", "6", "7", "8", "9" ] #initialises the list
won = False
turn = 0
playerScore = 0
AIscore = 0
while (not won and turn < 9):
if turn % 2 == 0:
won = chooseMove(boardList)
else:
won = chooseRandom(boardList)
turn = turn + 1
printGameBoard(boardList)
turn = turn -1
if (not won):
print ("The Game Ends in a Tie")
elif (turn % 2 == 0):
playerScore = playerScore + 1
print ("Player Has Won!")
else :
AIscore = AIscore + 1
print ("The Computer Has Won.")
if playerScore >= 3:
print("You won the whole game fam!")
