Skip to content
Open
Show file tree
Hide file tree
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
121 changes: 82 additions & 39 deletions I-O/Tic-Tac-Toe_partB.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,92 @@
#We are now going to incooperate the function into our while loop
# Tic-Tac-Toe Part B

def drawfield(field):

def draw_field(field):
for row in range(5):
if row %2 == 0: #0,2,4
#Since we need values 0,1,2 according to our list
#We will divide the values that are divisible by 2 to get 0,1,2
PracticalRow = int(row/2) #0,1,2
#print vertical lines
if row % 2 == 0:
practical_row = row // 2
for column in range(5):
if column %2 == 0 : # 0,2,4 that pass here
#Since we need values 0,1,2 according to our list
#We will divide the values that are divisible by 2 to get 0,1,2
PracticalColumn = int(column/2) #0,1,2
if column % 2 == 0:
practical_column = column // 2
if column != 4:
print(field[PracticalColumn][PracticalRow], end="")
print(field[practical_column][practical_row], end="")
else:
print(field[PracticalColumn][PracticalRow])
print(field[practical_column][practical_row])
else:
print("|", end="")
print("|", end="")
else:
print("-----")
print()


def check_winner(field):
# Check rows
for row in range(3):
if field[0][row] != " " and field[0][row] == field[1][row] == field[2][row]:
return field[0][row]

# Check columns
for col in range(3):
if field[col][0] != " " and field[col][0] == field[col][1] == field[col][2]:
return field[col][0]

# Check diagonals
if field[0][0] != " " and field[0][0] == field[1][1] == field[2][2]:
return field[0][0]
if field[2][0] != " " and field[2][0] == field[1][1] == field[0][2]:
return field[2][0]

return None


def is_board_full(field):
for col in field:
if " " in col:
return False
return True


def start_game():
current_field = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]]
player = 1

print("Welcome to Tic-Tac-Toe!\n")
print("Enter row and column as numbers 0, 1, or 2.\n")
draw_field(current_field)

while True:
tile = "X" if player == 1 else "O"
print(f"Player {player} ({tile}) turn:")

try:
move_row = int(input("Please enter the row (0-2): "))
move_column = int(input("Please enter column (0-2): "))
except ValueError:
print("Invalid input. Please enter a number between 0 and 2.\n")
continue

if move_row not in range(3) or move_column not in range(3):
print("Out of range. Please enter 0, 1, or 2.\n")
continue

if current_field[move_column][move_row] != " ":
print("That space is already taken. Try again.\n")
continue

current_field[move_column][move_row] = tile
draw_field(current_field)

winner = check_winner(current_field)
if winner:
print(f"Player {player} ({tile}) wins!\n")
break

if is_board_full(current_field):
print("It's a draw!\n")
break

player = 2 if player == 1 else 1


#Let us extend it a little using while loop

Player = 1
#Create a list that has all elemnts
#Eache element in a list has 3 elemnts inside it
CurrentField = [[" "," "," ",], [" "," "," ",], [" "," "," ",]]

drawfield(CurrentField)
while(True):
print("Player turn: ",Player)
MoveRow = int(input("Please enter the row\n"))
MoveColumn = int(input("Please enter column\n"))
if Player == 1:
#Make move for Player 1
if CurrentField[MoveColumn][MoveRow] == " " :
CurrentField[MoveColumn][MoveRow] = "X"
Player = 2


else:
#Make move for Player 2
if CurrentField[MoveColumn][MoveRow] == " ":
CurrentField[MoveColumn][MoveRow] = "O"
Player = 1
drawfield(CurrentField)
# Start the game
start_game()
126 changes: 86 additions & 40 deletions I-O/Tic-tac-toe-partA.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,91 @@
#Tic-Tac_Toe part A
# | |
# ----
# | |
# ----
# | |

def drawfield(n,m):
for row in range(n):
if row %2 == 0:
#print vertical lines
for column in range(m):
if column %2 == 0 :
if column != m-1:
print(" ", end="")
# Tic-Tac-Toe

def draw_field(current_field):
for row in range(5):
if row % 2 == 0:
actual_row = row // 2
for column in range(5):
if column % 2 == 0:
actual_col = column // 2
if column != 4:
print(current_field[actual_row][actual_col], end="")
else:
print(" ")
print(current_field[actual_row][actual_col])
else:
print("|", end="")
print("|", end="")
else:
print("-----")
drawfield(10,5)

#Let us extend it a little using while loop

Player = 1
#Create a list that has all elemnts
#Eache element in a list has 3 elemnts inside it
CurrentField = [[" "," "," ",], [" "," "," ",], [" "," "," ",]]
print(CurrentField)
while(True):
print("Player turn: ",Player)
MoveRow = int(input("Please enter the row\n"))
MoveColumn = int(input("Please enter column\n"))
if Player == 1:
#Make move for Player 1
CurrentField[MoveColumn][MoveRow] == "O"
Player = 2

else:
#Make move for Player 2
CurrentField[MoveColumn][MoveRow] == "X"
Player = 1
print(CurrentField)
print()


def check_winner(current_field):
# Check rows
for row in current_field:
if row[0] != " " and row[0] == row[1] == row[2]:
return row[0]

# Check columns
for col in range(3):
if current_field[0][col] != " " and current_field[0][col] == current_field[1][col] == current_field[2][col]:
return current_field[0][col]

# Check diagonals
if current_field[0][0] != " " and current_field[0][0] == current_field[1][1] == current_field[2][2]:
return current_field[0][0]
if current_field[0][2] != " " and current_field[0][2] == current_field[1][1] == current_field[2][0]:
return current_field[0][2]

return None


def is_board_full(current_field):
for row in current_field:
if " " in row:
return False
return True


def start_game():
current_field = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]]
player = 1

print("Welcome to Tic-Tac-Toe!\n")
print("Enter row and column as numbers 0, 1, or 2.\n")
draw_field(current_field)

while True:
tile = "O" if player == 1 else "X"
print(f"Player {player} ({tile}) turn:")

try:
move_row = int(input("Please enter the row (0-2): "))
move_column = int(input("Please enter the column (0-2): "))
except ValueError:
print("Invalid input. Please enter a number between 0 and 2.\n")
continue

if move_row not in range(3) or move_column not in range(3):
print("Out of range. Please enter a number between 0 and 2.\n")
continue

if current_field[move_row][move_column] != " ":
print("That space is already taken. Try again.\n")
continue

current_field[move_row][move_column] = tile
draw_field(current_field)

winner = check_winner(current_field)
if winner:
print(f"Player {player} ({tile}) wins!\n")
break

if is_board_full(current_field):
print("It's a draw!\n")
break

player = 2 if player == 1 else 1


# Start the game
start_game()
Loading