java游戏代码--五子棋
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
class WuZiQi extends JFrame implements Runnable, ActionListener {
final static int Player=1;
final static int AI =-1;
ClassLoader cl = this.getClass().getClassLoader();
Toolkit tk = Toolkit.getDefaultToolkit();
int length=14, game_state, winner, check, step;
int grid[][] = new int[length][length];
int locX, locY /* 囱竚 */, count /* 硈囱计 */, x, y /* 既竚 */, displace_x=0, displace_y=0 /* 簿秖 */, direction;
ArrayList steps = new ArrayList(); /* 癘魁囱? */
JPopupMenu control_menu = new JPopupMenu(); /* 龄匡虫 */
JMenuItem[] command = new JMenuItem[4];
String[] command_str={"囱", "郎", "弄郎", "秨"};
int[][] dir = { {-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1} };
boolean[] dir2 = new boolean[8];
boolean turn;
String message;
final JDialog dialog = new JDialog(this, "选择", true);
Font font=new Font("new_font", Font.BOLD, 20);
Grid grids[][] = new Grid[length][length];
Image white= tk.getImage(cl.getResource("white.png"));
Image black= tk.getImage(cl.getResource("black.png"));
Image title= tk.getImage(cl.getResource("title.png"));
Image temp;
JPanel boardPanel, bigpanel;
JRadioButton[] choice = new JRadioButton[2];
final static int Start =0;
final static int Select =1;
final static int Playing =2;
final static int End =3;
*/ final static int nil=-1; /* 礚よ
final static int oblique_1 =0; /* オ */
final static int oblique_2 =1; /* オ */
final static int horizontal =2; /* 绢 */
final static int vertical=3; /* */
WuZiQi()
{
super("五子棋");
boardPanel = new JPanel();
boardPanel.setLayout(new GridLayout(length, length, 0, 0));
boardPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
for(int i=0; i=0 && x=0 && y=0 && x=0 && y=5)
{
game_state=End;
winner=check;
ThreadStart();
}
else if(direction!=nil)
WinCheck2();
}
public void ReStart()
{
for(int i=0; i=length || y<0 || y>=length || grid[x][y]!=0) && displace_y<8);
for(int i=0; i<8; i++)
dir2[i]=false;
if(x>=0 && x=0 && y0; i--)
{
play=!play;
gobangAI4(play);
if(turn)
return;
}
for(int i=7; i>=4; i--)
{
play=!play;
step=i/2;
gobangAI2(play);
if(turn)
return;
}
gobangAI5();
if(!turn)
gobangRandom();
}
private void gobangAI2(boolean player)
{
check = player? Player: AI;
for(int i=0; i=0 && x=0 && y=0 && x+displace_x=0 &&
y+displace_y=length || y-step*displace_y<0 ||
y-step*displace_y>=length || !gobang_SpaceAI(x+displace_x, y+displace_y) ||
猵 */ (grid[x-step*displace_x][y-step*displace_y]!=0 && step<3) ) /* 癸翴
gobangAI3_2();
else
setMark2(x+displace_x, y+displace_y);
}
else
gobangAI3_2();
}
else
gobangAI3(x, y);
}
else
gobangAI3_2();
}
private void gobangAI3_2()
{
if(direction!=vertical)
{
count=1;
direction=direction+1;
gobangAI3(locX, locY);
}
}
private void gobangAI4(boolean player)
{
check = player? Player: AI;
for(int i=0; i=0 && x=0 && y=0 && x=0 && y=4)
setMark(locX, locY);
else if(direction!=nil)
gobangAI4();
}
private void gobangAI5()
{
for(int i=0; i=0 && x=0 && y=0 && x=0 && y1)
{
count = count==4? 3: count;
while(x>=0 && x=0 && y=0 && x=0 && y=5)
return true;
else
{
space=0;
x=locX-displace_x;
y=locY-displace_y;
while(x>=0 && x=0 && y=5)
return true;
else
return false;
}
}
public void ThreadStart()
{
new Thread(this).start();
}
private void setMark(int x, int y)
{
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>=0 && temp_x=0 && temp_y=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; i<2; 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; i
本文档为【java游戏代码--五子棋】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。