War Card Game Randomizer

Hello everyone, I recently made a simplified version of the card game War. However when I run the program, I get proper scores outputs but the results do not change between games.
For example, I can run the game 2 or 3 times and all the results would be the same. I think there is a problem with my deck shuffle equation but I'm at a loss to where it is. 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
#include <stdlib.h>
#include <algorithm>

using namespace std;

enum suits {spade, heart, club, diamond};

class card {
public:
	card ();
	card (suits, int);

	int rank;
	suits suit;
};

card::card (){
	rank = 1;
	suit = spade;
}

card::card (suits sv, int rv){
	rank = rv;
	suit = sv;
}

ostream & operator << (ostream & out, card & select){
	switch (select.rank){
		case 1:
			cout << "Ace";
			break;
		case 11:
			cout << "Jack";
			break;
		case 12:
			cout << "Queen";
			break;
		case 13:
			cout << "King";
			break;
		default:
			cout << select.rank;
			break;
	}
	switch (select.suit){
		case spade:
			cout << " of Spades";
			break;
		case heart:
			cout << " of Hearts";
			break;
		case club:
			cout << " of Clubs";
			break;
		case diamond:
			cout << " of Diamonds";
			break;
		}
	return out;
}

class randomint {
	public:
		unsigned int operator () (unsigned int);
} randomizer;

unsigned int randomint::operator () (unsigned int max){
	unsigned int rval = rand();
	return rval % max;
}

class deck {
public:
	deck();
	void shuffle(){
		random_shuffle (cards, cards+52, randomizer);
	}
	bool isempty(){
		return topcard <= 0;
	}
	card draw();
protected:
	card cards[52];
	int topcard;
};

deck::deck (){
	topcard = 0;
	for (int i = 1; i <= 13; i++){
		card c1(spade, i), c2(heart, i), c3(club, i), c4(diamond, i);
		cards[topcard++] = c1;
		cards[topcard++] = c2;
		cards[topcard++] = c3;
		cards[topcard++] = c4;
	}
}

card deck::draw(){
	if (! isempty()){
		return cards[--topcard];
	}
	else {
		card spadeace(spade, 1);
		return spadeace;
	}
}

class player{
public:
	player (deck &);

	card draw();
	void addpoints(int);
	int score();
	void replacecard(deck &);
protected:
	card mycards[3];
	int myscore;
	int removedcard;
};

player::player (deck & adeck){
	myscore = 0;
	for (int i = 0; i < 3; i++){
		mycards[i] = adeck.draw();
	}
	removedcard = 0;
}

card player::draw(){
	removedcard = randomizer(3);
	return mycards[removedcard];
}

void player::addpoints(int howmany){
	myscore += howmany;
}

int player::score (){
	return myscore;
}

void player::replacecard(deck & adeck){
	mycards[removedcard] = adeck.draw();
}

int main(){
	deck thedeck;
	thedeck.shuffle();

	player player1(thedeck);
	player player2(thedeck);

	while (! thedeck.isempty()){
		card card1 = player1.draw();
		cout << "Player 1 plays: " << card1 << endl;
		card card2 = player2.draw();
		cout << "Player 2 plays: " << card2 << endl;

		if (card1.rank == card2.rank) {
			player1.addpoints(1);
			player2.addpoints(1);
			cout << "Its a Draw!" << endl;
		}
		else if (card1.rank > card2.rank){
			player1.addpoints(2);
			cout << "Player 1 wins!" << endl;
		}
		else {
			player2.addpoints(2);
			cout << "Player 2 wins!" << endl;
		}
		player1.replacecard(thedeck);
		player2.replacecard(thedeck);
	}

	cout << "Player 1 score: " << player1.score() << endl;
	cout << "Player 2 score: " << player2.score() << endl;
}
You need srand(...) at the top of your main function:

http://www.cplusplus.com/reference/cstdlib/srand/?kw=srand

Use the time(...) function as shown in that example.
You can try this:

 
srand(time(0));

Or this (C++11):

1
2
3
4
5
6
7
#include<random>

void something(){
  random_device rd;
  //Replace rand() with:
  rd();
}

The problem is that the rand() method is a pseudorandom number generator. The numbers seem to be random, but they are not random. There is always a seed, hence predictable.

If your computer doesn't have a random hardware device, also the random_device implementaion will generate pseudo random numbers. You can check it with:

rd.entropy();

With pseudo random numbers generator the entropy remains unchanged.

For true random numbers you can try to adapt these codes:

http://ncomputers.org/projects/random

But I think that the srand() or random_device options will work pretty well for this case.
Last edited on
Topic archived. No new replies allowed.