Section of Project

Hi, I'm just making a c++ project on my free time (the card game of war) (not homework), and I'm stuck on the part where I have to deal 26 cards to the player and CPU. The partial code is below, and I'm wondering how to randomize the cards. I can do the rest. Is there any other tips you can give me? Thanks for the help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int pcards, ocards, knowtoplay, instructionrepeat;
string oplay, pplay, onextcard, pnextcard;
string deck [52] = {"AC", "AH", "AS", "AD", "2C", "2H", "2S", "2D", "3C", "3H", "3S", "3D", "4C", "4H", "4S", "4D", "5C", "5H", "5S", "5D", "6C", "6H", "6S", "6D", "7C", "7H", "7S", "7D", "8C", "8H", "8S", "8D", "9C", "9H", "9S", "9D", "10C", "10H", "10S", "10D", "JC", "JH", "JS", "JD", "QC", "QH", "QS", "QD", "KC", "KH", "KS", "KD"};


int instructions()
{
  cout << "Welcome to War, the famous card game! /nDo you know how to play? If yes, type 1. If no, type 0./n";
  thing:
  cin >> knowtoplay;
  if (knowtoplay == 1){
    darn:
    cout << "Great! Get ready to play!/n";
    goto play;
  }
  else if (knowtoplay == 0)
  {
    blah:
    cout << "Here's how it works: Each player starts with half the deck of cards. Together, they slam down the top card in their stack. Whichever card has a higher value (suits don't count), the person who played that card takes both cards. You may be wondering, what if the cards have the same value? Then, each player puts down 3 extra face-down cards, and the next one is turned face-up. Whoever has the higher face-up card gets ALL the cards! Whoever has every card in their posession at any time wins! /nWould you like to hear that again (type 1), or just play (type 0)?";
    cin >> instructionrepeat;
    if (instructionrepeat == 1) goto blah;
    
    else if (instructionrepeat == 0){
      goto darn;
    }
    else {
      cout << "Please enter either 0 or 1";
      goto thing;
    } ;
    
  };
  play:;
};



int starttoplay(){
  // What goes here?
  cout << "The deck has been shuffled and both you and the CPU have received 26 random cards.";
}
Can someone respond?
closed account (zb0S216C)
See here for randomisation of numbers: http://www.cplusplus.com/reference/clibrary/cstdlib/rand/

Wazzak
You'd also need an array to hold all the cards already chosen, then compare newly chosen cards with this array to prevent duplicates.

Since you and the computer start with a fixed amount (26), but that value can change based on who wins each round, consider using vectors. Better yet, since the top card(first element) is used in each round, and any cards won in a round are put on the bottom of the deck(last element), try queues instead.
Last edited on
Topic archived. No new replies allowed.