For a class assignment we need to make a C++ program in putty of the game war. It has to have 7 functions; it's suppose to use a time() function and srand() function in the dealRandomCard() function. The output should be asking you how many cards you want to play; then displaying your card; give you the option to throw away 1 card; display the new cards; sort your cards and the computer's cards in accending order; then put your #1 card against the computers #1 card who's ever higher gets a point until all cards are played; then the computer tallies the points and informs the user if they won or lost.
Here's the thing I am completely lost and have little clue what I am doing. I was hoping someone could help point me in the right direction. The code that I have so far lists the 7 functions I have to use but some of them are commented out because I'm trying to build up to them. I'm also not sure about the time or rand code, what I have is something that we were shown in class but I don't think it's right.
Thank you, SSR.

My code so far:

#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

//void dealCardSet
void dealRandomCard (int[], int[], int);
//void showAllCards
int getOneNewCard (int);
//void dealRandCard
void bubblesort (int[], int[], int);
//void playwar

int main ()
{

int N, throwaway, cards[N], suit[N];

cout<<" How many cards do you want to play? -> ";
cin>>N;

dealRandomCard (cards, suit, N);
getOneNewCard (throwaway);
bubblesort (cards, suit, N);

return 0;
}

dealRandomCard (int card[N]; int suit[N]; N)

  for(int a=0; a<N; a++)
      {
      card[a]=rand()%13+2;
      suit[a]=rand()%4;

      cout<<"Card"<<a <<" : " <<card[a]<<" ("<<suit[a]<<")"<<endl;
      }

int getOneNewCard(int throwaway)
cout<<"If you want a new card, choose index of card to throw away.
Otherwise, type -1."<<endl;
cout<<"Your Choice = ";
cin>> throwaway;
while (throwaway!=-1)
{
}

void bubblesort (int cards[N], int suit[N], int N)
{
   int a, b, temp, temp2;
      for (a=0; a<N; a++)
        for (b=0; (b<N-1); b++)
           if (cards[b]<cards[b+1])
            {
            temp=cards[b];
            cards[b]=cards[b+1];
            cards[b+1] = temp;
            temp2= suit[b];
            suit[b]= suit[b+1];
            suit[b+1]=temp2;
            }
}

Recommended Answers

All 6 Replies

You never gave N a size, and don't char array declarations have to be constants?
You have the convenience of vectors to use over constant sized arrays.

The user has to enter in the number of cards they want to play which is suppose to be N. Are you saying I need to set a limit like 23?
I keep going back and forth as to whether or not the suit array needs to be a char array or an int array. It could be int because I'm allowed to assign the values of 0-3; then have the values displayed as 0==spades 1==diamonds etc. So I'm thinking about making it an int array in the dealRandomCard function and then in the ShowCard function have if statements declaring the characters of the values. However I'm not sure if that makes any sense and if I"d be doing it in the correct arrays.

What he is saying is you cannot create an array of size N like that. Arrays are constant and cannot.

he suggested the use of vectors, which can be any size and you dynamically add to them!

The other option would be a dynamically assigned array... an example of what you are after is below. Please note the delete statement is VERY important. The last thing you want it a memory leak...this is why vectors may be better if you don't know what your doing with memory allocation and deallocation.

int *x;
int n;
    
cout << "Enter a number\n>>";
cin >> n;
    
x = new int[n];
// your code here
delete []x; //always call when you will not be using the array anymore

below is an example of how to use a vector(the basics)

#include <vector>
...
std::vector<int> v;
int n;
    
std::cout << "Enter a number\n>>";
std::cin >> n;
    
for(int i = 0; i < n; i++){
        v.push_back(i);
}
    
for(int i = 0; i < n; i++){
        std::cout << v.at(i); //or v[i] if that it is easier for you
}

http://www.cplusplus.com/doc/tutorial/dynamic.html
http://www.cplusplus.com/reference/stl/vector/
Chris

All the above posters are completely correct. You really can't size arrays like that.

BUT -- Please before you go any further, write down on paper your algorithm. i.e. what is going to happen. The work it through on paper.
You then play the routine on paper with the short limits, ie a deck of cards with only four cards, say AKQJ and one suit. It will make your life a lot easier on the long run.

The you will see:

You have fundamental mistakes. e.g. If player 1 gets the Ace-spades, what stops players 2 getting the same card! (or player 1 getting it again).

Bubblesort -- apart from ultra-specialized instances should NEVER be used it is so slow.

If you took the trouble to write a class Card, with an bool operator<(const Card&) const , you could then use the std sort from #include <algorithm> . Or you could use a priority_queue object for each player.

[There is a single queue version of this game, which gets the same result (the score) -- but a little more care is needed with counters.
It is more efficient than multiple queues if nearly the whole deck is used.]

Finally, there are several threads on creating decks of random cards e.g. http://www.daniweb.com/forums/thread159174.html, were you can read ArkM's brutally short method of shuffling a deck. [Note, I gave him reputation and I am referencing his post !! He can't complain about that! ;) ]

The professor has not taught us "class Card"; so I'm not really sure what you're talking about,

The professor has not taught us "class Card"; so I'm not really sure what you're talking about,

I like ArkM's solution! I'm going to +rep that one too! As to the many threads dealing with Card Shuffling, I recently added my own snippet.

http://www.daniweb.com/code/snippet1019.html

Of the two methods in it, the one below is easier to follow.

void GenerateRandomIntegers2 (int min, int max, int array[], int arraySize)

There is no Card class in C++. You have to write it yourself, which is what StuXYZ was suggesting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.