Question
ALL IN PYTHON. PLEASE HELP FULFILL ALL ASPECTS ASKED. Tic-tac-toe game Try/except will be nice, to process bad inputs gracefully, but not mandatory. Create a
ALL IN PYTHON. PLEASE HELP FULFILL ALL ASPECTS ASKED.
Tic-tac-toe game
Try/except will be nice, to process bad inputs gracefully, but not mandatory.
Create a tic-tac-toe game.
No graphics, so all input is typed into the console and all displays and prompts for input are printed to the console.
What classes are required?
-
A Player class with name, win/draw/loss data. Input name in constructor, set win/draw/loss to zero. Function to calculate and return win percentage (= 100*win/(win+draw+loss)), return 0 if total number of games is zero. Round the percentage to 2 decimal places. Function to reset win/draw/loss all to zero.
-
A TicTacToe class? What will it contain? A board? What else?
A leaderboard to show the scores of the top 10 players might be nice (sort by win percentage). If there are fewer than 10 players just show however many pkayers there are. The leaderboard may be a separate class or a function in the TicTacToe class, up to you.
Store the board and list of players as data members in the TicTacToe class? You decide. It might be helpful to write an outer loop and display a menu at the start of each pass.
Prompt for input and perform suitable actions depending on user input. Menu please select from one of the following (for example):
-
Exit program
-
Play new game
-
Change size of board n n (minimum 3, maximum 6). This is just to avoid ridicu- lously large boards, but I will test your code with larger board sizes.
-
Add new player (hence we require a list of players, do not allow two players to have same name)
-
Display leaderboard
-
Print rules & instructions to play your implementation of the game
-
Reset all player scores to zero
-
What else?
Schematic nested loops.
outer while loop print menu
prompt for input do something, depending on user selection inner while loop # if user selects "play new game"
play game
3
-
A schematic plan to play a new game is as follows.
-
Prompt for names of players (two players). Error message if list of players has fewer
than two items. Player 1 and Player 2 cannot be the same player.
-
Create a board (default size = 3 if value not input in menu prompt).
-
Print/display the board (all squares blank).
-
Initialize current player to Player 1.
-
Begin while loop to play game.
-
(a) Prompt current player to move: input the coordinates as a 1 or b 3 etc.
-
(b) The character indexes the column and the number indexes the row.
For example, for a 4 4 board the squares are indexed as follows. (I print dots to make the blank squares easier to see). 4.... 3....
2.... 1....
abcd
This is just symbolic, to demonstrate the indexing.
You can draw borders and stuff when you print the board in your implementation.
-
(c) Check if coordinates are out of bounds, if so print error message and prompt again.
-
(d) Check if square is already occupied, if so print error message and prompt again.
-
(e) If move is valid, update the data in the board.
-
(f) Check for win (does the move win the game?), if so then print congratulations to
winner, update player statistics (of both players), print the board and break out of
the loop.
-
(g) Check for draw (are all the squares occupied?), if so then print congratulations to
both players, update player statistics (of both players), print the board and break
out of the loop.
-
(h) If no win and no draw, display the updated board.
-
(i) Toggle the current player to the other player and go to next pass of loop.
-
-
-
To display moves, print X for Player 1 and print O for Player 2.
-
Definition of win: All values in a row or a column or a diagonal are equal (all X or all O). There are n rows and n columns, but only two diagonals (board size is n n).
-
Definition of draw: All squares are occupied and there is no winner.
-
Anything else?
4
Comments: Tic-tac-toe game
1. I said the input coordinates are a character and a number, for example a 1 or b 3 etc.
-
(a) You dont have to do the above, if you prefer to ask the user to input two numbers.
-
(b) If you employ the above (character, number) plan, how would you determine the column index if the input is a string?
-
(c) Here is a possible plan.
-
(d) Let us limit the board size to 3 n 6 (but see below).
-
(e) Define a tuple cols = (a,b,c,d,e,f).
-
(f) The above is to avoid ridiculously large boards, but I will test your code with larger board sizes. I will add extra items to the cols tuple (or however you implement your game), and see how easy it is to increase the board size in your implementation, to a value n > 6.
-
(g) Then after parsing the user input to obtain a string and number, convert the string to lower case and if it equals col[i], say that the column index is i.
cols = (a,b,c,d,e,f) #...
print("please input ...") # ... input1 = ... input2 = ...
if len(input1) != 1: print("bad input") ...
input1 = input1.lower() if input1 in cols:
# prompt for user input
# string # number
# string must be one character only # print suitable error message # prompt user again, to enter valid input
column_index = cols.index(input1) else:
column_index = -1 # bad value # use column_index in code, test for out of bounds, etc
(h) You can implement try, except to avoid a program crash in case of bad input, but there is no grade penalty if you dont.
9
2. A student came up with a good idea about checking for a draw. (a) A draw only happens when the board is full (all squares occupied).
(b) For an n n board, there are n2 squares to be occupied. 2
(c) Hence the maximum number of valid moves is n . (d) Hence instead of executing a while loop as I stated previously, we can instead execute
a for loop with a range.
-
(e) In the for loop, after each move we test for a win.
-
(f) We do not have to test for a draw inside the loop.
-
(g) If the for loop terminates without a win, then we know the board is full and the game is a draw, hence print a message to say so (and update the player statistics).
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started