I'm new to coding and this code pretty much goes to the extent of my current knowledge I'm looking for ways to improve this code. This code compiles and runs fine in Visual Studio 2013. I'm also using Windows 8.1 as my current OS for the computer I'm writing and compiling on.
I'd like a review over anything and any errors with the math involved in the game or the errors with the game.
What this code is suppose to do and from my testing does. Is be a simple game in which there are two players taking pegs off a ring one player is the user (human), the other player is a computer. Depending on the what moves the user makes is the computer responds with an ideal move. If there is no ideal move then the computer makes a random move of taking 1 ring off a random peg basically attempting to slow the game down giving more chances for the human user to make a mistake.
The math involved in this basically makes it so player 1 can always win. The computer is to know every ideal move and react based on human user moves. But since the computer can go second he is then just waiting for a error made by the human user or trying to slow down the game.
#include<iostream>
#include<string>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main()
{
char cAgain;
do
{
//Variable Declartion
int x, PegTotal, PegOne = 3, PegTwo = 5, PegThree = 7, Player, y;
bool switchRepeat = true;
cout << "Welcome to the 3 Peg game!!!" << endl;
cout << "This is a 2 player game." << endl << endl;
cout << "The game has 3 pegs and 15 rings." << endl
<< "The first peg has 3 rings, the second peg has 5, and the third peg has 7." << endl
<< "The object of the game is to get the other player to take off the last ring." << endl << endl;
system("PAUSE");
system("CLS");
cout << "The rules of the game are as follow:" << endl
<< "1. You must take at least one ring off a peg per turn." << endl
<< "2. You can only take rings off a single peg in a given turn." << endl
<< "3. You can take as many rings off a single peg as you like." << endl
<< "4. You cannot skip a turn." << endl
<< "5. The player who takes the last ring off loses." << endl << endl;
system("PAUSE");
system("CLS");
do
{
cout << "Would you like to go first or second?(Type 1 or 2)" << endl;
cin >> Player;
if (Player == 1)
{
cout << "You are player one!" << endl
<< "You will go first." << endl << endl;
goto check;
}
else if (Player == 2)
{
cout << "You are player 2!" << endl
<< "You will go second" << endl << endl;
cout << "Game Master takes 3 rings off peg 3" << endl;
PegThree = 4;
do
{
check:
system("PAUSE");
system("CLS");
do
{
PegTotal = PegOne + PegTwo + PegThree;
if (PegTotal == 1)
{
cout << "Peg One has " << PegOne << " rings left." << endl
<< "Peg Two has " << PegTwo << " rings left." << endl
<< "Peg Three has " << PegThree << " rings left." << endl << endl;
cout << "You must take the last ring. Your Lose!!!" << endl;
system("PAUSE");
system("CLS");
goto end;
}
else if (PegTotal == 0)
{
break;
}
else
cout << "Peg One has " << PegOne << " rings left." << endl
<< "Peg Two has " << PegTwo << " rings left." << endl
<< "Peg Three has " << PegThree << " rings left." << endl << endl;
cout << "Which peg will you take rings from?" << endl;
cin >> x;
if ((x == 1) && (PegOne == 0) || (x == 2) && (PegTwo == 0) || (x == 3) && (PegThree == 0))
{
cout << "That peg is empty." << endl;
system("PAUSE");
system("CLS");
}
else if (x != 1 && x != 2 && x != 3)
{
cout << "That is not a peg." << endl;
system("PAUSE");
system("CLS");
}
} while ((x != 1 && x != 2 && x != 3) || ((x == 1) && (PegOne == 0) || (x == 2) && (PegTwo == 0) || (x == 3) && (PegThree == 0)));
do
{
switch (x)
{
case 1:
cout << "How many rings would you like to take from peg " << x << ". Max(" << PegOne << ")." << endl;
cin >> y;
if (y > PegOne)
{
cout << "There are not that many rings on that peg." << endl << endl;
system("PAUSE");
system("CLS");
break;
}
else
{
PegOne = PegOne - y;
y = 0;
switchRepeat = false;
break;
}
case 2:
cout << "How many rings would you like to take from peg " << x << ". Max(" << PegTwo << ")." << endl;
cin >> y;
if (y > PegTwo)
{
cout << "There are not that many rings on that peg." << endl << endl;
system("PAUSE");
system("CLS");
break;
}
else
{
PegTwo = PegTwo - y;
y = 0;
switchRepeat = false;
break;
}
case 3:
cout << "How many rings would you like to take from peg " << x << ". Max(" << PegThree << ")." << endl;
cin >> y;
if (y > PegThree)
{
cout << "There are not that many rings on that peg." << endl << endl;
system("PAUSE");
system("CLS");
break;
}
else
{
PegThree = PegThree - y;
y = 0;
switchRepeat = false;
break;
}
default:
switchRepeat = false;
break;
}
} while (switchRepeat == true);
PegTotal = PegOne + PegTwo + PegThree;
//----------------------------Peg Three Operations----------------------------
if ((PegThree > PegOne && PegThree> PegTwo) && PegThree > 1)
{
if (PegThree > PegOne && PegTwo > PegOne && PegOne != 0)
{
PegThree = PegThree - PegOne;
cout << "The Game Master takes " << PegOne << " rings from peg three." << endl;
//cout << "3-1" << endl;
}
else if ((PegTwo == 0 && PegOne == 1) || (PegOne == 0 && PegTwo == 1))
{
cout << "The Game Master takes " << PegThree << " rings from peg three." << endl;
PegThree = 0;
//cout << "3-2" << endl;
}
else if (PegThree > PegTwo && PegOne > PegTwo && PegTwo != 0)
{
PegThree = PegThree - PegTwo;
cout << "The Game Master takes " << PegTwo << " rings from peg three." << endl;
//cout << "3-3" << endl;
}
else if (PegTwo == 0 && PegOne == 0)
{
cout << "The Game Master takes " << (PegThree-1) << " rings from peg three." << endl;
PegThree = PegThree - (PegThree - 1);
//cout << "3-4" << endl;
}
else if ((PegThree > 3) > PegTwo >= (PegOne == 0) || ((PegThree > 3) > PegOne > (PegTwo == 0)))
{
PegThree--;
cout << "The Game Master takes 1 rings from peg three." << endl;
//cout << "3-5" << endl;
}
}
//-------------------------Peg Two Operations-------------------------------
else if ((PegTwo > PegOne && PegTwo > PegThree) && PegTwo > 1)
{
if (PegTwo > PegOne && PegThree > PegOne && PegOne != 0)
{
PegTwo = PegTwo - PegOne;
cout << "The Game Master takes " << PegOne << " rings from peg two." << endl;
//cout << "2-1" << endl;
}
else if ((PegThree == 0 && PegOne == 1) || (PegOne == 0 && PegThree == 1))
{
cout << "The Game Master takes " << PegTwo << " rings from peg three." << endl;
PegTwo = 0;
//cout << "2-2" << endl;
}
else if (PegTwo > PegThree && PegOne > PegThree && PegThree != 0)
{
PegTwo = PegTwo - PegThree;
cout << "The Game Master takes " << PegThree << " rings from peg two." << endl;
//cout << "2-3" << endl;
}
else if (PegThree == 0 && PegOne == 0)
{
cout << "The Game Master takes " << (PegTwo-1) << " rings from peg two." << endl;
PegTwo = PegTwo - (PegTwo - 1);
//cout << "2-4" << endl;
}
else if ((PegTwo > 3) > PegThree >= (PegOne == 0) || ((PegTwo > 3) > PegOne > (PegThree == 0)))
{
PegTwo--;
cout << "The Game Master takes 1 rings from peg two." << endl;
//cout << "2-5" << endl;
}
}
//----------------------Peg One Operations---------------------------------
else if ((PegOne > PegTwo && PegOne >> PegThree) && PegOne > 1)
{
if (PegOne > PegTwo && PegTwo > PegThree && PegThree != 0)
{
PegOne = PegOne - PegThree;
cout << "The Game Master takes " << PegThree << " rings from peg one." << endl;
//cout << "1-1" << endl;
}
else if ((PegTwo == 0 && PegThree == 1) || (PegThree == 0 && PegTwo == 1))
{
cout << "The Game Master takes " << PegOne << " rings from peg three." << endl;
PegOne = 0;
//cout << "1-2" << endl;
}
else if (PegOne > PegThree && PegThree > PegTwo && PegTwo != 0)
{
PegOne = PegOne - PegTwo;
cout << "The Game Master takes " << PegTwo << " rings from peg one." << endl;
//cout << "1-3" << endl;
}
else if (PegTwo == 0 && PegThree == 0)
{
cout << "The Game Master takes " << PegOne << " rings from peg one." << endl;
PegOne = PegOne - (PegOne - 1);
//cout << "1-4" << endl;
}
else if ((PegOne > 3) > PegTwo >= (PegThree == 0) || ((PegOne > 3) > PegThree > (PegTwo == 0)))
{
PegOne--;
cout << "The Game Master takes 1 rings from peg one." << endl;
//cout << "1-5" << endl;
}
}
//------------------Computer must take last ring------------------
else if (PegTotal == 1)
{
cout << "The Game Master takes the last ring." << endl
<< "You Win!!!" << endl;
goto end;
}
//-------------------Computer has no winning scenarios-------------
else
{
do
{
srand(time(NULL));
x = rand() % 3 + 1;
if ((x == 1) && (PegOne == 0) || (x == 2) && (PegTwo == 0) || (x == 3) && (PegThree == 0))
{
//cout << "Error 01 x= " << x << endl;
}
else if (x != 1 && x != 2 && x != 3)
{
//cout << "Error 02 x= "<< x << endl;
}
else
{
cout << "The Game Master takes 1 ring from peg " << x << "." << endl;
break;
}
} while ((x != 1 && x != 2 && x != 3) || ((x == 1) && (PegOne == 0) || (x == 2) && (PegTwo == 0) || (x == 3) && (PegThree == 0)));
if ((x == 1 && PegOne != 0))
{
PegOne = PegOne - 1;
//cout << "PegOne-1" << endl;
goto check;
}
else if ((x == 2 && PegTwo != 0))
{
PegTwo = PegTwo - 1;
//cout << "PegTwo-1" << endl;
goto check;
}
else if ((x == 3 && PegThree != 0))
{
PegThree = PegThree - 1;
//cout << "PegThree-1" << endl;
goto check;
}
}
} while (PegOne > 0 || PegTwo > 0 || PegThree > 0);
}
else
{
cout << "You must choose to be player 1 or 2." << endl;
system("PAUSE");
system("CLS");
}
} while (Player != 1 && Player != 2);
end:
cout << "Would you like to play again?(Y/N)" << endl;
cin >> cAgain;
system("CLS");
} while (cAgain == 'Y' || cAgain == 'y');
return 0;
}
Here is the .cpp file.