Connecting Tech Pros Worldwide Help | Site Map

Thirteen Stones

coinjo
Guest
 
Posts: n/a
#1: Oct 20 '05
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?

red floyd
Guest
 
Posts: n/a
#2: Oct 20 '05

re: Thirteen Stones


coinjo wrote:[color=blue]
> 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?
>[/color]

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-lit...t.html#faq-5.2

Mike Wahler
Guest
 
Posts: n/a
#3: Oct 20 '05

re: Thirteen Stones



"coinjo" <coinjo@gmail.com> wrote in message
news:1129772179.244052.66240@g49g2000cwa.googlegro ups.com...[color=blue]
> 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?[/color]

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


John Harrison
Guest
 
Posts: n/a
#4: Oct 20 '05

re: Thirteen Stones


coinjo wrote:[color=blue]
> 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?
>[/color]

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
Karl Heinz Buchegger
Guest
 
Posts: n/a
#5: Oct 20 '05

re: Thirteen Stones


coinjo wrote:[color=blue]
>
> 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?[/color]

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.


--
Karl Heinz Buchegger
kbuchegg@gascad.at
coinjo
Guest
 
Posts: n/a
#6: Oct 20 '05

re: Thirteen Stones


#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?

mlimber
Guest
 
Posts: n/a
#7: Oct 20 '05

re: Thirteen Stones


coinjo wrote:[color=blue]
> #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?[/color]

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

Karl Heinz Buchegger
Guest
 
Posts: n/a
#8: Oct 20 '05

re: Thirteen Stones


coinjo wrote:[color=blue]
>[/color]
[color=blue]
> 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)[/color]

10 ???
[color=blue]
> {
> stones=stones-2;
> }[/color]
....[color=blue]
>
> This is the best i can come up with. But there are problems in its
> output.[/color]

My I suggest you output the number of stones left on the pile
before you ask each player for his turn?
[color=blue]
> 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?[/color]

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)

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Jim Langston
Guest
 
Posts: n/a
#9: Oct 20 '05

re: Thirteen Stones


"coinjo" <coinjo@gmail.com> wrote in message
news:1129814432.406075.269870@z14g2000cwz.googlegr oups.com...[color=blue]
> #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;[/color]
[color=blue]
> 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;
> }
> }[/color]

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
}
[color=blue]
> 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?[/color]

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 );
}


coinjo
Guest
 
Posts: n/a
#10: Oct 20 '05

re: Thirteen Stones


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.

mlimber
Guest
 
Posts: n/a
#11: Oct 20 '05

re: Thirteen Stones


coinjo wrote:[color=blue]
> 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.[/color]

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.)

coinjo
Guest
 
Posts: n/a
#12: Oct 21 '05

re: Thirteen Stones


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

coinjo
Guest
 
Posts: n/a
#13: Oct 21 '05

re: Thirteen Stones


#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.

coinjo
Guest
 
Posts: n/a
#14: Oct 21 '05

re: Thirteen Stones


#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.

Jim Langston
Guest
 
Posts: n/a
#15: Oct 21 '05

re: Thirteen Stones


"coinjo" <coinjo@gmail.com> wrote in message
news:1129851632.439056.211800@g14g2000cwa.googlegr oups.com...[color=blue]
> Can anybody tell me what does stones -= 1 means?
>[/color]

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.


Jim Langston
Guest
 
Posts: n/a
#16: Oct 21 '05

re: Thirteen Stones


"coinjo" <coinjo@gmail.com> wrote in message
news:1129853765.158212.326820@g49g2000cwa.googlegr oups.com...[color=blue]
> #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.[/color]

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.


Mike Wahler
Guest
 
Posts: n/a
#17: Oct 21 '05

re: Thirteen Stones



"coinjo" <coinjo@gmail.com> wrote in message
news:1129853765.158212.326820@g49g2000cwa.googlegr oups.com...[color=blue]
> #include<iostream.h>[/color]

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

using namespace std; /* all library names (except macros)
are defined in namespace 'std' */
[color=blue]
> void main()[/color]

int main() /* main() is *required* to have
return type 'int' */
[color=blue]
> {
> int selection=0;
> int stones=13;
> int player=0;
>
> while(stones>0)
>
> {
>
> cout<<"Enter Player"<<endl;
>
> cin>>player;[/color]

What happens if I type "abc" at this prompt?
[color=blue]
>
> {
>
> cout<<"Enter Your Selection"<<endl;
>
> cin>>selection;[/color]

What happens if I type "abc" at this prompt?
[color=blue]
>
> 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;
>
> }
>
> }[/color]

return 0; /* optional, but imo 'good practice' */
[color=blue]
> }[/color]

Please indent your code, it's hard to read otherwise.
[color=blue]
>
>
> 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.[/color]

See above.

-Mike


Mike Wahler
Guest
 
Posts: n/a
#18: Oct 21 '05

re: Thirteen Stones



"coinjo" <coinjo@gmail.com> wrote in message
news:1129851632.439056.211800@g14g2000cwa.googlegr oups.com...[color=blue]
> Can anybody tell me what does stones -= 1 means?[/color]

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


mlimber
Guest
 
Posts: n/a
#19: Oct 21 '05

re: Thirteen Stones


Mike Wahler wrote:[color=blue]
>
> #include <iostream> /* declares 'cin' and 'cout' */
> #include <ostream> /* declares 'endl' */[/color]
[snip]

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

Cheers! --M

mlimber
Guest
 
Posts: n/a
#20: Oct 21 '05

re: Thirteen Stones


Mike Wahler wrote:[color=blue]
> "coinjo" <coinjo@gmail.com> wrote in message[color=green]
> > cout<<"Enter Player"<<endl;
> >
> > cin>>player;[/color]
>
> What happens if I type "abc" at this prompt?[/color]
[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

Alf P. Steinbach
Guest
 
Posts: n/a
#21: Oct 21 '05

re: Thirteen Stones


* mlimber:[color=blue]
> Mike Wahler wrote:[color=green]
> >
> > #include <iostream> /* declares 'cin' and 'cout' */
> > #include <ostream> /* declares 'endl' */[/color]
> [snip]
>
> There's no need to include ostream if iostream is already included.[/color]

Formally there is, or might be, depending on one's faith in intentions instead
of the Literal Word (TM) of the standard.

<iostream> only forward-declares the eight or so standard iostream objects.

Practically I doubt there is any compiler where <iostream> isn't enough,
because it would break so much code to require <ostream> in addition, but just
to help people understand that compilesWithMyCompiler != isStandard, I usually
add <ostream> in my examples.


Cheers,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Karl Heinz Buchegger
Guest
 
Posts: n/a
#22: Oct 21 '05

re: Thirteen Stones


coinjo wrote:[color=blue]
>
> #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.[/color]

Well. First of all, as already noted, when the program is run
your users have no idea of how many stones are left on the pile.
Actually, the users *never* see (unless they study your code), how
many piles are there on the pile (eg. in europe this game is played
with 21 stones :-)

But then, try the following:
start out with 13 stones. Let the porgram ask user 1 for his choice.
And now user 1 wants 13 stones. user 1 wins. :-)

Start with fixing that. (A user cannot draw more then 3 or less then 1 stone,
it is a simple if)

All other things already mentioned, like inputting 'abc' can wait
until you get this right.

Also there seems to be something wrong with your overall game logic.
How come the program asks *me* whose turn it is. Shouldn't the program
know this? Otherwise I always enter 0 when the program asks me for the
player, and player 1 thus always wins.

If you need a variable to toggle between 0 and 1 you can use:

player = 1 - player;

each time that assignment is executed, it changes from 0 to 1 or vice versa
depending on the previous state.

I know, programmers are the worst testing persons. Nevertheless all of the
above mentioned things are simple ones. You should have, no, you *must* have
found them all by yourself.

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Default User
Guest
 
Posts: n/a
#23: Oct 21 '05

re: Thirteen Stones


Alf P. Steinbach wrote:
[color=blue]
> * mlimber:[color=green]
> > Mike Wahler wrote:[color=darkred]
> > >
> > > #include <iostream> /* declares 'cin' and 'cout' */
> > > #include <ostream> /* declares 'endl' */[/color]
> > [snip]
> >
> > There's no need to include ostream if iostream is already included.[/color]
>
> Formally there is, or might be, depending on one's faith in
> intentions instead of the Literal Word (TM) of the standard.[/color]


There's supposed to be an outstanding issue before the committee on
this subject (or there was a couple years ago). I'm sure what the
current status is.



Brian
Mike Wahler
Guest
 
Posts: n/a
#24: Oct 21 '05

re: Thirteen Stones



"mlimber" <mlimber@gmail.com> wrote in message
news:1129900609.285340.158360@f14g2000cwb.googlegr oups.com...[color=blue]
> Mike Wahler wrote:[color=green]
>>
>> #include <iostream> /* declares 'cin' and 'cout' */
>> #include <ostream> /* declares 'endl' */[/color]
> [snip]
>
> There's no need to include ostream if iostream is already included.[/color]

Not true. 'std::endl' is only guaranteed to be declared
by <ostream>. It might indeed be indirectly declared with
only <iostream> (via headers including headers), but you have
no guarantee of that from the standard language.

-Mike


Closed Thread