Hi!
I'm new to programming and need some help looking over the code.
There are a few things that need to be included (see list below) and I'm wondering how I can implement those to this code. And also does anything in the current code look weird?
One user is playing a dice game with the computer.
Each round both throw a dice and highest number wins the round. (First to win 2 rounds win the whole game). In the beginning the user has to decide how much money to bet. (100SEK, 300SEK or 500SEK).
*Each round the users and the computers dicethrow must be declared.
*Show clearly who won the round.
*If someone win 2 times in a row there´s no need to play a 3rd round.
*The game should somehow show if the user wins or loses the round and also money won/lost.
*If the computer win there should be a "sorry you lost"-message shown.
*The user can deposit no more than 5000SEJ / each game.
*If there in any money left before the next game there´s no need for a deposit.
nytt spel.
*There should be info shown about money being drawn from the users account.
*The dices can be symbolized by predefined randomfunction.
rand() that return a random whole number.
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
|
#include <iostream>
#include <string>
#include <ctime>
#include <algorithm>
#include <vector>
using namespace std;
// constants
const int CRED = 3000;
const string CURRENCY = "USD";
// prototypes
bool doRunAgain();
int betMoney(int &x);
string startGame();
void showInfo(int &x, int &y);
int main()
{
//using clock for randomgenerator
srand(time(0));
// Variabels
int pot = 0;
int usrR = 0;
int cmpR = 0;
// Money account
int usrAcc = CRED;
int cmpAcc = CRED;
// main program loop
do
{
cout << endl;
showInfo(usrAcc, cmpAcc);
pot = betMoney(usrAcc);
usrAcc -= pot;
cmpAcc -= pot;
pot += pot;
for (int i = 0; i < 3; i++)
{
string str = startGame();
if (str == "usr")
usrR++;
else if (str == "cmp")
cmpR++;
else
{} // nothing, just for demonstration
if (cmpR == 2 || usrR == 2)
i = 3;
}
if (cmpR == 2)
{
cout << endl << "Computer won. Pot goes to computer!" << endl;
cmpAcc += pot;
}
else if (usrR == 2)
{
cout << endl << "You won. You win the pot!" << endl;
usrAcc += pot;
}
else
{
cout << endl << "Game was a tie. Money transfered back to players." << endl;
usrAcc += pot/2;
cmpAcc += pot / 2;
}
cmpR = 0;
usrR = 0;
if (cmpAcc <= 0)
{
cout << "Computer have no money left. You won!" << endl;
cout << "Computers mobfriends added some more money!" << endl;
cmpAcc += CRED;
}
else if (usrAcc < 0)
{
}
cout << endl;
} while (doRunAgain());
return 0;
}
string startGame()
{
vector<int> usr;
vector<int> cmp;
usr.push_back(rand() % 6 + 1);
usr.push_back(rand() % 6 + 1);
cmp.push_back(rand() % 6 + 1);
cmp.push_back(rand() % 6 + 1);
sort(usr.begin(), usr.end(),
[](const int& left, const int& right)
{
return left < right;
});
sort(cmp.begin(), cmp.end(),
[](const int& left, const int& right)
{
return left < right;
});
if (cmp[1] == usr[1])
return "equal"; //not using this anyway
else if (cmp[1] > usr[1])
return "cmp";
else
return "usr";
}
int betMoney(int &x)
{
string bet;
while (true)
{
if (x < 100)
{
cout << "Your out of money. Please write 'ADD500' to add 500 " << CURRENCY << "." << endl;
}
cout << "Bet:\n1 = 100\n3 = 300\n5 = 500" << endl;
cout << "Your choice: ";
getline(cin, bet);
if (bet == "1")
return 100;
else if (bet == "3")
return 300;
else if (bet == "5")
return 500;
else if (bet == "ADD500")
x += 500;
else
cout << endl << "Wrong choice or out of money! Please try again.\n" << endl;
}
}
bool doRunAgain()
{
string ch;
cout << "Do you want to play again? (y/n): ";
while (true)
{
getline(cin, ch);
transform(ch.begin(), ch.end(), ch.begin(), ::toupper);
if (ch == "Y")
return true;
else if (ch == "N")
return false;
else
cout << "Please enter (y)es or (n)o: ";
}
}
void showInfo(int &x, int &y)
{
cout << "Stats: " << endl;
cout << "Player: " << x << CURRENCY << endl;
cout << "Computer: " << y << CURRENCY << endl;
cout << endl;
}
| |