how do i simplify this

FishBoy

Feeeesh
Joined
1 Aug 2004
Messages
1,685
hey guys im trying to shortten this code out it's in C, how do i make it to be more like regular expression like in unix, or how do i make it range from 1-F hex, and from A-Z normal char... here is the code

Code:
if((coord[0]>='1'||coord[0]<='9'||coord[0]>='A'||coord[0]<='F')&&(coord[1]>='A'||coord[1]<='Z')){
 
Code:
if( (coord[0]>='1' ) || (coord[0]<='9') || (coord[0]>='A') || (coord[0]<='F') ) && ( (coord[1]>='A') || (coord[1]<='Z')) ){

That is as optimized as it will get. I added in some extra spaces, and some ()'s as i feel it looks neater and makes it easier to read.

Regular expressions are parsed, and then the same sort of code is generated just like your code.

Flex is a hard to use tool, and has quite a steep learning curve.

http://www.linuxgazette.com/issue55/misc/tindale/mygrep.c.txt

Is a program that uses regular expressions to implement the function "grep".

http://en.wikipedia.org/wiki/Regular_expression

Check to see if it is worth learning something new, or what you ahve now will do. It is up to you.
 
X-Istance: the thing is my code isn't working as it is, coz when i enter the coordinates '1A' it tells me valid move, when i enter 'ZZ' it still tells me valid move when it's supposed to say invalid, lol even when i entered '..' it said valid move.
 
it should be:

Code:
if( ((coord[0]>= 1  && coord[0]<= 9) || (coord[0]>= 'A' && coord[0]<= 'F')) && ((coord[1]>='A') || (coord[1]<='Z')) ) {
 
Sorry, did not read your entire thread. Blame it on the fact that i was to busy programming Java code for class.

Show us your entire code, and we might be able to help you a lot better, as i still have no clue what you want to match in the first place. Or what the constraints are supposed to be.
 
well it's a project for my programming class, but here i attached it along
i have to make a battleship game

it's just in the check_move function all the way down at the bottom i want to make the if statement smaller, and to work coz it's giving me the wrong answer

thx for the support btw :D
 

Attachments

  • blattleship.txt
    2.7 KB · Views: 108
Quick testing and i think this works perfectly:

Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<ctype.h>
#include<ctype.h>

#define AIR "[CCCCC=>"
#define DES "[DDD=>"
#define SUB "[SS=>"
#define FRI "[F>"
#define SEED time(NULL)

void replace(char [], char []);
void play_battleship(void);
void update_data(char[],char[],int *,int *,int *,char[]);
void draw_game(char[],char[],int,int,int);
void init_game(char[],char[]);
void load_ships(char[],char[]);
void draw_board(char[]);
int check_move(char[]);

int main(){
	printf("\n   C Battleship...\n");
	play_battleship();
	return 0;
}

void play_battleship(void){
	char board[390],mask[390],ships[8];
	int maway=0,mleft=50,score=0;
	init_game(board,mask);
	draw_game(board,mask,maway,mleft,score);
}

void init_game(char board[],char mask[]){
	int i,t,randomP;

	for(t=0;t<390;t++){
		board[t]='~';
	}

	load_ships(board,AIR);
	load_ships(board,DES);
	load_ships(board,SUB);
	load_ships(board,FRI);
	draw_board(board);
}

void draw_board(char board[]){
	char alpha;
	int t,yn,ya,x;
	
	printf("  ");
	for(alpha='A';alpha<='Z';alpha++){
		printf("%c",alpha);
	}
	printf("\n");
	
	for(yn=1;yn<=15;yn++){
		if(yn>=10){
			for(ya=65;ya<=70;ya++){
				printf("%c|",ya);
				for(x=0;x<26;x++)
					printf("%c",board[x+yn*26]);
				printf("|\n");
			}
			break;
		}else{
			printf("%d|",yn);
			for(x=0;x<26;x++)
				printf("%c",board[x+yn*26]);
			printf("|\n");
		}
	}
}

void load_ships(char board[],char ships[]){
	int position_x,position_y,place,i;

	srand(SEED);
	position_x=rand()%25;
	position_y=rand()%14;
	place=(position_y*26)+position_x;
	replace(&board[place],ships);
}

void draw_game(char board[],char mask[],int maway,int mleft,int score){
	char ya,alpha,input[3],check;
	int t,x,yn;
	
	
	printf("  ");
	for(alpha='A';alpha<='Z';alpha++){
		printf("%c",alpha);
	}
	printf("\n");
	
	for(yn=1;yn<=15;yn++){
		if(yn>=10){
			for(ya=65;ya<=70;ya++){
				printf("%c|",ya);
				for(x=0;x<26;x++)
					printf("%c",board[x+yn*26]);
				printf("|\n");
			}
			break;
		}else{
			printf("%d|",yn);
			for(x=0;x<26;x++)
				printf("%c",board[x+yn*26]);
			printf("|\n");
		}
	}
	printf("\n Missiles Away: %02d   Missiles Left: %02d\n",maway,mleft);
	printf(" Current Score: %03d Last Move: \n",score);
	printf(" Enter Target Coordinates--> ");
	scanf("%2s",input);
	check_move(input);
}

int check_move(char coord[]){
    coord[0] = toupper(coord[0]);
    coord[1] = toupper(coord[1]);
    
	if( ( ( (coord[0] >= '1') && (coord[0] <= '9') || (coord[0] >= 'A') && (coord[0] <= 'F') ) &&
          ( (coord[1] >= 'A') && (coord[1] <= 'Z') ) ) ) {    
		printf("Valid move\n");
		return 1;
	}else{
		printf("Invalid move\n");
		return 0;
	}       
}

void replace(char to[],char from[]){
	int i=0;
	
	while(from[i]!='\0'){
		to[i]=from[i++];
	}
}
 
It also takes in account the fact that they could type it in lowercase and not just uppercase, so i added in an toupper so that the char will be capatilized before it is checked in the if statement. Otherwise you would have to rewrite your if statement to also take in accoun lowercase letters which would be a pain.


Making the If statement smaller is not possible. Right now it is the smallest possible, and really, there is no reason why this is not acceptable.
 
aargh i hate that everytime my code is fine there is alway a minor thing to change like in that all that i had to do is put &&s, and the toupper is not bad, thanks X-Istance the function is perfect :D:D
 
You sir are very welcome. Any more questions, let us know.

Also, when you are done, publish a copy, so that we an take a look. Plenty of licenses out there to release it under.
 
well i'm like the least experienced programmer in my class other students are able to finish this project in a day and i've been working on it for the past 2 weekd :(
 
You should not look at the time it took to complete the project, but rather the coding style, and the way it was put together.

I could probably write this program in a day as well, but i would feel bad, as that means i would have just written some awfull code, which might even be buggy, which is why i prefer to just write neat code and not worry about how long it takes.

Cheer up, and trust me, i was not always this good at writing code :p.
 
i like to write the whole code just like the one posted and then work on the style and looks of it but i dont like to have so many spaces and stuff, but trust me it bugs you more to take 2 weeks and not knowing what to do im not even halfway done with this code, i still have to get the ships not to overlap, hide them, and then replace the miss with an 'X', update score, suck up to the teacher....
 
Sometimes writing code means having a lot of whitespace so that it remainds readable.

It is a critical part in an app.
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back