PDA

View Full Version : Coin Game Help



TehNeonFishy
11-02-2012, 09:48 PM
Hey guys, for uni I have to do a game that kinda is just taking coins away from a pile and the last one to take from the pile is the loser. It is against the computer, which I have done, but I have a function that handles the input and output.

I've been told that I can't do such things, what would be better, having a procedure that does it? Or having it in the main game loop.

Rezozo
11-02-2012, 09:49 PM
Procedure. Always trust the procedures ;)
Edit: Unless its a very short program. What are the specifics of the game?

Enslaved
11-02-2012, 10:25 PM
Hey guys, for uni I have to do a game that kinda is just taking coins away from a pile and the last one to take from the pile is the loser. It is against the computer, which I have done, but I have a function that handles the input and output.

I've been told that I can't do such things, what would be better, having a procedure that does it? Or having it in the main game loop.

google "nim algorithms" it should give you the awnser.

Stack handler?

TehNeonFishy
11-03-2012, 12:23 AM
Ah yeah it seems alot like Nim, but with coins. Just a stack of say 25 coins, each turn the player or computer takes an amount of coins from 1-5 trying to make the other person take the last coin.

I guess it would be alright in the main loop since it is pretty much a very short/simple game, if I have time I might make it look all fancy. xD

Rezozo
11-03-2012, 12:40 AM
Lol, nice! You would need a predicting algorithm for that.

Enslaved
11-03-2012, 12:42 AM
Lol, nice! You would need a predicting algorithm for that.

not really, simple maths formula

Rezozo
11-03-2012, 12:50 AM
Not simple for me ;)
Also...I see a RunAway!

TehNeonFishy
11-03-2012, 09:51 PM
I need to have the game show how many coins left in 'o's, how the hell do I do that o.O

Brandon
11-09-2012, 12:55 PM
I need to have the game show how many coins left in 'o's, how the hell do I do that o.O


Meh.. close enough.. I don't know any algorithms and I'm lazy to find/write one.


#include <iostream>

void Computer(int &Coins)
{
if (Coins > 11 || Coins == 6)
{
Coins -= 5;
std::cout<<"The computer removed 5 coins leaving a total of: "<<Coins<<std::endl;
}
else if (Coins > 6)
{
Coins -= 1;
std::cout<<"\nThe computer removed 1 coin leaving a total of: "<<Coins<<std::endl;
}
else
{
for (int I = 0; I < Coins; ++I)
{
if (Coins - I == 1)
{
Coins -= I;
std::cout<<"\nThe computer removed "<<I<<" coin(s) leaving a total of: "<<Coins<<std::endl;
}
}
}
}

void Player(int &Coins)
{
int AmountRemoved = 0;
while(true)
{
std::cout<<"\nPlease enter how many coins you'd like to remove: ";
std::cin>> AmountRemoved;
std::cin.ignore();
if (AmountRemoved <= Coins)
break;
}
Coins -= AmountRemoved;
std::cout<<"\nYou removed "<<AmountRemoved<<" coin(s) leaving a total of: "<<Coins<<std::endl;
}

int main()
{
int Coins = 25;
bool WhosTurn = false;

while(Coins != 0)
{
WhosTurn = true;
Computer(Coins);
WhosTurn = false;
Player(Coins);
std::cout<<"\n\n";
}

if (WhosTurn)
std::cout<<"\n\nYou won!"<<std::endl;
else
std::cout<<"\n\nYou Lost!"<<std::endl;
}