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

added tic_tac_toe project in python #4

Merged
merged 27 commits into from
Oct 2, 2023
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
5cadd78
added tic_tac_toe project in python
akshatkhatri Oct 1, 2023
78a11e7
Merge branch 'main' into tictactoe
wbhoomika Oct 1, 2023
80217e0
Merge branch 'main' into tictactoe
wbhoomika Oct 1, 2023
7a4dd69
Merge branch 'main' into tictactoe
wbhoomika Oct 1, 2023
f216211
Merge branch 'main' into tictactoe
wbhoomika Oct 1, 2023
7c25492
Merge branch 'main' into tictactoe
wbhoomika Oct 1, 2023
9ad9f77
Merge branch 'main' into tictactoe
wbhoomika Oct 1, 2023
fee5490
Merge branch 'main' into tictactoe
wbhoomika Oct 1, 2023
42e496e
Merge branch 'main' into tictactoe
wbhoomika Oct 1, 2023
d033e17
Merge branch 'main' into tictactoe
wbhoomika Oct 1, 2023
744c8e4
Merge branch 'main' into tictactoe
wbhoomika Oct 1, 2023
7ea3a4a
Merge branch 'main' into tictactoe
wbhoomika Oct 1, 2023
4f6975a
Merge branch 'main' into tictactoe
wbhoomika Oct 1, 2023
e4cc6aa
Merge branch 'main' into tictactoe
wbhoomika Oct 1, 2023
7e63f4a
Merge branch 'main' into tictactoe
wbhoomika Oct 1, 2023
9f54d69
Merge branch 'main' into tictactoe
wbhoomika Oct 1, 2023
6d4aebb
Merge branch 'main' into tictactoe
wbhoomika Oct 1, 2023
6f1a277
Merge branch 'main' into tictactoe
wbhoomika Oct 2, 2023
41c407e
Merge branch 'main' into tictactoe
wbhoomika Oct 2, 2023
3233943
Merge branch 'main' into tictactoe
wbhoomika Oct 2, 2023
6277b45
Merge branch 'main' into tictactoe
wbhoomika Oct 2, 2023
abe1bd2
Merge branch 'main' into tictactoe
wbhoomika Oct 2, 2023
19710ca
Merge branch 'main' into tictactoe
wbhoomika Oct 2, 2023
98f04b2
Merge branch 'main' into tictactoe
wbhoomika Oct 2, 2023
8122d33
Merge branch 'main' into tictactoe
wbhoomika Oct 2, 2023
a68c801
Merge branch 'main' into tictactoe
wbhoomika Oct 2, 2023
867f1f6
Merge branch 'main' into tictactoe
wbhoomika Oct 2, 2023
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
60 changes: 60 additions & 0 deletions Python Projects/tic_tac_toe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
def print_board(board):
for row in board:
print(" | ".join(row))
print("-" * 5)

def check_winner(board):
# Check rows
for row in board:
if row.count(row[0]) == len(row) and row[0] != ' ':
return True

# Check columns
for col in range(len(board[0])):
if all(board[row][col] == board[0][col] and board[row][col] != ' ' for row in range(len(board))):
return True

# Check diagonals
if board[0][0] == board[1][1] == board[2][2] != ' ':
return True
if board[0][2] == board[1][1] == board[2][0] != ' ':
return True

return False

def is_board_full(board):
return all(all(cell != ' ' for cell in row) for row in board)

def is_valid_move(board, row, col):
return 0 <= row < 3 and 0 <= col < 3 and board[row][col] == ' '

def tic_tac_toe():
board = [[' ' for _ in range(3)] for _ in range(3)]
current_player = 'X'

while True:
print_board(board)

row = int(input(f"Player {current_player}, enter row (0, 1, or 2): "))
col = int(input(f"Player {current_player}, enter column (0, 1, or 2): "))

if not is_valid_move(board, row, col):
print("Invalid move. Try again.")
continue

board[row][col] = current_player

if check_winner(board):
print_board(board)
print(f"Player {current_player} wins!")
break

if is_board_full(board):
print_board(board)
print("It's a tie!")
break

current_player = 'O' if current_player == 'X' else 'X'

if __name__ == "__main__":
tic_tac_toe()