#include <iostream>
#include "graphics.h"
#include "math.h"
using namespace std;
// This program will allow the user to play a game of Nim
// against the computer
// Here is where I will put all my Prototypes
void welcome (); // Welcomes the user and displays the rules.
int pile_size (); // Determines the size of the pile.
int users_turn (); // The users turn
int main ()
{
int sticks;
int sticks_removed;
welcome ();
pile_size ();
users_turn ();
cin.get ();
return 0;
}
// Allows the user to set how many sticks are in the pile
int pile_size ()
{
initwindow (700,700); // Set the window size
int sticks; // Number of sticks variable
int stickx = 250; // Starting x value of stick one
int sticky = 100; // Starting y value of stick one
do // This loop will run until they input a good number
{
cout << "How many sticks would you like the pile to start with?\n";
cout << "max is 40.\n";
cin >> sticks; cin.get ();
if (sticks > 40) // check to input to assure it meets the requirements
{
cout << "Maxium number of sticks is 40. Try Again.\n\n\n";
}
}
while (sticks > 40);
// This will draw sticks equal to the number chosen
for (int i = 0; i < sticks; ++i)
{
bar(stickx,sticky,stickx+200,sticky+10);
sticky = sticky + 14;
}
return sticks;
}
void welcome () //The function to welcome the user and display the rules.
{
char rules='y';
cout << "Welcome! We are going to play a game of Nim. \n";
cout << "Would you like to see the rules of the game?\n";
cin >> rules; cin.get ();
if (rules == 'y' || rules == 'Y')
{
cout << "The rules of the game are simple. \n"
<< "You will be asked to choose a number of sticks\n"
<< "in the pile to start. After that you and the computer\n"
<< "will take turns removing between 1 and 4 sticks from the\n"
<< "pile. The one who removes the last stick is the winner.\n"
<< "Choose wisely!\n\n";
}
cout << "Let's start the game!\n\n";
}
int users_turn ()
{
int sticks_removed;
int sticks;
do
{
cout << "It's your turn how many sticks would you like to remove?\n"
<< "Choose a number between 1-4.\n";
cin >> sticks_removed; cin.get ();
if (sticks_removed > 4 || sticks_removed < 1)
cout << "Please choose a number between 1-4\n";
}
while (sticks_removed > 4 || sticks_removed < 1);
sticks = sticks - sticks_removed;
cout << "There are " <<sticks <<" sticks remaining.";
return sticks;
}
Nim game help in dev c++
Page 1 of 15 Replies - 1961 Views - Last Post: 15 November 2009 - 08:09 PM
#1
Nim game help in dev c++
Posted 14 November 2009 - 05:58 PM
Hi, I'm trying to write a game of nim using devc++, with a player vs computer using functions. The problem I'm having is tracking the number of sticks. I thought the return sticks; command would allow them to be tracked through various functions, but I must be missing something. Thanks for any help!
Replies To: Nim game help in dev c++
#2
Re: Nim game help in dev c++
Posted 14 November 2009 - 06:07 PM
You have to save the value returned by a function into a variable.
sticks = pile_size();
#3
Re: Nim game help in dev c++
Posted 15 November 2009 - 09:53 AM
#4
Re: Nim game help in dev c++
Posted 15 November 2009 - 06:14 PM
Ok I got the program to track the correct number of sticks by placing sticks = pile_size (); in the users_turn (0); function, but now when I run the program it runs the pile_size function twice. What did I do wrong? Thanks for any help.
[/quote]
[/quote]
#include <iostream>
#include "graphics.h"
#include "math.h"
using namespace std;
// This program will allow the user to play a game of Nim
// against the computer
// Here is where I will put all my Prototypes
void welcome (); // Welcomes the user and displays the rules.
int pile_size (); // Determines the size of the pile.
int users_turn (); // The users turn
int main ()
{
welcome ();
pile_size ();
users_turn ();
cin.get ();
return 0;
}
// Allows the user to set how many sticks are in the pile
int pile_size ()
{
initwindow (700,700); // Set the window size
int sticks; // Number of sticks variable
int stickx = 250; // Starting x value of stick one
int sticky = 100; // Starting y value of stick one
do // This loop will run until they input a good number
{
cout << "How many sticks would you like the pile to start with?\n";
cout << "max is 40.\n";
cin >> sticks; cin.get ();
if (sticks > 40) // check to input to assure it meets the requirements
{
cout << "Maxium number of sticks is 40. Try Again.\n\n\n";
}
}
while (sticks > 40);
// This will draw sticks equal to the number chosen
for (int i = 0; i < sticks; ++i)
{
bar(stickx,sticky,stickx+200,sticky+10);
sticky = sticky + 14;
}
}
void welcome () //The function to welcome the user and display the rules.
{
char rules='y';
cout << "Welcome! We are going to play a game of Nim. \n";
cout << "Would you like to see the rules of the game?\n";
cin >> rules; cin.get ();
if (rules == 'y' || rules == 'Y')
{
cout << "The rules of the game are simple. \n"
<< "You will be asked to choose a number of sticks\n"
<< "in the pile to start. After that you and the computer\n"
<< "will take turns removing between 1 and 4 sticks from the\n"
<< "pile. The one who removes the last stick is the winner.\n"
<< "Choose wisely!\n\n";
}
cout << "Let's start the game!\n\n";
}
int users_turn ()
{
int sticks_removed;
int sticks;
sticks = pile_size ();
do
{
cout << "It's your turn how many sticks would you like to remove?\n"
<< "Choose a number between 1-4.\n";
cin >> sticks_removed; cin.get ();
if (sticks_removed > 4 || sticks_removed < 1)
cout << "Please choose a number between 1-4\n";
}
while (sticks_removed > 4 || sticks_removed < 1);
sticks = sticks - sticks_removed;
cout << "There are " <<sticks <<" sticks remaining.";
return sticks;
}
[\code]
[quote name='JackOfAllTrades' post='834171' date='14 Nov, 2009 - 05:07 PM']
You have to save the value returned by a function into a variable.
[code=cpp]sticks = pile_size();
[/quote]
[/quote]
#5
Re: Nim game help in dev c++
Posted 15 November 2009 - 07:40 PM
I don't know if you realize this or not but you do call it twice. Once in 'int main' and once in 'int users_turn'. Judging by the way your program acts i assume you didn't intend for it to be called in 'int main'?
#6
Re: Nim game help in dev c++
Posted 15 November 2009 - 08:09 PM
That was dumb. thanks! Its working now.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote






|