NIM in C++

This is a discussion on NIM in C++ within the C++ Programming forums, part of the General Programming Boards category; I need help trying to decide if the user is entering 3 numbers next to each other. In the game ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    12

    Question NIM in C++

    I need help trying to decide if the user is entering 3 numbers next to each other. In the game of circular Nim, the user can enter the total number of pieces he/she wants to use, than who goes first. How would i write code to say: if the entered numbers are next to each other (e.g 1 2 3, or in a 15 piece game 14,0,2) than its legal otherwise its not.

    See code below. Highlighted is the user function.


    Code:
    #include<iostream>
    #include<string>
    #include <iomanip>
    #include<cmath>
    using namespace std;
    
    
    void SetParamters(int&, bool&); //prompts user for paramters
    void FillBoard(int, char[]);
    void PrintBoard(char [], int);
    void ComputerTurn(char [], int&, int);
    void HumanTurn(char[], int&, int);
    
    
    
    
    int main ()
    {
    	int pieces,size, piecesSlection=0;
    	char GamePieces[20];
    	bool  COMPUTER_TURN=false;
    	
    
    SetParamters(pieces, COMPUTER_TURN);
    size=pieces-1;
    FillBoard(pieces, GamePieces);
    //PrintBoard(GamePieces, pieces);
    do
    {
    if (COMPUTER_TURN==true)
    {
    	ComputerTurn(GamePieces, piecesSlection, pieces);
    	PrintBoard(GamePieces,pieces);
    	COMPUTER_TURN=false;
    }
    else 
    { 
    	HumanTurn(GamePieces, piecesSlection, size);
    	PrintBoard(GamePieces,pieces);
    	COMPUTER_TURN=true;
    }
    }while (GamePieces[0]!='_');//needs adjustment
    
    
    return 0;
    }
    
    void SetParamters(int& pieces, bool& COMPUTER_TURN)
    {
    	char answer;
    	cout<<"Welcome To NIM"<<endl<<endl;
    	do
    	{
    
    	cout<<"How many pieces would you like to start with?"<<endl;
    	cin>>pieces;
    	if (pieces<5||20<pieces)
    	
    		cout<<"You can only choose a number between 5 and 20 try again."<<endl<<endl;
    	
    	}while(pieces<5||20<pieces);
    
    	cout<<"Would you like to go first? (Y/N)";
    	cin>>answer;
    	if (answer=='y'||answer=='Y')
    	{
    		cout<<"OK, you can go first."<<endl;
    		COMPUTER_TURN=false;
    	}
    		
    
    	else 
    	{
    	COMPUTER_TURN=true;
    	cout<<"OK, I will go first."<<endl; 
    	}
    	
    		
    }
    void FillBoard(int pieces, char GamePieces[])
    {
    	for (int i=0; i<pieces;i++)
    	
    		GamePieces[i]='o';
    	
    }
    void PrintBoard(char GamePieces[], int pieces)
    {
    	cout<<endl<<endl<<"HERE IS OUR CURRENT BOARD"<<endl<<endl;
    		
    	for (int i=0; i<pieces; i++)
    		cout<<left<<setw(3)<<i;
    	cout<<endl;
    	for (int i=0; i<pieces; i++)
    		cout<<setw(2)<<GamePieces[i]<<' ';
    	cout<<endl;
    
    	
    }
    void ComputerTurn(char GamePieces[], int& piecesChoice, int pieces)
    {
    	int computerMove= piecesChoice+1;
    	cout<<endl<<endl<<"OK, ITS MY TURN!"<<endl;
    	if (computerMove<=pieces&&GamePieces[computerMove]!='_')
    	GamePieces[computerMove]='_';
    }
    void HumanTurn(char GamePieces[],int& piecesSlection, int size)
    {
    	char answer, count=0;
    
    	cout<<"It's Your turn! Which Pieces Would you like?";
    	do{
    		cin>>piecesSlection;
    		count=count++;
    	if (piecesSlection>size||GamePieces[piecesSlection]=='_')
        {
            do
            {
            cout<<"This is an illegal move \n";
            cout<<"Please enter anthor number \n";
            cin>>piecesSlection;
            
            
            }while(piecesSlection>size||GamePieces[piecesSlection]=='_');
        }
    	GamePieces[piecesSlection]='_';
    	cout<<"Would you like anthor? \n";
    	cin>>answer;
    	if(answer=='y'&&count<3||answer=='Y'&&count<3)
    		cout<<"Please enter your choice \n";
    	if(count>2)
    		cout<<"You have reached the limit. You are only allowed three."<<endl;
    }while(answer=='y'&&count<3||answer=='Y'&&count<3);
    }
    Last edited by mmiskand; 11-14-2008 at 09:36 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You should check pairwise differences -- you should get 1, 1, and 2 (in some order). You may have to be willing to change 14 to -1 or so to get the circularity.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nim Trainer
    By guesst in forum Game Programming
    Replies: 3
    Last Post: 05-04-2008, 04:11 PM
  2. nim game
    By mixalissen in forum C++ Programming
    Replies: 4
    Last Post: 04-30-2008, 04:32 PM
  3. C++ NIM with functions
    By Middlechild47 in forum C++ Programming
    Replies: 9
    Last Post: 11-20-2006, 12:22 AM
  4. does anyone have the game nim programed?
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 11-27-2001, 07:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21