I am very new to C++, Ive only been learning it for the last week. Anyway Ive written this simple program that should create a number guessing game with player 1 and 2. The use can input the number of rounds they wish to play then at the end of each guess the winner of that round (the one with the the number closest to the random number) should be displayed. At the end the program should display the winner, how many rounds they won, and how many points they beat the other player by.
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef unsigned short int USHORT;
USHORT Game(USHORT, USHORT);
int main()
{
USHORT rnd_amount;
USHORT P1_guess;
USHORT P2_guess;
USHORT result;
USHORT wincnt_P1;
USHORT wincnt_P2;
USHORT winner;
std::cout << "Welcome to the guess the number game.\n\n";
std::cout << "You can input the number of rounds you wish to play, whoever wins the most rounds wins the game.\n\n";
std::cout << "A random number will be generated by the computer. Each player has attempt to guess the number.\n\n";
std::cout << "The winner of the round is the person with the guess closest to the generated number\n\n";
std::cout << "\n\n\n\nPlease enter the number of rounds you wish to play";
std::cin >> rnd_amount;
std::cout << "\n\nTHE GAME HAS BEGUN";
USHORT rnd_counter;
wincnt_P1 = 0;
wincnt_P2 = 0;
for (rnd_counter = 0; rnd_counter < rnd_amount; rnd_counter++)
{
std::cout << "\n\nThis is round: " << rnd_counter + 1;
std::cout << "\n\nPlease enter your guess Player 1:";
std::cin >> P1_guess;
std::cout << "\n\nPlease enter your guess Player 2:";
std::cin >> P2_guess;
result = Game(P1_guess, P2_guess);
switch (result)
{
case 1:
std::cout << "\n\nThe winner of this round is Player 1";
wincnt_P1++;
break;
case 2:
std::cout << "The winner of this round is Player 2";
wincnt_P2++;
break;
case 3:
std::cout << "This round was a draw";
break;
}
}
std::cout << "\n\nIt is the end of the game!";
std::cout << "\n\nPlayer 1, you won " << wincnt_P1 << "games!";
std::cout << "\n\nPlayer 2, you won " << wincnt_P2 << "games!";
winner = 0;
if (wincnt_P2 < wincnt_P1)
{ winner = 1; }
if (wincnt_P1 < wincnt_P2)
{ winner = 2; }
else
{ winner = 3;}
switch (winner)
{
case 1:
std::cout << "\n\nPlayer 1, you have won this game with " << wincnt_P1 << " points. You beat player 2 by " << (wincnt_P1 - wincnt_P2) << " points";
break;
case 2:
std::cout << "\n\nPlayer 2, you have won this game with " << wincnt_P2 << " points. You beat player 1 by " << (wincnt_P2 - wincnt_P1) << " points";
break;
case 3:
std::cout << "\n\nNeither player won the game, it was a draw.";
break;
}
std::cout << "\n\nThank you for playing the number guessing game! Please play again";
std::cout << "\n\n\n\n\n\nThe Number Guessing Game v1.0. Built by Andrew";
int nil;
std::cout << ". ";
std::cin >> nil;
return 0;
}
USHORT Game(USHORT P1_guess, USHORT P2_guess)
{
srand( (unsigned)time( NULL ) );
USHORT random = (rand() % 10) + 1;
USHORT P1_result;
USHORT P2_result;
USHORT z;
P1_result = 0;
P2_result = 0;
if (P1_guess < random)
{ P1_result = random - P1_guess; }
else
{ P1_result = P1_guess - random; };
if (P2_guess < random)
{ P2_result = random - P2_guess; }
else
{ P2_result = P2_guess - random; };
z = 0;
if (P2_result < P1_result)
{ z = 2; }
if (P1_result < P2_result)
{ z = 1; }
else (P2_result == P1_result);
{ z = 3; };
return z;
}
It will compile but returns some warnings about variables in the Game function being assigned values and never used.
When I run the program it simply says the round is a draw whatever number is input.
I've been over the code but my skills with C++ are pretty limited and I can't work out where I have gone wrong.
Any help would be much appreciated.
Thanks in advance.