import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
class Gobang 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("res/white.png"));
Image black= tk.getImage(cl.getResource("res/black.png"));
Image title= tk.getImage(cl.getResource("res/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; /* 直向 */
Gobang()
{
super("五子棋");
boardPanel = new JPanel();
boardPanel.setLayout(new GridLayout(length, length, 0, 0));
boardPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
for(int i=0; i<length; i++)
for(int j=0; j<length; j++)
{
grids[i][j] = new Grid(i, j);
boardPanel.add(grids[i][j]);
}
bigpanel = new JPanel();
bigpanel.add(boardPanel, BorderLayout.CENTER);
getContentPane().add(bigpanel, BorderLayout.SOUTH);
game_state=Start;
ThreadStart();
dialog.setSize(160, 100);
dialog.setResizable(false);
dialog.setLocationRelativeTo(null);
ButtonGroup choice_group = new ButtonGroup();
JPanel choice_menu = new JPanel();
choice[0] = new JRadioButton("黑子", new ImageIcon(black), true);
choice[1] = new JRadioButton("白子", new ImageIcon(white));
for(int i=0; i<choice.length; i++)
{
choice_menu.add(choice[i]);
choice_group.add(choice[i]);
}
for(int i=0; i<command.length; i++)
{
command[i] =new JMenuItem(command_str[i]);
command[i].addActionListener(this);
control_menu.add(command[i]);
}
JButton select = new JButton("確定");
JPanel select_menu = new JPanel();
select_menu.add(select);
select.addActionListener(this);
dialog.getContentPane().add(choice_menu, BorderLayout.NORTH);
dialog.getContentPane().add(select_menu, BorderLayout.CENTER);
setIconImage(title);
setResizable(false);
setSize(300, 335);
setVisible(true);
setLocationRelativeTo(null);
}
public static void main(String[] arg)
{
Gobang application = new Gobang();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent event)
{
if( event.getSource().equals(command[0]) )
{
if(steps.size()!=0)
undo();
}
else if(event.getSource().equals(command[1]) )
Save();
else if(event.getSource().equals(command[2]) )
Load();
else if(event.getSource().equals(command[3]) )
ReStart();
else
{
if(choice[1].isSelected())
{
temp=white;
white=black;
black=temp;
}
dialog.dispose();
}
}
public void WinCheck()
{
check = turn? Player: AI;
direction=oblique_1;
WinCheck2();
}
public void WinCheck2()
{
count=1;
switch(direction)
{
case oblique_1: displace_x=1;
displace_y=-1;
direction=oblique_2;
break;
case oblique_2: displace_x=displace_y=1;
direction=horizontal;
break;
case horizontal: displace_x=1;
displace_y=0;
direction=vertical;
break;
case vertical: displace_x=0;
displace_y=1;
direction=nil;
break;
}
x=locX+displace_x;
y=locY+displace_y;
while(x>=0 && x<length && y>=0 && y<length && grid[x][y]==check)
{
count=count+1;
x=x+displace_x;
y=y+displace_y;
}
x=locX-displace_x;
y=locY-displace_y;
while(x>=0 && x<length && y>=0 && y<length && grid[x][y]==check)
{
count=count+1;
x=x-displace_x;
y=y-displace_y;
}
if(count>=5)
{
game_state=End;
winner=check;
ThreadStart();
}
else if(direction!=nil)
WinCheck2();
}
public void ReStart()
{
for(int i=0; i<length; i++)
for(int j=0; j<length; j++)
grids[i][j].Initial();
winner=0;
steps.clear();
game_state=Playing;
}
public void gobangRandom()
{
displace_y=0;
do
{
displace_x=(int)(Math.random()*8);
x =locX+dir[displace_x][0];
y =locY+dir[displace_x][1];
if(!dir2[displace_x])
{
displace_y=displace_y+1;
dir2[displace_x]=true;
}
}while((x<0 || x>=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<length && y>=0 && y<length && grid[x][y]==0)
setMark(x, y);
else
gobangRandom2();
}
private void gobangRandom2()
{
do
{
x=(int)(Math.random()*length);
y=(int)(Math.random()*length);
}while(grid[x][y]!=0);
setMark(x, y);
}
private void gobangAI()
{
boolean play=true;
for(int i=2; i>0; 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<length; i++)
for(int j=0; j<length; j++)
{
if(turn)
break;
if(grid[i][j] == check)
{
count=1;
direction=oblique_1;
gobangAI3(i, j);
}
}
}
private void gobangAI3(int x, int y)
{
if(count==1)
{
locX=x;
locY=y;
}
switch(direction)
{
case oblique_1: displace_x=1;
displace_y=-1;
break;
case oblique_2: displace_x=displace_y=1;
break;
case horizontal: displace_x=1;
displace_y=0;
break;
case vertical: displace_x=0;
displace_y=1;
break;
}
x=x+displace_x;
y=y+displace_y;
if(x>=0 && x<length && y>=0 && y<length && grid[x][y]==check)
{
count=count+1;
if(count==step)
{
if(x+displace_x>=0 && x+displace_x<length && y+displace_y>=0 && y+displace_y<length && grid[x+displace_x][y+displace_y]==0)
{
if(x-step*displace_x<0 || x-step*displace_x>=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()
{
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
经典java小程序源代码合集-java编的五子棋经典java小程序源代码合集-java编的五子棋经典java小程序源代码合集-java编的五子棋经典java小程序源代码合集-java编的五子棋经典java小程序源代码合集-java编的五子棋经典java小程序源代码合集-java编的五子棋经典java小程序源代码合集-java编的五子棋经典java小程序源代码合集-java编的五子棋经典java小程序源代码合集-java编的五子棋经典java小程序源代码合集-java编的五子棋
资源推荐
资源详情
资源评论
收起资源包目录
java编的五子棋.rar (2个子文件)
java编的五子棋
Gobang.java 17KB
Gobang.jar 10KB
共 2 条
- 1
资源评论
deepboat
- 粉丝: 161
- 资源: 517
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功