0% found this document useful (0 votes)
3 views

Practical No.2

The document contains a Python implementation of a Tic Tac Toe game, featuring a class that manages the game board, player turns, and win conditions. It includes methods for creating the board, checking for wins, and displaying the board state. The game runs in a loop until a player wins or the game ends in a draw.

Uploaded by

nileshmahale7755
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Practical No.2

The document contains a Python implementation of a Tic Tac Toe game, featuring a class that manages the game board, player turns, and win conditions. It includes methods for creating the board, checking for wins, and displaying the board state. The game runs in a loop until a player wins or the game ends in a draw.

Uploaded by

nileshmahale7755
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Practical No:2

import random

class TicTacToe:

def __init__(self):

self.board = []

def create_board(self):

for i in range(3):

row = []

for j in range(3):

row.append('-')

self.board.append(row)

def get_random_first_player(self):

return random.randint(0, 1)

def fix_spot(self, row, col, player):

self.board[row][col] = player

def is_player_win(self, player):

win = None

n = len(self.board)

# checking rows

for i in range(n):

win = True

for j in range(n):

if self.board[i][j] != player:

win = False

break
if win:

return win

# checking columns

for i in range(n):

win = True

for j in range(n):

if self.board[j][i] != player:

win = False

break

if win:

return win

# checking diagonals

win = True

for i in range(n):

if self.board[i][i] != player:

win = False

break

if win:

return win

win = True

for i in range(n):

if self.board[i][n - 1 - i] != player:

win = False

break

if win:

return win

return False
def is_board_filled(self):

for row in self.board:

for item in row:

if item == '-':

return False

return True

def swap_player_turn(self, player):

return 'X' if player == 'O' else 'O'

def show_board(self):

for row in self.board:

for item in row:

print(item, end=" ")

print()

def start(self):

self.create_board()

player = 'X' if self.get_random_first_player() == 1 else 'O'

while True:

print(f"Player {player} turn")

self.show_board()

# taking user input

row, col = list(map(int, input("Enter row and column numbers to fix spot: ").split()))

print()

# fixing the spot

self.fix_spot(row - 1, col - 1, player)


# checking whether current player is won or not

if self.is_player_win(player):

print(f"Player {player} wins the game!")

break

# checking whether the game is draw or not

if self.is_board_filled():

print("Match Draw!")

break

# swapping the turn

player = self.swap_player_turn(player)

# showing the final view of board

print()

self.show_board()

# starting the game

tic_tac_toe = TicTacToe()

tic_tac_toe.start()

Output:
Player X turn

---

---

---

Enter row and column numbers to fix spot: 1 2

-X-

---
---

Player O turn

-X-

---

---

Enter row and column numbers to fix spot: 2 3

-X-

--O

---

Player X turn

-X-

--O

---

Enter row and column numbers to fix spot: 2 2

-X-

-XO

---

Player O turn

-X-

-XO

---

Enter row and column numbers to fix spot: 3 1

-X-

-XO

O--
Player X turn

-X-

-XO

O--

Enter row and column numbers to fix spot: 2 1

-X-

XXO

O--

Player O turn

-X-

XXO

O--

Enter row and column numbers to fix spot: 1 1

OX-

XXO

O--

Player X turn

OX-

XXO

O--

Enter row and column numbers to fix spot: 3 2

Player X wins the game!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy