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 to Expert-Tailored 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

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions

Question

What would you do about the verbal homophobic insults?

Answered: 1 week ago

Question

=+5. What do you want them to think?

Answered: 1 week ago

Question

=+What the product does for the end-user.)

Answered: 1 week ago