Skip to content

Commit ee6d569

Browse files
Merge pull request #2615 from Shikhar9425/master-1
Create Dice_Rolling.py
2 parents d0ee849 + 8e1f59e commit ee6d569

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

Dice Rolling Game/Dice_Rolling.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import random
2+
3+
def roll_dice():
4+
return random.randint(1, 6)
5+
6+
def ai_roll():
7+
return roll_dice()
8+
9+
def main():
10+
print("Welcome to Dice Rolling AI!")
11+
12+
while True:
13+
user_input = input("Press 'Enter' to roll the dice or 'q' to quit: ")
14+
if user_input.lower() == 'q':
15+
print("Thanks for playing!")
16+
break
17+
18+
user_roll = roll_dice()
19+
ai_roll_result = ai_roll()
20+
21+
print(f"You rolled: {user_roll}")
22+
print(f"AI rolled: {ai_roll_result}")
23+
24+
if user_roll > ai_roll_result:
25+
print("Congratulations! You won!")
26+
elif user_roll < ai_roll_result:
27+
print("AI wins! Better luck next time.")
28+
else:
29+
print("It's a tie!")
30+
31+
if __name__ == "__main__":
32+
main()

Dice Rolling Game/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Package/Script Name: Dice Rolling AI
2+
3+
Short description of package/script: This is a simple Python script that allows the user to play a Dice Rolling game against an AI opponent. It simulates rolling a six-sided dice for both the user and the AI and determines the winner based on the rolled values.
4+
5+
Functionalities/Scripts:
6+
7+
roll_dice(): Simulates rolling a six-sided dice and returns the result.
8+
ai_roll(): Simulates the AI rolling a dice with a delay to create a more interactive experience.
9+
main(): Implements the game loop, allowing the user to play the Dice Rolling AI game.
10+
Setup Instructions:
11+
12+
Make sure you have Python installed on your system.
13+
Download the Dice Rolling AI script from the provided source.
14+
Save the script in a directory of your choice.
15+
Explain how to setup and run your package/script in the user's system:
16+
17+
Open a terminal or command prompt.
18+
Navigate to the directory where you saved the Dice Rolling AI script using the cd command.
19+
Run the script using the command: python dice_rolling_ai.py
20+
The game will start, and you can play against the AI by pressing 'Enter' to roll the dice. To quit the game, press 'q' and hit 'Enter'.
21+
Detailed Explanation:
22+
The script defines two functions, roll_dice() and ai_roll(), to simulate rolling the dice. The roll_dice() function generates a random number between 1 and 6 using the random.randint() function. The ai_roll() function uses the roll_dice() function and adds a delay of 1 second using time.sleep(1) to simulate the AI's processing time before revealing its result.
23+
24+
In the main() function, a while loop is used to create a game loop. The user is prompted to press 'Enter' to roll the dice or 'q' to quit the game. The dice is rolled for both the user and the AI using roll_dice() and ai_roll() functions, respectively. The results are then displayed, and the winner is determined based on the rolled values.
25+
26+
Output:
27+
The output of the script will be displayed in the terminal or command prompt. It will show the dice roll results for both the user and the AI and announce the winner of each round. The game continues until the user decides to quit by pressing 'q'. The AI's roll will be accompanied by a delay to simulate its processing time before displaying the result.
28+
29+
Author(s):
30+
Author: Shikhar9425
31+
32+
33+
34+

0 commit comments

Comments
 (0)