Tic-Tac-Toe Game Using Python
Hello coder, welcome to the codewithrandom blog. It’s time to code a game in Python. In this article, we create a tic-tac-toe Game using Python Programming with Complete Source Code.
Tic-tac-toe is a classic two-player game that has been played for generations. The game is simple to play, but can be quite challenging to master.
what is Tic-Tac-Toe Game
Tic-tac-toe is a classic two-player game that can be played using Python. The game is played on a 3×3 grid, and each player takes turns placing their symbol (X or 1) on the board. The objective of the game is to get three of your symbols in a row (horizontally, vertically, or diagonally) before the other player does. If the grid is filled and no player has three in a row, the game is a draw. To create a Tic-tac-toe game in Python, you can use various programming concepts such as functions, loops, and conditional statements.
Building the tic-tac-toe Game
To build our tic-tac-toe game, we’ll use Python. Specifically, we’ll use Python 3. Here are the steps we’ll follow:
How To Run The Code :
step 1: open any python code Editor.
step 2 : Copy the code for the tic-tac-toe Game game in Python, which I provided Below in this article, and save it in a file named “main.py” (or any other name you prefer).
step 3: Run this python file main.py
to start the game.
That’s it! Have fun playing tic-tac-toe Game in Python.
Complete tic-tac-toe Game code here👇👇
def main(): # The main function introduction = intro() board = create_grid() pretty = printPretty(board) symbol_1, symbol_2 = sym() full = isFull(board, symbol_1, symbol_2) # The function that starts the game is also in here. def intro(): # This function introduces the rules of the game Tic Tac Toe print("Hello! Welcome to Tic Tac Toe game!") print("\n") print("Rules: Player 1 and player 2, represented by X and O, take turns " "marking the spaces in a 3*3 grid. The player who succeeds in placing " "three of their marks in a horizontal, vertical, or diagonal row wins.") print("\n") input("Press enter to continue.") print("\n") def create_grid(): # This function creates a blank playboard print("Here is the playboard: ") board = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]] return board def sym(): # This function decides the players' symbols symbol_1 = input("Player 1, do you want to be X or O? ") if symbol_1 == "X": symbol_2 = "O" print("Player 2, you are O. ") else: symbol_2 = "X" print("Player 2, you are X. ") input("Press enter to continue.") print("\n") return (symbol_1, symbol_2) def startGamming(board, symbol_1, symbol_2, count): # This function starts the game. # Decides the turn if count % 2 == 0: player = symbol_1 elif count % 2 == 1: player = symbol_2 print("Player "+ player + ", it is your turn. ") row = int(input("Pick a row:" "[upper row: enter 0, middle row: enter 1, bottom row: enter 2]:")) column = int(input("Pick a column:" "[left column: enter 0, middle column: enter 1, right column enter 2]")) # Check if players' selection is out of range while (row > 2 or row < 0) or (column > 2 or column < 0): outOfBoard(row, column) row = int(input("Pick a row[upper row:" "[enter 0, middle row: enter 1, bottom row: enter 2]:")) column = int(input("Pick a column:" "[left column: enter 0, middle column: enter 1, right column enter 2]")) # Check if the square is already filled while (board[row][column] == symbol_1)or (board[row][column] == symbol_2): filled = illegal(board, symbol_1, symbol_2, row, column) row = int(input("Pick a row[upper row:" "[enter 0, middle row: enter 1, bottom row: enter 2]:")) column = int(input("Pick a column:" "[left column: enter 0, middle column: enter 1, right column enter 2]")) # Locates player's symbol on the board if player == symbol_1: board[row][column] = symbol_1 else: board[row][column] = symbol_2 return (board) def isFull(board, symbol_1, symbol_2): count = 1 winner = True # This function check if the board is full while count < 10 and winner == True: gaming = startGamming(board, symbol_1, symbol_2, count) pretty = printPretty(board) if count == 9: print("The board is full. Game over.") if winner == True: print("There is a tie. ") # Check if here is a winner winner = isWinner(board, symbol_1, symbol_2, count) count += 1 if winner == False: print("Game over.") # This is function gives a report report(count, winner, symbol_1, symbol_2) def outOfBoard(row, column): # This function tells the players that their selection is out of range print("Out of boarder. Pick another one. ") def printPretty(board): # This function prints the board nice! rows = len(board) cols = len(board) print("---+---+---") for r in range(rows): print(board[r][0], " |", board[r][1], "|", board[r][2]) print("---+---+---") return board def isWinner(board, symbol_1, symbol_2, count): # This function checks if any winner is winning winner = True # Check the rows for row in range (0, 3): if (board[row][0] == board[row][1] == board[row][2] == symbol_1): winner = False print("Player " + symbol_1 + ", you won!") elif (board[row][0] == board[row][1] == board[row][2] == symbol_2): winner = False print("Player " + symbol_2 + ", you won!") # Check the columns for col in range (0, 3): if (board[0][col] == board[1][col] == board[2][col] == symbol_1): winner = False print("Player " + symbol_1 + ", you won!") elif (board[0][col] == board[1][col] == board[2][col] == symbol_2): winner = False print("Player " + symbol_2 + ", you won!") # Check the diagnoals if board[0][0] == board[1][1] == board[2][2] == symbol_1: winner = False print("Player " + symbol_1 + ", you won!") elif board[0][0] == board[1][1] == board[2][2] == symbol_2: winner = False print("Player " + symbol_2 + ", you won!") elif board[0][2] == board[1][1] == board[2][0] == symbol_1: winner = False print("Player " + symbol_1 + ", you won!") elif board[0][2] == board[1][1] == board[2][0] == symbol_2: winner = False print("Player " + symbol_2 + ", you won!") return winner def illegal(board, symbol_1, symbol_2, row, column): print("The square you picked is already filled. Pick another one.") def report(count, winner, symbol_1, symbol_2): print("\n") input("Press enter to see the game summary. ") if (winner == False) and (count % 2 == 1 ): print("Winner : Player " + symbol_1 + ".") elif (winner == False) and (count % 2 == 0 ): print("Winner : Player " + symbol_2 + ".") else: print("There is a tie. ") # Call Main main()
output👇👇
Hello! Welcome to Tic Tac Toe game! Rules: Player 1 and player 2, represented by X and O, take turns marking the spaces in a 3*3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins. Press enter to continue. Here is the playboard: ---+---+--- | | ---+---+--- | | ---+---+--- | | ---+---+--- Player 1, do you want to be X or O? 1 Player 2, you are X. Press enter to continue.1 Player X, it is your turn. Pick a row:[upper row: enter 0, middle row: enter 1, bottom row: enter 2]:1 Pick a column:[left column: enter 0, middle column: enter 1, right column enter 2]1 ---+---+--- | | ---+---+--- | X | ---+---+--- | | ---+---+--- Player 1, it is your turn. Pick a row:[upper row: enter 0, middle row: enter 1, bottom row: enter 2]:1 Pick a column:[left column: enter 0, middle column: enter 1, right column enter 2]2 ---+---+--- | | ---+---+--- | X | 1 ---+---+--- | | ---+---+--- Player X, it is your turn. Pick a row:[upper row: enter 0, middle row: enter 1, bottom row: enter 2]:2 Pick a column:[left column: enter 0, middle column: enter 1, right column enter 2]1 ---+---+--- | | ---+---+--- | X | 1 ---+---+--- | X | ---+---+--- Player 1, it is your turn. Pick a row:[upper row: enter 0, middle row: enter 1, bottom row: enter 2]:0 Pick a column:[left column: enter 0, middle column: enter 1, right column enter 2]0 ---+---+--- 1 | | ---+---+--- | X | 1 ---+---+--- | X | ---+---+--- Player X, it is your turn. Pick a row:[upper row: enter 0, middle row: enter 1, bottom row: enter 2]:0 Pick a column:[left column: enter 0, middle column: enter 1, right column enter 2]1 ---+---+--- 1 | X | ---+---+--- | X | 1 ---+---+--- | X | ---+---+--- Player X, you won! Game over.
Summary
Hurray! You have successfully Create the a most popular tic-tac-toe Game project using the Python . We learned to create amazing python project , tic-tac-toe Game . but it’s also popular amongst the python developers to develop games in python. Hope you enjoyed building with us! Visit our homepage and you get lot’s of projects💝.
Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my python articles.