C to Java...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alireza6485
    New Member
    • Jan 2009
    • 19

    C to Java...

    Hi,
    Could sme one please translate the following code from C to Java for me?

    Code :
    Code:
    /*
          Polytechnic University of the Philippines
    		 Sta.Mesa, Manila
    College of Computer Management and Information Technology
    
    			A
    		    CASE STUDY
    			in
    		  DATA STRUCTURES
    			AND
    		    ALGORITHM
    
    */
    #include<stdio.h>
    #include<conio.h>
    #include<ctype.h>
    #include<string.h>
    #include<stdlib.h>
    
    #define MAXROW 15
    #define MAXCOL 30
    
    #define ALIVE 1
    #define DEAD 0
    
    struct conway{
      int organism;
      int neighbor;
    }cell[MAXROW+2][MAXCOL+2];
    
    void twolinebox(int x1,int y1,int x2,int y2){
     int x,y;
     gotoxy(x1,y1); printf("É"); //alt-201
     gotoxy(x2,y1); printf("»"); //alt-187
      for(y=y1+1;y<y2;y++){
        gotoxy(x1,y); printf("º"); //alt-186
        gotoxy(x2,y); printf("º"); //alt-186
      }
     gotoxy(x1,y2); printf("È"); //alt-200
     gotoxy(x2,y2); printf("¼"); //alt-188
      for(x=x1+1;x<x2;x++){
        gotoxy(x,y1); printf("Í"); //alt-205
        gotoxy(x,y2); printf("Í"); //alt-205
      }
     gotoxy(x1+1,y1+1);
    }
    
    void printxy(int x,int y,char string[]){
     gotoxy(x,y); printf("%s",string);
    }
    
    void center(int y,char string[]){
    int x=(80-strlen(string)+1)/2;
    gotoxy(x,y);printf("%s",string);
    }
    
    
    void initializeMatrix(void){
    int row,col;
       for(row=-1;row<MAXROW+1;row++){
         for(col=-1;col<MAXCOL+1;col++){
    	 cell[row][col].organism=DEAD;
    	 cell[row][col].neighbor=0;
         }//end of inner for loop
       }//end of outer for loop
    }//end of initializeMatrix
    
    void showMatrix(void){
      int row,col;
      twolinebox(1,1,80,24);
       for(row=0;row<MAXROW;row++){
         for(col=0;col<MAXCOL;col++){
    	gotoxy(5+col,5+row); printf("²"); //alt-178
    	if(cell[row][col].organism==ALIVE){
    	   gotoxy(5+col,5+row);
    	   printf("%c",cell[row][col].organism);
    	}//end of if
         }//end of inner for loop
       }//end of outer for loop
    
    }//end of showMatrix
    
    void inputOrganism(void){
      int org,row,col,err,a;
         initializeMatrix();
         do{
           clrscr();
           showMatrix();
           gotoxy(5,3); printf("Input no. of organism to display(1-30 
    only):
    ");
           gotoxy(50,3); scanf("%d",&org);
         }while(org<1||org>30);
    
         for(a=1;a<=org;a++){
          clrscr();
          showMatrix();
          do{
           do{
    	 gotoxy(36,6); printf("Input cell row position of organism %d",a);
    	 printxy(36,7,"row:(1-15)only\>     ");
    	 gotoxy(55,7); scanf("%d",&row); row--;
           }while(row>15||row<0);
           do{
    	 gotoxy(36,9); printf("Input cell column position of organism 
    %d",a+1);
    	 gotoxy(36,10); printf("col:(1-30)only\>     ");
    	 gotoxy(55,10); scanf("%d",&col); col--;
           }while(col>30||col<0);
           if(cell[row][col].organism==ALIVE){
    	 gotoxy(36,20); printf("cell[%d][%d] is already 
    occupied!",row+1,col+1);
    	 gotoxy(36,21); printf("Try other cell...");
    	 getch();
    	 gotoxy(36,20); clreol();
    	 gotoxy(36,21); clreol();
           }//end of if
          }while(cell[row][col].organism==ALIVE);
           cell[row][col].organism=ALIVE;
           showMatrix();
         }//end of for loop
    
    }//end of inputOrganism
    
    void checkCell(void){
       int row,col;
    
       //in this part the neighboring cells are checked,
       //if the neighborng cell is an occupied cell
       //then the neighbor element of the structure cell is increased by 1
    
         for(row=0;row<MAXROW;row++){
          for(col=0;col<MAXCOL;col++){
    	     cell[row][col].neighbor=0;    //initialize neighbor to 0
    	  if(cell[row-1][col-1].organism==ALIVE)
    	     cell[row][col].neighbor++;
    	  if(cell[row-1][col].organism==ALIVE)
    	     cell[row][col].neighbor++;
    	  if(cell[row][col-1].organism==ALIVE)
    	     cell[row][col].neighbor++;
    	  if(cell[row][col+1].organism==ALIVE)
    	     cell[row][col].neighbor++;
    	  if(cell[row+1][col].organism==ALIVE)
    	     cell[row][col].neighbor++;
    	  if(cell[row+1][col-1].organism==ALIVE)
    	     cell[row][col].neighbor++;
    	  if(cell[row-1][col+1].organism==ALIVE)
    	     cell[row][col].neighbor++;
    	  if(cell[row+1][col+1].organism==ALIVE)
    	     cell[row][col].neighbor++;
          }//end of inner for loop
         }//end of outer for loop
    
        //in this part we will check the cell's no. of neighbor then
        //place or kill organism in that particular cell
        //based on its neighboring cell according to Conway's Rule
    
         for(row=0;row<MAXROW;row++){
    	for(col=0;col<MAXCOL;col++){
    	  if(cell[row][col].organism==ALIVE){  //rule2 and 3 if and occupied 
    cell
    	     if(cell[row][col].neighbor<2) //rule2: has less than 2 neighbor
    		cell[row][col].organism=DEAD; //organism will die in that cell
    	     else if(cell[row][col].neighbor>3) //rule3: has greater than 3
    		cell[row][col].organism=DEAD; //organism will die in that cell
    	  }
    	  else //if(cell[row][col].organism!=1) //rule1. if an empty cell
    	     if(cell[row][col].neighbor==3) //has exactly 3 neigbor
    		cell[row][col].organism=ALIVE;  //an organism is born in that cell
    
    	}//end of inner for loop
         }//end of outer for loop
    
    }//end of checkCell
    
    void showGeneration(void){
      int gen,g;
        clrscr();
          showMatrix();
          gotoxy(5,3); printf("Enter no. of generation you want to 
    see...");
          gotoxy(5,4); printf("gen:\>     ");
          gotoxy(12,4); scanf("%d",&gen);
          for(g=1;g<=gen;g++){
    	 clrscr();
    	 showMatrix();
    	 gotoxy(36,10); printf("GENERATION %d",g);
    	 checkCell();
           getch();
          }//end of for loop
    }//end of showGeneration
    
    
    void welcome(void){
           clrscr();
           twolinebox(1,1,80,24);
           gotoxy(35,8); printf("WELCOME");
           gotoxy(37,10); printf("to");
           gotoxy(35,12); printf("CONWAY'S");
           gotoxy(36,13); printf("GAME");
           gotoxy(38,14); printf("of");
           gotoxy(37,15); printf("LIFE");
           gotoxy(26,23); printf("press any key to continue...");
           getch();
           clrscr();
           twolinebox(1,1,80,24);
           center(3,"CONWAYS GAME OF LIFE");
           printxy(5,5,"RULE OF THE GAME");
           printxy(5,7,"If in one generation,");
           printxy(10,8,"an empty cell has exactly three neighboring cells
    containing");
           printxy(10,9,"organisms, then a new organism is born in that 
    cell
    in the ");
           printxy(10,10,"next generation.");
           printxy(5,11,"If in one generation,");
           printxy(10,12,"an organism has fewer than two neighboring cells
    containing");
           printxy(10,13,"organisms, then it dies from isolation before 
    the");
           printxy(10,14,"next generation and its cell become empty.");
           printxy(5,15,"If in one generation,");
           printxy(10,16,"an organism has more than three neighboring cells
    containing");
           printxy(10,17,"organisms, then it dies from overcrowding before
    the");
           printxy(10,18,"next generation and its cell become empty.");
           printxy(5,19,"All other organisms survive unchanged to the next
    generation.");
           center(21,"Press any key to continue...");
           getch();
    }
    
    void quit(void){
           clrscr();
           twolinebox(1,1,80,24);
           center(7,"PROGRAMMED");
           center(8,"BY");
           center(9,"BSCS 2-2");
           center(11,"Frederick Badion");
           center(12,"Michelle Baylon");
           center(13,"Kirby Adriano");
           center(14,"Jamil Soller");
           center(15,"Sweet Joan Zamora");
           getch();
    }
    
    void main(){
    char opt;
     textmode(BW80);
     do{
       clrscr();
       welcome();
       inputOrganism();
       showGeneration();
       gotoxy(5,22);
       printf("Try again? [Y/N] ");
       opt=getch();
     }while(toupper(opt)=='Y');
    quit();
    exit(1);
    }//end of main
  • Dököll
    Recognized Expert Top Contributor
    • Nov 2006
    • 2379

    #2
    Hey alireza6485!

    Looks like you have your hands full. There are certain pieces of software out there that can do what you need. Care to fetch all of Google!

    Good luck with the porject!

    Comment

    • alireza6485
      New Member
      • Jan 2009
      • 19

      #3
      I figured it out...........

      Comment

      • AlphaZionix
        New Member
        • Feb 2010
        • 1

        #4
        Frederick Badion

        Hi I'm Frederick Badion.

        I created that program...

        Thanks for copying it! BUT I would appreciate it more if you converted the program to JAVA yourself, then share it to everyone here...

        Students like you are not good learner.. because they rely mostly to Internet rather than figuring out problems themselves. just like this one.. It is already created you only have to convert it to JAVA. why bother other people to convert it for you.

        Comment

        Working...