Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have set up a code that should make the black pieces(player2s) jump on the whites then over it but it only jumps onto the piece and jumping worked for player one
C++
	void p1Turn(string name1)
	{	
		int letter=1;
		int irow = 0;
		int frow = 0;
		int icol = 0;
		int fcol = 0;
		int error=0;
		int error1=0;
		while(letter==1)
		{
		letter=1;
		string initial;
		string final;
		irow = 0;
		frow = 0;
		icol = 0;
		fcol = 0;
  		cout << name1 << "'s turn.\n";
		cout << "Input square of piece you want to move (example: A1): ";
		cin >>initial;
		cout << "Now enter square you want to move it to: ";
		cin >> final;
		int bugcounter=0;
				for(int y=0;y<32;y++)
		{
			if(final!=spaces[y])
			{
				error++;
			}
			if(final==spaces[y])
			{
				y=32;
			}
		}
		for(int j=0;j<32;j++)
		{
			if(initial!=spaces[j])
			{
				error1++;
			}
			if(initial==spaces[j])
			{
				j=32;		
			}
		}
		if(error==32 and error1==32)
		{
			cout<<"choose your own piece and somewhere on the board";
		}
		if(error!=32 and error1!=32)
		{
		letter=0;
		switch (initial[0])
		{
			case 'A':
				irow = 0;
				if(final[0]!='B')
				{
				  cout<<"you can only move to B until you become a king\n";
				  bugcounter=1;
				}
				break;
			case 'B':
				irow = 1;
				if(final[0]!='C')
				{
				  cout<<"you can only move to C until you become a king\n";
				  bugcounter=1;
				}
				break;
			case 'C':
				irow = 2;
				if(final[0]!='D')
				{
				  cout<<"you can only move to D until you become a king\n";
				  bugcounter=1;
				}
				break;
			case 'D':
				irow = 3;
				if(final[0]!='E')
				{
				  cout<<"you can only move to E until you become a king\n";
				  bugcounter=1;
				}
				break;
			case 'E':
				irow = 4;
				if(final[0]!='F')
				{
				  cout<<"you can only move to F until you become a king\n";
				  bugcounter=1;
				}
				break;
			case 'F':
				irow = 5;
				if(final[0]!='G')
				{
				  cout<<"you can only move to G until you become a king\n";
				  bugcounter=1;
				}
				break;
			case 'G':
				irow = 6;
				if(final[0]!='H')
				{
				  cout<<"you can only move to H until you become a king\n";
				  bugcounter=1;
				}
				break;
			case 'H':
				irow = 7;
				if(final[0]!='G')
				{
				  cout<<"you can only move to G right now as a king\n";
				  bugcounter=1;
				}
				break;
		}
		switch (initial[1])
		{
			case '1':
				icol = 1;
				if(final[1]!='2')
				{
				  bugcounter=1;
				}
				break;
			case '2':
				icol = 2;
				if(final[1]!='1' or '3')
				{
				  bugcounter=1;
				}
				break;
			case '3':
				icol = 3;
				if(final[1]!='2' or '4')
				{
				  bugcounter=1;
				}
				break;
			case '4':
				icol = 4;
				if(final[1]!='3'or'5')
				{
				  bugcounter=1;
				}
				break;
			case '5':
				icol = 5;
				if(final[1]!='4'or'6')
				{
				  bugcounter=1;
				}
				break;
			case '6':
				icol = 6;
				if(final[1]!='5'or'7')
				{
				  bugcounter=1;
				}
				break;
			case '7':
				icol = 7;
				if(final[1]!='6' or '8')
				{
				  bugcounter=1;
				}
				break;
			case '8':
				icol = 8;
				if(final[1]!='7')
				{
				  bugcounter=1;
				}
				break;
		}
		switch (final[0])
		{
			case 'A':
				frow = 0;
				break;
			case 'B':
				frow = 1;
				break;
			case 'C':
				frow = 2;
				break;
			case 'D':
				frow = 3;
				break;
			case 'E':
				frow = 4;
				break;
			case 'F':
				frow = 5;
				break;
			case 'G':
				frow = 6;
				break;
			case 'H':
				frow = 7;
				break;
		}
		switch (final[1])
		{
			case '1':
				fcol = 1;
				break;
			case '2':
				fcol = 2;
				break;
			case '3':
				fcol = 3;
				break;
			case '4':
				fcol = 4;
				break;
			case '5':
				fcol = 5;
				break;
			case '6':
				fcol = 6;
				break;
			case '7':
				fcol = 7;
				break;
			case '8':
				fcol = 8;
				break;
		}
			if(board[irow][icol]=="⛁")
			{
		switch (initial[0])
		{
			case 'A':
				irow = 0;
				if(final[0]!='B')
				{
				  cout<<"you can only move to B until you become a king\n";
				  bugcounter=1;
				}
				break;
			case 'B':
				irow = 1;
				if(final[0]!='C'or final[0]!='A')
				{
				  cout<<"you can only move to C or A as a king\n";
				  bugcounter=1;
				}
				break;
			case 'C':
				irow = 2;
				if(final[0]!='D'or final[0]!='B')
				{
				  cout<<"you can only move to D or B as a king\n";
				  bugcounter=1;
				}
				break;
			case 'D':
				irow = 3;
				if(final[0]!='E'or final[0]!='C')
				{
				  cout<<"you can only move to E or C as a king\n";
				  bugcounter=1;
				}
				break;
			case 'E':
				irow = 4;
				if(final[0]!='F' or final[0]!='D')
				{
				  cout<<"you can only move to F or D as a king\n";
				  bugcounter=1;
				}
				break;
			case 'F':
				irow = 5;
				if(final[0]!='G'or final[0]!='E')
				{
				  cout<<"you can only move to G or E as a king\n";
				  bugcounter=1;
				}
				break;
			case 'G':
				irow = 6;
				if(final[0]!='H' or final[0]!='F')
				{
				  cout<<"you can only move to H or G as a king\n";
				  bugcounter=1;
				}
				break;
			case 'H':
				irow = 7;
				if(final[0]!='G')
				{
				  cout<<"you can only move to G right now as a king\n";
				  bugcounter=1;
				}
				break;
		}
}
			if(board[irow][icol]!="⛀|")
			{
			  cout<<"that is not a possible movement try again";
			  bugcounter=1;
			}
			if(board[frow][fcol]!="_|")
			{
					if(board[irow][icol]=="⛁|")
					{
						if(icol < fcol and irow < frow)
						{
							if(fcol+1>8)
							{
							cout<<"that is not a possible movement";
							bugcounter=1;
							}
							else
							{
							board[frow][fcol] = "_|";
							frow = frow+1;
							fcol = fcol+1;
							}
						}
						if(icol > fcol and irow < frow)
						{
							if(fcol-1<=0)
							{
							cout<<"that is not a possible movement";
							bugcounter=1;
							}
							else
							{
							board[frow][fcol] = "_|";
							frow = frow+1;
							fcol = fcol-1;
							}
						}
						if(icol < fcol and irow > frow)
						{
							if(fcol+1>8)
							{
							cout<<"that is not a possible movement";
							bugcounter=1;
							}
							else
							{
							board[frow][fcol] = "_|";
							frow = frow-1;
							fcol = fcol+1;
							}
						}
						if(icol > fcol and irow > frow)
						{
							if(fcol-1<=0)
							{
							cout<<"that is not a possible movement";
							bugcounter=1;
							}
							else
							{
							board[frow][fcol] = "_|";
							frow = frow-1;
							fcol = fcol-1;
							}
						}
					}
				if(board[frow][fcol]=="⛂|")
				{
					if(icol < fcol)
					{
						if(fcol+1 >=8)
						{
							cout<<"that is not a possible movement";
							bugcounter=1;
						}
						else
						{
						board[frow][fcol]="_|";
						frow=frow+1;
						fcol=fcol+1;
						}
					}
					if(icol > fcol)
					{
						if(fcol-1 <=0)
						{
							cout<<"that is not a possible movement";
							bugcounter=1;
						}
						else
						{
						board[frow][fcol]="_|";
						frow=frow+1;
						fcol=fcol-1;
						}
					}
				}
			}
			if(board[irow][icol]=="⛀|" and board[frow][fcol]=="_|")
			{
				bugcounter=0;
			}
			if(bugcounter==1)
			{
			  letter=1;
			}
			if(bugcounter==0)
			{
			  letter=0;
			}
		}//end of if statement
		}//end of whileloop
		if(error!=32 and error1!=32)
		{
		board[frow][fcol] = board[irow][icol];
		board[irow][icol] = board[0][2];
		if(board[7][2] == "⛀|"or board[7][4] == "⛀|"or board[7][6] == "⛀|"or board[7][8] == "⛀|")
		{
			board[frow][fcol] = "⛁|";
		}
		}//end of if statement
	}//end of p1Turn

	void p2Turn(string name2)
	{
		int letter=1;
		int irow = 0;
		int frow = 0;
		int icol = 0;
		int fcol = 0;
		int error=0;
		int error1=0;
		int bugcounter=0;
		while(letter==1)
		{
		letter=0;
		string initial;
		string final;
		irow = 0;
		frow = 0;
		icol = 0;
		fcol = 0;
  		cout << name2 << "'s turn.\n";
		cout << "Input square of piece you want to move (example: A1): ";
		cin >>initial;
		cout << "Now enter square you want to move it to: ";
		cin >> final;
				for(int y=0;y<32;y++)
		{
			if(final!=spaces[y])
			{
				error++;
			}
			if(final==spaces[y])
			{
				y=32;
			}
		}
		for(int j=0;j<32;j++)
		{
			if(initial!=spaces[j])
			{
				error1++;
			}
			if(initial==spaces[j])
			{
				j=32;		
			}
		}
		if(error==32 and error1==32)
		{
			cout<<"choose your own piece and somewhere on the board";
		}
		if(error!=32 and error1!=32)
		{
		letter=0;
		switch (initial[0])
		{
			case 'A':
				irow = 0;
				if(final[0]!='B')
				{
				  cout<<"you can only move to B as a king\n";
				  bugcounter=1;
				}
				break;
			case 'B':
				irow = 1;
				if(final[0]!='A')
				{
				  cout<<"you can only move to A until you become a king\n";
				  bugcounter=1;
				}
				break;
			case 'C':
				irow = 2;
				if(final[0]!='B')
				{
				  cout<<"you can only move to B until you become a king\n";
				  bugcounter=1;
				}
				break;
			case 'D':
				irow = 3;
				if(final[0]!='C')
				{
				  cout<<"you can only move to C until you become a king\n";
				  bugcounter=1;
				}
				break;
			case 'E':
				irow = 4;
				if(final[0]!='D')
				{
				  cout<<"you can only move to D until you become a king\n";
				  bugcounter=1;
				}
				break;
			case 'F':
				irow = 5;
				if(final[0]!='E')
				{
				  cout<<"you can only move to E until you become a king\n";
				  bugcounter=1;
				}
				break;
			case 'G':
				irow = 6;
				if(final[0]!='F')
				{
				  cout<<"you can only move to F until you become a king\n";
				  bugcounter=1;
				}
				break;
			case 'H':
				irow = 7;
				if(final[0]!='G')
				{
				  cout<<"you can only move to G until you become a king\n";
				  bugcounter=1;
				}
				break;
		}
		switch (initial[1])
		{
			case '1':
				icol = 1;
				if(final[1]!='2')
				{
				  bugcounter=1;
				}
				break;
			case '2':
				icol = 2;
				if(final[1]!='1' or '3')
				{
				  bugcounter=1;
				}
				break;
			case '3':
				icol = 3;
				if(final[1]!='2' or '4')
				{
				  bugcounter=1;
				}
				break;
			case '4':
				icol = 4;
				if(final[1]!='3'or'5')
				{
				  bugcounter=1;
				}
				break;
			case '5':
				icol = 5;
				if(final[1]!='4'or'6')
				{
				  bugcounter=1;
				}
				break;
			case '6':
				icol = 6;
				if(final[1]!='5'or'7')
				{
				  bugcounter=1;
				}
				break;
			case '7':
				icol = 7;
				if(final[1]!='6' or '8')
				{
				  bugcounter=1;
				}
				break;
			case '8':
				icol = 8;
				if(final[1]!='7')
				{
				  bugcounter=1;
				}
				break;
		}
		switch (final[0])
		{
			case 'A':
				frow = 0;
				break;
			case 'B':
				frow = 1;
				break;
			case 'C':
				frow = 2;
				break;
			case 'D':
				frow = 3;
				break;
			case 'E':
				frow = 4;
				break;
			case 'F':
				frow = 5;
				break;
			case 'G':
				frow = 6;
				break;
			case 'H':
				frow = 7;
				break;
		}
		switch (final[1])
		{
			case '1':
				fcol = 1;
				break;
			case '2':
				fcol = 2;
				break;
			case '3':
				fcol = 3;
				break;
			case '4':
				fcol = 4;
				break;
			case '5':
				fcol = 5;
				break;
			case '6':
				fcol = 6;
				break;
			case '7':
				fcol = 7;
				break;
			case '8':
				fcol = 8;
				break;
		}
		}
			if(board[irow][icol]=="⛃|")
			{
		switch (initial[0])
		{
			case 'A':
				irow = 0;
				if(final[0]!='B')
				{
				  cout<<"you can only move to B as a king\n";
				  bugcounter=1;
				}
				break;
			case 'B':
				irow = 1;
				if(final[0]!='C'or final[0]!='A')
				{
				  cout<<"you can only move to C or A as a king\n";
				  bugcounter=1;
				}
				break;
			case 'C':
				irow = 2;
				if(final[0]!='D'or final[0]!='B')
				{
				  cout<<"you can only move to D or B as a king\n";
				  bugcounter=1;
				}
				break;
			case 'D':
				irow = 3;
				if(final[0]!='E'or final[0]!='C')
				{
				  cout<<"you can only move to E or C as a king\n";
				  bugcounter=1;
				}
				break;
			case 'E':
				irow = 4;
				if(final[0]!='F' or final[0]!='D')
				{
				  cout<<"you can only move to F or D as a king\n";
				  bugcounter=1;
				}
				break;
			case 'F':
				irow = 5;
				if(final[0]!='G'or final[0]!='E')
				{
				  cout<<"you can only move to G or E as a king\n";
				  bugcounter=1;
				}
				break;
			case 'G':
				irow = 6;
				if(final[0]!='H' or final[0]!='F')
				{
				  cout<<"you can only move to H or G as a king\n";
				  bugcounter=1;
				}
				break;
			case 'H':
				irow = 7;
				if(final[0]!='G')
				{
				  cout<<"you can only move to G right now as a king\n";
				  bugcounter=1;
				}
				break;
		}
					if(board[irow][icol]=="⛃|")
					{
						if(icol < fcol and irow < frow)
						{
							if(fcol+1>8)
							{
							cout<<"that is not a possible movement";
							bugcounter=1;
							}
							else
							{
							board[frow][fcol] = "_|";
							frow = frow+1;
							fcol = fcol+1;
							}
						}
						if(icol > fcol and irow < frow)
						{
							if(fcol-1<=0)
							{
							cout<<"that is not a possible movement";
							bugcounter=1;
							}
							else
							{
							board[frow][fcol] = "_|";
							frow = frow+1;
							fcol = fcol-1;
							}
						}
						if(icol < fcol and irow > frow)
						{
							if(fcol+1>8)
							{
							cout<<"that is not a possible movement";
							bugcounter=1;
							}
							else
							{
							board[frow][fcol] = "_|";
							frow = frow-1;
							fcol = fcol+1;
							}
						}
						if(icol > fcol and irow > frow)
						{
							if(fcol-1<=0)
							{
							cout<<"that is not a possible movement";
							bugcounter=1;
							}
							else
							{
							board[frow][fcol] = "_|";
							frow = frow-1;
							fcol = fcol-1;
							}
						}
					}
			if(board[irow][icol]!="⛂|")
			{
			  cout<<"that is not a possible movement try again";
			  bugcounter=1;
			}
			if(board[frow][fcol]!="_|")
			{
					if(board[irow][icol]=="⛃|")
					{
						if(icol < fcol and irow < frow)
						{
							if(fcol+1>8)
							{
							cout<<"that is not a possible movement";
							bugcounter=1;
							}
							else
							{
							board[frow][fcol] = "_|";
							frow = frow+1;
							fcol = fcol+1;
							}
						}
						if(icol > fcol and irow < frow)
						{
							if(fcol-1<=0)
							{
							cout<<"that is not a possible movement";
							bugcounter=1;
							}
							else
							{
							board[frow][fcol] = "_|";
							frow = frow+1;
							fcol = fcol-1;
							}
						}
						if(icol < fcol and irow < frow)
						{
							if(fcol+1>=8)
							{
							cout<<"that is not a possible movement";
							bugcounter=1;
							}
							else
							{
							board[frow][fcol] = "_|";
							frow = frow-1;
							fcol = fcol+1;
							}
						}
						if(icol > fcol and irow > frow)
						{
							if(fcol-1<=0)
							{
							cout<<"that is not a possible movement";
							bugcounter=1;
							}
							else
							{
							board[frow][fcol] = "_|";
							frow = frow-1;
							fcol = fcol-1;
							}
						}
					}
				if(board[frow][fcol]=="⛀|")
				{
					if(icol < fcol)
					{
						if(fcol+1 <=8)
						{
							cout<<"that is not a possible movement";
							bugcounter=1;
						}
						else
						{
						bugcounter=0;
						board[frow][fcol]="_|";
						frow=frow-1;
						fcol=fcol+1;
						}
					}
					if(icol > fcol)
					{
						if(fcol-1 <=0)
						{
							cout<<"that is not a possible movement";
							bugcounter=1;
						}
						else
						{
						bugcounter=0;
						board[frow][fcol]="_|";
						frow=frow-1;
						fcol=fcol-1;
						}
					}
				}
			}
			if(board[irow][icol]=="⛂|" and board[frow][fcol]=="_|")
			{
				bugcounter=0;
			}
			if(bugcounter==1)
			{
			  letter=1;
			}
			if(bugcounter==0)
			{
			  letter=0;
			}
		}
		}
		if(error!=32 and error1!=32)
		{
		board[frow][fcol] = board[irow][icol];
		board[irow][icol] = board[0][2];
		if(board[0][1] == "⛂|"or board[0][3] == "⛂|"or board[0][5] == "⛂|"or board[0][7] == "⛂|")
		{
			board[frow][fcol] = "⛃|";
		}
		}
	}//end of p2Turn

I have only sent a script of it and the first function fully works the second one I have been trying to fix for over a week plz help me
Posted
Comments
SamuelBray 23-May-12 20:27pm    
also the squares are actually unicode in linux(ubuntu)if that helps
Malli_S 24-May-12 3:27am    
Can't you modulerize your code? Better break it up into small functions, it would be better for debugging yourself.
Aescleal 24-May-12 8:35am    
Gave this a 5 - not because there was anything too insightful about the question BUT SA's answer's well worth a read and as most people have filters set at 3.0 they wouldn't have seen it.

1 solution

The only way I can help you is to give you some practical advice: stop doing what you are doing: you are not yet ready for such problems. Instead, learn some programming. You code is all wrong from the very beginning so fixing and even reading it would be a total waste of time. If has almost nothing to do with programming.

To illustrate this, let's look at:
C++
switch (initial[1])
        {
            case '1':
                icol = 1;
                if(final[1]!='2')
                {
                  bugcounter=1;
                }
                break;
             // 2, 3... 8
        }

This it not bad programming, this is not programming at all. Programming is all about re-use and abstraction. It should be something like
C++
icol = initial[1];
if (final[icol] != icol + 1) //...

Same thing about case 'A'… case 'H'. You should not use letters at all, you should use a matrix 8x8; the letters should come only when you need to draw a board on screen or record the party. And the letters could be calculated out of numbers in one line, without any switch statements. The identifier bugcounter along tells the tail.
I don't say that the "fixed" code makes sense, but the original code should not exist at all. If you try to continue with such understanding of programming and even with such level of elementary logic, you will get sunken even in much simpler problems. Right now your attempts to make working code looks like "fixing" a TV set by kicking it until it starts showing a picture.

Seriously. Going forward would not make any sense at all; it's wasting too much time in vain. You need to go back, try to learn programming from the very basics, starting with simple exercises. You would need a good book on C++ and general programming technique, do many of the exercises, read it from the very beginning to the very end. This can be orders of magnitude more effective.

—SA
 
Share this answer
 
Comments
SamuelBray 23-May-12 20:18pm    
That isn't my whole code the only way I could get you to help me is if I had you read the whole code from top to bottom the bugcounter is only a thing that causes the loops to run over again the grid has ABCDEFGH as its left side markings and 12345678as its bottom I have been working on this for a good amount of time in C++ class and was having trouble with pieces my logic can get confuzeing but it works I have been programming for the past few months and know that programming is alot easier than all this typing but why change what I already have?
SamuelBray 23-May-12 20:19pm    
PS right now I dont even have the code its on my laptop which is in two pieces I must hook it up to get code from it.
SamuelBray 23-May-12 20:21pm    
I agree with you in the fact that I should have used 8x8 but I was sick for some time and was unable to right code at the time
SamuelBray 23-May-12 20:22pm    
thank you your idea of making it a 8x8 would remove the purpose of even having the fcol and icol...
SamuelBray 23-May-12 20:30pm    
I will have to say sorry for making the code so hard to follow

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900