Skip to content

Commit 3319bc1

Browse files
authored
Merge pull request animator#309 from Harish-2003/tictactoegame
Create TIC_TAC_TOE_GAME.md
2 parents 34a6716 + 1ed2c2f commit 3319bc1

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

contrib/mini-projects/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
- [Password strength checker](password_strength_checker.md)
66
- [Path Finder](path-finder.md)
77
- [Hangman Game Based on Movies](hangman_game.md)
8+
- [Tic-tac-toe](tic-tac-toe.md)

contrib/mini-projects/tic-tac-toe.md

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Python Code For The Tic Tac Toe Game
2+
# Tic Tac Toe Game
3+
4+
## Overview
5+
6+
### Objective
7+
- Get three of your symbols (X or O) in a row (horizontally, vertically, or diagonally) on a 3x3 grid.
8+
9+
### Gameplay
10+
- Two players take turns.
11+
- Player 1 uses X, Player 2 uses O.
12+
- Players mark an empty square in each turn.
13+
14+
### Winning
15+
- The first player to align three of their symbols wins.
16+
- If all squares are filled without any player aligning three symbols, the game is a draw.
17+
18+
```python
19+
print("this game should be played by two people player1 takes x player2 takes o")
20+
board = [['1','2','3'],['4','5','6'],['7','8','9']]
21+
x = 'X'
22+
o = 'O'
23+
def displayBoard():
24+
print(f" {board[0][0]} | {board[0][1]} | {board[0][2]}")
25+
print("----------------------------------------")
26+
print(f" {board[1][0]} | {board[1][1]} | {board[1][2]}")
27+
print("----------------------------------------")
28+
print(f" {board[2][0]} | {board[2][1]} | {board[2][2]}")
29+
print("----------------------------------------")
30+
def updateBoard(character,position):
31+
row = (position-1)//3
32+
column = (position-1)%3
33+
board[row][column] = character
34+
def check_win():
35+
for i in range(3):
36+
if board[i][0] == board[i][1] == board[i][2]:
37+
return 1
38+
elif board[0][i] == board[1][i] == board[2][i]:
39+
return 1
40+
if board[0][2] == board[1][1] == board[2][0]:
41+
return 1
42+
elif board[0][0] == board[1][1] == board[2][2]:
43+
return 1
44+
return 0
45+
def check_position(position):
46+
row = (position-1)//3
47+
column = (position-1)%3
48+
if board[row][column] == x or board[row][column] == o:
49+
return 0
50+
return 1
51+
print("==============================welcome to tic tac toe game =====================")
52+
counter = 0
53+
while 1:
54+
if counter % 2 == 0:
55+
displayBoard()
56+
while 1:
57+
choice = int(input(f"player{(counter%2)+1},enter your position('{x}');"))
58+
if choice < 1 or choice > 9:
59+
print("invalid input oplease try againn")
60+
if check_position(choice):
61+
updateBoard(x,choice)
62+
if check_win():
63+
print(f"Congratulations !!!!!!!!!!!Player {(counter % 2)+1} won !!!!!!!!!!")
64+
exit(0)
65+
else :
66+
counter += 1
67+
break
68+
else:
69+
print(f"position{choice} is already occupied.Choose another position")
70+
if counter == 9:
71+
print("the match ended with draw better luck next time")
72+
exit(0)
73+
else:
74+
displayBoard()
75+
while 1:
76+
choice = int(input(f"player{(counter%2)+1},enter your position('{o}'):"))
77+
if choice < 1 or choice > 9:
78+
print("invalid input please try again")
79+
if check_position(choice):
80+
updateBoard(o,choice)
81+
if check_win():
82+
print(f"congratulations !!!!!!!!!!!!!!! player{(counter%2)+1} won !!!!!!!!!!!!!1")
83+
exit(0)
84+
else:
85+
counter += 1
86+
break
87+
else:
88+
print(f"position {choice} is already occupied.choose another position")
89+
print()
90+
```
91+

0 commit comments

Comments
 (0)