diff --git a/Snake_and_Ladder Game/Snake_and_ladder.py b/Snake_and_Ladder Game/Snake_and_ladder.py new file mode 100644 index 0000000..5ab0b15 --- /dev/null +++ b/Snake_and_Ladder Game/Snake_and_ladder.py @@ -0,0 +1,105 @@ +import random +import time +import os + +def clear_screen(): + """Clears the console screen.""" + os.system('cls' if os.name == 'nt' else 'clear') + +def draw_board(player1_pos, player2_pos, snakes, ladders): + """Draws the game board in the terminal.""" + clear_screen() + print("--- Snake and Ladder ---") + + board_map = {i: f"{i: >4}" for i in range(1, 101)} + + for start in snakes: + board_map[start] = " 🐍 " + for start in ladders: + board_map[start] = " πŸͺœ " + + if player1_pos == player2_pos and player1_pos != 0: + board_map[player1_pos] = "πŸ§›β€β™€οΈπŸ¦Έβ€β™‚οΈ" + else: + if player1_pos != 0: + board_map[player1_pos] = " πŸ§›β€β™€οΈ " + if player2_pos != 0: + board_map[player2_pos] = " πŸ¦Έβ€β™‚οΈ " + + for i in range(10, 0, -1): + row = "" + for j in range(1, 11): + cell_num = (i - 1) * 10 + j + if i % 2 == 0: + cell_num = (i * 10) - (j - 1) + + row += f"|{board_map[cell_num]}" + print(row + "|") + print("-" * 51) + print("πŸ§›β€β™€οΈ: Player 1 | πŸ¦Έβ€β™‚οΈ: Player 2") + print("🐍: Snake Head | πŸͺœ: Ladder Bottom") + # Added details for each snake and ladder + if snakes: + snake_details = " ".join([f"🐍 {head}->{tail} (-{head-tail})" for head, tail in sorted(snakes.items())]) + print(snake_details) + if ladders: + ladder_details = " ".join([f"πŸͺœ {bottom}->{top} (+{top-bottom})" for bottom, top in sorted(ladders.items())]) + print(ladder_details) + print("-" * 51) + + +def roll_dice(): + """Rolls a six-sided die.""" + return random.randint(1, 6) + +def main(): + """Main function to run the Snake and Ladder game.""" + snakes = { + 17: 7, 54: 34, 62: 19, 64: 60, + 87: 24, 93: 73, 95: 75, 99: 78 + } + ladders = { + 4: 14, 9: 31, 20: 38, 28: 84, + 40: 59, 51: 67, 63: 81, 71: 91 + } + + player_positions = {'Player 1': 0, 'Player 2': 0} + current_player = 'Player 1' + + while True: + draw_board(player_positions['Player 1'], player_positions['Player 2'], snakes, ladders) + + print(f"\n{current_player}'s turn.") + input("Press Enter to roll the dice...") + + dice_roll = roll_dice() + print(f"{current_player} rolled a {dice_roll}.") + + current_pos = player_positions[current_player] + new_pos = current_pos + dice_roll + + if new_pos > 100: + print("Overshot! You need to land exactly on 100.") + new_pos = current_pos + elif new_pos in snakes: + print("Oh no! Landed on a snake.") + new_pos = snakes[new_pos] + elif new_pos in ladders: + print("Wow! Found a ladder.") + new_pos = ladders[new_pos] + + player_positions[current_player] = new_pos + + draw_board(player_positions['Player 1'], player_positions['Player 2'], snakes, ladders) + print(f"\n{current_player} is now at position {new_pos}.") + time.sleep(2) + + if new_pos == 100: + print(f"\nCongratulations! {current_player} wins!") + break + + # Switch player + current_player = 'Player 2' if current_player == 'Player 1' else 'Player 1' + +if __name__ == "__main__": + main() diff --git a/Tic Tac Toe/Tic-Tac-Toe.py b/Tic Tac Toe/Tic-Tac-Toe.py new file mode 100644 index 0000000..d9ec6bc --- /dev/null +++ b/Tic Tac Toe/Tic-Tac-Toe.py @@ -0,0 +1,131 @@ +import random +import os +import time + +def clear_screen(): + os.system('cls' if os.name == 'nt' else 'clear') + +def board(spaces): + clear_screen() + print(f" {spaces[0]} | {spaces[1]} | {spaces[2]} ") + print("___|___|___") + print(" | | ") + print(f" {spaces[3]} | {spaces[4]} | {spaces[5]} ") + print("___|___|___") + print(" | | ") + print(f" {spaces[6]} | {spaces[7]} | {spaces[8]} ") + print() + +def player_move(spaces, player_symbol, player_name): + while True: + try: + move = int(input(f"{player_name} ({player_symbol}), enter a number (1-9): ")) + if 1 <= move <= 9: + if spaces[move - 1] == ' ': + spaces[move - 1] = player_symbol + break + else: + print("That space is already taken. Try again.") + else: + print("Invalid input. Please enter a number between 1 and 9.") + except ValueError: + print("Invalid input. Please enter a number.") + +def computer_move(spaces, computer_symbol): + print("Computer is thinking...") + time.sleep(1) + + empty_indices = [i for i, space in enumerate(spaces) if space == ' '] + if empty_indices: + move = random.choice(empty_indices) + spaces[move] = computer_symbol + +def check_win(spaces): + win_conditions = [ + [0, 1, 2], [3, 4, 5], [6, 7, 8], + [0, 3, 6], [1, 4, 7], [2, 5, 8], + [0, 4, 8], [2, 4, 6] + ] + for condition in win_conditions: + if spaces[condition[0]] == spaces[condition[1]] == spaces[condition[2]] != ' ': + return spaces[condition[0]] + return None + +def check_tie(spaces): + return ' ' not in spaces + +def play_game(): + while True: + spaces = [' '] * 9 + player1_symbol = 'X' + player2_symbol = 'O' + computer_symbol = 'O' + + choice = '' + while choice not in ['1', '2']: + clear_screen() + print("Welcome to Tic-Tac-Toe!") + choice = input("Press 1 to play with another player.\nPress 2 to play with the computer.\nEnter your choice: ") + + if choice == '1': + current_player_symbol = player1_symbol + current_player_name = "Player 1" + while True: + board(spaces) + player_move(spaces, current_player_symbol, current_player_name) + + winner = check_win(spaces) + if winner: + board(spaces) + print(f"Congratulations {current_player_name}, you won!") + break + + if check_tie(spaces): + board(spaces) + print("It's a tie!") + break + + if current_player_symbol == player1_symbol: + current_player_symbol = player2_symbol + current_player_name = "Player 2" + else: + current_player_symbol = player1_symbol + current_player_name = "Player 1" + + else: + while True: + board(spaces) + player_move(spaces, player1_symbol, "Player 1") + + winner = check_win(spaces) + if winner: + board(spaces) + print("Congratulations, you won!") + break + + if check_tie(spaces): + board(spaces) + print("It's a tie!") + break + + board(spaces) + computer_move(spaces, computer_symbol) + + winner = check_win(spaces) + if winner: + board(spaces) + print("You lose! The computer has won.") + break + + if check_tie(spaces): + board(spaces) + print("It's a tie!") + break + + play_again = input("Do you want to play again? (Y/N): ").upper() + if play_again != 'Y': + print("Thanks for playing!") + break + +if __name__ == "__main__": + play_game()