建立一个主函数
public static void main(String args[]){
//新建对象
FiveChess chess = new FiveChess();
//调用显示界面函数
chess.showUI();
}
接着设置窗体
//新建窗体对象
JFrame frame = new JFrame();
//设置窗体名字
frame.setTitle("五子棋");
//设置窗体大小
frame.setSize(650, 580);
//设置窗体位置
frame.setLocation(700, 230);
//新建流水式布局对象
FlowLayout layout = new FlowLayout();
frame.setLayout(layout);
//新建两个面板对象
JPanel draw_panel = new JPanel();//下棋面板
frame.add(panel);
//创建五个按钮对象
JButton button1 = new JButton("开始");
JButton button2 = new JButton("人人");
JButton button3 = new JButton("人机");
JButton button4 = new JButton("悔棋");
JButton button5 = new JButton("帮助");
//设置按钮大小
button1.setPreferredSize(new Dimension(80,50));
button2.setPreferredSize(new Dimension(80,50));
button3.setPreferredSize(new Dimension(80,50));
button4.setPreferredSize(new Dimension(80,50));
button5.setPreferredSize(new Dimension(80,50));
//添加按钮
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(button5);
大致界面已经完成,现在考虑下棋,为了区分黑棋,白棋,可以设置标记int flag_chess =1;//1为黑棋 2为白棋,然后可以创建二维数组int chess[][] = new int[11][11];(这里假设为11*11的格局)保存棋子信息以防止重复下棋,相互覆盖掉了。
有了下棋的方向当然要画笔了Graphics g=null;//构造画笔对象,//获取画布 g = draw_panel.getGraphics();
现在解决绘制棋盘
//画棋盘
public void drawBoard(Graphics g){
//横线
for(int i=0;i<=10;i++)
{
g.setColor(Color.black);
g.drawLine(10, 10+50*i, 510, 10+50*i);
}
//竖线
for(int i=0;i<=10;i++)
{
g.setColor(Color.black);
g.drawLine(10+50*i,10 ,10+50*i ,510);
}
}
}
接着需要重绘棋盘
JPanel draw_panel = new JPanel(){
//重新绘制面板的方法
public void paint(Graphics g){
super.paint(g);
//画棋盘
drawBoard(g);
}
};
所有工作准备好了,现在判断输赢,六个方向,上,下,左,右,斜上,斜下(分为左斜,右斜)
/*
*判断棋子链接是否五颗
*int count_black=1;
*for(int n=i+1;n<11;n++)
*{ //向右横向判断
*if(chess[i][j]==chess[i][n])
* count_black++;
*if(chess[i][j]!=chess[i][n])
* break;
*}
*for(int n=i-1;n>=0;n--)
*{ //向左横向判断
*if(chess[i][j]==chess[i][n])
* count_black++;
*if(chess[i][j]!=chess[i][n])
* break;
*}
*if(count_black==5||count_black>5)
*{JOptionPane.showMessageDialog(null, "你赢了!");
*}
count_black=1;
*for(int n=i+1;n<11;n++)
*{ //向下判断
*if(chess[i][j]==chess[n][j])
* count_black++;
*if(chess[i][j]!=chess[n][j])
* break;
*}
*for(int n=i-1;n>=0;n--)
*{ //向上判断
*if(chess[i][j]==chess[n][j])
* count_black++;
*if(chess[i][j]!=chess[n][j])
* break;
*}
*if(count_black==5||count_black>5)
*JOptionPane.showMessageDialog(null, "你赢了!");
count_black=1;
*for(int n=i+1,m=j+1;n<11;n++)
*{ //斜向下判断
*if(chess[i][j]==chess[n][m])
* count_black++;
*if(chess[i][j]!=chess[n][m])
* break;
*}
*for(int n=i-1,m=j-1;n>=0;n--)
*{ //向上判断
*if(chess[i][j]==chess[n][m])
* count_black++;
*if(chess[i][j]!=chess[n][m])
* break;
*}
*if(count_black==5||count_black>5)
*JOptionPane.showMessageDialog(null, "你赢了!");
*
*count_black=1;
for(int n=i+1,m=j-1;n<11;n++,m--)
{ //右斜向下判断
if(chess[i][j]==chess[n][m])
count_black++;
if(chess[i][j]!=chess[n][m])
break;
}
for(int n=i-1,m=j+1;n>=0;n--,m++)
{ //右斜向上判断
if(chess[i][j]==chess[n][m])
count_black++;
if(chess[i][j]!=chess[n][m])
break;
}
if(count_black==5||count_black>5)
JOptionPane.showMessageDialog(null, "恭喜,黑棋赢了!");
* */
以上是黑棋判断,白棋一样的方法。
五子棋已经基本成型,人机对战需要算法,以后补充,现在只是人人对战。