You are on page 1of 13

TIC TAC TOE

PRESENTED BY: PRESENTED TO:


VASUDHA JAIN (220231) DR. HITESH JANGIR
YASHI JINDAL (220232)
YASHIKA HARLALKA (220233)
LEESHA LALWANI (220248)
PRAGYA TOMAR (220253)
PROJECT DESCRIPTION

Written in Python Programming Language


This Python project is based on CUI board based games.
All the gaming rules are same.
Reduces the manual struggle to play this game.
This will provide lots of TIC TAC TOE matches without any error.
IMPLEMENTATION
A hash shape square board grid.
One of the player has to choose ,’O’ and the others ,’X’
to mark their respective cells.
The player will have to input a numerical
character,from 1to9, to select a position for X or O into
the space.
The games end when one whole row/column/diagonal
filled with his/her respective character(’O’,’X’).
If the blank spaces in the grid are all filled, and there is no
winner,then the game is said to be a draw.
TECHNOLOGIES USED AND REQUIRED:

Software Platform : Front end : Python3


Back end : Python 3.6.0Interpreter
Hardware Platform : Processor(intel),Hard disk , Memory.
MODULAR
MODULAR DESCRIPTION
DESCRIPTION
Following are the modules that are essential toimplement the TIC-TAC-TOE game in python:
Initialization
In your Python 3 IDLE click File and create a New File.Start by saving your file and calling it py
• Now run your code, just click RUN (or press F5 on the keyboard)
Newboard: To play TIC TAC TOE, we are going to need to print out the game board grid that consists of three rows, and the columns. With the function create_line thegrid can be printed on
the screen.
Player’s name: It is important to save the name of the two players. For this, store each name in a variable. Use input to get each player’s name and store them invariables.
New move: When we start the game only a 3x3 square grid will be displayed. It will receive the current player and the input of X and O from the keyboard.Ask the player what symbol they want,
using input.Save it in variable input_symbol.
Draw X: This module is all about drawing X on the grid. So, for this, we have to write code in create_line package. The grid co-ordinates have change into a pixel, andthen have to adjust X in the
board.
Draw O: This module is all about drawing O on the grid. So, for this, we have to write code in create_line package. The grid co-ordinates have change into a pixel, andthen have to adjust O in the
board.
Game over screen: This module displays the result according to the outcome on the screen. If the outcome is on side of the player with symbol X, then, “X Wins”appear on the screen in the form
of text.If the outcome is on the side of the player with symbol O, then, “O Wins” appear on the screen.
Has won: It’s all about checking the winner and sharing the result in on the screen. The result is checked by using Has_won function in python3 library.
No win: In this module, it will check that the result is a tie or not. The result is a draw when blank spaces in the grid are all filled, and there is no win It will print “drawmatch” on the screen.
FUNCTIONS
Enter key () : The function scans a charater from the keyboard& return its
corresponding code in integer form.
Display() : The function prints the whole Tic Tac Toe grid.
Winner() : The function checks if any of the two players has won the team or
not.
Draw() : The function checks if the game is draw.
End() : The function displays the result message , close the graphics mode & exist
from the program.
Outtexty() : “Tic Tac Toe”& current player with its symbol on screen.
def print_board(board):
for row in board:
print(" | ".join(row))
print("-" * 9)

def check_win(board, player):


for row in board:
if all(cell == player for cell in row):
return True

CODING for col in range(3):


if all(board[row][col] == player for row in
range(3)):
return True

if all(board[i][i] == player for i in range(3)) or


all(board[i][2 - i] == player for i in range(3)):
return True

return False
def check_draw(board):
return all(cell != " " for row in board for cell in row)

def tic_tac_toe():
board = [[" " for _ in range(3)] for _ in range(3)]
player = "X"

while True:
print_board(board)

CODING print(f"Player {player}'s turn")

while True:
try:
row = int(input("Enter row (0, 1, 2): "))
col = int(input("Enter column (0, 1, 2): "))
if board[row][col] == " ":
board[row][col] = player
break
else:
print("That space is already taken. Try again.")
except (ValueError, IndexError):
except (ValueError, IndexError):
print("Invalid input. Try again.")

if check_win(board, player):
print_board(board)
print(f"Player {player} wins!")
break
elif check_draw(board):

CODING print_board(board)
print("It's a draw!")
break

player = "O" if player == "X" else "X"

if __name__ == "__main__":
tic_tac_toe()
ABOUT THE CODE
1.print_board(board) Function:
This function is used to display the Tic Tac Toe game board in the console.
It takes the board as input, which is a 2D list representing the current state of
the game board.
It iterates through the rows and prints each row with "|" separators and
horizontal lines to create the board's visual representation.
2.check_win(board, player) Function:
This function checks if a player (X or O) has won the game.
It takes the board and the current player as input.
It checks for wins by examining the rows, columns, and diagonals on the
board.
If any row, column, or diagonal contains the same symbol (player), the
function returns True, indicating a win. Otherwise, it returns False.
3. check_draw(board) Function:
This function checks if the game has ended in a draw (a tie).
It takes the board as input.
It iterates through all the cells on the board and checks if there are no empty
spaces left (no " " characters).
If all cells are filled, the function returns True, indicating a draw. Otherwise, it
returns False.
4. tic_tac_toe() Function:
This is the main function that orchestrates the Tic Tac Toe game.
It initializes the game by creating an empty 3x3 board and setting the starting player to "X".
It enters a loop that continues until the game is over (either a player wins or it's a draw).
Inside the loop, it displays the current state of the board and prompts the current player to make a
move.
It handles user input, ensuring that the selected cell is empty, and it updates the board accordingly.
After each move, it checks for a win or a draw using the check_win and check_draw functions.
If there's a winner, it displays the final board and announces the winning player.
If it's a draw, it displays the final board and announces a draw.
Finally, it switches the player for the next turn (from "X" to "O" or vice versa) and continues the game
loop.
5. if __name__ == "__main__": Block:
This block ensures that the tic_tac_toe function is only executed when the script is run as the
main program.
It's a common Python idiom to prevent code from running when the script is imported as a
module.

The code allows two players to take turns playing Tic Tac Toe in the console. It checks for wins
and draws and displays the final result. Players can enter their moves by specifying the row and
column they want to place their symbol in. The game continues until there's a winner or it ends
in a draw.
THANK
YOU

You might also like