Nano'nun Günlüğü…

Ideallerimi gerceklestirmek icin arastiriyorum, Unutmamak icin yaziyorum!

  • Bulundugunuz Sayfa: 
  • Ana Sayfa
  • Java ile Satranc Oyunu

Java ile Satranc Oyunu

Gönderim Ağustos 16th, 2014

javaBu makalede, Java’da oyun programlama yonunuzu gelistirebileceginiz bir uygulamanin kodlarini paylasiyor olucam.

Internet ortaminda da buna benzer bir cok oyun programlama orneklerini bulup kendinizini daha iyi bir sekilde gelistirebilirsiniz.

Board.Java

import java.applet.Applet;
import java.awt.*;

public class Board extends java.awt.Canvas
implements
java.awt.event.MouseListener, java.awt.event.MouseMotionListener,
java.lang.Runnable
{
int [] board = new int [120];

Color dunkel = new Color (0x999999);
Color hell = new Color (0xFFFFCC);
Color red = new Color (0xCC0000);
Color green = new Color (0x009900);
Color blue = new Color (0x000099);

Image [] pieces = new Image [18];

Applet parent;

int code     = 0,
start     = 21,
alt     = 21,
end    = 21,
x    = 0,
y     = 0;

int [] movelist = new int [250];
int movecounter = 0;
int color = 1;
int deep = 0;
int target = 4;
int move;

public Board (java.applet.Applet ref) {
super();
newgame ();

MediaTracker controler = new MediaTracker (ref);

pieces [1] = ref.getImage (ref.getCodeBase (), “images/beyaz_piyon.png”);
pieces [2] = ref.getImage (ref.getCodeBase (), “images/beyaz_at.png”);
pieces [3] = ref.getImage (ref.getCodeBase (), “images/beyaz_fil.png”);
pieces [4] = ref.getImage (ref.getCodeBase (), “images/beyaz_kale.png”);
pieces [5] = ref.getImage (ref.getCodeBase (), “images/beyaz_vezir.png”);
pieces [6] = ref.getImage (ref.getCodeBase (), “images/beyaz_sah.png”);

pieces [11] = ref.getImage (ref.getCodeBase (), “images/siyah_piyon.png”);
pieces [12] = ref.getImage (ref.getCodeBase (), “images/siyah_at.png”);
pieces [13] = ref.getImage (ref.getCodeBase (), “images/siyah_fil.png”);
pieces [14] = ref.getImage (ref.getCodeBase (), “images/siyah_kale.png”);
pieces [15] = ref.getImage (ref.getCodeBase (), “images/siyah_vezir.png”);
pieces [16] = ref.getImage (ref.getCodeBase (), “images/siyah_sah.png”);

for (int i = 1; i < 7; i++)
{
controler.addImage (pieces [i], 0);
controler.addImage (pieces [i + 10], 0);
}

try {
controler.waitForAll ();
} catch (InterruptedException e) {
System.out.println (“Images not successfull loaded – Trying again …”);
controler.checkID (0, true);
}

parent = ref;

addMouseListener (this);
addMouseMotionListener (this);
}

public void execute (int start, int end)
{
board [end] = board [start];
board [start] = 0;

paintField (end);
paintField (start);

if (color == 1)
color = 2;
else
color = 1;

movecounter = 0;
genmove ();
}

public void genmove () {
for (int i = 21; i < 99; i++)
{
if (board [i] % 100 / 10 == color)
{
switch (board [i] % 10)
{
case 1:
if (color == 1)
{
if (board [i-10] == 0)
simulize ( i, i-10);
if (board [i- 9] % 100 / 10 == 2)
simulize ( i, i-9 );
if (board [i-11] % 100 / 10 == 2)
simulize ( i, i-11);
if ( (i>80) && ( ( board [i-10] == 0) && (board [i-20] == 0)))
simulize ( i, i-20);
} else {
if (board [i+10] == 0)
simulize ( i, i+10);
if (board [i+9] % 100 / 10 == 1)
simulize (i, i+9);
if (board [i+11] % 100 / 10 == 1)
simulize (i, i+11);
if ( (i<39) && ( (board [i+10] == 0) && (board [i+20] == 0)))
simulize (i, i+20);
}
break;
case 2:
simulize (i, i+12);
simulize (i, i-12);
simulize (i, i+21);
simulize (i, i-21);
simulize (i, i+19);
simulize (i, i-19);
simulize (i, i+8 );
simulize (i, i-8 );
break;
case 5:
case 3:
multisimulize ( i,  -9);
multisimulize ( i, -11);
multisimulize ( i,  +9);
multisimulize ( i, +11);

if (board [i] % 10 == 3)
break;
case 4:
multisimulize (i, -10);
multisimulize (i, +10);
multisimulize (i,  -1);
multisimulize (i,  +1);
break;
case 6:
simulize (i, i+1);
simulize (i, i-1);
simulize (i, i+10);
simulize (i, i-10);
simulize (i, i+9);
simulize (i, i-9);
simulize (i, i+11);
simulize (i, i-11);
}
}
if ( i%10 == 8)
i += 2;
}
}

public boolean ischeck () {
int king = 0;

for ( int i = 21; i < 99; i++)
{
if ((board [i] % 100 / 10 == color) && (board [i] % 10 == 6))
{
king = i;
break;
}

if ( i % 10 == 8)
i += 2;
}

if ((board [king-21] % 10 == 2) && (board [king-21] % 100 / 10 != color))
return true;
if ((board [king+21] % 10 == 2) && (board [king+21] % 100 / 10 != color))
return true;
if ((board [king-19] % 10 == 2) && (board [king-19] % 100 / 10 != color))
return true;
if ((board [king+19] % 10 == 2) && (board [king+19] % 100 / 10 != color))
return true;
if ((board [king- 8] % 10 == 2) && (board [king- 8] % 100 / 10 != color))
return true;
if ((board [king+ 8] % 10 == 2) && (board [king+ 8] % 100 / 10 != color))
return true;
if ((board [king-12] % 10 == 2) && (board [king-12] % 100 / 10 != color))
return true;
if ((board [king+12] % 10 == 2) && (board [king+12] % 100 / 10 != color))
return true;

int j = king;
while (board [j – 9] != 99)
{
j -= 9;
if (board [j] % 100 / 10 == color)
break;
if (board [j] == 0)
continue;
if ((board [j] % 10  == 3) || (board [j] % 10  == 5))
return true;
else
break;
}

j = king;
while (board [j+9] != 99)
{
j += 9;
if (board [j] % 100 / 10 == color)
break;
if (board [j] == 0)
continue;
if ((board [j] % 10 == 3) || (board [j] % 10 == 5))
return true;
else
break;
}

j = king;
while (board [j-11] != 99)
{
j -= 11;
if (board [j] % 100 / 10 == color)
break;
if (board [j] == 0)
continue;
if ( (board [j] % 10 == 3) || (board [j] % 10 == 5))
return true;
else
break;
}

j = king;
while (board [j+11] != 99)
{
j +=11;
if (board [j] % 100 / 10 == color)
break;
if (board [j] == 0)
continue;
if ( (board [j] % 10 == 3) || (board [j] % 10 == 5))
return true;
else
break;
}

j = king;
while (board [j-10] != 99)
{
j -= 10;
if (board [j] % 100 / 10 == color)
break;
if (board [j] == 0)
continue;
if ((board [j] % 10 == 4) || (board [j] % 10 == 5))
return true;
else
break;
}
j = king;
while (board [j+10] != 99)
{
j += 10;
if (board [j] % 100 / 10 == color)
break;
if (board [j] == 0)
continue;
if ((board [j] % 10 == 4) || (board [j] % 10 == 5))
return true;
else
break;
}
j = king;
while (board [j-1] != 99)
{
j -=1;
if (board [j] % 100 / 10 == color)
break;
if (board [j] == 0)
continue;
if ((board [j] % 10 == 4) || (board [j] % 10 == 5))
return true;
else
break;
}
j = king;
while (board [j+1] != 99)
{
j +=1;
if (board [j] % 100 / 10 == color)
break;
if (board [j] == 0)
continue;
if ((board [j] % 10 == 4) || (board [j] % 10 == 5))
return true;
else
break;
}

if (color == 1)
{
if ((board [king-11] % 10 == 1) && (board [king-11] % 100 / 10 == 2))
return true;
if ((board [king- 9] % 10 == 1) && (board [king- 9] % 100 / 10 == 2))
return true;
} else {
if ((board [king+11] % 10 == 1) && (board [king+11] % 100 / 10 == 1))
return true;
if ((board [king+ 9] % 10 == 1) && (board [king+ 9] % 100 / 10 == 1))
return true;
}

if ( board [king+ 1] % 10 == 6 )
return true;
if ( board [king- 1] % 10 == 6 )
return true;
if ( board [king+10] % 10 == 6 )
return true;
if ( board [king-10] % 10 == 6 )
return true;
if ( board [king+11] % 10 == 6 )
return true;
if ( board [king-11] % 10 == 6 )
return true;
if ( board [king+ 9] % 10 == 6 )
return true;
if ( board [king- 9] % 10 == 6 )
return true;

return false;
}

public boolean isvalid (int move)
{
for (int i = 0; i < movecounter; i++) {
if (movelist [i] == move)
return true;
}
return false;
}
public void mouseClicked(java.awt.event.MouseEvent e) {
}
public void mouseDragged(java.awt.event.MouseEvent e) {
x = e.getX() / 40;
if (x < 0 )
x = 0;
if (x > 7 )
x = 7;

y = e.getY() / 40;
if (y < 0)
y = 0;
if (y > 7 )
y = 7;

end = 21 + y * 10 + x;

if ( end != alt)
{
if    (alt != start)
paintField (alt);

if ( end != start)
{
Graphics g = getGraphics ();

if ( (code != 1) && (isvalid (start * 100 + end) ))
g.setColor (green);
else
g.setColor (red);

g.fillRect (x * 40, y * 40, 40, 40);
try {
g.drawImage (pieces [board [end] % 100 – 10], x * 40, y * 40, 40, 40, parent);
} catch (ArrayIndexOutOfBoundsException ex) {}
}

alt = end;
}
}
public void mouseEntered(java.awt.event.MouseEvent e) {
}
public void mouseExited(java.awt.event.MouseEvent e) {
}
public void mouseMoved(java.awt.event.MouseEvent e) {
}
public void mousePressed(java.awt.event.MouseEvent e)
{
x = e.getX() / 40;
if (x < 0)
x = 0;
if (x > 7)
x = 7;

y = e.getY() / 40;
if (y < 0)
y = 0;
if (y > 7)
y = 7;

start = 21 + y*10 + x;
alt = start;
end = start;

Graphics g = getGraphics ();
g.setColor (blue);
g.fillRect (x * 40, y * 40, 40, 40);
try {
g.drawImage (pieces [board [start] % 100 – 10], x * 40, y * 40, 40, 40, parent);
} catch (ArrayIndexOutOfBoundsException ex) {}
}
public void mouseReleased(java.awt.event.MouseEvent e) {

paintField (start);
paintField (end);

if ((code != 1) && (isvalid (start * 100 + end ) ))
execute (start, end);
}

public void multisimulize (int start, int inc) {
int to = start;

while ((board [to + inc ] != 99) && (board [to + inc] % 100 / 10  != color))
{
to += inc;

if (board [to] != 0)
{
simulize (start, to);
return;
}
simulize (start, to);
}
simulize (start, to);
}

public void newgame ()
{
int [] org = {
99,    99,    99,    99,    99,    99,    99,    99,    99,    99,
99,    99,    99,    99,    99,    99,    99,    99,    99,    99,
99,    124,    22,    23,    25,    126,    23,    22,    124,    99,
99,    21,    21,    21,    21,    21,    21,    21,    21,    99,
99,    00,    00,    00,    00,    00,    00,    00,    00,    99,
99,    00,    00,    00,    00,    00,    00,    00,    00,    99,
99,    00,    00,    00,    00,    00,    00,    00,    00,    99,
99,    00,    00,    00,    00,    00,    00,    00,    00,    99,
99,    11,    11,    11,    11,    11,    11,    11,    11,    99,
99,    114,    12,    13,    15,    116,    13,    12,    114,    99,
99,    99,    99,    99,    99,    99,    99,    99,    99,    99,
99,    99,    99,    99,    99,    99,    99,    99,    99,    99 };

for (int i=0; i < 120; i++)
board [i] = org [i];

repaint ();

movecounter = 0;
color = 1;
genmove ();
}

public void paint (Graphics g) {
for ( int i = 21; i < 99; i++)
{
paintField (i);
if ( i%10 == 8)
i += 2;
}
}

public void paintField (int index)
{
Graphics g = getGraphics ();

int x = (index – 21) % 10;
int y = (index – 21) / 10;

if ( (x*11 + y) % 2 == 0)
g.setColor( hell );
else
g.setColor( dunkel );

g.fillRect ( x * 40, y * 40, 40, 40);

try {
g.drawImage (pieces [board [index] % 100 – 10], x * 40, y * 40, 40, 40, parent);
} catch (ArrayIndexOutOfBoundsException e) {}
}

public void run() {
code = 1;

deep = 0;
target = 4;

movecounter = 0;
genmove ();

if (movecounter == 0)
{
if (ischeck () )
parent.getAppletContext ().showStatus (“white wins!”);
else
parent.getAppletContext ().showStatus (“game is a draw!”);
return;
}

execute ( move / 100, move % 100 );

code = 0;
}

public void simulize (int start, int end) {
if ((board [end] == 99) || (board [end] % 100 / 10 == color))
return;

int orgstart = board [start];
int orgend = board [end];

board [end] = board [start];
board [start] = 0;

if (! ischeck ())
{
movelist [movecounter] = start * 100 + end;
movecounter++;
}

board [start] = orgstart;
board [end] = orgend;
}
}

ChessPartner.Java

import java.applet.*;
import java.awt.*;

public class ChessPartner extends Applet
{
Board brain;
Button newgame = new Button (“Yeni Oyun Baslat”);

public boolean action (Event evt, Object arg) {
if ( ((String) arg).equals (“Yeni Oyun Baslat”))
brain.newgame ();
return true;
}

public void init() {
super.init();    brain = new Board (this);
setBackground (Color.lightGray);
setLayout (new BorderLayout (10,10));
add (“Center”, brain);
add (“South”, newgame);
}

public Insets insets () {
return new Insets (10,10,10,10);
}
}

Ekran Goruntusu

 

satranc

 

 

 

 

 

 

 

 

 

 

 

Keyifli Calismalar Dilerim.

Etiketler: , , ,
Bulundugu Konu Etiketleri Java, Yazilim |

Lutfen Yorumlarinizi Burdan Ulastiriniz!...

You must be Kullanici Adiniz : to post a comment.

Istatistik

  • 1 Uye
  • 334 Yazi
  • 16 Yorum Var