I am trying to write a program that requires input validation through functions. The idea behind it is much like the 21 stones only it is with 13 and the computer always wins. The game starts with 13 stones and the computer will always choose 1 on the first turn creating a multiple of 4 scenario. This means if the user takes 3 computer takes 1, user takes 2 computer takes 2 and so on until no stones remain. My problem is I am having a hard time getting my head around functions and how data is called from the parameters within so any help with this would be greatly appreciated!
/*******************************************************************************
The Stone Game...
Written By - Ralph C.
11/11/15
*******************************************************************************/
#include <iostream>
usingnamespace std;
//function prototypes
bool validPick(int numStones);
int computerPick(int stones_in_pile, int player2taken);
int playerPick(int stones_in_pile);
int main()
{
int stones_left = 13, P1Taken, P2Taken;
cout << "You have shosen to play the game 13 stones against me, the MIGHTY "
<< "COMPUTER!\nThe object of the game is to take 1, 2 or 3 stones from"
<< " the pile on your turn.\nThe player that removes the last stone "
<< "or stones from the pile wins the game.\nGood Luck... You will need"
<< " it! I NEVER LOOSE!!"
<< endl << endl;
computerPick(stones_left, P2Taken);
playerPick(P1Taken);
validPick(stones_left);
//game logic here -- This is far from done.
stones_left -= P1Taken;
stones_left -= P2Taken;
return 0;
}
/******************************************************************************\
* Validate the picked number 1-3 are only valid numbers to choose from. *
\******************************************************************************/
bool validPick(int numStones)
{
if((numStones < 1) || (numStones >3))
cout << "Invalid Selection. 1-3 is all you can have!";
elsereturn numStones;
}
/******************************************************************************\
* Computer's function calls. Should start with 1. We always want the computer *
* to win the game. *
\******************************************************************************/
int computerPick(int stones_in_pile, int player2taken)
{
if(player2taken == 0)
stones_in_pile -= 1;
else
{
if(player2taken == 1)
stones_in_pile -= 3;
elseif(player2taken == 2)
stones_in_pile -= 2;
else
stones_in_pile -=1;
}
return stones_in_pile;
}
/******************************************************************************\
* Player's Pick function call goes here. The player goes second *
\******************************************************************************/
int playerPick(int stones_in_pile)
{
cout << "Please choose the ammount of stones. 1-3 only! : ";
cin >> stones_in_pile;
return stones_in_pile;
}
Made suggested additions and it for the most part functions. Logic is screwed up however as it shows computers selections as the remainder of stones and when it reaches 0 the number just goes negative also the computer is not making a selection as intended.
/*******************************************************************************
The Stone Game...
Written By - Ralph C.
11/11/15
*******************************************************************************/
#include <iostream>
usingnamespace std;
//function prototypes
bool validPick(int numStones);
int computerPick(int stones_in_pile, int player2taken);
int playerPick(int stones_in_pile);
int main()
{
int stones_left = 13, P1Taken, P2Taken;
cout << "You have shosen to play the game 13 stones against me, the MIGHTY "
<< "COMPUTER!\nThe object of the game is to take 1, 2 or 3 stones from"
<< " the pile on your turn.\nThe player that removes the last stone "
<< "or stones from the pile wins the game.\nGood Luck... You will need"
<< " it! I NEVER LOOSE!!"
<< endl << endl;
P2Taken = playerPick(stones_left);
P1Taken = computerPick(stones_left, P2Taken);
do{
if(validPick(stones_left >=1))
{
stones_left -= P2Taken;
cout << "After taken " << P2Taken << " there are " << stones_left
<< " stones remaining.";
cout << "The computer has taken " << P1Taken << " stones. Now only "
<< stones_left << " stones remain.";
playerPick(stones_left);
}
}while(validPick(stones_left >=1));
/* validPick(stones_left);
computerPick(stones_left, P2Taken);
playerPick(P1Taken);
do{
stones_left -= P1Taken;
stones_left -= P2Taken;
if
*/
return 0;
}
/******************************************************************************\
* Validate the picked number 1-3 are only valid numbers to choose from. *
\******************************************************************************/
bool validPick(int numStones)
{
if((numStones < 1) || (numStones >3))
{
cout << "Invalid Selection. 1-3 is all you can have!";
}
elsereturn numStones;
}
/******************************************************************************\
* Computer's function calls. Should start with 1. We always want the computer *
* to win the game. *
\******************************************************************************/
int computerPick(int stones_in_pile, int player2taken)
{
if(player2taken == 0)
stones_in_pile -= 1;
else
{
if(player2taken == 1)
stones_in_pile -= 3;
elseif(player2taken == 2)
stones_in_pile -= 2;
else
stones_in_pile -=1;
}
return stones_in_pile;
}
/******************************************************************************\
* Player's Pick function call goes here. The player goes second *
\******************************************************************************/
int playerPick(int stones_in_pile)
{
cout << "Please choose the ammount of stones. 1-3 only! : ";
cin >> stones_in_pile;
return stones_in_pile;
}
Further editing done. started adding logic but having trouble getting calls to execute properly. Would be great if someone can point me in the correct direction.
/*******************************************************************************
written by ralph c
11/11/15
*******************************************************************************/
#include <iostream>
usingnamespace std;
//function prototypes
bool validPick(int numStones);
int computerPick(int stones_in_pile, int player2taken);
int playerPick(int stones_in_pile);
int main()
{
int stones_left = 13, P1Taken, P2Taken;
cout << "You have shosen to play the game 13 stones against me, the MIGHTY "
<< "COMPUTER!\nThe object of the game is to take 1, 2 or 3 stones from"
<< " the pile on your turn.\nThe player that removes the last stone "
<< "or stones from the pile wins the game.\nGood Luck... You will need"
<< " it! I NEVER LOOSE!!"
<< endl << endl;
P2Taken = playerPick(stones_left);
P1Taken = computerPick(stones_left, P2Taken);
do{
if(validPick(stones_left >= 1) || validPick(stones_left <= 3))
{
playerPick(stones_left);
cout << "The computer has taken " << P1Taken << " stones. Now only "
<< stones_left << " stones remain.";
stones_left -= P2Taken;
cout << "After taken " << P2Taken << " there are " << stones_left
<< " stones remaining.";
}
}while(validPick(stones_left >= 1) || validPick(stones_left <= 3));
cout << "The computer wins!";
return 0;
}
/******************************************************************************/
bool validPick(int numStones)
{
if((numStones <= 0) || (numStones >= 4))
{
cout << "Invalid Selection. 1-3 is all you can have!";
returnfalse;
}
elsereturntrue;
}
/******************************************************************************/
int computerPick(int stones_in_pile, int player2taken)
{
if(player2taken == 1)
return 3;
elseif(player2taken == 2)
return 2;
elsereturn 1;
}
/******************************************************************************/
int playerPick(int stones_in_pile)
{
cout << "Please choose the ammount of stones. 1-3 only! : ";
cin >> stones_in_pile;
return stones_in_pile;
}
/*******************************************************************************
written by ralph c
11/11/15
*******************************************************************************/
#include <iostream>
usingnamespace std;
//function prototypes
bool validPick(int numStones);
int computerPick(int stones_in_pile, int player2taken);
int playerPick(int stones_in_pile);
int main()
{
int stones_left = 13, P1Taken, P2Taken;
cout << "You have shosen to play the game 13 stones against me, the MIGHTY "
<< "COMPUTER!\nThe object of the game is to take 1, 2 or 3 stones from"
<< " the pile on your turn.\nThe player that removes the last stone "
<< "or stones from the pile wins the game.\nGood Luck... You will need"
<< " it! I NEVER LOOSE!!"
<< endl << endl;
P2Taken = playerPick(stones_left);
P1Taken = computerPick(stones_left, P2Taken);
do{
if(validPick(stones_left >= 1) || validPick(stones_left <= 3))
{
playerPick(stones_left);
stones_left -= P1Taken;
cout << "The computer has taken " << P1Taken << " stones. Now only "
<< stones_left << " stones remain.";
stones_left -= P2Taken;
cout << "After taken " << P2Taken << " there are " << stones_left
<< " stones remaining.";
}
}while(validPick(stones_left >= 1) || validPick(stones_left <= 3));
cout << "The computer wins!";
return 0;
}
/******************************************************************************/
bool validPick(int numStones)
{
if((numStones <= 0) || (numStones >= 4))
{
cout << "Invalid Selection. 1-3 is all you can have!";
returntrue;
}
elsereturnfalse;
}
/******************************************************************************/
int computerPick(int stones_in_pile, int player2taken)
{
if(player2taken = 1)
return 3;
elseif(player2taken = 2)
return 2;
elsereturn 1;
}
/******************************************************************************/
int playerPick(int stones_in_pile)
{
cout << "Please choose the ammount of stones. 1-3 only! : ";
cin >> stones_in_pile;
return stones_in_pile;
}
After a lot of trial and error and more irritation than I had thought was coming for something as simple as this I have completed the program and it works as intended. Posting the final code just for the record. Much thanks to Coder777 for steering me in the right direction!!
#include <iostream>
#include <cstdlib>
usingnamespace std;
//prototype functions
int computerPick(int stones_in_pile, int player2taken);
int playerPick();
bool validPick(int numStones);
int main()
{
int stones_left = 13, P1Taken = 1, P2Taken = 0;
cout << "You have shosen to play the game 13 stones against me, the MIGHTY "
<< "COMPUTER!\nThe object of the game is to take 1, 2 or 3 stones from"
<< " the pile on your turn.\nThe player that removes the last stone "
<< "or stones from the pile wins the game.\nGood Luck... You will need"
<< " it! I NEVER LOOSE!!"
<< endl << endl;
cout << "\nThe Computer goes first!\nThe computer has choosen 1 ";
stones_left -= P1Taken;
cout << "stone.\nNow only " << stones_left << " stones remain.\n";
system("PAUSE");
do{
P2Taken = playerPick();
stones_left -= P2Taken;
cout << "\nYou have taken " << P2Taken << " stone"
<< (P2Taken > 1 ? "s" : " ") << " now only: "
<< stones_left << " stone"<< (stones_left > 1 ? "s" : " ")
<< " remain.";
P1Taken = computerPick(stones_left, P2Taken);
stones_left -= P1Taken;
cout << "\nThe computer has taken " << P1Taken<< " stone"
<< (P1Taken > 1 ? "s" : " ") << " now only "
<< stones_left << " stone" << (stones_left > 1 ? "s" : " ")
<< " remain.";
}while(stones_left > 0);
if(stones_left == 0)
cout << "\n\nThe computer wins!\n";
system("Pause");
return -4;
}
/******************************************************************************/
int computerPick(int stones_in_pile, int player2taken)
{
stones_in_pile = 4 - player2taken;
return stones_in_pile;
}
/******************************************************************************/
int playerPick()
{
int x;
cout << "\n\nPick the number of stones you would like to take."
<< "\nThis must be 1, 2 or 3."
<< "\n\nPlease Enter your selection : ";
cin >> x;
if (validPick(x))
return x;
else
x = playerPick();
return x;
}
/******************************************************************************/
bool validPick(int num_of_stones)
{
if(num_of_stones < 1 || num_of_stones > 3)
{
cout << "\n\nThat is not a valid selection.\nThe choice can only be "
<< "1, 2 or 3!\nPlease try again...";
returnfalse;
}
elsereturntrue;
}
I like the game and how it runs, but you should add a way to set the amount of stones by increments of four at the beginning to make it seem to let the user be able to win even though the computer would win anyway. There are also various typos in your program still. I have modified your code here:
#include <iostream>
#include <cstdlib>
usingnamespace std;
//prototype functions
int computerPick(int stones_in_pile, int player2taken);
int playerPick();
bool validPick(int numStones);
void Player_win();
int main()
{
int stones_left = 13, P1Taken = 1, P2Taken = 0;
cout << "You have chosen to play the game against me, the MIGHTY "
<< "COMPUTER!\nThe object of the game is to take 1, 2 or 3 stones from"
<< " the pile on your turn.\nThe player that removes the last stone "
<< "or stones from the pile wins the game.\nGood Luck... You will need"
<< " it! I NEVER LOOSE!!"
<< endl << endl;
system("pause");
system("cls");
cout << "\nThe Computer goes first!\nThe computer has chosen 1 ";
stones_left--;
cout << "stone.\nNow only " << stones_left << " stones remain.\n";
system("PAUSE");
do{
P2Taken = playerPick();
stones_left -= P2Taken;
cout << "\nYou have taken " << P2Taken << " stone"
<< (P2Taken > 1 ? "s" : " ") << " now only: "
<< stones_left << " stone"<< (stones_left > 1 ? "s" : " ")
<< " remain.";
P1Taken = computerPick(stones_left, P2Taken);
stones_left -= P1Taken;
cout << "\nThe computer has taken " << P1Taken<< " stone"
<< (P1Taken > 1 ? "s" : " ") << " now only "
<< stones_left << " stone" << (stones_left > 1 ? "s" : " ")
<< " remain.";
}while(stones_left > 0);
if(stones_left == 0)
cout << "\n\nThe computer wins!\n";
else
Player_win();
system("Pause");
return -4;
}
/******************************************************************************/
int computerPick(int stones_in_pile, int player2taken)
{
stones_in_pile = 4 - player2taken;
return stones_in_pile;
}
/******************************************************************************/
int playerPick()
{
int x;
cout << "\n\nPick the number of stones you would like to take."
<< "\nThis must be 1, 2 or 3."
<< "\n\nPlease Enter your selection : ";
cin >> x;
if (validPick(x))
return x;
else
x = playerPick();
return x;
}
/******************************************************************************/
bool validPick(int num_of_stones)
{
if(num_of_stones < 1 || num_of_stones > 3)
{
cout << "\n\nThat is not a valid selection.\nThe choice can only be "
<< "1, 2 or 3!\nPlease try again...";
returnfalse;
}
elsereturntrue;
}
/******************************************************************************/
void Player_win()
{
cout << "\n\nCongrats you have beaten me.\n";
}