Complete this code using python language #Write the conditions to check if anyone has won the game. This function will be called from the placeCharacter function. # r : In which row the current character was placed # c : In which column the current character was placed #returned value: True if anyone has won;Otherwise Flase def checkBoard(r,c): #Write your code here #Write the necessary code to put the "char" in proper position of the board and check if anyone has won. #pos : The position that has been given by the player as input. #char : The character representing the player. It can be X or O.
Control structures
Control structures are block of statements that analyze the value of variables and determine the flow of execution based on those values. When a program is running, the CPU executes the code line by line. After sometime, the program reaches the point where it has to make a decision on whether it has to go to another part of the code or repeat execution of certain part of the code. These results affect the flow of the program's code and these are called control structures.
Switch Statement
The switch statement is a key feature that is used by the programmers a lot in the world of programming and coding, as well as in information technology in general. The switch statement is a selection control mechanism that allows the variable value to change the order of the individual statements in the software execution via search.
Complete this code using python language
#Write the conditions to check if anyone has won the game. This function will be called from the placeCharacter function.
# r : In which row the current character was placed
# c : In which column the current character was placed
#returned value: True if anyone has won;Otherwise Flase
def checkBoard(r,c):
#Write your code here
#Write the necessary code to put the "char" in proper position of the board and check if anyone has won.
#pos : The position that has been given by the player as input.
#char : The character representing the player. It can be X or O.
#count: It represents the number of turns. It can be from 0 to 8.
#returned value: True if anyone has won;Otherwise Flase
def placeCharacter(pos,char,count):
#Write your code here
def gameInitialization():
global player1,player2
player1 = input("Enter player 1's name:")
player2 = input("Enter player 2's name:")
print(f"{player1}, your character is X")
print(f"{player2}, your character is O")
def runGame():
counter = 0
f=False
p_name=None
while counter<9:
printBoard()
if counter%2==0:
if (placeCharacter(input(f"{player1}, where do you want to place 'X':"),'X',counter)):
p_name = player1
f = True
break
else:
if (placeCharacter(input(f"{player2}, where do you want to place 'O':"),'O',counter)):
p_name = player2
f = True
break
counter+=1
printBoard()
if f == False:
print("The game ends in a draw.")
else:
print(f"{p_name} has won the game!!!!")
board = [[1,2,3],[4,5,6],[7,8,9]]
player1 = player2 = None #Take input of player names in these 2 variables
gameInitialization()
runGame()
Step by step
Solved in 2 steps