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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
#include<cstdlib>
#include<cmath>
#include<iomanip>
#include<string>
#include<iostream>
#include<fstream>
#include<ctime>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;
//constant
const int MAX = 52;
//prototypes
void rules();
void shuffle();
//class
class card {
public:
char value;
string suit;
int x = 25;
void printcard()
{
cout << value << " of " << suit;
}
};
card deck[MAX], temp;
int play(deque<card> const& player, deque<card> const&comp);
int main()
{
//variables
char again;
string name;
cout << "Welcome to the game of WAR!";
cout << "\n";
cout << "Please enter your name! : ";
cin >> name;
cout << "\n";
cout << "Welcome " << name << " lets play!" << endl;
do {
srand(time(NULL));
int x = 0;
char value[]{ 'A','2','3','4','5','6','7','8','9','X','J','Q','K' };
string suit[]{ "Hearts","Diamonds","Clubs","Spades" };
int i, j, l = 0;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 13; j++)
{
deck[l].value = value[j];
deck[l].suit = suit[i];
l++;
}
}
//input
rules();
//calculations
shuffle();
deque<card> player_cards(deck, deck + 25);
deque<card> computer_cards(deck + 25, deck + 52);
while (!player_cards.empty() && !computer_cards.empty())
{
cout << name << " cards left" << "\t\tComputer Cards left" << endl;
cout << player_cards.size() << "\t\t\t" << computer_cards.size() << endl;
cout << "\n";
int player_higher = play(player_cards, computer_cards);
if (player_higher == 1 && !computer_cards.empty())
{
computer_cards.push_front(computer_cards.back());
computer_cards.pop_back();
computer_cards.push_front(player_cards.back());
player_cards.pop_back();
}
else if (player_higher == 2 && !player_cards.empty())
{
player_cards.push_front(player_cards.back());
player_cards.pop_back();
player_cards.push_front(computer_cards.back());
computer_cards.pop_back();
}
else
{
for (int i = 0; i < 2; ++i) {
if (!player_cards.empty()) {
computer_cards.push_front(player_cards.back());
player_cards.pop_back();
}
if (!computer_cards.empty()) {
player_cards.push_front(computer_cards.back());
computer_cards.pop_back();
}
}
}
if (player_cards.empty()) cout << "===================================You Win!========================================================================"<< endl;
if (computer_cards.empty()) cout << "==========================================Computer wins=================================================================" << endl;
}
cout << "\n";
cout << "Would you like to play again " << name << "? Y for yes or N for no: ";
cin >> again;
//output
cout << "\n";
}
while (again == 'y' || again == 'Y');
cout << "\n";
cout << "Have a Great Day!";
cout << "\n";
return 0;
}
//function definitions
void rules()
{
int ch;
cout << "Would you like to hear the rules? Press 1 for yes or 2 for no: ";
cin >> ch;
if (ch == 1)
{
cout << "\n";
cout << "You will be playing the Computer, each of you will be given 26 cards." << endl;
cout << "You will both throw down one card, whoever lower card will collect both cards and put them in their decks.\n";
cout << "If you both have the same value card then you will commence the game of war!\n";
cout << "Both players will lay down 3 cards face down and then flip a card.\n";
cout << "Whoever has the lower card loses and collects all the cards in the pile!\n";
cout << "The first person with zero cards left is declared the winner!\n";
cout << "Goodluck!\n";
cout << "\n";
}
else
{
cout << "no rules" << endl;
}
}
void shuffle()
{
int n, m;
for (n = 0; n < 52; n++)
{
m = rand() % 52;
temp = deck[n];
deck[n] = deck[m];
deck[m] = temp;
}
}
int play(deque<card> const& player, deque<card> const&comp)
{
auto const& p = player.back();
auto const& c = comp.back();
if (p.value > c.value)
{
cout << p.value << " is higher than " << c.value << endl;
cout << "Player wins this hand" << endl;
cout << "\n";
return 1;
}
else if (p.value < c.value)
{
cout << c.value << " is higher than " << p.value << endl;
cout << "Computer wins this hand" << endl;
cout << "\n";
return 2;
}
else
{
cout << c.value << " is the same as " << p.value << endl;
cout << "THIS MEANS WAR!" << endl;
return 3;
}
}
|