5 Replies - 2698 Views - Last Post: 15 November 2009 - 08:09 PM Rate Topic: -----

#1 Ajaxx00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-November 09

Nim game help in dev c++

Post icon  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!


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



Is This A Good Question/Topic? 0
  • +

Replies To: Nim game help in dev c++

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6034
  • View blog
  • Posts: 23,417
  • Joined: 23-August 08

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

Was This Post Helpful? 0
  • +
  • -

#3 Ajaxx00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-November 09

Re: Nim game help in dev c++

Posted 15 November 2009 - 09:53 AM

Great! So, I'd put the sticks = pile_size (); in the main correct? Thanks!



View PostJackOfAllTrades, on 14 Nov, 2009 - 05:07 PM, said:

You have to save the value returned by a function into a variable.
sticks = pile_size();

Was This Post Helpful? 0
  • +
  • -

#4 Ajaxx00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-November 09

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.


#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]
Was This Post Helpful? 0
  • +
  • -

#5 GenHornet18  Icon User is offline

  • Darken the Radar

Reputation: 36
  • View blog
  • Posts: 629
  • Joined: 19-October 09

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'?
Was This Post Helpful? 1
  • +
  • -

#6 Ajaxx00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-November 09

Re: Nim game help in dev c++

Posted 15 November 2009 - 08:09 PM

That was dumb. thanks! Its working now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1