Irfan Satria

mengenai kehidupan sang penulis

Cari Blog Ini

Tutorial Pembuatan Game Catur (menggunakan java)

Sekerdar Sharing aja...

udah lama gak upload artiker, kali ini saya akan menjelaskan cara membuat game catur sederhana dengan menggunakan java sebagai developernya dan menggunakan editor Netbeans



Pertama-tama buat dahulu class utama sebagai inti dari program yang dapat menghasilkan execute yaitu class Chess.java, kemudian simpan class itu dalam folder /src/ChessGame


/* coding program pemanggilnya*/


package ChessGame;

import MainFrame.ChessFrame.MainFrame;

public class Chess {

public static void main(String[] args) {

MainFrame F=new MainFrame() ;

}

}



Dibawah ini adalah listing dari langkah-langkah catur :

1 .Untuk langkah castle (benteng) adalah berjalan lurus dengan alur horizontal atau vertikal.


/*listing langkah castle (benteng)*/

public boolean PieceInMYway(int x, int y,Point othersPostion) {

int j=y;

int i=x;

if(((y==Y)&&(x>(X)||(x<(X))))) {


if((X


while( (i!=X+1)) {

i--;

if(((othersPostion.y)==j)&&((othersPostion.x==i)))//there Same Color piece

{

return true;

}

}

else if((X>i)) {

while( (i!=X-1)) {

i++;

if(((othersPostion.y)==j)&&((othersPostion.x==i))) {

return true;

}

}

}

} else if((((y>Y)||(y

if((Y

while((j!=Y+1)) {

j--;

if(((othersPostion.y)==j)&&((othersPostion.x==i))) {

return true;

}

}

} else if((Y>j)) {

while((j!=Y-1)) {

j++;

if(((othersPostion.y)==j)&&((othersPostion.x==i))) {

return true;

}

}

}

}

return false;

}


2. Untuk langkah Elephant (Sluncur) adalah berjalan lurus diagonal ke kanan & kekiri.


/* langkah-langkah (elephant) sluncur */

public boolean checkKing(int x, int y,Point othersPostion)

{

int j=y;

int i=x;

if((x-y)==(X-Y))

{

if(x>X&&y>Y)

{

while((j!=Y )&&(i!=X ))

{

j--;i--;

if(((othersPostion.y)==j)&&((othersPostion.x==i)))

{

return true;

}

}

}

else if(x

while((j!=Y )&&(i!=X ))

{

j++;i++;

if(((othersPostion.y)==j)&&((othersPostion.x==i)))

{

return true;

}

}

}

else if(((x+y))==((X+Y)))

{

if((Xj))

{

while(((j!=Y ))&&((i!=X )))

{

j++;i--;

if(((othersPostion.y)==j)&&((othersPostion.x==i)))

{

return true;

}

}

}

else if((X>i)&&(Y

{

while((j!=X )&&(i!=X ))

{

j--;i++;

if(((othersPostion.y)==j)&&((othersPostion.x==i)))

{

return true;

}

}

}

}

return false;

}


3. Untuk langkah Horse (Kuda) adalah berjalan membentuk huruf “L”.


/* listting langkah horse (kuda)*/

public boolean Canmove(int x, int y) {

if((x+1==X)&&(y+2==Y)||(x+1==X)&&(y-2==Y)||(x-1==X)&&(y+2==Y)||(x-1==X)&&(y-2==Y)||(x+2==X)&&(y+1==Y)||(x+2==X)&&(y-1==Y)||(x-2==X)&&(y+1==Y)||(x-2==X)&&(y-1==Y))

{

return true;

} else {

return false;}

}


4. Untuk langkah Queen (Patih) adalah berjalan bebas ke berbagai arah.


/* listing langkah queen (patih)*/

public boolean PieceInMYway(int x, int y,Point othersPostion)

{

int j=y;

int i=x;

if(((y==Y)&&(x>(X)||(x<(X)))))

{

if((X

while( (i!=X+1))

{

i--;

if(((othersPostion.y)==j)&&((othersPostion.x==i)))//there Same Color piece

{

return true;

}

}

else if((X>i))

{

while( (i!=X-1))

{

i++;

if(((othersPostion.y)==j)&&((othersPostion.x==i)))

{

return true;

}

}

}

}

else if((((y>Y)||(y

{

if((Y

{

while((j!=Y+1))

{

j--;

if(((othersPostion.y)==j)&&((othersPostion.x==i)))

{

return true;

}

}

}

else if((Y>j))

{

while((j!=Y-1))

{

j++;

if(((othersPostion.y)==j)&&((othersPostion.x==i)))

{

return true;

}

}

}

}

else if((x-y)==(X-Y))

{

if(x>X&&y>Y)

{

while((j!=Y+1)&&(i!=X+1))

{

j--;i--;

if(((othersPostion.y)==j)&&((othersPostion.x==i)))

{

return true;

}

}

}

else if(x

while((j!=Y-1)&&(i!=X-1))

{

j++;i++;

if(((othersPostion.y)==j)&&((othersPostion.x==i)))

{

return true;

}

}

}

else if((x+y)==(X+Y))

{

if((Xj))

{

while((j!=Y-1)&&(i!=X+1))

{

j++;i--;

if(((othersPostion.y)==j)&&((othersPostion.x==i)))

{

return true;

}

}

}

else if((X>i)&&(Y

{

while((j!=Y+1)&&(i!=X-1))

{

j--;i++;

if(((othersPostion.y)==j)&&((othersPostion.x==i)))

{

return true;

}

}

}

}

return false;

}


5. Untuk langkah King (Raja) adalah berjalan bebas ke berbagai arah dengan langkah 1 kotak.


/* listing langkah king (raja)*/

public boolean Canmove(int x, int y)

{

if(((y==Y)&&(x==(X-1)))||((y==Y-1)&&(x==(X+1)))

||((y==Y-1)&&(x==(X-1)))||((y==Y+1)&&(x==(X+1)))

||(((y==Y+1)&&x==(X-1)))||((y==Y)&&(x==(X+1)))

||((y==Y-1)&&x==((X)))||((y==Y+1)&&(x==(X))))

{

return true;

}

return false;

}


6. Untuk langkah Piece (Pion) adalah berjalan vertikal 2 kotak atau 1 kotak.


/* listing langkah piece (pion)*/

public boolean PieceInMYway(int x, int y,Point othersPostion ,String typeColor ) {

if(Y-y==2||Y-y==-2) {

if((typeColor.equals("black"))) {

if((((y-1==othersPostion.y)&&(x==(othersPostion.x))))&&!movedbefore) {

return true;

} else return false;

}

else if (typeColor.equals("white")) {

if(((y+1==othersPostion.y)&&(x==(othersPostion.x)) &&!movedbefore)) {

return true;

} else

return false;

}

}

return false;

}


Dan inilah tampilan dari program catur :



Selamat mencoba…….!