Change language

Python implementation of an automatic tic-tac-toe game using a random number

| | |

The game is automatically launched by the program and therefore no user input is required. However, developing an automated game will be very fun. Let’s see how.

numpy and random Python libraries are used to create this game. Instead of asking the user to place a mark on the board, the code randomly selects a spot on the board and marks it. It will display the board after every move if the player doesn’t win. If the game gets a draw, it returns -1.

Explanation:
play_game () — this is the main function that does the following:

  • Calls create_board () to create a 9x9 board and initializes at 0.
  • For each player (1 or 2), calls the random_place () function to randomly select a place on the board and label that place with the player’s number.
  • Print the board after each move.
  • Evaluate the board after each move to check if there is a row, column or diagonal has the same player number. If so, the name of the winner is displayed. If after 9 moves there is no winner, -1 is displayed.

Below is the code for the above game:

# Tic- Tac-Toe Program using
# random number in Python

  
# import all required libraries

import numpy as np

import random

from time import sleep

 
# Creates a blank board

def create_board ():

  return (np.array ([[ 0 , 0 , 0 ],

[ 0 , 0 , 0 ],

[ 0 , 0 , 0 ]]))

 
# Check for empty seats on board

def possibilities (board):

  l = []

 

for i in range ( len (board)):

for j in range ( len (board)):

 

if board [i] [j] = = 0 :

  l.append ((i, j))

  return (l)

 
# Pick a random place for the player

def random_place (board , player):

selection = possibilities (board)

current_loc = random.choice (selection)

board [current_loc] = player

return (board)

  
# Checks if the player has three
# their marks in the horizontal row

def row_win (board, player):

for x in range ( len (board)):

win = True

 

for y in range ( len (board )) :

if board [x, y]! = player:

win = False

continue

 

if win = = True :

return (win)

return (win)

 
 # Checks if the player has three
# of their marks in a vertical row

def col_win (board, player):

for x in range ( len (board)):

  win = True

 

for y in range ( len (board)):

if board [y] [x]! = player :

win = False

continue

 

if win = = True :

return (win)

return (win)

 
# Checks if the player has three
# their marks in the diagonal row

def diag_win (board, player):

win = True

  

  for x in range ( len (board)):

if board [x, x]! = player:

  win = False

return (win)

 
# Evaluates, yes whether
# winner or draw

def evaluate (board):

  winner = 0

  

for player in [ 1 , 2 ]:

if (row_win (board, player) or

  col_win (board, player) < / code> or  

diag_win (board, player) ):

 

winner = player

  

if np. all (board! = 0 ) and winner = = 0 :

winner = - 1

return winner

 
# Main function for starting the game

def play_game ():

board, winner, counter = create_board (), 0 , 1

print (board)

sleep ( 2 )

  

while winner = 0 :

for player in [ 1 , 2 ]:

board = random_place (board, player)

print ( "Board after" + str (counter) + "move" )

print (boa rd)

sleep ( 2 )

counter + = 1

  winner = evaluate (board)

if winner! = 0 :

break

  return (winner)

 
Code driver

print < / code> ( "Winner is:" + str (play_game ()))

Exit:

 [[0 0 0] [0 0 0] [0 0 0]] Board after 1 move [[ 0 0 0] [0 0 0] [1 0 0]] Board after 2 move [[0 0 0] [0 2 0] [1 0 0]] Board after 3 move [[0 1 0] [0 2 0 ] [1 0 0]] Board after 4 move [[0 1 0] [2 2 0] [1 0 0]] Board after 5 move [[1 1 0] [2 2 0] [1 0 0]] Board after 6 move [[1 1 0] [2 2 0] [1 2 0]] Board after 7 move [[1 1 0] [2 2 0] [1 2 1]] Board after 8 move [[1 1 0 ] [2 2 2] [1 2 1]] Winner is: 2 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method