//I have finished writing the program but it does not execute properly. It promo
ID: 3550011 • Letter: #
Question
//I have finished writing the program but it does not execute properly. It promopts for number of decks to play with and whether I wish to watch the process of game or not, but it does not continue to execute afterwards. In the main function, I tried cout for the deck I created but it does not show. I think that is where the problem begins. I hope someone can find the problem and fix it for me. Also, I would appreciate it if the program is debugged all the way through. This is my first semester of Computer Science and I have used all I have learned. So, please do not add anything beyond what is in the program and just check logics and algorithms. Thank you!
#include <iostream>
#include <stdlib.h>
#include <string>
#include <time.h>
using namespace std;
int deck_num();
int watch_stat();
int shuffle(int , int , int);
void playcard(int, int, int, int);
bool warornot(int, int);
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;
int watchreturn;
while (true) {
if (watch.compare("Yes") == 0 || watch.compare("yes") == 0) {
watchreturn = 1;
break;
}
else if(watch.compare("No") == 0 || watch.compare("no") == 0) {
watchreturn = 0;
break;
}
cin.clear();
cin.ignore(100, ' ');
cout << "Please enter only Yes or No :";
cin >> watch;
}
return watchreturn;
}
void playcard(int arr1[], int arr2[], int show, int num) {
string cardshapes[4] = {"Spades", "Hearts", "Clubs", "Diamonds"};
string cardtypes[13] = {"2", "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "Jack" , "Queen" , "King" , "Ace"};
int cardnum1 = 26*num;
int cardnum2 = 26*num;
int hand = 0;
int gain;
int warcount = 0;
int p1win = 0;
int p2win = 0;
int p1winwar = 0;
int p2winwar = 0;
if (show == 1) { //watch status "yes"
while (cardnum1 >= 0 && cardnum2 >= 0) {
cout << "Player 1 now has " << cardnum1 << " cards." << endl;
cout << "Player 2 now has " << cardnum2 << " cards." << endl << endl;
hand ++;
cout << "Hand #" << hand << endl;
cout << "Player 1: ";
int whatcard1 = arr1[26*num-1]%100;
cout << cardtypes[whatcard1];
int shape1 = arr1[cardnum1]-(arr1[cardnum1]%100);
if (shape1 == 0) {
cout << " of " << cardshapes[0] << endl;
}
else if (shape1 == 1) {
cout << " of " << cardshapes[1] << endl;
}
else if (shape1 == 2) {
cout << " of " << cardshapes[2] << endl;
}
else {
cout << " of " << cardshapes[3] << endl;
}
cout << "Player 2: ";
int whatcard2 = arr2[26*num-1]%100;
cout << cardtypes[whatcard2];
int shape2 = arr2[cardnum1]-(arr2[cardnum1]%100);
if (shape2 == 0) {
cout << " of " << cardshapes[0] << endl;
}
else if (shape2 == 1) {
cout << " of " << cardshapes[1] << endl;
}
else if (shape2 == 2) {
cout << " of " << cardshapes[2] << endl;
}
else {
cout << " of " << cardshapes[3] << endl;
}
int warinarow = 1;
do {
int wardeck[26*num];
while (cardnum1 >= 4 && cardnum2 >= 4) {
cout << endl << "WAR!" << endl << endl;
cout << "Player 1 places 3 cards down." << endl;
cout << "Player 2 places 3 cards down." << endl << endl;
for (int q = 1+(warinarow*4); q < 4+(warinarow*4); q++) {
wardeck[q] = arr1[cardnum1 - q];
wardeck[26*num - q] = arr2[cardnum2 - q];
}
if (arr1[cardnum1-4*warinarow] > arr2[cardnum2-4*warinarow]) {
cout << "Player 1 wins." << endl << endl;
cardnum1 = cardnum1 + 4;
cardnum2 = cardnum2 - 4;
for (gain = 0; gain < 4*warinarow; gain++) {
arr1[gain + cardnum1] = wardeck[cardnum2 - gain];
arr2[cardnum2 - gain] = -1;
}
p1win++;
warcount++;
p1winwar++;
}
if (arr2[cardnum2-4*warinarow] > arr1[cardnum1-4*warinarow]) {
cout << "Player 2 wins." << endl << endl;
cardnum2 = cardnum2 + 4;
cardnum1 = cardnum1 - 4;
for (gain = 0; gain < 4*warinarow; gain++) {
arr2[gain + cardnum2] = wardeck[cardnum1 - gain];
arr1[cardnum1 - gain] = -1;
}
p2win++;
warcount;
p2winwar++;
}
else {
warinarow++;
}
}
} while (!warornot(whatcard1, whatcard2) == false);
if (arr1[cardnum1] > arr2[cardnum2]) {
cout << "Player 1 wins." << endl << endl;
cardnum1 = cardnum1 + 1;
cardnum2 = cardnum2 - 1;
arr1[gain + cardnum1] = arr2[cardnum2 - gain];
arr2[cardnum2 - gain] = -1;
p1win++;
}
if (arr2[cardnum2] > arr1[cardnum1]) {
cout << "Player 2 wins." << endl << endl;
cardnum2 = cardnum2 + 1;
cardnum1 = cardnum1 - 1;
arr2[cardnum2] = arr1[cardnum1];
arr1[cardnum1] = -1;
p2win++;
}
cout << endl << "--------------------------" << endl;
}
}
if (show == 0) { //watch status "no"
while (cardnum1 >= 0 && cardnum2 >= 0) {
int whatcard1 = arr1[26*num-1]%100;
int whatcard2 = arr2[26*num-1]%100;
hand++;
int warinarow = 1;
do {
int wardeck[26*num];
while (cardnum1 > 4 && cardnum2 > 4) {
for (int q = 1+(warinarow*4); q < 4+(warinarow*4); q++) {
wardeck[q] = arr1[cardnum1 - q];
wardeck[26*num - q] = arr2[cardnum2 - q];
}
if (arr1[cardnum1-4*warinarow] > arr2[cardnum2-4*warinarow]) {
cardnum1 = cardnum1 + 4;
cardnum2 = cardnum2 - 4;
for (gain = 0; gain < 4*warinarow; gain++) {
arr1[gain + cardnum1] = wardeck[cardnum2 - gain];
arr2[cardnum2 - gain] = -1;
}
p1win++;
warcount++;
p1winwar++;
}
if (arr2[cardnum2-4*warinarow] > arr1[cardnum1-4*warinarow]) {
cardnum2 = cardnum2 + 4;
cardnum1 = cardnum1 - 4;
for (gain = 0; gain < 4*warinarow; gain++) {
arr2[gain + cardnum2] = wardeck[cardnum1 - gain];
arr1[cardnum1 - gain] = -1;
}
p2win++;
warcount++;
p2winwar++;
}
else {
warinarow++;
}
}
} while (!warornot(whatcard1, whatcard2) == false);
if (arr1[cardnum1] > arr2[cardnum2]) {
cardnum1 = cardnum1 + 1;
cardnum2 = cardnum2 - 1;
arr1[cardnum1] = arr2[cardnum2];
arr2[cardnum2] = -1;
p1win++;
}
if (arr2[cardnum2] > arr1[cardnum1]) {
cardnum2 = cardnum2 + 1;
cardnum1 = cardnum1 - 1;
arr2[cardnum2] = arr1[cardnum1];
arr1[cardnum1] = -1;
p2win++;
}
}
}
if (cardnum1 < cardnum2) { //game ends
cout << "Player 2 WINS the game!" << endl << endl;
}
else {
cout << "Player 1 WINS the game!" << endl << endl;
}
cout << "--------------------------" << endl << endl;
float p1winper = (p1win/hand)*100;
float p2winper = (p2win/hand)*100;
float warper = (warcount/hand)*100;
float p1winwarper = (p1winwar/warcount)*100;
float p2winwarper = (p2winwar/warcount)*100;
cout << "Statistics" << endl << endl;
cout << "Number of Hands: " << hand << endl;
cout << "Number of Hands Player 1 Won: " << p1win << "(" << p1winper << ")" << endl;
cout << "Number of Hands Player 2 Won: " << p2win << "(" << p2winper << ")" << endl;
cout << "Number of War Hands: " << warcount << endl << "(" << warper << ")" << endl;
cout << "Number of War Hands Player 1 Won: " << p1winwar << "(" << p1winwarper << ")" << endl;
cout << "Number of War Hands Player 2 Won: " << p2winwar << "(" << p2winwarper << ")" << endl;
return;
}
bool warornot(int card1, int card2) {
if (card1 == card2) {
return false;
}
return true;
}
int shuffle(int arr[], int num, int show) {
srand(time(NULL));
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++) {
while (scounter < 26) {
player1[scounter+(26*counter)] = arr[rand()&52];
}
player2[scounter+(26*counter)] = arr[rand()%52];
}
}
playcard(player1, player2, show, num);
return 1;
}
int main() {
int decknum = deck_num(); //begin
int showornot = watch_stat();
int deck[51] = {};
cout << deck;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 13; j++) {
deck[(13*i)+j] = (i*100)+j;
}
}
cout << deck;
shuffle(deck, decknum, showornot);
return 1;
}
Explanation / Answer
The following is quoted verbatim from a Wikipedia page:
The deck is divided evenly among the two players, giving each a face-down stack. In unison, each player reveals the top card on his stack (a "battle"), and the player with the higher card takes both the cards played and moves them to the bottom of his stack. If the two cards played are of equal value, each player lays down three face-down cards and a fourth card face-up (a "war"), and the higher-valued card wins all of the cards on the table, which are then added to the bottom of the player's stack. In the case of another tie, the war process is repeated until there is no tie.
A player wins by collecting all the cards. If a player runs out of cards while dealing the face-down cards of a war, he may play the last card in his deck as his face-up card and still have a chance to stay in the game.
ASSIGNMENT SPECIFICATIONS
Arrays will be required in this assignment -- it would be nearly impossible to make a readable program without them.
Arrays may serve in various different ways:
* A deck of cards may be represented as 52 distinct values in an array. The precise meaning and interpretation of each value is not specified in this assignment, but the 52 cards should be distinguishable. Rank is necessary for the game; suit is not, but may provide some color.
The values representing the cards could simply be integers. 0 to 12 (or 1 to 13) could represent 13 ranks of cards; or 0 to 51 could represent 52 diffderent possible cards.
* Each player has a stack, which would best be represented by an array.
* Battle piles representing the cards currently in competition, can also be represented as arrays -- especially in the cases of a war where the winner is not yet known. Those cards would have no known owner until the battle is resolved.
* Arrays may be assist in interpreting values in user-friendly ways -- e.g. translating some integer subscript to the name of a rank "King" or "Two". The subscript itself would be obtained from the numeric value used to stand for the card (as in the first bullet point above).
http://mhelpdesk.com/ is a source one can attain a good reliable knowledge from.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.