From fa86c19e6a7a742a00fbcc6ce3b0ec4ff4b655e7 Mon Sep 17 00:00:00 2001 From: SMRITI PRAJAPATI <143401846+Smriti-Prajapati@users.noreply.github.com> Date: Sun, 20 Oct 2024 23:16:14 +0530 Subject: [PATCH] Created Dice game.cpp The Dice Game in C++ allows two players to take turns rolling a dice for five rounds, with the player rolling the higher number winning each round. At the end of the game, the total scores are compared to determine the overall winner. --- Dice game.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Dice game.cpp diff --git a/Dice game.cpp b/Dice game.cpp new file mode 100644 index 0000000..bd26132 --- /dev/null +++ b/Dice game.cpp @@ -0,0 +1,64 @@ +#include +#include +#include + +using namespace std; + +// Function to roll a dice +int rollDice() { + return (rand() % 6) + 1; // Generate a random number between 1 and 6 +} + +// Function to play the game +void playGame() { + int player1Score = 0; + int player2Score = 0; + + cout << "Welcome to the Dice Game!" << endl; + cout << "Player 1 and Player 2 will take turns rolling a dice." << endl; + cout << "The player with the higher number wins the round." << endl; + + for (int i = 1; i <= 5; i++) { + cout << "\nRound " << i << ":" << endl; + + // Player 1's turn + cout << "Player 1 rolls the dice..." << endl; + int player1Roll = rollDice(); + cout << "Player 1 rolled a " << player1Roll << endl; + + // Player 2's turn + cout << "Player 2 rolls the dice..." << endl; + int player2Roll = rollDice(); + cout << "Player 2 rolled a " << player2Roll << endl; + + // Determine the winner of the round + if (player1Roll > player2Roll) { + cout << "Player 1 wins this round!" << endl; + player1Score++; + } else if (player2Roll > player1Roll) { + cout << "Player 2 wins this round!" << endl; + player2Score++; + } else { + cout << "It's a tie!" << endl; + } + + cout << "Score - Player 1: " << player1Score << ", Player 2: " << player2Score << endl; + } + + // Determine the overall winner + if (player1Score > player2Score) { + cout << "\nPlayer 1 wins the game!" << endl; + } else if (player2Score > player1Score) { + cout << "\nPlayer 2 wins the game!" << endl; + } else { + cout << "\nIt's a tie game!" << endl; + } +} + +int main() { + srand(time(0)); // Seed the random number generator + + playGame(); + + return 0; +}