#include #include #include #include #include using namespace std; // constant to specify the size of the deck to use const int DECK_SIZE = 20; // our deck will have random cards with values between MIN_CARD and MAX_CARD (inclusive) const int MIN_CARD = 1; const int MAX_CARD = 13; // losing messages const string YOU_LOSE_MESSAGE = "You lose, foolish human! I am number 1! You are number 10!"; const string COMPUTER_LOSE_MESSAGE = "Noooooo! I cannot lose! ARRgrhrghrhrjGRGrghjRGRJGRG! ... How about a nice game of chess?"; // prototypes for all the functions below // play one entire game void playWar(); // array functions, not specific to this game void arrayFill(int array[], int min, int max, int n); void arrayShuffle(int array[], int n); void arraySplit(int array[], int n, int a1[], int &a1_n, int a2[], int &a2_n); int arrayPop(int array[], int &n); void arrayTransfer(int src[], int &src_n, int dest[], int &dest_n); // useful random function int random(int min, int max); // "game logic" functions - how the game works void showCards(int hand_one_size, int hand_two_size, int discard_one_size, int discard_two_size); void resolvePlay(int card_one, int card_two, int discard_one[], int &discard_one_size, int discard_two[], int &discard_two_size); bool checkForLoss(int hand[], int &hand_size, int discard[], int &discard_size, string lose_message); int main() { // Seed the random number generator srand(static_cast(time(NULL))); // decompose the problem! playing multiple games is just a loop around playing one game cout << "Welcome to War. PREPARE TO DIE." << endl; cout << endl; char again = 'y'; while (again == 'y' || again == 'Y') { playWar(); cout << endl; cout << "Play again? (y/n): "; cin >> again; } cout << "Have a nice dead. Day." << endl; system("Pause"); } void playWar() { // allocate space for a deck, two hands and two discard piles // for simplicity, our deck will have four 1s, four 2s, four 3s, etc. int deck[DECK_SIZE]; int hand_one[DECK_SIZE] = { 0 }; // A hand of cards, with all elements initialized to zero int hand_one_size = 0; int hand_two[DECK_SIZE] = { 0 }; // A hand of cards, with all elements initialized to zero int hand_two_size = 0; int discard_one[DECK_SIZE] = { 0 }; // A pile of cards, with all elements initialized to zero int discard_one_size = 0; int discard_two[DECK_SIZE] = { 0 }; // A pile of cards, with all elements initialized to zero int discard_two_size = 0; string response; int card_one, card_two; // fill an array with cards, shuffle and split into the two hands arrayFill(deck, DECK_SIZE, MIN_CARD, MAX_CARD); arrayShuffle(deck, DECK_SIZE); arraySplit(deck, DECK_SIZE, hand_one, hand_one_size, hand_two, hand_two_size); while (true) { // play out a turn // get a card from each hand card_one = arrayPop(hand_one, hand_one_size); card_two = arrayPop(hand_two, hand_two_size); cout << "-------------------------------------------" << endl; cout << "Human plays: " << card_one << endl; cout << "Computer plays: " << card_two << endl; cout << "-----" << endl; // figure out who won the turn and put the cards in the right discard resolvePlay(card_one, card_two, discard_one, discard_one_size, discard_two, discard_two_size); // show the player what cards are left showCards(hand_one_size, hand_two_size, discard_one_size, discard_two_size); if (checkForLoss(hand_one, hand_one_size, discard_one, discard_one_size, YOU_LOSE_MESSAGE) || checkForLoss(hand_two, hand_two_size, discard_two, discard_two_size, COMPUTER_LOSE_MESSAGE)) { // somebody lost, game is over break; } // pause at the end of each turn cout << "Press return to continue. "; getline(cin, response); cout << endl; } } // fill the array with n random numbers between min and max (including min and max) // (you can use the random function I gave you above) void arrayFill(int array[], int n, int min, int max) { } // return a random number between min and max (not including max) int random(int min, int max) { return min + (rand() % (max - min)); } // shuffle the values in the array (reorder them randomly) // you'll need the random function above void arrayShuffle(int array[], int n) { } // split the values in array into a1 and a2 (first value into a1, second into a2, third into a1 and so on) // a1_n and a2_n need to be updated with the resulting lengths of a1 and a2 void arraySplit(int array[], int n, int a1[], int &a1_n, int a2[], int &a2_n) { } // add new_value to the end of array, and update n with the new length void arrayAppend(int new_value, int array[], int &n) { } // "remove" and return the first (index 0) value from the array // you'll need to shift all the other values over one spot, and update n int arrayPop(int array[], int &n) { return 0; // stub so that it compiles } // copy all the values from src to desc, then "remove" all values from dest // don't forget to update src_n and dest_n void arrayTransfer(int src[], int &src_n, int dest[], int &dest_n) { } // show the player where all the cards are (complete) void showCards(int hand_one_size, int hand_two_size, int discard_one_size, int discard_two_size) { cout << "-----" << endl; cout << "Human: " << hand_one_size << " cards in hand (" << discard_one_size << " in winnings pile)" << endl; cout << "Computer: " << hand_two_size << " cards in hand (" << discard_two_size << " in winnings pile)" << endl; cout << "-----" << endl; } // figure out who won the round, and put the two cards into the correct discard pile // don't forget to update the size of the two piles // (incomplete) void resolvePlay(int card_one, int card_two, int discard_one[], int &discard_one_size, int discard_two[], int &discard_two_size) { if (card_one > card_two) { // player one gets the cards in their discard cout << "Human takes this one!" << endl; } else if (card_one < card_two) { // player two gets the cards in their discard cout << "Your cards are mine!" << endl; } else { // tie, both cards are lost (nothing to do) cout << "Ties are for weaklings. Throw these two to the wolves." << endl; } } // check to see if the given hand is out of cards // if so, reshuffle the discard and transfer the cards to the hand // UNLESS the discard is also empty, in which case this player loses! (return true) bool checkForLoss(int hand[], int &hand_size, int discard[], int &discard_size, string lose_message) { return false; }