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