Page 1 of 1

Game of Nim C++

Posted: Fri Apr 27, 2007 8:24 am
by Anonymous
Hey again,
I can't seem to get my Game of Nim to work! I have it set up for each player to take turns, but it can't remove a designated peg, only the top one.

Solution please :(

Its C++ PegClass.

Code: Select all

#include <iostream.h>
#include <H:lvppegclass.h>

int main()
{
	int NumRings;
	int peg1;
	int peg2;
	char retry;

	PegClass P(6), Q(6);
  P.AddRing(4); Q.AddRing(4);
  P.AddRing(3); Q.AddRing(3);
  P.AddRing(2); Q.AddRing(2);
  P.AddRing(1); Q.AddRing(1);
  
  P.ShowAll();
	do{
  do{
  	NumRings = 4;
  	cout<<"Player 1:"<<endl;
  	cout<<"	Enter a peg: ";
  	cin>>peg1;
  	cout<<"Player 2:"<<endl;
  	cout<<"	Enter a peg: ";
  	cin>>peg2;
  	if (peg1=1) {
    P.RemoveRing();
    NumRings=-1;
  	}
  	else if (peg1=2) {
    P.RemoveRing();
    NumRings=-1;
  	}	
  	else if (peg1=3) {
    P.RemoveRing();
    NumRings=-1;
  	}
  	else if (peg1=4) {
    P.RemoveRing();
    NumRings=-1;
  	}
  	else if (peg1>=4) {
  	cout<<"Invalid Peg."<<endl;
  	}
  	if (peg2=1) {
    Q.RemoveRing();
    NumRings=-1;
  	}
  	else if (peg2=2) {
    Q.RemoveRing();
    NumRings=-1;
  	}
  	else if (peg2=3) {
    Q.RemoveRing();
    NumRings=-1;
  	}
  	else if (peg2=4) {
    Q.RemoveRing();
    NumRings=-1;
  	}

  	else if (peg2 >=4) {
  	cout<<"Invalid Peg."<<endl;
  	}
  	Q.ShowAll();
  }while (NumRings >=0 || NumRings !=0);
  cout<<"You Win"<<endl;
  cout<<"Play again? Y/N ";
  cin>>retry;
	}while (retry=='y' || retry=='Y');
	return(0);
}
[code] 

[color=#888888][size=85]Archived topic from Iceteks,  old topic ID:4791, old post ID:37784[/size][/color]