We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Programming a Simple Chess Game
Page Index Toggle Pages: 1
Programming a Simple Chess Game (Read 787 times)
Programming a Simple Chess Game
Apr 28th, 2010, 6:28pm
 
Hello! I'm attempting to create a simple chess game for an upcoming programming project.

I'm needing some help visualizing what I'm wanting to do. I'm a huge fan of chess and I understand it inside in out. I'd like to have the player play against the computer, but I'm afraid that'd be too hard at my level of programming. So I intend to have one player versus another (they "take turns" using the mouse to make their move).

I'm deciding between trying to texture a square with a picture of a chess piece, or creating the pieces out of simple shapes.

If I were to do the latter, how would I allow the player to "click and drag" a certain piece to where he or she wants it?

When I try to texture a rectangle, I get the error of having the wrong renderer or something. I'll post the code I have for it when I get it working. Any tips for that?

I also intend to have a chess clock on the side, and maybe some sort of "Choose Your Country" option which would change the design of a flag above and below the clocks. But that's all to be done far in the future.

Any advice for me? How do I solve my player's "click and drag" problem? Thank you! Smiley
Re: Programming a Simple Chess Game
Reply #1 - Apr 28th, 2010, 7:08pm
 
That's a lot to cover. I suggest you ditch the clock idea for now, and focus on working out the simulation of a chess game, probably with a 2D view so you can see what's going on. I started one myself a while ago...

Maybe it'll help (if not, oh well), but here's as far as I got (keep in mind this is some of my older code; it's not up to my current standards!):

Quote:
static int squareSize = 40;
ChessSet MySet = new ChessSet(squareSize);

void setup(){
 size( (8*squareSize)+1, (8*squareSize)+1 );
}

void draw(){
 MySet.draw();
}



class ChessSet {

 int isquareSize;
 ChessPiece[] pieces;


 ChessSet(int squareSize) {
   isquareSize = ( squareSize > 0 ) ? squareSize : 20;
   pieces = new ChessPiece[32];
   pieces[0] = new ChessPiece( 0, 6, 1, true );
   pieces[1] = new ChessPiece( 1, 6, 1, true );
   pieces[2] = new ChessPiece( 2, 6, 1, true );
   pieces[3] = new ChessPiece( 3, 6, 1, true );
   pieces[4] = new ChessPiece( 4, 6, 1, true );
   pieces[5] = new ChessPiece( 5, 6, 1, true );
   pieces[6] = new ChessPiece( 6, 6, 1, true );
   pieces[7] = new ChessPiece( 7, 6, 1, true );
   pieces[8] = new ChessPiece( 0, 1, 1, false );
   pieces[9] = new ChessPiece( 1, 1, 1, false );
   pieces[10] = new ChessPiece( 2, 1, 1, false );
   pieces[11] = new ChessPiece( 3, 1, 1, false );
   pieces[12] = new ChessPiece( 4, 1, 1, false );
   pieces[13] = new ChessPiece( 5, 1, 1, false );
   pieces[14] = new ChessPiece( 6, 1, 1, false );
   pieces[15] = new ChessPiece( 7, 1, 1, false );
   pieces[16] = new ChessPiece( 0, 7, 2, true );
   pieces[17] = new ChessPiece( 1, 7, 3, true );
   pieces[18] = new ChessPiece( 2, 7, 4, true );
   pieces[19] = new ChessPiece( 3, 7, 6, true );
   pieces[20] = new ChessPiece( 4, 7, 5, true );
   pieces[21] = new ChessPiece( 5, 7, 4, true );
   pieces[22] = new ChessPiece( 6, 7, 3, true );
   pieces[23] = new ChessPiece( 7, 7, 2, true );
   pieces[24] = new ChessPiece( 0, 0, 2, false );
   pieces[25] = new ChessPiece( 1, 0, 3, false );
   pieces[26] = new ChessPiece( 2, 0, 4, false );
   pieces[27] = new ChessPiece( 3, 0, 6, false );
   pieces[28] = new ChessPiece( 4, 0, 5, false );
   pieces[29] = new ChessPiece( 5, 0, 4, false );
   pieces[30] = new ChessPiece( 6, 0, 3, false );
   pieces[31] = new ChessPiece( 7, 0, 2, false );    
 }  

 int pieceAt(int column, int row ){
   for( int i = 0; i< pieces.length; i++) {
     if ( ( pieces[i].getColumn() == column ) && ( pieces[i].getRow() == row ) ) {
       return( pieces[i].getType() );
     }
   }
   return(0);
 }

 boolean whoOwns(int column, int row ){
   if( !isEmpty( column, row ) ) {
     for( int i = 0; i< pieces.length; i++) {
       if ( ( pieces[i].getColumn() == column ) && ( pieces[i].getRow() == row ) ) {
         return( pieces[i].getIsWhite() );
       }
     }
   }
   return(false);
 }

 boolean isEmpty(int column, int row ){
   return( pieceAt(column, row) == 0 );
 }

 void draw(){
   stroke(color(128));
   for ( int i=0; i<8; i++ ) {
     for ( int j=0; j<8; j++ ) {
       fill(color(0));
       if( ((i+j) % 2) == 1){
         fill(color(255));
       }
       rect( (i*isquareSize), (j*isquareSize), isquareSize, isquareSize); // Draw a background square
       // Draw a piece if there is one there.
       if( !isEmpty(i, j) ){
         fill(color(0,0,128));
         if( whoOwns( i, j ) ) {
           fill(color(128,0,0));
         }
         int temp = pieceAt( i, j );
         rect( (i*isquareSize)+5, (j*isquareSize)+5, isquareSize-29+(3*temp), isquareSize-29+(3*temp) );
       }  
     }
   }
 } // End ChessSet.draw()

}

class ChessPiece {

 int icolumn;
 int irow;
 int itype;
 boolean iisLive = true;
 boolean iisWhite;

 ChessPiece( int column, int row, int type, boolean isWhite ) {
   icolumn = column;
   irow = row;
   itype = type;
   iisWhite = isWhite;
 }

 int getRow(){
   return(irow);
 }

 int getColumn(){
   return(icolumn);
 }

 int getType(){
   return(itype);
 }

 boolean getIsWhite(){
   return(iisWhite);
 }

 void setColumn(int column){
   if( iisLive ){
     icolumn = column;
   }
 }

 void setRow(int row){
   if( iisLive ){
     irow = row;
   }
 }

 void setType(int type){
   if( iisLive ){
     itype = type;
   }
 }

 void kill(){
   iisLive = false;
   irow = -1;
   icolumn = -1;
   itype = 0;
 }

}
Page Index Toggle Pages: 1