Close Menu
Techs Slash

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    Designing for Milliseconds: The Precision Tech Behind Aviator 

    April 6, 2026

    The One App You Only Open When Something Is Already Happening

    April 6, 2026

    375 Suppressor Guide: Enhancing Performance of the 375 Raptor Platform

    April 6, 2026
    Facebook X (Twitter) Instagram
    Techs Slash
    • Home
    • News
      • Tech
      • Crypto News
      • Cryptocurrency
    • Entertainment
      • Actors
      • ANGEL NUMBER
      • Baby Names
      • Beauty
      • beauty-fashion
      • facebook Bio
      • Fitness
      • Dubai Tour
    • Business
      • Business Names
    • Review
      • Software
      • Smartphones & Apps
    • CONTRIBUTION
    Facebook X (Twitter) Instagram
    Techs Slash
    Home»python»Tic Tac Toe Game In Python
    python

    Tic Tac Toe Game In Python

    Ranveer KumarBy Ranveer KumarOctober 3, 2022No Comments4 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email

    Warning: Trying to access array offset on value of type bool in /home/cadesimu/techsslash.com/wp-content/themes/smart-mag/partials/single/featured.php on line 78
    Share
    Facebook Twitter LinkedIn Pinterest Email

    What we will see through this article is we will find exhaustively about Spasm Tac Toe Game In Python. Spasm Tac-Toe is an extremely straightforward two-player game. So just two players can play simultaneously.

    This game is otherwise called Noughts and Cross or Xs and operating system game. One player plays with X, the other player plays with O. In this game we have a board with a 3X3 framework. The quantity of stages can be expanded.

    This game is played by two people. In the first place, we draw a board with a 3 × 3 square lattice. On the off chance that the main player chooses ‘X’ and attracts something the square framework, the subsequent player has the chance to attract ‘O’ the accessible spaces.

    Essentially, players draw ‘X’ and ‘O’ then again to fill in the spaces until a player prevails with regards to attracting 3 focuses a line on a level plane, in an upward direction, or slantingly. Then, at that point, the player dominates the match, any other way, the game will get a draw when every one of the seats are filled.

    How about we see some ongoing interactions outwardly.

    Tic Tac Toe Game in Python

    Below is the code for the above game :

    # Tic-Tac-Toe Program using
    # random number in Python
    
    # importing all necessary libraries
    import numpy as np
    import random
    from time import sleep
    
    # Creates an empty board
    def create_board():
    	return(np.array([[0, 0, 0],
    					[0, 0, 0],
    					[0, 0, 0]]))
    
    # Check for empty places 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)
    
    # Select 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 whether the player has three
    # of their marks in a 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 whether 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 whether the player has three
    # of their marks in a diagonal row
    def diag_win(board, player):
    	win = True
    	y = 0
    	for x in range(len(board)):
    		if board[x, x] != player:
    			win = False
    	if win:
    		return win
    	win = True
    	if win:
    		for x in range(len(board)):
    			y = len(board) - 1 - x
    			if board[x, y] != player:
    				win = False
    	return win
    
    # Evaluates whether there is
    # a winner or a tie
    def evaluate(board):
    	winner = 0
    
    	for player in [1, 2]:
    		if (row_win(board, player) or
    			col_win(board,player) or
    			diag_win(board,player)):
    
    			winner = player
    
    	if np.all(board != 0) and winner == 0:
    		winner = -1
    	return winner
    
    # Main function to start 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(board)
    			sleep(2)
    			counter += 1
    			winner = evaluate(board)
    			if winner != 0:
    				break
    	return(winner)
    
    # Driver Code
    print("Winner is: " + str(play_game()))
    

    Program Execution of Tic-Tac-Toe game

    Output

    The result will print on the screen utilizing the print() capability like displayed beneath:

    [[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

    Final Words

    What we realized through this article is Spasm Tac Toe Game In Python. Likewise in the event that you have any questions kindly leave a remark through the remark box. What’s more, we ask that you benefit every individual who imparted this article to your companions.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Ranveer Kumar
    • Website

    Related Posts

    JSONDecodeError: Expecting value: line 1 column 1 (char 0)

    December 18, 2023

    Why does math.log result in ValueError: math domain error?

    December 17, 2023

    “inconsistent use of tabs and spaces in indentation” [duplicate]

    December 16, 2023
    Leave A Reply Cancel Reply

    Top Posts

    Sapne Me Nahane Ka Matlab

    March 18, 2024

    Sapne Me Nagn Stri Dekhna

    March 18, 2024

    Self Reliance: Release Date, Cast, Plot, Trailer, and More Information

    March 18, 2024

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    ABOUT TECHSSLASH

    Welcome to Techsslash! We're dedicated to providing you with the best of technology, finance, gaming, entertainment, lifestyle, health, and fitness news, all delivered with dependability.

    Our passion for tech and daily news drives us to create a booming online website where you can stay informed and entertained.

    Enjoy our content as much as we enjoy offering it to you

    Most Popular

    Sapne Me Nahane Ka Matlab

    March 18, 2024

    Sapne Me Nagn Stri Dekhna

    March 18, 2024

    Self Reliance: Release Date, Cast, Plot, Trailer, and More Information

    March 18, 2024
    CONTACT DETAILS

    Phone: +92-302-743-9438
    Email: contact@serpinsight.com

    Our Recommendation

    Here are some helpfull links for our user. hopefully you liked it.

    kakekmerah4d

    Techs Slash
    Facebook X (Twitter) Instagram Pinterest
    • Home
    • About us
    • contact us
    • Affiliate Disclosure
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • Write for us
    • Daman Game
    © 2026 Techsslash. All Rights Reserved

    Type above and press Enter to search. Press Esc to cancel.