Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am working on the game of war programming with c++. I was in the middle of the

ID: 3549865 • Letter: I

Question

I am working on the game of war programming with c++.

I was in the middle of the last step of making the game process but I just found out through cout that the random numbers assigned were not as I expected them to be.

For one deck, I am asking for random integer from 0 to 52 and assign half to each player. Then, for two decks, I do the same process twice and so on.

How can I make sure that random number is from 0 to 52 without any overlapping numbers for each deck???

I'm getting random integers from fucntion shuffle(), and destributing them to each player in fuction playcard().


.#include <iostream>

.#include <stdlib.h>

.#include <string>

.

.using namespace std;

.

.

.int deck_num(){

. int numin;

. cout << "How many decks are being used? :";                                  //prompting number of decks

. cin >> numin;

. while (cin.fail() || numin < 1) {

. cin.clear();

. cin.ignore(100, ' ');

. cout << "Please enter a natural number greater than 0:";

. cin >> numin;

. }

. return numin;

.}

.

.

.int watch_stat() {

. string watch;                                                                           //prompting watch status

. cout << "Would you like to watch the game? (Yes or No) :";

. cin >> watch;

. while (watch != "Yes" && watch != "yes" && watch != "No" && watch != "no") {

. cin.clear();

. cin.ignore(100, ' ');

. cout << "Please enter only Yes or No :";

. cin >> watch;

. }

. int watchreturn;

. if (watch == "Yes" || watch == "yes") {

. watchreturn = 1;

. }

. else {

. watchreturn = 0;

. }

. return watchreturn;

.}

.

.

.int shuffle() {

. int card = rand() %52;

. return card;

.}

.

.

.int playcard(int num, int show) {

. int player1[num*52-1];

. int player2[num*52-1];

. for (int counter = 0; counter < num; counter++) {                 //shuffling and destributing

. for (int scounter = 0; scounter < 52; scounter++) {

. if (scounter <= 20) {

. player1[scounter] = shuffle();

. }

. else {

. player2[scounter] = shuffle();

. }

. }

. }

. for (int j=0; j<(num*52-1); j++) {

. cout << player1[j] << endl;

. cout << player2[j] << endl;                                             //Output where I found out that random integers are not from 0 to 52

. }

. int cardnum1 = 26*num;

. int cardnum2 = 26*num;

. if (show == 1) {

. while (cardnum1 >= 0 && cardnum2 >= 0) {

. cout << "Player 1 now has " << cardnum1 << " cards." << endl;

. cout << "Player 2 now has " << cardnum2 << " cards." << " " << endl;

. cardnum2--;

.

.

.

. }                 //******************************************************************** Part not done yet

. }

. cout << endl << "--------------------------" << endl << endl;

. if (cardnum1 == -1) {

. cout << "Player 2 WINS the game!" << endl << endl;

. }

. if (cardnum2 == -1) {

. cout << "Player 1 WINS the game!" << endl << endl;

. }

. cout << "--------------------------" << endl;

. return 1;

.}

.

.

.int main() {

. int decknum = deck_num();

. int showornot = watch_stat();

. int deck[51] = {};

. for (int i=0; i<51; i++) {

. deck[i] = i+1;

. }

. playcard(decknum, showornot);

.}

.

.

.

.







Explanation / Answer

To shuffle accurately and to stop overlapping in rand operation just do following steps you will get your best shuffle

1) Include a header file #include<time.h>

2) In shuffle function, add following add before int card = rand() %52;

srand(time(NULL)); // as first line of shuffle function.


Now run program you will always get exactly randoms numbers.



Thanks.Please Rate

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote