java悔棋代码 java实现悔棋思路( 四 )


do
{
space=space+1;
x=x+displace_x;
y=y+displace_y;
}while(x=0xlengthy=0ylengthgrid[x][y]==0space4);
if(space+count=5) /* 逞缁?フ场だΤ硈絬ぇ?? */
return true;
else
{
space=0;
x=locX-displace_x;
y=locY-displace_y;
while(x=0xlengthy=0ylengthgrid[x][y]==0space4)
{
space=space+1;
x=x-displace_x;
y=y-displace_y;
}
if(space+count=5) /* 逞缁?フ场だΤ硈絬ぇ?? */
return true;
else
return false;
}
}
public void ThreadStart()
{
new Thread(this).start();
}
private void setMark(int x, int y)
【java悔棋代码 java实现悔棋思路】{
steps.add(grids[x][y]);
grids[x][y].setValue(-1);
WinCheck();
turn=true;
}
/* 璸衡?ê?よ?Τ?隔 */
private void setMark2(int x, int y)
{
int space=0, temp_x=x, temp_y=y;
do
{
space=space+1;
temp_x=temp_x+displace_x;
temp_y=temp_y+displace_y;
}while(temp_x=0temp_xlengthtemp_y=0temp_ylengthgrid[temp_x][temp_y]==0space4);
if(space+step=5)
setMark(x, y);
else
setMark(x-(step+1)*displace_x, y-(step+1)*displace_y); /* э?癸翴 */
}
public void run()
{
try
{
switch(game_state)
{
case Start: Thread.sleep(2000);
dialog.show();
game_state=Playing;
repaint();
break;
case End: repaint();
Thread.sleep(1500);
ReStart();
repaint();
break;
}
}
catch(InterruptedException ex)
{
}
}
public void paint(Graphics g)
{
super.paint(g);
switch(game_state)
{
case Start: g.drawString("2005.7 by Yu Lin Tao", 91, 240);
g.setFont(font);
g.setColor(Color.BLUE);
g.drawString("Gobang", 112, 120);
g.drawImage(title, 134, 135, this);
break;
case Playing:
case End:
g.drawString("?产: 筿福:", 105, 40);
g.drawImage(black, 132, 30, this);
g.drawImage(white, 182, 30, this);
if(winner!=0)
{
g.setFont(font);
g.setColor(Color.RED);
message = winner==1? "?墓?":"?块?";
g.drawString(message, 120, 185);
}
break;
}
}
public void undo()
{
if(steps.size()0)
{
for(int i=0; i2; i++)
{
((Grid)steps.get(steps.size()-1)).Initial();
steps.remove(steps.size()-1);
}
}
}
private void Save()
{
String str;
try
{
File file = new File("innings.men");
file.createNewFile();
BufferedWriter write = new BufferedWriter(new FileWriter(file));
for(int i=0; isteps.size(); i++)
{
str=(((Grid)steps.get(i)).getLocX())+","+(((Grid)steps.get(i)).getLocY())+","+(((Grid)steps.get(i)).getValue());
write.write(str);
write.newLine();
}
write.close();
}
catch(Exception ex)
{
}
}
private void Load()
{
try
{
String[] step_array;
String str;
File file = new File("innings.men");
if(!file.exists())
{
JOptionPane.showMessageDialog(null, "⊿Τ?郎");
return;
}
BufferedReader read = new BufferedReader(new FileReader(file));
ReStart();
while(read.ready())
{
str=read.readLine();
step_array=str.split(",");
grids[Integer.parseInt(step_array[0])][Integer.parseInt(step_array[1])].setValue(Integer.parseInt(step_array[2]));
steps.add(grids[Integer.parseInt(step_array[0])][Integer.parseInt(step_array[1])]);
}
}
catch(Exception ex)
{
}
}
private class Grid extends JPanel implements MouseListener
{
int x, y, value;
boolean selected;
public Grid(int x, int y)

推荐阅读