Skip to main content

Tic Tac Toe Python 3: The Standard Tic-Tac-Toe Game in Python 3

I just finished Tic tac toe game as my first python project. This is a very simple classic game, for beginners in programming tic-tac-toe game is the best choice. I am using Jupyter notebook, you can use any python ide like Pycharm, Spyder etc.

Scenario of tic-tac-toe game:

Your task is to write a simple program which pretends to play Tic-tac-toe game with the user. To make it easier for you. We have decided to simplify the game. Here are our assumptions.

  • the computer should play a game using 'X'
  • the user should play a game using 'O'
  • the first move belongs to the computer, it always 'X' in the middle of the board. 
Tic Tac Toe Python 3: The Standard TicTacToe Game in Python 3
  • all squares are numbered row by row, start with '1'.
  • the user move is to enter the number of the square they choose, the number must be valid, that is it must be an integer, it must be greater than  0 and less than 10, and it cannot point to a field which is already occupied.
  • the program checks the game is over, there are four possibilities- the game should continue, the game should tie, computer's win and you win.
  • don't implement any form of artificial intelligence's, a random field is chosen by a computer this is enough for the game.
John Academy

Requirements to build tic-tac-toe game:

  1. Better knowledge about if-elif-else, for-while loop, tuple, list, function.
  2. Draw a random function you can use randrange() 

from random import randrange

for i in range(10): print(randrange(8))  

1. Display Board function of tic-tac-toe game:

The function accepts one parameter containing the board's current status and prints it out to the console.

2.Enter a move:

The function accepts the board current status, asks the user about their move,  checks the input and updates the board according to the user's decision.

3. Make a list of the free field:

The function browses the board, builds a list of all the free squares and the list consists of tuples, while each tuple is a pair of row-column numbers.

4.Victory for :

The function analyzes the board status to check if the player using 'O's or 'X's has won the game.

5.Draw move:

The function draws the computer's move and updates the board.

6.Main program:

And then combine this function so, the main program:

Comments

  1. I am developer and i make a website check it.

    ReplyDelete
  2. Anonymous9/27/2023

    Hi. Can you share the code please? (able to be copied)

    ReplyDelete
  3. Anonymous1/21/2024

    Hello, at the victor, the last if it's actually: board[2-rc][rc] != sign so it can take the other cross correctly.

    ReplyDelete

Post a Comment

Thanks for the comment.

Popular posts from this blog

Prime Number Program in Java: Print All Prime Numbers

Question You've to print all prime numbers between a range. Take as input "low", the lower limit of the range. Take as input "high", the higher limit of the range. For the range print all the primes numbers between low and high (both included). Input Format low high Output Format n1 n2 .. all primes between low and high (both included) Constraints 2 <= low < high < 10 ^ 6 Sample Input 6 24 Sample Output 7 11 13 17 19 23 Program import java.util.*; public class Main{ public static void main(String[] args) { // write your code here Scanner scn = new Scanner(System.in); int low = scn.nextInt(); int high = scn.nextInt(); for (int i = low; i <= high; i++){ int count = 0; //try to divide n and incrtease count for (int div = 2; div * div <= i; div++){ if ( i % div == 0){ count++;