Thirteen Stones

C

coinjo

In "Thirteen Stones" game, two players alternately take 1, 2, or 3
stones from a pile of 13 stones until no stones are left. The last
player to pick up a stone is the winner.

I need to make a program that simulates the "Thirteen stones" game.
My program should alternately ask the players to select how many stones
they wish to remove from the pile. If the selection is valid (1, 2, or
3 stones), the selected number of stones should be removed from the
pile. If a player enters an invalid number of stones, the program
should display an error message and ask the player to select again.
Continue play until no stones are left, and display a message
indicating the winner.

Input: Number of stones (1, 2, or 3) chosen on each turn
Output: A message declaring the winner.

Any hints please?
 
R

red floyd

coinjo said:
In "Thirteen Stones" game, two players alternately take 1, 2, or 3
stones from a pile of 13 stones until no stones are left. The last
player to pick up a stone is the winner.

I need to make a program that simulates the "Thirteen stones" game.
My program should alternately ask the players to select how many stones
they wish to remove from the pile. If the selection is valid (1, 2, or
3 stones), the selected number of stones should be removed from the
pile. If a player enters an invalid number of stones, the program
should display an error message and ask the player to select again.
Continue play until no stones are left, and display a message
indicating the winner.

Input: Number of stones (1, 2, or 3) chosen on each turn
Output: A message declaring the winner.

Any hints please?

Accept input.
Process input.
Generate output.

In other words, we won't do your homework for you. Make your best
effort, show us what you've come up with, and then we'll help.

See FAQ 5.2: http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2
 
M

Mike Wahler

coinjo said:
In "Thirteen Stones" game, two players alternately take 1, 2, or 3
stones from a pile of 13 stones until no stones are left. The last
player to pick up a stone is the winner.

I need to make a program that simulates the "Thirteen stones" game.
My program should alternately ask the players to select how many stones
they wish to remove from the pile. If the selection is valid (1, 2, or
3 stones), the selected number of stones should be removed from the
pile. If a player enters an invalid number of stones, the program
should display an error message and ask the player to select again.
Continue play until no stones are left, and display a message
indicating the winner.

Input: Number of stones (1, 2, or 3) chosen on each turn
Output: A message declaring the winner.

Any hints please?

Yes. Show some effort. If you actually try, and
when you get stuck, post your code with specific
questions, then we'll help. Nobody is going to
do it for you.

-Mike
 
J

John Harrison

coinjo said:
In "Thirteen Stones" game, two players alternately take 1, 2, or 3
stones from a pile of 13 stones until no stones are left. The last
player to pick up a stone is the winner.

I need to make a program that simulates the "Thirteen stones" game.
My program should alternately ask the players to select how many stones
they wish to remove from the pile. If the selection is valid (1, 2, or
3 stones), the selected number of stones should be removed from the
pile. If a player enters an invalid number of stones, the program
should display an error message and ask the player to select again.
Continue play until no stones are left, and display a message
indicating the winner.

Input: Number of stones (1, 2, or 3) chosen on each turn
Output: A message declaring the winner.

Any hints please?

If you really can't make some progress on that question by yourself then
maybe you should consider another direction. Programming assignments
don't get much easier, its a single simple loop.

Now of course in attempting to solve this you might get stuck, that's
expected, but you should be able to at least attempt a solution by yourself.

john
 
K

Karl Heinz Buchegger

coinjo said:
In "Thirteen Stones" game, two players alternately take 1, 2, or 3
stones from a pile of 13 stones until no stones are left. The last
player to pick up a stone is the winner.

I need to make a program that simulates the "Thirteen stones" game.
My program should alternately ask the players to select how many stones
they wish to remove from the pile. If the selection is valid (1, 2, or
3 stones), the selected number of stones should be removed from the
pile. If a player enters an invalid number of stones, the program
should display an error message and ask the player to select again.
Continue play until no stones are left, and display a message
indicating the winner.

Input: Number of stones (1, 2, or 3) chosen on each turn
Output: A message declaring the winner.

Any hints please?

It boils down to having an int variable, maybe you call it 'pile',
that is counted down, where users tell the program how much it
has to count down. When 'pile' reaches 0, the user doing the last
input is the winner.
 
C

coinjo

#include<iostream.h>
void main()
{
char selection;
int stones=13;
int done1=0;
while(stones>0 && done1!=1)
{
if(stones>0)
{
selection=0;
cout<<"Player 1 Enter Your Selection"<<endl;
cin>>selection;
if(selection=='1')
{
if(stones>0)
stones=stones-1;
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}
if(selection=='2')
{
if(stones>1)
stones=stones-2;
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}
if(selection=='3')
{
if(stones>2)
stones=stones-3;
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}

if(stones==0)
{
cout<<"Player 1 Wins"<<endl;
done1=1;
}
}

if(stones>0 && done1!=1)
{cout<<"Player 2 Enter Your Selection"<<endl;
cin>>selection;
if(selection=='1')
{
if(stones>0)
{
stones=stones-1;
}
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}


if(selection=='2')
{
if(stones>10)
{
stones=stones-2;
}
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}
if(selection=='3')
{
if(stones>2)
{
stones=stones-3;
}
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}
if(stones==0)
{
cout<<"Player 2 wins"<<endl;
done1=1;
}

}
}
}

This is the best i can come up with. But there are problems in its
output. When i constantly enter 1. It should stop at player 1 wins but
it stops at player 2 wins. Now that i have tried the code. Can anyone
help me?
 
M

mlimber

coinjo said:
#include<iostream.h>
void main()
{
char selection;
int stones=13;
int done1=0;
while(stones>0 && done1!=1)
{
if(stones>0)
{
selection=0;
cout<<"Player 1 Enter Your Selection"<<endl;
cin>>selection;
if(selection=='1')
{
if(stones>0)
stones=stones-1;
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}
if(selection=='2')
{
if(stones>1)
stones=stones-2;
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}
if(selection=='3')
{
if(stones>2)
stones=stones-3;
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}

if(stones==0)
{
cout<<"Player 1 Wins"<<endl;
done1=1;
}
}

if(stones>0 && done1!=1)
{cout<<"Player 2 Enter Your Selection"<<endl;
cin>>selection;
if(selection=='1')
{
if(stones>0)
{
stones=stones-1;
}
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}


if(selection=='2')
{
if(stones>10)
{
stones=stones-2;
}
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}
if(selection=='3')
{
if(stones>2)
{
stones=stones-3;
}
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}
if(stones==0)
{
cout<<"Player 2 wins"<<endl;
done1=1;
}

}
}
}

This is the best i can come up with. But there are problems in its
output. When i constantly enter 1. It should stop at player 1 wins but
it stops at player 2 wins. Now that i have tried the code. Can anyone
help me?

Here's a more readable version of the program:

#include<iostream> // N.B., iostream.h is deprecated
using namespace std;

int main() // N.B., not void main()
{
char selection;
int stones=13;
int done1=0; // Could be a bool instead of an int
while(stones>0 && done1!=1)
{
if(stones>0) // Redundant. The while condition ensures that
// this is true.
{
selection=0;
cout<<"Player 1 Enter Your Selection"<<endl;
cin>>selection;
if(selection=='1')
{
if(stones>0) // Redundant
stones=stones-1; // stones -= 1; is more
compact
else
{ // This else condition will never be reached
// because stones will always be > 0. If it were
reached,
// however, it would ask the user for input but not
test
// to see if the input is 1 again. The same thing
applies
// to the other else's below, except they can be
reached!

cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}

if(selection=='2') // Should be: else if( selection=='2' )
{
if(stones>1)
stones=stones-2;
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}

if(selection=='3') // Use else if here, too
{
if(stones>2)
stones=stones-3;
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;

}
}

if(stones==0)
{
cout<<"Player 1 Wins"<<endl;
done1=1;
// continue; // Would skip the next part
}
}

if(stones>0 && done1!=1)
{
cout<<"Player 2 Enter Your Selection"<<endl;
cin>>selection;
if(selection=='1')
{
if(stones>0) // Redundant
{
stones=stones-1;
}

else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;

}
}

if(selection=='2')
{
if(stones>10)
{
stones=stones-2;
}

else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}

if(selection=='3')
{
if(stones>2)
{
stones=stones-3;
}

else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}

if(stones==0)
{
cout<<"Player 2 wins"<<endl;
done1=1;
}
}
}
return 0;
}


On a cursory reading, I'd guess that the faulty error checking is your
problem. If you enter the thirteen 1's without error, it might work
(though I didn't test it). What you need to do is either learn to use
the debugger that is undoubtedly included with your development
environment, or more simply put some extra cout's in the code so you
can tell what the program is doing at each point.

Cheers! --M
 
K

Karl Heinz Buchegger

if(stones>0 && done1!=1)
{cout<<"Player 2 Enter Your Selection"<<endl;
cin>>selection;
if(selection=='1')
{
if(stones>0)
{
stones=stones-1;
}
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}

if(selection=='2')
{
if(stones>10)

10 ???
{
stones=stones-2;
} ....

This is the best i can come up with. But there are problems in its
output.

My I suggest you output the number of stones left on the pile
before you ask each player for his turn?
When i constantly enter 1. It should stop at player 1 wins but
it stops at player 2 wins. Now that i have tried the code. Can anyone
help me?

If I run your program and always enter 1, then the program ends with
'Player 1 wins'

Hint: it is always a good idea, to output a few intermediate results.
Eg. before you ask each player for his choice, show him the number
of stones left on the pile.
Also: After getting an input from the user, it is also a good idea
to report to the user what the program has read and what it does with that
input.
eg.

if( stones > 0 )
{
selection = 0;
cout << "There are " << stones << " stones left on the pile.\n";
cout << "Player 1 Enter Your Selection (1-3)" << endl;

cin >> selection;
if( selection == '1' )
{
if( stones > 0 )
{
cout << "Player 1 choosed to take 1 stone.\n";
stones = stones-1;
}
else
{
...

This not only reduces the confusion for the person sitting infront
of the computer. It also allows the devloper of the program to monitor
if things go the way he intended them to go.

Hint 2:
A few whitespace characters in the source code can do wonders in readability.
Indenting the code is not only a luxary, but is also a good tool in fighting
bugs (although that might have happend during your code posting)
 
J

Jim Langston

coinjo said:
#include<iostream.h>
void main()
{
char selection;
int stones=13;
int done1=0;
while(stones>0 && done1!=1)
{
if(stones>0)
{
selection=0;
cout<<"Player 1 Enter Your Selection"<<endl;
cin>>selection;
if(selection=='1')
{
if(stones>0)
stones=stones-1;
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}
if(selection=='2')
{
if(stones>1)
stones=stones-2;
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}
if(selection=='3')
{
if(stones>2)
stones=stones-3;
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}

Why are you making selection a character instead of an int? Would make it a
lot easier for you.

int selection;
....
cin >> selection;

Now you can get rid of your 3 if statments and do a little math.

if ( stones >= selection )
stones -= selection;
else
cout << "Invalid Selection" << std::endl;

You'll need to change your logic there, beucase your
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}

isn't enough. You're having the user reinput the selection, but you've
already passed up where you do anything with it. Perhaps you might want to
look at a while block...

while ( selection = 0 && stones > 0 )
{
// do input here and checking and subtraction, etc...
// if they selected an invalid amount set selection to 0
// so they have to put it in again
}
if(stones==0)
{
cout<<"Player 1 Wins"<<endl;
done1=1;
}
}

if(stones>0 && done1!=1)
{cout<<"Player 2 Enter Your Selection"<<endl;
cin>>selection;
if(selection=='1')
{
if(stones>0)
{
stones=stones-1;
}
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}


if(selection=='2')
{
if(stones>10)
{
stones=stones-2;
}
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}
if(selection=='3')
{
if(stones>2)
{
stones=stones-3;
}
else
{
cout<<"Invalid Selection. Enter Again"<<endl;
cin>>selection;
}
}
if(stones==0)
{
cout<<"Player 2 wins"<<endl;
done1=1;
}

}
}
}

This is the best i can come up with. But there are problems in its
output. When i constantly enter 1. It should stop at player 1 wins but
it stops at player 2 wins. Now that i have tried the code. Can anyone
help me?

You are duplicating code for the players. You do input for 1, then the
other. You don't need to. The exact same logic applies in both cases. The
only thing that changes is the player number.

int PlayerNumber = 1;
while(stones>0 && done1!=1)
{
// blah blah
// input, see if won, etc.. here. at end:

if ( PlayerNumber == 1 }
PlayerNumber = 2;
else
PlayerNumber = 1;

// If you want you can use trinary for that instead:
// PlayerNumber = ( PlayerNumber == 1 ? 2 : 1 );
}
 
C

coinjo

Please suggest changes in the program posted by mlimber and also please
post the full code again as it is very hard to understand your
suggestions.
 
M

mlimber

coinjo said:
Please suggest changes in the program posted by mlimber and also please
post the full code again as it is very hard to understand your
suggestions.

We're not going to do your homework for you. Read the post again; I
think it is sufficiently clear. If you disagree, ask specific questions
about it. More importantly, note that this discussion is not concerned
with the C++ language proper and is thus off topic (as I've noted in
the changed subject line). Please ask in one of the groups I mentioned
in my previous post.

Cheers! --M

PS, You should quote the post you're referring to and put your comments
*below* it. Not everyone is using Google groups to read these messages,
and quoting makes it easier for them to follow the discussion. (To
quote with Google, click on "show options" beside the message you want
to respond to, and then click "Reply" in the revealed header.)
 
C

coinjo

#include<iostream.h>
void main()

{
char selection;
int stones=13;
int done1=0;
while(stones>0 && done1!=1)
{
if(stones>0)
{
selection=0;
cout<<"Player 1 Enter Your Selection"<<endl;
cin>>selection;
if(selection=='1')
{
if(stones>0)
stones=stones-1;
}


else if(selection=='2')
{
if(stones>1)
stones=stones-2;
}


else if(selection=='3')
{
if(stones>2)
stones=stones-3;
}


if(stones==0)
{
cout<<"Player 1 Wins"<<endl;
done1=1;

}
}


if(stones>0 && done1!=1)
{
cout<<"Player 2 Enter Your Selection"<<endl;
cin>>selection;
if(selection=='1')
{
stones=stones-1;
}


if(selection=='2')
{
if(stones>10)
{
stones=stones-2;
}
}


if(selection=='3')
{
if(stones>2)
{
stones=stones-3;
}
}


if(stones==0)
{
cout<<"Player 2 wins"<<endl;
done1=1;
}
}
}
}

i have edited the code somewhat and removed the error part of the
program. But still the program only works when i enter 1's and not for
two's or three's. Please suggest changes in the above code.
 
C

coinjo

#include<iostream.h>
void main()
{
int selection=0;
int stones=13;
int player=0;

while(stones>0)

{

cout<<"Enter Player"<<endl;

cin>>player;

{

cout<<"Enter Your Selection"<<endl;

cin>>selection;

if(stones>=selection)

{

stones=stones-selection;

}

else

cout<<"Invalid Selestion"<<endl;

}

if(stones==0)

{

if(player==1)

{

cout<<"Player 1 Wins!"<<endl;

}

else

cout<<"Player 2 Wins!"<<endl;

}

}
}


Thanks to all of you for your generous help. I was finally able to make
the program work with the above syntax. Please Check it and tell me if
you find any errors in it.
 
J

Jim Langston

coinjo said:
#include<iostream.h>
void main()
{
int selection=0;
int stones=13;
int player=0;

while(stones>0)

{

cout<<"Enter Player"<<endl;

cin>>player;

{

cout<<"Enter Your Selection"<<endl;

cin>>selection;

if(stones>=selection)

{

stones=stones-selection;

}

else

cout<<"Invalid Selestion"<<endl;

}

if(stones==0)

{

if(player==1)

{

cout<<"Player 1 Wins!"<<endl;

}

else

cout<<"Player 2 Wins!"<<endl;

}

}
}


Thanks to all of you for your generous help. I was finally able to make
the program work with the above syntax. Please Check it and tell me if
you find any errors in it.

If you provide here a complete working program that does what you want, I'll
show you how I would do it.

You should probably be posting in:
alt.comp.lang.learn.c-c++

as there is a lot more tolerance there for beginner questions on language
syntax.
 
J

Jim Langston

coinjo said:
Can anybody tell me what does stones -= 1 means?

stones = stones - 1;
stones -= 1;

Those two lines do the exact same thing. As would:

stones = stones + x;
stones += x;

+= and -= are just a shortcut way of doing it and is quite common in use.
 
M

Mike Wahler

coinjo said:
Can anybody tell me what does stones -= 1 means?

It's the same as:

stones = stones - 1;

Similar operators are:

x += y /* x = x + y */
x *= y /* x = x * y */
x /= y /* x = x / y */


Also equivalent to

stones -= 1

is

--stones

-Mike
 
M

Mike Wahler

coinjo said:
#include<iostream.h>

#include <iostream> /* declares 'cin' and 'cout' */
#include <ostream> /* declares 'endl' */

using namespace std; /* all library names (except macros)
are defined in namespace 'std' */
void main()

int main() /* main() is *required* to have
return type 'int' */
{
int selection=0;
int stones=13;
int player=0;

while(stones>0)

{

cout<<"Enter Player"<<endl;

cin>>player;

What happens if I type "abc" at this prompt?
{

cout<<"Enter Your Selection"<<endl;

cin>>selection;

What happens if I type "abc" at this prompt?
if(stones>=selection)

{

stones=stones-selection;

}

else

cout<<"Invalid Selestion"<<endl;

}

if(stones==0)

{

if(player==1)

{

cout<<"Player 1 Wins!"<<endl;

}

else

cout<<"Player 2 Wins!"<<endl;

}

}

return 0; /* optional, but imo 'good practice' */

Please indent your code, it's hard to read otherwise.
Thanks to all of you for your generous help. I was finally able to make
the program work with the above syntax. Please Check it and tell me if
you find any errors in it.

See above.

-Mike
 
M

mlimber

Mike said:
#include <iostream> /* declares 'cin' and 'cout' */
#include <ostream> /* declares 'endl' */
[snip]

There's no need to include ostream if iostream is already included.

Cheers! --M
 
M

mlimber

Mike said:
What happens if I type "abc" at this prompt?
[snip]

One more comment for the OP: what happens if you enter any other number
(e.g., -10, 42000, 8675309)? You shouldn't be letting the user control
this variable since the rules of the game mandate that turns alternate
between two and only two players. Instead, you should put the logic for
the players in a loop:

while( stones > 0 )
{
for( int player = 1; player <= 2; player++ )
{
// Put stone selection logic here
}
}

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top