Skip to content

Commit 1f67e7b

Browse files
Merge pull request #2627 from Shikhar9425/master-4
Puzzle.py
2 parents 4c8daab + 8106c27 commit 1f67e7b

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

Door Puzzle AI/Puzzle.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import random
2+
3+
def generate_doors(num_doors):
4+
doors = [False] * num_doors
5+
prize_door = random.randint(0, num_doors - 1)
6+
doors[prize_door] = True
7+
return doors
8+
9+
def choose_door(num_doors):
10+
return random.randint(0, num_doors - 1)
11+
12+
def open_door(doors, chosen_door):
13+
reveal_door = random.choice([i for i in range(len(doors)) if i != chosen_door and not doors[i]])
14+
print(f"Door {reveal_door + 1} has no prize.")
15+
16+
def switch_door(chosen_door, num_doors):
17+
return random.choice([i for i in range(num_doors) if i != chosen_door])
18+
19+
def main():
20+
num_doors = 3
21+
doors = generate_doors(num_doors)
22+
23+
print("Welcome to the Door Puzzle AI!")
24+
print("There are three doors. Behind one of the doors is a prize.")
25+
print("You need to choose a door, and then we will reveal one door that does not have the prize.")
26+
print("You will then have the option to switch your chosen door or stick with the current choice.")
27+
print("Let's get started!")
28+
29+
chosen_door = choose_door(num_doors)
30+
print(f"\nYou have chosen Door {chosen_door + 1}.")
31+
32+
open_door(doors, chosen_door)
33+
34+
switch_choice = input("Do you want to switch your chosen door? (yes/no): ").lower()
35+
36+
if switch_choice == "yes":
37+
chosen_door = switch_door(chosen_door, num_doors)
38+
39+
if doors[chosen_door]:
40+
print("Congratulations! You have won the prize!")
41+
else:
42+
print("Sorry, you did not win this time. Better luck next time!")
43+
44+
if __name__ == "__main__":
45+
main()

Door Puzzle AI/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Package/Script Name: Door Puzzle AI
2+
3+
Short description of package/script: This is a simple Python script that allows the user to play a Door Puzzle game against an AI opponent. The user needs to choose one door out of three, and the AI will reveal a door without the prize. The user then has the option to switch their chosen door or stick with the current choice to win the prize.
4+
5+
Functionalities/Scripts:
6+
7+
generate_doors(num_doors): Generates a list of doors, where one door contains the prize (True) and the others don't (False).
8+
choose_door(num_doors): Allows the user to select a door number from 1 to the total number of doors.
9+
open_door(doors, chosen_door): Reveals a door that does not have the prize, except for the chosen door.
10+
switch_door(chosen_door, num_doors): Allows the user to switch their chosen door.
11+
Setup Instructions:
12+
13+
Make sure you have Python installed on your system.
14+
Download the Door Puzzle AI script from the provided source.
15+
Save the script in a directory of your choice.
16+
Explain how to set up and run your package/script on the user's system:
17+
18+
Open a terminal or command prompt.
19+
Navigate to the directory where you saved the Door Puzzle AI script using the cd command.
20+
Run the script using the command: python door_puzzle_ai.py
21+
The game will start, and the user will be prompted to choose one door out of three.
22+
After choosing, the AI will reveal one door that doesn't have the prize.
23+
The user will then have the option to switch their chosen door or stick with the current choice.
24+
The script will reveal whether the user has won the prize or not.
25+
Detailed Explanation:
26+
The script defines four functions to implement the Door Puzzle AI game. The generate_doors(num_doors) function generates a list of doors, where one door contains the prize (True) and the others don't (False). The choose_door(num_doors) function allows the user to select a door number from 1 to the total number of doors. The open_door(doors, chosen_door) function reveals a door that doesn't have the prize, except for the chosen door. The switch_door(chosen_door, num_doors) function allows the user to switch their chosen door.
27+
28+
Output:
29+
The output of the script will be displayed in the terminal or command prompt. After the user chooses a door and optionally switches their choice, the script will reveal whether the user has won the prize or not. The game outcome will be displayed in the terminal.
30+
31+
Author(s):
32+
Author: Shikhar9425

0 commit comments

Comments
 (0)