Fill This Form To Receive Instant Help
Homework answers / question archive / CS119 Python Programming PJ 10 – Tic-Tac-Toe Game Check Please write a Python program to check each tic-tac-toe game and show all its winning result in detail
CS119 Python Programming
PJ 10 – Tic-Tac-Toe Game Check
Please write a Python program to check each tic-tac-toe game and show all its winning result in detail. This is an application program of a 3-dimensional list or array.
Your complete test run output of 9 games must look exactly as follows including the data. These nice tests really check to make sure your program is performing perfectly to check every possible winning situation for both X and O in each game.
Welcome to the TicTacToe Game Check of "Dr. Simon Lin"! ç must use your name!
1=====================================.
GAME 1 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
2=====================================.
GAME 2 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
3======================================.
GAME 3 is as follows:
XOX
XXO
XOO
X won by column 1
X won by diagonal 2
4======================================.
GAME 4 is as follows:
XOO
OXO
XXO
O won by column 3
5====================================.
GAME 5 is as follows:
XOX
OXO
XOO
X won by diagonal 2
6====================================.
GAME 6 is as follows:
OXO
XOX
XOX
It is a tie.
7====================================.
Please enter your game board (* to exit)>OOOXOXOXO
GAME 7 is as follows:
OOO
XOX
OXO
O won by row 1
O won by diagonal 1
O won by diagonal 2
8=====================================.
Please enter your game board (* to exit)>OXOOXOOXO
GAME 8 is as follows:
OXO
OXO
OXO
O won by column 1
X won by column 2
O won by column 3
9====================================.
Please enter your game board (* to exit)>XOXXXXOXO
GAME 9 is as follows:
XOX
XXX
OXO
X won by row 2
10====================================.
Please enter your game board (* to exit)>*
11====================================.
Thank you for playing the TicTacToe Game Check of "Dr. Simon Lin"!ç must use your name!
12 ====================================.
Your main program must have the following code to set up the preset 6 games.
# MAIN PROGRAM: ================================================.
print ("Welcome to the TicTacToe Game Check of \"Dr. Simon Lin\"!")
O = 'O' # player O
X = 'X' # player X
tlist = [ [ [O,O,O], # Game 1 # tlist [ 0 ] to get this game
[O,O,O], # tlist is like a 3-dimensional array or list
[O,O,O] ] ,
[ [X,X,X], # Game 2 # tlist [ 1 ] to get this game
[X,X,X],
[X,X,X] ] ,
[ [X,O,X], # Game 3 # tlist [ 2 ] to get this game
[X,X,O],
[X,O,O] ] ,
[ [X,O,O], # Game 4 # tlist [ 3 ] to get this game
[O,X,O],
[X,X,O] ] ,
[ [X,O,X], # Game 5 # tlist [ 4 ] to get this game
[O,X,O],
[X,O,O] ] ,
[ [O,X,O], # Game 6 # It is a tie. # tlist [ 5 ] to get this game
[X,O,X],
[X,O,X] ] ]
for i in range(1, 6+1): # 6 games to check one by one: i = 1 2 3 4 5 6
print("GAME", i ," is as follows:")
show( tlist[i-1] ) # to show 3x3 game board: index = 0 1 2 3 4 5 in tlist[ index ]
checkwin( tlist[i-1] ) # to check all 8 winning situations for both X and O players
# 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 ########################################
Program Specifications, Considerations, and Regulations:
(1) You must define show( ) function to show or print the 3X3 game board.
(2) You must define checkwin( ) function to check all 8 winning situations for both X and O players.
(3) You must define the following 8 functions to check 8 possibilities to win. Checkwin( ) function will call all 8 functions to check all 8 winning situations for both X and O players.
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) # True | False # end of winrow1( )
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) # end of windia2( )
As you can see from the above code, the actual code for function winrow1( ) and function windia2( ) has been provided. Please use the same technique to complete other 6 functions.
(4) 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. In fact, the user should enter only 3 games here.
(5) 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.
(6) You must have the following code in your program because you must let Python know that G is a 3x3 array. This is to prepare to copy a string of 9 characters (called S) into a 3x3 game board (called G).
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)>") # To get 9-character string into S
(7) 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 that the order is row by row, instead of column by column.
(8) How to detect that a game is a tie?
One easy way is to keep counting the winning situations for X and O in checkwin( ) function. At the end of checking, if the count is 0, the game is a tie because there is no winning situation at all.
=========================================================================.
How to submit your Project Assignment (PJ)?
(1) Each program must be well documented with block comments and proper line comments.
The beginning of each program must have a block comment to show its author, date, and purpose.
The following is an example of block comments:
# Author: _______________ ç must use your full name here!
# Date: _________ ç must put today’s date here!
# Purpose: _______________ ç must show the purpose of this program here!
(2) You must submit to Canvas the following two items as attachments:
(a) Your source program (for example, CS119-PJ10.py ), and
(b) Your WORD document (for example, CS119-PJ10-report .docx ) containing the listing of
your Python program, and the complete output of your test run as specified above.
You must not submit a zip file to Canvas.
=========================================================================.
//You must delete everything above & including this line to make this your Word document to be submitted.
PJ 10 Report My Name: ______________
A. The following is my Python source program:
// Please copy your source program into here from your Visual Studio IDE.
// Your code here must be in color. You must not show screen prints here.
B. The following is the console output of my 9 games:
// One way to copy the console output is to press Ctrl+Alt+PrtScn.
// Another way to copy is to use the snipping tool. To paste the image is to press Ctrl+v.
// The console display must not be too wide, otherwise it will be too hard to read once pasted.
// Please make sure your console is long enough to show all your output lines to be captured.
// Please copy your console output and paste into here. You complete output may have many parts.
Part 1:
Part 2:
Part 3:
Already member? Sign In