Tic-tac-toe is a game for two players, X and O, who take turns marking the spaces in a 3 x 3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row is a winner. Write a python program to simulate playing this game. Draw the grid on your terminal screen and display your marks on the grid. You need to import the graphics package into your program, download and install the Xming program (from the Internet) to your computer to activate the graphics package.

One of the players starts playing the game who is randomly chosen between the two players. You can play the game for as many times as you wish, but for a final test, do it for 12 consecutive games. To start your program, follow the example on page 100 of the textbook. To simulate a game, you can use a random number generator (RNG), randrange ( ), importing from the random package, and to get the same sequence of random numbers for each time to execute the program, set the seed value to 1 in your main ( ) routine. The functions described below are just suggestions. You can design your program anyway you like but make it sure that it generates the correct results.

  • def main ( ): For each new play, to create a window on the terminal screen for a given size, say 400 x 400 pixels, it calls the function draw_board ( ) that draws a 3 x 3 grid on the screen. It prints the game number and calls the function init_play ( ) to choose the player who makes the first move. To simulate of playing a game, it calls the function play ( ), which returns the identity of the winner or returns None if the game is a draw and updates the number of wins for the winner. After each game, it pauses for the user input to continue to the next game, waiting for the user to enter a "yes" and closes the window for a fresh start. If the user prompt is a no, it quits playing the games and calls the function print_stat ( ) to print out the final winning scores.
  • def draw_board ( win ): In addition to drawing horizonal and vertical lines on the grid, this function also prints the corresponding index values from 0 to 8 in the 9 cells of the grid, where the index value of leftmost of the first row of the grid has assigned to 0 and the index value of rightmost of the last row of the grid has assigned to 8, and the index values increases from left to right and from bottom to top. The argument win refers to the object of the window drawn on the screen.
  • def init_play ( win ): It calls the RNG to choose the first and second player (like a tossing a coin), where the first player will start the game, and it returns the index values of the two players to the main ( ) routine.
  • def play ( win, players ): It simulates a single game by alternating between two players for each step. A game is over when a player ends up of marking three horizontal, vertical, or diagonal cells on the grid; or it's over when all 9 cells are marked on the grid. For each move, a cell number between 0 to 8 is chosen by the RNG. However, if the cell is chosen before, it calls the RNG to get another cell. The player with index value 0 uses the letter X and the player with index value 1 uses a circle, and places it in the center of the chosen cell. If a player wins a game, then the routine returns the identity of the player to the main ( ) routine, but if the game is a draw, then it returns the value None. After a player makes 3 or more moves in a game, it calls the function after_3_steps ( ) to determine if the player is a winner, and if its, it terminates the game.
  • def after_3_steps ( n, cell, player): It determines if player is a winner, where n is the number of moves made by the player and cell is the cell numbers for those n >= 3 moves. To figure out if combinations of 3 out of n cells results a winning: they are located on the same horizontal, vertical, or diagonal positions, it calls the function check_win ( ) for each possible combination: for n = 3, there is only 1 combination, for n = 4, there are 3 combinations, and for n =5, there are 6 combinations. If any one of the possible combinations shows a winning, then it prints the player as a winner and returns the identity of the player to the calling function; otherwise, it returns None.
  • def check_win ( f, s, t, cell ): It returns True if the cells in the argument cell with the index values f, s, and t are in the same horizontal, vertical, or diagonal positions; otherwise, it returns False. For each cell, it computes horizontal and vertical ( x, y ) coordinates on the 3 x 3 grid. If those three cells are on the same row, their x values must be the same, if they are on the same column, their y values must be the same, and if they are on the same diagonal, the absolute value of the difference of their x values must be equal to the absolute value of the difference of their y values.
  • def print_stat ( no_wins, no_games ): It prints out the number and percentage of wins for each player and the number of draws, where no_wins have the winning scores of each player and no_games are the total number of games in the simulation, which are passed by the main ( ) routine.

Name your program as prog4.py and to test it, execute: Make N=4. When the execution is over, the output file prog4.out will contain the output of your program as well as any error messages that might be generated by your program. See the correct output in file prog4.out for 12 games in directory: ~cs503/progs/20f/p4. However, the grid displayed on the terminal screen is not stored in the output file but a demonstration to play the game will be done in a class session.

Insert the following lines at the top of your program and make your program executable.

#!/usr/bin/python3
from graphics import * # You need this to use the graphical package.
from random import seed, randrange # You need this to generate random numbers.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.