Question

In: Computer Science

Can you write a program for the card game WAR using linked lists in c++!

Can you write a program for the card game WAR using linked lists in c++!

Solutions

Expert Solution

WAR Card Game

we will write a program to play a simple card game known as War. In War, a standard deck of 52 playing cards is shuffled and divided evenly among two players (the user and the computer). Players simultaneously turn over the top card of their respective decks, and the player with the high value card takes both cards, adding them to the bottom of their deck. In the case of a tie (the two cards have equal value), there is a war where players each place three cards face down and then one card face up; the player with the higher value takes all (ten) cards. In the case of another tie, this process repeats until there is a winner. For added complexity, a player may choose to shuffle their deck of cards before each battle. The game continues until one player is out of cards.

In playing the game, there are several piles of cards: the two players' decks, and the piles of cards in the "battle" by each player. Cards are continually moved between these different piles. Since the cards in the pile have a specific order, we will use a List to represent these card piles.

#include<iostream>
#include <cstdlib>
#include <ctime>
#include <stdio.h>
using namespace std;

int funcCount = 0;

//Deck class to hold an array of cards
class Deck
{
public:
        int wDeck[52];
        int shuffledDeck[52];
        //Populate the deck with cards
        void fillDeck()
        {
                int i = 0;
                int cardnum = 2;
                //Start with card 2 and fill 4 of each up to 14 
                //We will handle printing J Q K A later from 11-14
                while (i<52)
                {
                        for (int j = 1; j<5; j++)
                        {
                                wDeck[i] = cardnum;
                                i++;
                        }
                        cardnum++;
                }
        }
        //Print method for debugging
        void printDeck()
        {
                for (int i = 0; i<52; i++)
                {
                        switch (shuffledDeck[i])
                        {
                        case 11:
                                cout << "J" << endl;
                                break;
                        case 12:
                                cout << "Q" << endl;
                                break;
                        case 13:
                                cout << "K" << endl;
                                break;
                        case 14:
                                cout << "A" << endl;
                                break;
                        default: cout << shuffledDeck[i] << endl;
                        }
                }
        }
        void shuffleDeck()
        {
                short int nums[52];
                short int x = 0, y = 0;
                srand(time(NULL));
                x = 0;
                while (x < 52)
                {
                        y = 0;
                        //Generate a random number in the range of 0-52
                        nums[x] = rand() % 52; 
                        //Check each number in the array to make sure the current random number isn't in it
                        while (y < 52)
                        {
                                //If another number is equal to this one.
                                if (nums[y] == nums[x] && x != y) //If another number is equal to this one.
                                {
                                        --x; //Set x back one. 
                                        y = 0; //Reset y;
                                        break; //Try again.
                                }
                                else
                                {
                                        ++y; //Otherwise, check the next number.
                                }
                        }
                        ++x; //When we reach here, if --x happened, we need to start over.
                }
                x = 0; 
                //Fill the empty array of shuffled cards with the value of a random array element as the index of the ordered deck
                while (x < 52)
                {
                        shuffledDeck[x] = wDeck[nums[x]];
                        ++x;
                }

        }
};

//Class to hold each players hand as a linked list
class Hand
{
public:
        int value;
};

class Link
{
public:
        Hand *pHand;
        Link *Next;
};

class LinkedList
{
private:
        Link *First;
        Link *Last;
public:
        LinkedList()
        {
                First = NULL;
        }
//Add a card to the beginning of the list
        void AddCard(int cValue)
        {
                Link *newLink = new Link;
                Hand *nHand = new Hand;
                newLink->pHand = nHand;
                nHand->value = cValue;
                newLink->Next = First; //makes next null
                First = newLink;
        }
//Add a card at the end or in this case the bottom of the pile so that it's not used by the player in the next hand after a winning one
        void AddCardEnd(int cValue)
        {
                //We setup two pointers to Link objects
                Link *lastItem = new Link;
                Link *newNode = new Link;
                Hand *nHand = new Hand;
                //This pointer called lastItem will start at the first element and go through the list
                lastItem = First;
                //Set the parameters of the hand
                newNode->pHand = nHand;
                nHand->value = 1;
                //If the first item is null then set it to the new Node
                if (First == NULL)
                {
                        First = newNode;
                        First->Next = NULL;
                }
                else
                {
                        //Loop through the linked list to find the last element
                        while (lastItem->Next != NULL)
                        {
                                lastItem = lastItem->Next;
                        }
                        //The item after the last item will be our new item
                        lastItem->Next = newNode;
                }
        }


        void RemoveCard()
        {
                delete First->pHand;;
                First = First->Next;
        }

        //Remove the entire linked list
        void RemoveList(LinkedList LL)
        {
                Link *current = new Link;
                Link *temp = new Link;
                current = First;
                //Loop through the list for the last item
                while (current != NULL)
                {
                        //Since we are going to remove the current link, we must store the next node in a temp variable to reuse
                        temp = current->Next;
                        //Remove the hand and the link, then move to the next item by setting current back to temp which held the node after the one we delet
                        delete current->pHand;
                        delete current;
                        current = temp;
                }
                //Since you've reached the end, there is one more link and card that is null that must be deleted
                delete current->pHand;
                delete current;
        }

        int Display()
        {
        Link *current = First;
        while(current != NULL)
        {
        cout <<  current->pHand->value << "\n";
        current=current->Next;
        return current->pHand->value;
        }
        }
        
        int CountCards()
        {
        Link *current = First;
        int counter=0;
        while(current != NULL)
        {
        counter++;
        current=current->Next;
        }
        return counter;
        }
        int turn()
        {
                int val;
                Link *current = First;
                val = current->pHand->value;
                cout << val;
                //First = First->Next;
                return val;
        }
        //Deal out three cards from each player if there is a tie aka war
        int war()
        {
                int val1, val2, val3;
                val1 = turn();
                val2 = turn();
                val3 = turn();
                return turn();
        }
};

int main()
{
//Setup a deck object to hold the shuffled array and two linkedlist for a players hand
        Link *First= new Link;
        Link *Current=First;
        Deck nDeck;
        int z=0;
        LinkedList P1;
        LinkedList P2;
        int x;
        int counter = 0;
//Fill in the deck then shuffle it
        nDeck.fillDeck();
        nDeck.shuffleDeck();
        nDeck.printDeck();
        int p1count,p2count;
//Deal out the cards to each player from the shuffled deck
        for (int i = 0; i < 52; i++)
        {
                if (i % 2 == 0)
                {
                        P1.AddCard(nDeck.shuffledDeck[i]);
                }
                else
                {
                        P2.AddCard(nDeck.shuffledDeck[i]);
                }
        }
        cout << "-----Player1-----" << endl;
        cout << "-----Player2-----" << endl;
        cout << "-------------------------" << endl;
        int P1Card, P2Card, wCard1, wCard2;//holds the value of the turn
        int wCount = 0;
        do
        {
                cout <<"PLAYER ONE SCORE IS  " << p1count << "\n";

                cout <<"PLAYER TWO SCORE IS  " << p2count << "\n";
                //cin >> x;
                cout << "P1 card: ";
                P1Card = P1.turn();
                cout << endl << "P2 card : ";
                P2Card = P2.turn();
                cout << endl;
                //these ifs compare the value of the cards turned
                if (P1Card == 1 || P2Card==1)
                {
                P1.RemoveCard();P2.RemoveCard();
                continue;
                }
                if (P1Card > P2Card)
                {
                        cout << "Player1 wins this round!";
                        P1.RemoveCard();
                        P1.AddCardEnd(P1Card);
                        P1.AddCardEnd(P2Card);
                        P2.RemoveCard();
                        cout << "Player 1 now has ";
                }
                if (P2Card > P1Card)
                {
                        cout << "Player2 wins this round!";
                        P2.RemoveCard();
                        P2.AddCardEnd(P2Card);
                        P2.AddCardEnd(P1Card);
                        P1.RemoveCard();
                }
                do
                {
                if (P1Card == P2Card)
                {
                        cout << "WAR" << "\n";
                        LinkedList war;

                        war.AddCard(P1Card);
                        P1.RemoveCard();
                        war.AddCard(P1.turn());
                        P1.RemoveCard();
                        war.AddCard(P1.turn());
                        P1.RemoveCard();
                        war.AddCard(P1.turn());
                        P1.RemoveCard();
                        war.AddCard(P1.turn());
                        P1.RemoveCard();

                        war.AddCard(P2Card);
                        P2.RemoveCard();
                        war.AddCard(P2.turn());
                        P2.RemoveCard();
                        war.AddCard(P2.turn());
                        P2.RemoveCard();
                        war.AddCard(P2.turn());
                        P2.RemoveCard();
                        war.AddCard(P2.turn());
                        P2.RemoveCard();
                        wCard1=war.turn();
                        war.RemoveCard();
                        wCard2=war.turn();
                        war.RemoveCard();
                        if (wCard1==1 || wCard2==1)
                        {
                        P1.RemoveCard();P2.RemoveCard();
                        break;
                        }

                        int addme=1;
                        cout << "WCARD1 IS " << wCard1 << "\n";
                        
                        cout << "WCARD2 IS " << wCard2 << "\n";
                                if (wCard1 > wCard2)
                                {
                                cout << "P1 wins war" << "\n";
                                         while (addme!=0)
                                        
                                        {
                                                
                                                addme=war.Display();
                                                cout << "I ADDED " << addme << "\n";
                                                P1.AddCardEnd(addme);
                                                war.RemoveCard();       
                                        }
                                        
                                }
                                if (wCard1 < wCard2)
                                {
                                cout << "P2 wins war" << "\n";
                                        while (addme!=0)
                                        {
                                                addme=war.Display();
                                                cout << "I ADDED " << addme << "\n";
                                                P2.AddCardEnd(addme);
                                                war.RemoveCard();
                                        }       
                                
                                }
                }
                } while (wCard1 == wCard2);//repeats war while the cards turned last in both hands equal each other
        
                 p1count=P1.CountCards();
                 p2count = P2.CountCards();
        } while (p1count<52 & p2count<52);

        return 0;
}

Related Solutions

Can you program Exploding kittens card game in c++ using linked lists and classes! The game...
Can you program Exploding kittens card game in c++ using linked lists and classes! The game is played with between 2 and 4 players. You'll have a deck of cards containing some Exploding Kittens. You play the game by putting the deck face down and taking turns drawing cards until someone draws an Exploding Kitten. When that happens, that person explodes. They are now dead and out of the game. This process continues until there's only one player left, who...
Can you write a program for snakes and ladder board game using linked lists in c++
Can you write a program for snakes and ladder board game using linked lists in c++
In C++, Write the following program using a vector: A common game is the lottery card....
In C++, Write the following program using a vector: A common game is the lottery card. The card has numbered spots of which a certain number are selected at random. Write a Lotto() function that takes two arguments. The first should be the number of spots on a lottery card and the second should be the number of spots selected at random. The function should return a vector object that contains, in sorted order, the numbers selected at random. Use...
Using C++, you will create a program, where you will create two doubly linked lists. These...
Using C++, you will create a program, where you will create two doubly linked lists. These doubly linked lists will contain integers within them. Using the numbers in both of these linked lists, you add the numbers together, and insert the addition of the two numbers into a singly linked list. the input can be from the user or you just write the input. for example, if one number in the doubly linked list is 817 and in the other...
Please include comments on what you are doing.   Using linked lists, write a Python program that...
Please include comments on what you are doing.   Using linked lists, write a Python program that performs the following tasks: store the records for each college found in the input file - colleges.csv - into a linked list. (File includes name and state data fields) allow the user to search the linked list for a college’s name; display a message indicating whether or not the college’s name was in the database allow the user to enter a state's name and...
IN C++ Write a program to play the Card Guessing Game. Your program must give the...
IN C++ Write a program to play the Card Guessing Game. Your program must give the user the following choices: - Guess only the face value of the card. -Guess only the suit of the card. -Guess both the face value and suit of the card. Before the start of the game, create a deck of cards. Before each guess, use the function random_shuffle to randomly shuffle the deck.
Create a java program. War is a card game for two players. A standard deck of...
Create a java program. War is a card game for two players. A standard deck of 52 cards is dealt so that both players have 26 cards. During each round of play (or "battle"), both players play a card from the top of their hand face up. The player who plays the card of the higher rank wins both cards and places them at the bottom of his stack of cards. If both cards played are of the same rank,...
in C language only. Write a program called gamecards.c that has a card game logic by...
in C language only. Write a program called gamecards.c that has a card game logic by comparing the cards from 4 people and calculating which player has the best card. 1. Every card must be represented by exactly two chars representing a rank and a suit. ranks: '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'. ('T' = 10) Suits: 'H', 'D', 'S', 'C'. 7D represents the “7 of Diamond” etc.. 2. Write a function called...
I'm having trouble programming connect four board game using linked lists, sets and maps in c++....
I'm having trouble programming connect four board game using linked lists, sets and maps in c++. Can you code connect four game using these concepts.
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of songs/artist pairs. Allow your user to add song / artist pairs to the list, remove songs (and associated artist) from the list and be sure to also write a function to print the list! Have fun! Make sure you show your implementation of the use of vectors in this lab (You can use them too ) You MUST modularize your code ( meaning, there...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT