Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python: Help writing the winner module for tictactoe code. Need help adding a checkWinner ( p , board ) function where p is the parameter

Python: Help writing the winner module for tictactoe code.
Need help adding a checkWinner(p,board) function where p is the parameter for player whose move it is, that is p is parameter for the argument player (either "X" or "O") and board is the 2 dimensional list (array) that is the checkerboard. The function should return a Boolean that is True if the player won and False otherwise.
My code:
def main():
# initializes board and alternates getting player's moves
# and checking for winner after 5 moves and if 9 moves cat's game
gameOver = False
player="X"
moveCounter=0
board =[["1","2","3"],["4","5","6"],["7","8","9"]]
while not (gameOver):
#get player move
printBoard(board)
moveCounter=moveCounter+1
if moveCounter >9:
print("cat's game")
break
getMove(player,board)
if moveCounter >=5:
gameOver=checkWinner(player,board)
if player=="X":
player="O"
else:
player="X"
def printBoard(b):
print ("||")
print (""+b[0][0]+""+"|"+b[0][1]+"|"+b[0][2])
print ("---|---|---")
print (""+b[1][0]+""+"|"+b[1][1]+"|"+b[1][2])
print ("---|---|---")
print (""+b[2][0]+""+"|"+b[2][1]+"|"+b[2][2])
print ("||")
def getMove(p,b):
validMove = False
while not validMove:
move = input("player "+p+" enter valid untaken square (1-9) on board shown above: ")
if (move >="1" and move <="9") and len(move)==1:
row =(int(move)-1)//3
col =(int(move)-1)%3
if b[row][col]=="X" or b[row][col]=="O":
print("square on board taken try again")
else:
validMove=True
b[row][col]= p
else:
print("not valid square on board try again")
def checkWinner(p,b):
# check rows, cols and diagonals
print("check winner player",p) #this print() statement is a "program stub"
#to be removed when you add checkWinner logic
main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advanced Accounting

Authors: Joe Hoyle, Thomas Schaefer, Timothy Doupnik

10th edition

0-07-794127-6, 978-0-07-79412, 978-0077431808

Students also viewed these Databases questions