Please write a Python program to check a tic-tac-toe game and show its winning result in detail. This is an application program of a 3-dimensional list or array.
Your complete test run output must look as follows. This test really checks to make sure your program is performing perfectly to check every possible winning situation for X and O.
GAME 0 is as follows:
OOO
OOO
OOO
O won by row 1
O won by row 2
O won by row 3
O won by column 1
O won by column 2
O won by column 3
O won by diagonal 1
O won by diagonal 2
GAME 1 is as follows:
XXX
XXX
XXX
X won by row 1
X won by row 2
X won by row 3
X won by column 1
X won by column 2
X won by column 3
X won by diagonal 1
X won by diagonal 2
GAME 2 is as follows:
XOX
XXO
XOO
X won by column 1
X won by diagonal 2
GAME 3 is as follows:
XOO
OXO
XXO
O won by column 3
GAME 4 is as follows:
XOX
OXO
XOO
X won by diagonal 2
GAME 5 is as follows:
OXO
XOX
XOX
It is a tie.
Please enter your game board (* to exit)>OOOXOXOXO
Your game board is as follows:
OOO
XOX
OXO
O won by row 1
O won by diagonal 1
O won by diagonal 2
Please enter your game board (* to exit)>OXOOXOOXO
Your game board is as follows:
OXO
OXO
OXO
O won by column 1
X won by column 2
O won by column 3
Please enter your game board (* to exit)>XOXXXXOXO
Your game board is as follows:
XOX
XXX
OXO
X won by row 2
Please enter your game board (* to exit)>*
Thank you for playing this game designed by "you"
========================================================================.
Your main program must have the following code to set up the preset 6 games.
# MAIN PROGRAM: ================================================.
O = 'O' # player O
X = 'X' # player X
tlist = [ [ [O,O,O], # Game 0 # tlist is like a 3-dimensional array.
[O,O,O],
[O,O,O] ] ,
[ [X,X,X], # Game 1
[X,X,X],
[X,X,X] ] ,
[ [X,O,X], # Game 2
[X,X,O],
[X,O,O] ] ,
[ [X,O,O], # Game 3
[O,X,O],
[X,X,O] ] ,
[ [X,O,X], # Game 4
[O,X,O],
[X,O,O] ] ,
[ [O,X,O], # Game 5 # It is a tie.
[X,O,X],
[X,O,X] ] ]
for i in range(6): # 6 games to check one by one
print("GAME", i ," is as follows:")
show( tlist[i] )
checkwin( tlist[i] )
# More code to keep getting input from the user for the next game
# Stop this program if user’s input is ‘*’
# Thank the user before exit
# End of Program ########################################
You must define show( ) function to show the 3X3 game board.
You must define checkwin( ) function to check all the winning situations for X and O.
You must define the following 8 functions to check 8 possibilities to win:
def winrow1( t, p ): # show whether row1 wins for player p: 'X' or 'O'
r = 0 # for board row 1 # p can be ‘X’ or ‘O’ , t is the 3x3 game board
return (t[r][0]==t[r][1] and t[r][1]==t[r][2] and t[r][2]==p)
def winrow2( t, p ): # show whether row1 wins for player p: 'X' or 'O'
def winrow3( t, p ): # show whether row1 wins for player p: 'X' or 'O'
def wincol1( t, p ): # show whether col1 wins for player p: 'X' or 'O'
def wincol2( t, p ): # show whether col2 wins for player p: 'X' or 'O'
def wincol3( t, p ): # show whether col3 wins for player p: 'X' or 'O'
def windia1( t, p ): # show whether diagonal1 wins for player p: 'X' or 'O'
def windia2( t, p ): # show whether diagonal2 wins for player p: 'X' or 'O'
return (t[0][2]==t[1][1] and t[1][1]==t[2][0] and t[2][0]==p)
As you can see from the above code, the code for function winrow1( ) and function windia2( ) has been provided. Please use the same technique to complete all other functions.
After your program checks the preset 6 games and shows the output, your program must allow the user to enter a string of 9 characters for the next game to check. Your program must stop when the users enter ‘*’ as the string.
Your program should have a nested loop to copy the 9-character string S into a 3x3 game board G, show the game board to the user, and then check who’s winning for this game for all situations.
You must have the following code in your program because you must let Python know that G is a 3x3 array.
G = [ [X,X,X], [X,X,X], [X,X,X] ] # G is a game board of 3x3
S = input("Please enter your game board (* to exit)>")
How to copy the 9-character string S into a 3x3 game board G? Basically, S[0] goes to G[0][0], S[1] goes to G[0][1], … , and lastly S[8] goes to G[2][2]. You must use a nested for loops with row = 0, 1, 2 and column = 0, 1, 2 to copy string S into G array using row-major ordering. Row-major ordering means the order is row by row, instead of column by column.
Comment if there are any issues, queries or if you require any code modifications to be done.
Code in text form, as well as screenshots, have been put below.
Code:
def show(board): #function to print board
for row in board:
for col in row:
print(col, end="")
#print newline
print()
def checkwin(board):
win=0
for p in ['X', 'O']:
#ROWS
if winrow1(board, p):
print(p, "won by row 1")
win=1
if winrow2(board, p):
print(p, "won by row 2")
win=1
if winrow3(board, p):
print(p, "won by row 3")
win=1
#COLS
if wincol1(board, p):
print(p, "won bY column 1")
win=1
if wincol2(board, p):
print(p, "won bY column 2")
win=1
if wincol3(board, p):
print(p, "won bY column 3")
win=1
#DIAGS
if windia2(board, p):
print(p,"won by diagonal 1")
win=1
if windia1(board, p):
print(p,"won by diagonal 2")
win=1
#TIE
if win == 0:
print("Its a tie")
def winrow1( t, p ): # show whether row1 wins for player p: 'X' or 'O'
r = 0 # for board row 1 # p can be ‘X’ or ‘O’ , t is the 3x3 game board
return (t[r][0]==t[r][1] and t[r][1]==t[r][2] and t[r][2]==p)
def winrow2( t, p ): # show whether row1 wins for player p: 'X' or 'O'
r = 1 # for board row 2
return (t[r][0]==t[r][1] and t[r][1]==t[r][2] and t[r][2]==p)
def winrow3( t, p ): # show whether row1 wins for player p: 'X' or 'O'
r = 2 # for board row 3
return (t[r][0]==t[r][1] and t[r][1]==t[r][2] and t[r][2]==p)
def wincol1( t, p ): # show whether col1 wins for player p: 'X' or 'O'
c = 0 # for board col 1
return (t[0][c]==t[1][c] and t[1][c]==t[2][c] and t[2][c]==p)
def wincol2( t, p ): # show whether col2 wins for player p: 'X' or 'O'
c = 1 # for board col 2
return (t[0][c]==t[1][c] and t[1][c]==t[2][c] and t[2][c]==p)
def wincol3( t, p ): # show whether col3 wins for player p: 'X' or 'O'
c = 2 # for board col 3
return (t[0][c]==t[1][c] and t[1][c]==t[2][c] and t[2][c]==p)
def windia1( t, p ): # show whether diagonal1 wins for player p: 'X' or 'O'
return (t[0][2]==t[1][1] and t[1][1]==t[2][0] and t[2][0]==p)
def windia2( t, p ): # show whether diagonal2 wins for player p: 'X' or 'O'
return (t[0][0]==t[1][1] and t[1][1]==t[2][2] and t[2][2]==p)
# MAIN PROGRAM: ================================================.
O = 'O' # player O
X = 'X' # player X
tlist = [ [ [O,O,O], # Game 0 # tlist is like a 3-dimensional array.
[O,O,O],
[O,O,O] ] ,
[ [X,X,X], # Game 1
[X,X,X],
[X,X,X] ] ,
[ [X,O,X], # Game 2
[X,X,O],
[X,O,O] ] ,
[ [X,O,O], # Game 3
[O,X,O],
[X,X,O] ] ,
[ [X,O,X], # Game 4
[O,X,O],
[X,O,O] ] ,
[ [O,X,O], # Game 5 # It is a tie.
[X,O,X],
[X,O,X] ] ]
for i in range(6): # 6 games to check one by one
print("GAME", i ," is as follows:")
show( tlist[i] )
checkwin( tlist[i] )
G = [ [X,X,X], [X,X,X], [X,X,X] ] # G is a game board of 3x3
while True:
S = input("Please enter your game board (* to exit)>")
# Stop this program if user’s input is ‘*’
if S=='*':
break
#copy string into board
for i in range(3):
for j in range(3):
#validate
if i * 3 + j < len(S):
G[i][j] = S[i * 3 + j]
#show the board
print("Your game board is as follows:")
show(G)
checkwin(G)
print("Thank you!")
# End of Program ########################################
Code Screenshots:




Output:


Please write a Python program to check a tic-tac-toe game and show its winning result in...
Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user to enter...
18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Write . Displays the contents of the board array. . Allows player 1 to select a location on the board for an X. The program should ask the...
Objective: To write a program to allow a game of Tic Tac Toe to be played, and to determine when the game is over Complete the TicTacToe program below. It will allow for a full game of Tic Tac Toe between two players, and it will tell you who won or if the game is over with no winner. Details: The TicTacToe board is stored in a 3x3 multidimensional list of characters containing spaces at the start of the game...
Tic-Tac-Toe (arrays and multidimensional arrays) C++ Write an interactive program that plays tic-tac-toe. Represent the board as a 3 X 3 character array. Initialize the array to blanks and ask each player in turn to input a position. The first player's position is marked on the board with a O and the second player's position is marked with an X. Continue the process until a player wins or the game is a draw. To win, a player must have 3...
You will create a PYTHON program for Tic-Tac-Toe game. Tic-Tac-Toe is normally played with two people. One player is X and the other player is O. Players take turns placing their X or O. If a player gets three of his/her marks on the board in a row, column, or diagonal, he/she wins. When the board fills up with neither player winning, the game ends in a draw 1) Player 1 VS Player 2: Two players are playing the game...
Write a program to Simulate a game of tic tac toe. A game of tic tac toe has two players. A Player class is required to store /represent information about each player. The UML diagram is given below.Player-name: string-symbol :charPlayer (name:string,symbol:char)getName():stringgetSymbol():chargetInfo():string The tic tac toe board will be represented by a two dimensional array of size 3 by 3 characters. At the start of the game each cell is empty (must be set to the underscore character ‘_’). Program flow:1) ...
I need screenshots for this solution done in Flowgorithm. Thank you. Tic-Tac-Toe Game Design a program that allows two players to play a game of tic-tac-toe. Use a two- dimensional String array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: a. Displays the contents of the board array. b. Allows player 1 to select a location...
Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game. The objective of this project is to demonstrate your understanding of various programming concepts including Object Oriented Programming (OOP) and design. Tic Tac Toe is a two player game. In your implementation one opponent will be a human player and the other a computer player. ? The game is played on a 3 x 3 game board. ? The first player is known as...
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...
Java project In a game of tic-tac-toe, two players take turns marking an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Create...