Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create tic_tac_toe game #386

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions tic_tac_toe game
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
board=[['0','1','2'],
['3','4','5'],
['6','7','8']]

def print_board(board):
for row in board:
print(row)

def check_game(mark):
#first let's check rows
for i in range(3):
if board[i][0]==mark and board[i][1]==mark and board[i][2]==mark:
return True

#then columns
for i in range(3):
if board[0][i]==mark and board[1][i]==mark and board[2][i]==mark:
return True

if board[0][0]==mark and board[1][1]==mark and board[2][2]==mark:
return True
if board[0][2]==mark and board[1][1]==mark and board[0][2]==mark:
return True


def play_tic_tac_toe():
current_player="1"
mark="X"
moves=0

while (moves<9):
print_board(board)

print("player"+current_player+"move.")

print("please enter the cell no:")
cell=int(input())

row=cell//3
col=cell%3

if board[row][col]!="X" and board[row][col]!="O":
board[row][col]= mark
moves+=1
result=check_game(mark)
else:
print("this cell is already occupied!!, try ANOTHER ONE.")
continue

if result:
print("Player" +current_player+"has won the game")
break

if moves==9:
print("!!!!Draw!!!!")

if current_player=="1":
current_player="2"
mark="O"
else:
current_player="1"
mark="X"




play_tic_tac_toe()