GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rating: Thread Rating: 3 votes, 2.00 average.
  #1  
Old 14-May-2004, 23:40
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 168
dabigmooish will become famous soon enough

there has to be a better way


Howdy I've got two functions that are almost exactly the same. The only difference is that one checks for uppercase letters the other does lowercase. I was just wondering if anyone could think of a more productive method of doing this?

Here's my code:

CPP / C++ / C Code:
int gamerules::CheckPiece(char alpha, int row, int player)
{

	gamesaveload g;
	int alphaNum;
	int returnValue = 0;


	switch(alpha)
	{
		case 'A':
		case 'a':
			alphaNum = 0;
		break;

		case 'B':
		case 'b':
			alphaNum = 1;
		break;

		case 'C':
		case 'c':
			alphaNum = 2;
		break;

		case 'D':
		case 'd':
			alphaNum = 3;
		break;

		case 'E':
		case 'e':
			alphaNum = 4;
		break;

		case 'F':
		case 'f':
			alphaNum = 5;
		break;

		case 'G':
		case 'g':
			alphaNum = 6;
		break;

		case 'H':
		case 'h':
			alphaNum = 7;
		break;
	}


	if(player == 1)
	{
		switch(g.board[alpha][row])
		{
			case 'r':
			case 'n':
			case 'b':
			case 'k':
			case 'q':
			case 'p':
				cout <<"I'm sorry that is not your piece.  Please select another.\n";
			break;
			
			default:
				returnValue = 1;
		}
	}
	else
	{
		switch(g.board[alpha][row])
		{
			case 'R':
			case 'N':
			case 'B':
			case 'K':
			case 'Q':
			case 'P':
				cout <<"I'm sorry that is not your piece.  Please select another. \n";
			break;
			
			default:
				returnValue = 1;
		}
	}

	return(returnValue);
}

and the other function:
CPP / C++ / C Code:
int gamerules::CheckMove(char alpha, int row, int player)
{
	gamesaveload g;
    int alphaNum;
    int returnValue = 0;


	switch(alpha)
	{
		case 'A':
		case 'a':
			alphaNum = 0;
		break;

		case 'B':
		case 'b':
			alphaNum = 1;
		break;

		case 'C':
		case 'c':
			alphaNum = 2;
		break;

		case 'D':
		case 'd':
			alphaNum = 3;
		break;

		case 'E':
		case 'e':
			alphaNum = 4;
		break;

		case 'F':
		case 'f':
			alphaNum = 5;
		break;

		case 'G':
		case 'g':
			alphaNum = 6;
		break;

		case 'H':
		case 'h':
			alphaNum = 7;
		break;
	}


	if(player == 1)
	{
		switch(g.board[alpha][row])
		{
			case 'R':
			case 'N':
			case 'B':
			case 'K':
			case 'Q':
			case 'P':
				cout <<"I'm sorry you can't move there.  Please move again.\n";
			break;
			
			default:
				returnValue = 1;
		}
	}
	else
	{
		switch(g.board[alpha][row])
		{
			case 'r':
			case 'n':
			case 'b':
			case 'k':
			case 'q':
			case 'p':
				cout <<"I'm sorry you can't move there.  Please move again. \n";
			break;
			
			default:
				returnValue = 1;
		}
	}

	return(returnValue);

}
  #2  
Old 15-May-2004, 06:20
machinated machinated is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
upper case characters are 65 - 90
lower case characters are 97 - 120

to convert from upper case to lower case: add 32
to convert form lower case to upper case: subract 32

CPP / C++ / C Code:
char ch = 'A';
ch+=32;
cout<<ch; //prints 'a'
  #3  
Old 15-May-2004, 06:48
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
I always use the functions tolower or toupper when I want to convert case. They are standard ansi C and they do checking to make sure that your can be converted to requested case. They are defined as:

CPP / C++ / C Code:
#include <ctype.h>

int toupper(int c);
int tolower(int c);

So that if have something like:
CPP / C++ / C Code:
char ch1 = 'a';
char ch2 = 'A';

printf("Uppercase of ch1=%c\n",toupper(ch1));
printf("Uppercase of ch2=%c\n",toupper(ch2));

That should print a capital A in both cases.

Cheers,
d
  #4  
Old 15-May-2004, 10:18
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,435
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Remove the switch block and add another parameter:
CPP / C++ / C Code:
int gamerules::CheckPiece(char alpha, int row, int player, bool piece)
{
    gamesaveload g;
    int alphaNum;
    int returnValue = 0;

    alphaNum = toupper(alpha) - 'A'; // convert character to binary

    if (piece)    // boolean value TRUE if testing a piece, FALSE if testing a move
    {
	if(player == 1)
	{
		switch(g.board[alpha][row])
		{
			case 'r':
			case 'n':
			case 'b':
			case 'k':
			case 'q':
			case 'p':
				cout <<"I'm sorry that is not your piece.  Please select another.\n";
			break;
			
			default:
				returnValue = 1;
		}
	}
	else
	{
		switch(g.board[alpha][row])
		{
			case 'R':
			case 'N':
			case 'B':
			case 'K':
			case 'Q':
			case 'P':
				cout <<"I'm sorry that is not your piece.  Please select another. \n";
			break;
			
			default:
				returnValue = 1;
		}
	}
    }
    else
    {
        // add the other block here from CheckMove()
        // Then look again to see it you can combine more
    }
	return(returnValue);
}
(and don't use TABs, they format badly on forums, as you can see above)
__________________

Definition: Politics
Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
  #5  
Old 15-May-2004, 10:31
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 570
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Tabs also don't port well to other IDEs. I agree with dsmith, and that's what I would've said. Just convert the characters to the proper case.
__________________
-Aaron
  #6  
Old 16-May-2004, 19:00
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 168
dabigmooish will become famous soon enough
thanks everyone for your help. I like the ideal of using a booleen but it really just puts both functions into one, not really improving the code any. I also think that there is a little missunderstanding as to what I'm trying to do. I've making a chess game. The board (which is q.board[][] in these functions) is made of charaters. White pieces are uppercase (R, N, B, K, Q, P) and black pieces are lowercase (r, n, b, k, q, p). What the first function - CheckPiece- does is make sure that player one is selecting to move his own piece, i.e. the uppercase letters and vice verse with player 2. The second function - CheckMove- checks to see if the player is moving his piece onto his own piece. The reason for the first switch statement is to turn the user input row into the actual matrix index. Let me explain

Here's what the board looks like when printed:

1 2 3 4 5 6 7 8
A R N B K Q B N R
B P P P P P P P P
C
D
E
F
G p p p p p p p p
H r n b k q b n r

The user is then asked to enter which piece he would like to move. A1 would be a white rook, H4 would be a black king, and so on. In the matrix A1 is actually 0,0 so the first switch takes the A and converts it to zero. That switch has to be there.
  #7  
Old 16-May-2004, 19:32
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
Here is what I would do,find the ASCII numbers for each char and do some math on them..:

CPP / C++ / C Code:
int gamerules::CheckMove(char alpha, int row, int player)
{
  gamesaveload g;
    int alphaNum;
    int returnValue = 0;

//if alpha is in range (A-H)
if(alpha >=65 && alpha<=72)
     alphaNum=alpha-65;
//if alpha is in range (a-h)
else if(alpha >=97 && alpha<=104)
     alphaNum=alpha-97;
...
...
...
}

you can do the same on other functions..
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #8  
Old 16-May-2004, 22:31
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,435
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by dabigmooish
In the matrix A1 is actually 0,0 so the first switch takes the A and converts it to zero. That switch has to be there.
Really? If you type in A1, and the A goes into chr, doesn't chr - 'A' equal 0?
If chr has 'C', doesn't chr - 'A' equal 2?
If chr has 'G', doesn't chr - 'A' equal 6?

So what does the switch accomplish that chr - 'A' doesn't other than taking extra time and code space?
__________________

Definition: Politics
Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
  #9  
Old 17-May-2004, 10:24
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 168
dabigmooish will become famous soon enough
Quote:
Originally Posted by WaltP
Really? If you type in A1, and the A goes into chr, doesn't chr - 'A' equal 0?
If chr has 'C', doesn't chr - 'A' equal 2?
If chr has 'G', doesn't chr - 'A' equal 6?

So what does the switch accomplish that chr - 'A' doesn't other than taking extra time and code space?

my apologie's for misunderstanding your code. That is a much better way of doing it. Many, Many, Many thanks for all of your help
Last edited by Garth Farley : 17-May-2004 at 10:41. Reason: Now now, behave!
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:22.


vBulletin, Copyright © 2000 - 2017, Jelsoft Enterprises Ltd.