From 39881107ef335eb554713df9f8109ea94e4956b6 Mon Sep 17 00:00:00 2001 From: samwata Date: Wed, 11 Mar 2026 12:37:44 +0300 Subject: [PATCH 1/3] Update the game 4 --- I-O/connect4-game.py | 232 +++++++++++++++++++++++++------------------ 1 file changed, 136 insertions(+), 96 deletions(-) diff --git a/I-O/connect4-game.py b/I-O/connect4-game.py index 62e26ed..971db82 100644 --- a/I-O/connect4-game.py +++ b/I-O/connect4-game.py @@ -1,162 +1,202 @@ - from termcolor import colored, cprint import os import sys - -CurrentField = [[" "," ", " "," "," "," "," "],[" "," ", " "," "," "," "," "],[" "," ", " "," "," "," "," "],[" "," ", " "," "," "," "," "],[" "," ", " "," "," "," "," "],[" "," ", " "," "," "," "," "],[" "," ", " "," "," "," "," "]] -def draw_board(CurrentField): +def create_empty_board(): + return [[" " for _ in range(7)] for _ in range(7)] + + +def draw_board(current_field): for row in range(13): if row % 2 == 0: - practical_row = int(row/2) + practical_row = int(row / 2) for column in range(13): if column % 2 == 0: - practical_column = int(column/2) + practical_column = int(column / 2) color = "green" - if CurrentField[practical_column][practical_row] == "X": + if current_field[practical_column][practical_row] == "X": color = "red" - tile = colored(CurrentField[practical_column][practical_row], color, attrs=['bold']) + elif current_field[practical_column][practical_row] == "O": + color = "yellow" + tile = colored( + current_field[practical_column][practical_row], + color, + attrs=["bold"], + ) if column != 12: - print(tile,end="") + print(tile, end="") else: - print(tile) + print(tile) else: print("|", end="") else: print("-------------") print("\n") -draw_board(CurrentField) -def updateBoard(num, player): - column = CurrentField[num] - index = "" + +def update_board(current_field, num, player): + column = current_field[num] + placed = False reversed_column = column[::-1] - for row in reversed_column: + for idx, row in enumerate(reversed_column): if row == " ": - index = reversed_column.index(row) - reversed_column[index] = "X" if player == 1 else "O" + reversed_column[idx] = "X" if player == 1 else "O" + placed = True break - if index == "": + if not placed: return False column = reversed_column[::-1] - CurrentField[num] = column - draw_board(CurrentField) + current_field[num] = column + draw_board(current_field) + return True + + +def is_board_full(current_field): + for column in current_field: + if " " in column: + return False return True -def checkIfFourInRow(): - winner = False - for column in CurrentField: + +def check_four_in_row(current_field): + for column in current_field: counter = 0 length = len(column) for i in range(1, length): - if column[i - 1] != " " and column[i] != " " and column[i - 1 ] == column[i]: + if column[i - 1] != " " and column[i] != " " and column[i - 1] == column[i]: counter += 1 else: counter = 0 if counter == 3: - winner = column[i - 1] - return winner - return winner + return column[i - 1] + return False + -def checkIfFourInColumn(column_matrix): - winner = False +def check_four_in_column(column_matrix): for column in column_matrix: counter = 0 length = len(column) for i in range(1, length): - if column[i - 1] != " " and column[i] != " " and column[i -1 ] == column[i]: - counter += 1 + if column[i - 1] != " " and column[i] != " " and column[i - 1] == column[i]: + counter += 1 else: counter = 0 if counter == 3: - winner = column[i - 1] - return winner - return winner + return column[i - 1] + return False + -def checkIfFourInForwardDiagonal(column_matrix, player): - for i in range(0, len(column_matrix)): - for j in range(0, len(column_matrix[i])): +def check_four_in_forward_diagonal(column_matrix, player): + for i in range(len(column_matrix)): + for j in range(len(column_matrix[i])): + if j - 3 < 0: + continue try: - if column_matrix[i][j] == player and column_matrix[i + 1][j - 1] == player and column_matrix[i + 2][j - 2] == player and column_matrix[i + 3][j - 3] == player: + if ( + column_matrix[i][j] == player + and column_matrix[i + 1][j - 1] == player + and column_matrix[i + 2][j - 2] == player + and column_matrix[i + 3][j - 3] == player + ): return True except IndexError: - next - + continue return False -def checkIfFourInBackwardDiagonal(column_matrix, player): - for i in range(0, len(column_matrix)): - for j in range(0, len(column_matrix[i])): +def check_four_in_backward_diagonal(column_matrix, player): + for i in range(len(column_matrix)): + for j in range(len(column_matrix[i])): try: - if column_matrix[i][j] == player and column_matrix[i + 1][j + 1] == player and column_matrix[i + 2][j + 2] == player and column_matrix[i + 3][j + 3] == player: + if ( + column_matrix[i][j] == player + and column_matrix[i + 1][j + 1] == player + and column_matrix[i + 2][j + 2] == player + and column_matrix[i + 3][j + 3] == player + ): return True except IndexError: - next + continue return False -def isValidMove(column_number): - if column_number >=1 and column_number <=7: - return True - else: - return False -def createColumnMatrix(): - column_matrix = [[" "," ", " "," "," "," "," "],[" "," ", " "," "," "," "," "],[" "," ", " "," "," "," "," "],[" "," ", " "," "," "," "," "],[" "," ", " "," "," "," "," "],[" "," ", " "," "," "," "," "],[" "," ", " "," "," "," "," "]] - for i in range(7): - for j in range(len(CurrentField[i])): - column_matrix[j][i] = CurrentField[i][j] +def is_valid_move(column_number): + return 1 <= column_number <= 7 + +def create_column_matrix(current_field): + column_matrix = [[" " for _ in range(7)] for _ in range(7)] + for i in range(7): + for j in range(len(current_field[i])): + column_matrix[j][i] = current_field[i][j] return column_matrix - -def startConnect4(): + + +def start_connect4(): + current_field = create_empty_board() player = 1 no_win = True winner = "" - while(no_win): - ask_column = colored('Player ' + str(player) + ' turn, input column number:\n', "green",attrs=["bold"]) - column_number = input(ask_column) - if column_number: - column_number = int(column_number) - if isValidMove(column_number) == False: - cprint('Please input correct move.\n', 'red', attrs=['bold']) - else: - updated_flag = updateBoard(column_number - 1, player) - if updated_flag: - print("") - current_player = player - tile = "X" if player == 1 else "O" - player = 2 if player == 1 else 1 - winner = checkIfFourInRow() - if winner: - no_win = False - else: - column_matrix = createColumnMatrix() - winner = checkIfFourInColumn(column_matrix) - if winner: - no_win = False - elif checkIfFourInBackwardDiagonal(column_matrix, tile): - winner = current_player - no_win = False - elif checkIfFourInForwardDiagonal(column_matrix, tile): - winner = current_player - no_win = False - else: - cprint('Please input correct move.\n', 'red', attrs=['bold']) - else: - cprint('Please input correct move.\n', 'red', attrs=['bold']) - if winner == "X": + print("Starting new game!\n") + draw_board(current_field) + + while no_win: + ask_column = colored( + "Player " + str(player) + " turn, input column number:\n", + "green", + attrs=["bold"], + ) + column_input = input(ask_column) + + if not column_input: + cprint("Please input correct move.\n", "red", attrs=["bold"]) + continue + + try: + column_number = int(column_input) + except ValueError: + cprint("Please input a number between 1 and 7.\n", "red", attrs=["bold"]) + continue + + if not is_valid_move(column_number): + cprint("Please input correct move.\n", "red", attrs=["bold"]) + continue + + updated_flag = update_board(current_field, column_number - 1, player) + if not updated_flag: + cprint("That column is full. Please choose another.\n", "red", attrs=["bold"]) + continue + + current_player = player + tile = "X" if player == 1 else "O" + player = 2 if player == 1 else 1 + + winner = check_four_in_row(current_field) + if not winner: + column_matrix = create_column_matrix(current_field) + winner = check_four_in_column(column_matrix) + if not winner: + if check_four_in_backward_diagonal(column_matrix, tile): + winner = current_player + elif check_four_in_forward_diagonal(column_matrix, tile): + winner = current_player + + if winner: + no_win = False + elif is_board_full(current_field): + cprint("It's a draw! The board is full.\n", "yellow", attrs=["bold"]) + return + + if winner == "X" or winner == 1: winner = "1" else: winner = "2" - cprint('THE WINNER IS PLAYER '+ str(winner), 'green', attrs=['bold']) - + cprint("THE WINNER IS PLAYER " + str(winner), "green", attrs=["bold"]) -print('Starting new game!\n') -draw_board(CurrentField) -startConnect4() +# Start the game +start_connect4() -// worked on by samson \ No newline at end of file +# worked on by samson \ No newline at end of file From 1a248ad61e0331a85f7c8d63efaa9f89f37a3310 Mon Sep 17 00:00:00 2001 From: samwata Date: Wed, 11 Mar 2026 12:44:05 +0300 Subject: [PATCH 2/3] Update the Tic Tac game Signed-off-by: samwata --- I-O/Tic-tac-toe-partA.py | 126 ++++++++++++++++++++++++++------------- 1 file changed, 86 insertions(+), 40 deletions(-) diff --git a/I-O/Tic-tac-toe-partA.py b/I-O/Tic-tac-toe-partA.py index 0e1a779..ffa5eb0 100644 --- a/I-O/Tic-tac-toe-partA.py +++ b/I-O/Tic-tac-toe-partA.py @@ -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() \ No newline at end of file From 1b18d90bd4752817df9e4a2150c00bab31e4ab1c Mon Sep 17 00:00:00 2001 From: samwata Date: Wed, 11 Mar 2026 12:53:25 +0300 Subject: [PATCH 3/3] Update the Tic Tac toe game Signed-off-by: samwata --- I-O/Tic-Tac-Toe_partB.py | 121 ++++++++++++++++++++++++++------------- 1 file changed, 82 insertions(+), 39 deletions(-) diff --git a/I-O/Tic-Tac-Toe_partB.py b/I-O/Tic-Tac-Toe_partB.py index c982c47..5788bb0 100644 --- a/I-O/Tic-Tac-Toe_partB.py +++ b/I-O/Tic-Tac-Toe_partB.py @@ -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() \ No newline at end of file