我们都玩过五子棋那么,这么一个看似简单的游戏怎么用JAVA代码去实现呢??
五子棋需要双方来下棋
所以我们需要两个用户来对战
双方可以是人也可以是AI
一方手执白棋、一方手执黑棋
-我们通过黑白棋子来区分用户双方
属性
-棋子颜色
-棋子尺寸
-棋子行列坐标
-下的棋顺序
主要方法
-绘制棋子
-判断胜负
-有棋盘我们才有地方下棋,我们需要棋盘
属性
-棋盘尺寸
-棋盘坐标
-棋盘背景
-棋盘格
方法
-棋盘绘制
-我们无论绘制棋盘还是绘制棋子,都需要在窗口体上获取画笔,在窗口体上作画
-我们用户登录需要有登录界面、游戏模式选择要有模式选项界面、胜负判断胜负判断界面、游戏要有游戏界面
属性
-尺寸
-功能按钮
-界面标题
-界面背景
-界面布局
方法
-窗口体创建、可视化
-监听器
-地址传输
-布局设置
-按钮、面板、标签的设置
-胜负判断:五子棋顾名思义,从纵向、横向、两个斜向只要有五个同色棋子相连便赢了
-下棋顺序:白棋为先,黑白棋轮流下棋
-下棋规则:可以悔一步棋,不能重复一个地方下棋,不能将棋子下到边界之外
-输入javax.swing.*的包:import java.swing.*; //我们需要用里面的JFrame来创建窗口对象
-通过继承JFrame,用this来直接引用父类的方法设置窗口体,所以我们这里一开始可以不创建对象---子类继承父类的方法
-窗口体设置:运用JFrame的几个方法我们设置窗口体的标题、尺寸、小图标、关闭设置、可视化
package com.xsx0805;
import javax.swing.*;
import java.awt.*;
public class WuZiqi extends JFrame{
//设置窗口体
public void initUI() {
//设置窗口体属性
this.setTitle("五子棋0");//设置窗口标题
this.setSize(1100, 1000);//设置尺寸
this.setIconImage(new ImageIcon("image\\saierda.png").getImage());//设置窗口体的小图标
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭设置
this.setLocationRelativeTo(null);//让窗口提居中
this.setVisible(true);//设置可视化
}
-创建按钮对象
按钮创建格式:
JButton 对象名=new JButton();//创建按钮对象的格式
-设置按钮的各种属性---尺寸、文本内容、字体
JButton btn_1=new JButton();
JButton btn_2=new JButton();
JButton btn_3=new JButton();
JButton btn_4=new JButton();
JButton btn_5=new JButton();
JButton btn_6=new JButton();
JButton btn_7=new JButton();
JButton btn_8=new JButton();
//设置按钮内容---一些现在用的和以后慢慢完善的
btn_1.setText("悔棋");
btn_2.setText("回放");
btn_3.setText("存档");
btn_4.setText("读档");
btn_5.setText("返回菜单");
btn_6.setText("开始录制");
btn_7.setText("停止录制");
btn_8.setText("重新开始");
//设置字体
btn_1.setFont(new Font("SimSun",1,15));
btn_2.setFont(new Font("SimSun",1,15));
btn_3.setFont(new Font("SimSun",1,15));
btn_4.setFont(new Font("SimSun",1,15));
btn_5.setFont(new Font("SimSun",1,15));
btn_6.setFont(new Font("SimSun",1,15));
btn_7.setFont(new Font("SimSun",1,15));
btn_8.setFont(new Font("SimSun",1,15));
Dimension dim_2=new Dimension(100,40);
//设置按钮尺寸
btn_1.setPreferredSize(dim_2);
btn_2.setPreferredSize(dim_2);
btn_3.setPreferredSize(dim_2);
btn_4.setPreferredSize(dim_2);
btn_5.setPreferredSize(dim_2);
btn_6.setPreferredSize(dim_2);
btn_7.setPreferredSize(dim_2);
btn_8.setPreferredSize(dim_2);
-布局选择:按钮在窗口体上不可以影响到棋盘的绘制,所以我们需要将他们设置边界布局—BorderLayout,它分为 SOUTH\NORTH\CENTER\EAST\WEST五个方位
-面板设置:按钮很多,所以我们选择创建一个面板将他们加到面板上—JPanel通过面板类来创建面板对象
-加入窗体:最后将面板加入窗口体
public void initUI() {
//设置窗口体属性
this.setTitle("五子棋0");
this.setSize(1100, 1000);
this.setIconImage(new ImageIcon("image\\saierda.png").getImage());
//创建按钮面板对象
JPanel jp_1=new JPanel(new FlowLayout(FlowLayout.CENTER,20,10));
jp_1.setBackground(Color.gray);//设置面板的背景颜色
JButton btn_1=new JButton();
JButton btn_2=new JButton();
JButton btn_3=new JButton();
JButton btn_4=new JButton();
JButton btn_5=new JButton();
JButton btn_6=new JButton();
JButton btn_7=new JButton();
JButton btn_8=new JButton();
//设置按钮内容
btn_1.setText("悔棋");
btn_2.setText("回放");
btn_3.setText("存档");
btn_4.setText("读档");
btn_5.setText("返回菜单");
btn_6.setText("开始录制");
btn_7.setText("停止录制");
btn_8.setText("重新开始");
btn_1.setFont(new Font("SimSun",1,15));
btn_2.setFont(new Font("SimSun",1,15));
btn_3.setFont(new Font("SimSun",1,15));
btn_4.setFont(new Font("SimSun",1,15));
btn_5.setFont(new Font("SimSun",1,15));
btn_6.setFont(new Font("SimSun",1,15));
btn_7.setFont(new Font("SimSun",1,15));
btn_8.setFont(new Font("SimSun",1,15));
Dimension dim_1=new Dimension(1000,70);
Dimension dim_2=new Dimension(100,40);
//设置按钮大小
btn_1.setPreferredSize(dim_2);
btn_2.setPreferredSize(dim_2);
btn_3.setPreferredSize(dim_2);
btn_4.setPreferredSize(dim_2);
btn_5.setPreferredSize(dim_2);
btn_6.setPreferredSize(dim_2);
btn_7.setPreferredSize(dim_2);
btn_8.setPreferredSize(dim_2);
//设置面板大小
jp_1.setPreferredSize(dim_1);
//在面板上加入按钮
jp_1.add(btn_1);
jp_1.add(btn_2);
jp_1.add(btn_3);
jp_1.add(btn_4);
jp_1.add(btn_5);
jp_1.add(btn_6);
jp_1.add(btn_7);
jp_1.add(btn_8);
this.add(jp_1,BorderLayout.SOUTH);//将面板以边界布局加到窗口上
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
- 获取画笔--Graphics它需要在窗口体上获取
- 设置监听器:将画笔传入监听器来画棋盘和网格
//获取画笔并将画笔传入监听器
wl.setGraphics(this.getGraphics());
- 棋盘网格:设置好网格的各种属性--行列数、行列间距、网格绘制初始坐标,我们可以将他们一起写入接口再将他们接入;
package com.xsx0805;
public interface GoBaangConfig {
/*** SIZE: 棋盘间距 兼 棋子直径 */
int SIZE=50;
/*** 棋盘左上角 X坐 标 */
int X=100;
/*** 棋盘左上角 Y 坐 标 */
int Y=100;
/*** 棋盘的行列值 */
int ROW=15;
int CLOUN=15;
}
- 棋盘背景图像--创建一个文件夹image存上我们选好的画板图像和背景图像,使用的时候用ImageIcon来创建图片对象供画笔用
- 棋盘绘制:通过网格的尺寸和坐标,设置好棋盘的尺寸和绘制坐标来绘制
package com.xsx0805;
import javax.swing.*;
import java.awt.*;
public class WuZiqi extends JFrame implements GoBaangConfig{
private static final Image bgimg=new ImageIcon("image\\yellow.png").getImage();
//创建监听器对象
private WuZiqiListener wl=new WuZiqiListener();
//设置窗口体
public void initUI() {
//设置窗口体属性
this.setTitle("五子棋0");
this.setSize(1100, 1000);
this.setIconImage(new ImageIcon("image\\saierda.png").getImage());
//创建按钮面板对象
JPanel jp_1=new JPanel(new FlowLayout(FlowLayout.CENTER,20,10));
jp_1.setBackground(Color.gray);
JButton btn_1=new JButton();
JButton btn_2=new JButton();
JButton btn_3=new JButton();
JButton btn_4=new JButton();
JButton btn_5=new JButton();
JButton btn_6=new JButton();
JButton btn_7=new JButton();
JButton btn_8=new JButton();
//设置按钮内容
btn_1.setText("悔棋");
btn_2.setText("回放");
btn_3.setText("存档");
btn_4.setText("读档");
btn_5.setText("返回菜单");
btn_6.setText("开始录制");
btn_7.setText("停止录制");
btn_8.setText("重新开始");
btn_1.setFont(new Font("SimSun",1,15));
btn_2.setFont(new Font("SimSun",1,15));
btn_3.setFont(new Font("SimSun",1,15));
btn_4.setFont(new Font("SimSun",1,15));
btn_5.setFont(new Font("SimSun",1,15));
btn_6.setFont(new Font("SimSun",1,15));
btn_7.setFont(new Font("SimSun",1,15));
btn_8.setFont(new Font("SimSun",1,15));
Dimension dim_1=new Dimension(1000,70);
Dimension dim_2=new Dimension(100,40);
//设置按钮大小
btn_1.setPreferredSize(dim_2);
btn_2.setPreferredSize(dim_2);
btn_3.setPreferredSize(dim_2);
btn_4.setPreferredSize(dim_2);
btn_5.setPreferredSize(dim_2);
btn_6.setPreferredSize(dim_2);
btn_7.setPreferredSize(dim_2);
btn_8.setPreferredSize(dim_2);
//设置面板大小
jp_1.setPreferredSize(dim_1);
//在面板上加入按钮
jp_1.add(btn_1);
jp_1.add(btn_2);
jp_1.add(btn_3);
jp_1.add(btn_4);
jp_1.add(btn_5);
jp_1.add(btn_6);
jp_1.add(btn_7);
jp_1.add(btn_8);
this.add(jp_1,BorderLayout.SOUTH);//将面板以边界布局加到窗口上
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
//获取画笔并将画笔传入监听器
wl.setGraphics(this.getGraphics());
//加上监听器
this.addMouseListener(wl);
}
public void paint(Graphics g) {
//调用父类的窗体绘制方法
super.paint(g);
//加入背景图并且设计它的尺寸
//我们这里还设置了一个背景板,通过画笔的先后顺序先画背景后画棋盘来覆盖,从而实现设置背景
g.drawImage(bg1img, 0, 0, 1200, 920,null);
g.drawImage(bgimg, X-SIZE, Y-SIZE, ROW*SIZE+2*SIZE, ROW*SIZE+2*SIZE, null);
//设置棋盘网格
for(int i=0;i<=ROW;i++) {
g.drawLine(X, Y+i*SIZE, X+ROW*SIZE, Y+i*SIZE);
g.drawLine(X+i*SIZE, Y, X+i*SIZE, Y+CLOUN*SIZE);
}
}
package com.xsx0805;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class WuZiqiListener implements MouseListener,GoBaangConfig {
private Graphics g;
//设置getGraphics来传址
public void setGraphics(Graphics g) {this.g=g;}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
package com.xsx0805;
public interface GoBaangConfig {
/*** SIZE: 棋盘间距 兼 棋子直径 */
int SIZE=50;
/*** 棋盘左上角 X坐 标 */
int X=100;
/*** 棋盘左上角 Y 坐 标 */
int Y=100;
/*** 棋盘的行列值 */
int ROW=15;
int CLOUN=15;
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
int x=e.getX();
int y=e.getY();
g.fillOval(x, y,SIZE, SIZE);
//设置全局变量
int flag=0;
//然后在方法里设置循环
if(flag==0) {
g.setColor(Color.white);
flag++;
}
else if(flag==1){
g.setColor(Color.black);
flag--;
}
这样就可以实现白黑棋子轮流下棋
-问题:我们下棋不能下到网格交叉处该怎么办?
-解决办法:通过将获取到的棋子坐标转化为行列值来画图
-实现:设置全局变量chessx,chessy来表示行列值
选定好下棋的区域通过chessx=(x-X+SIZE/2)/SIZE、chessy=(y-Y+SIZE/2)/SIZE;;来实现坐标的行列值化
将画笔的画图坐标改成行列值坐标
//设置全局变量chessx,chessy来表示行列值
int chessx;
int chessy;
//选定好下棋的区域通过chessx=(x-X+SIZE/2)/SIZE、chessy=(y-Y+SIZE/2)/SIZE;;来实现坐标的行列值化
chessx=(x-X+SIZE/2)/SIZE;
chessy=(y-Y+SIZE/2)/SIZE;
//将画笔的画图坐标改成行列值坐标
g.fillOval(chessx*SIZE+X-SIZE/2, chessy*SIZE+Y-SIZE/2,SIZE, SIZE);
-问题:我们下棋有可能下到棋盘外面该如何解决?
-解决办法:通过判断所下棋子的行和列是否大于0小于行列数判断它落子是否正确
-实现:设置if语句
if(chessx<0||chessx>ROW) {
return;
}
if(chessy<0||chessy>CLOUN) {
return;
}
- 创建二维数组:int[][] chessNumArr=new chessNumArr[ROW+1][CLOUN+1]来存储信息
- 设置棋子信息:0表示没有棋子;1表示黑棋;2表示白棋;用chessNum来设置
- 存储信息、画图:先将数组对象传址给窗口体的绘图方法;通过for循环遍历棋盘给数组赋值然后画图;
//设置数组来存储棋盘信息
//设置数组来存储棋盘信息
int chessNum=0;//设置棋盘数据表示棋盘布局黑棋:1、白棋:2、空:0
int[][] chessNumArr=new int[ROW+1][CLOUN+1];
//用此方法获取数组地址
public int[][] getchessNumArr() {
return chessNumArr;
}
//窗体方法中
//获取监听器的数组数据
int [][] chessNumArr=wl.getchessNumArr();
//利用棋盘布局来再次绘制棋盘
for(int i=0;i<chessNumArr.length;i++) {
for(int j=0;j<chessNumArr[i].length;j++) {
// System.out.print(chessNumArr[j][i]+" ");//用此可以将棋盘打印出来
int chessNum=chessNumArr[i][j];
if(chessNum==2) {
g.setColor(Color.white);
g.fillOval(i*SIZE+X-SIZE/2, j*SIZE+Y-SIZE/2,SIZE, SIZE);
}
else if(chessNum==1) {
g.setColor(Color.black);
g.fillOval(i*SIZE+X-SIZE/2, j*SIZE+Y-SIZE/2,SIZE, SIZE);
} else{
}
}
}
if(chessNumArr[chessx][chessy]!=0) {
return;
}
-创建一个chess类来存储下棋的顺序和属性;
-通过取出上一下好的棋子删除;
-重画;
-加上动作监听器来获取按钮“悔棋”的操作
-将窗体类的对象传址过来使用重画刷新界面
//创建一个类存储信息
package com.xsx0805;
public class Chess {
int index;
int chessNum;
int row,clun;
public Chess(int index,int chessNum,int row,int clun) {
this.index=index;
this.chessNum=chessNum;
this.row=row;
this.clun=clun;
}
/*
* index:落子顺序
* chessNum:棋子的黑白序号
* row:行
* clun:列
* */
}
//在监听器中创建对象来存入信息
//用这个方法来存储下棋顺序
Chess[] chesslist=new Chess[ROW*CLOUN];
int chessIndex;
//下完棋子之后
//用此来存储一个棋子对象
chesslist[chessIndex]=new Chess(chessIndex,chessNum,chessx,chessy);
chessIndex++;
//在窗口体类、监听器类中加入动作监听器
//监听器中再加上动作监听让他实现鼠标和按钮监听
import java.awt.event.ActionListener;
class WuZiqiListener implements MouseListener,GoBaangConfig,ActionListener
//将窗体对象传址过来
public WuZiqi wzq;
public void setWuZiqi(WuZiqi wzq) {this.wzq=wzq;}
//写撤回按钮内容,通过取出上一次棋子信息并清除来实现悔棋目的
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String btner=e.getActionCommand();
if(btner.equals("悔棋")) {
System.out.println("撤回");
//从存储数据中取出这个棋子对象
Chess chess=chesslist[chessIndex-1];
if(chessNumArr[chess.row][chess.clun]==1) {
}else if(chessNumArr[chess.row][chess.clun]==2) {
}
//由这个棋子的行列值删除这个棋子在chessNumArr中的数据
chessNumArr[chess.row][chess.clun]=0;
//删去它在数据中的值
chesslist[chessIndex-1]=null;
chessIndex--;
wzq.repaint();
}
}
//窗口体中给按钮加上动作监听
//给按钮加上动作监听器
btn_1.addActionListener(wl);
btn_2.addActionListener(wl);
btn_3.addActionListener(wl);
btn_4.addActionListener(wl);
btn_5.addActionListener(wl);
btn_6.addActionListener(wl);
btn_7.addActionListener(wl);
btn_8.addActionListener(wl);
//使用方法传址给监听器
wl.setWuZiqi(this);
}
package com.xsx0813;
public class Chess {
int index;
int chessNum;
int row,clun;
public Chess(int index,int chessNum,int row,int clun) {
this.index=index;
this.chessNum=chessNum;
this.row=row;
this.clun=clun;
}
/*
* index:落子顺序
* chessNum:棋子的黑白序号
* row:行
* clun:列
* */
}
package com.xsx0813;
public interface GoBaangConfig {
/*** SIZE: 棋盘间距 兼 棋子直径 */
int SIZE=50;
/*** 棋盘左上角 X坐 标 */
int X=100;
/*** 棋盘左上角 Y 坐 标 */
int Y=100;
/*** 棋盘的行列值 */
int ROW=15;
int CLOUN=15;
}
package com.xsx0813;
import javax.swing.*;
import java.awt.*;
public class WuZiqi extends JFrame implements GoBaangConfig{
private static final Image bgimg=new ImageIcon("image\\yellow.png").getImage();
private static final Image bg1img=new ImageIcon("image\\背景板.png").getImage();
//创建监听器对象
private WuZiqiListener wl=new WuZiqiListener();
//设置窗口体
public void initUI() {
//设置窗口体属性
this.setTitle("五子棋0");
this.setSize(1100, 1000);
this.setIconImage(new ImageIcon("image\\saierda.png").getImage());
//创建按钮面板对象
JPanel jp_1=new JPanel(new FlowLayout(FlowLayout.CENTER,20,10));
jp_1.setBackground(Color.gray);
JButton btn_1=new JButton();
JButton btn_2=new JButton();
JButton btn_3=new JButton();
JButton btn_4=new JButton();
JButton btn_5=new JButton();
JButton btn_6=new JButton();
JButton btn_7=new JButton();
JButton btn_8=new JButton();
//设置按钮内容
btn_1.setText("悔棋");
btn_2.setText("回放");
btn_3.setText("存档");
btn_4.setText("读档");
btn_5.setText("返回菜单");
btn_6.setText("开始录制");
btn_7.setText("停止录制");
btn_8.setText("重新开始");
btn_1.setFont(new Font("SimSun",1,15));
btn_2.setFont(new Font("SimSun",1,15));
btn_3.setFont(new Font("SimSun",1,15));
btn_4.setFont(new Font("SimSun",1,15));
btn_5.setFont(new Font("SimSun",1,15));
btn_6.setFont(new Font("SimSun",1,15));
btn_7.setFont(new Font("SimSun",1,15));
btn_8.setFont(new Font("SimSun",1,15));
Dimension dim_1=new Dimension(1000,70);
Dimension dim_2=new Dimension(100,40);
//设置按钮大小
btn_1.setPreferredSize(dim_2);
btn_2.setPreferredSize(dim_2);
btn_3.setPreferredSize(dim_2);
btn_4.setPreferredSize(dim_2);
btn_5.setPreferredSize(dim_2);
btn_6.setPreferredSize(dim_2);
btn_7.setPreferredSize(dim_2);
btn_8.setPreferredSize(dim_2);
//设置面板大小
jp_1.setPreferredSize(dim_1);
//在面板上加入按钮
jp_1.add(btn_1);
jp_1.add(btn_2);
jp_1.add(btn_3);
jp_1.add(btn_4);
jp_1.add(btn_5);
jp_1.add(btn_6);
jp_1.add(btn_7);
jp_1.add(btn_8);
//给按钮加上动作监听器
btn_1.addActionListener(wl);
btn_2.addActionListener(wl);
btn_3.addActionListener(wl);
btn_4.addActionListener(wl);
btn_5.addActionListener(wl);
btn_6.addActionListener(wl);
btn_7.addActionListener(wl);
btn_8.addActionListener(wl);
this.add(jp_1,BorderLayout.SOUTH);//将面板以边界布局加到窗口上
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
//使用方法传址给监听器
wl.setWuZiqi(this);
//获取画笔并将画笔传入监听器
wl.setGraphics(this.getGraphics());
//加上监听器
this.addMouseListener(wl);
}
public void paint(Graphics g) {
//调用父类的窗体绘制方法
super.paint(g);
//加入背景图并且设计它的尺寸
g.drawImage(bg1img, 0, 0, 1200, 920,null);
g.drawImage(bgimg, X-SIZE, Y-SIZE, ROW*SIZE+2*SIZE, ROW*SIZE+2*SIZE, null);
//设置棋盘网格
for(int i=0;i<=ROW;i++) {
g.drawLine(X, Y+i*SIZE, X+ROW*SIZE, Y+i*SIZE);
g.drawLine(X+i*SIZE, Y, X+i*SIZE, Y+CLOUN*SIZE);
}
//获取监听器的数组数据
int [][] chessNumArr=wl.getchessNumArr();
//利用棋盘布局来再次绘制棋盘
for(int i=0;i<chessNumArr.length;i++) {
for(int j=0;j<chessNumArr[i].length;j++) {
// System.out.print(chessNumArr[j][i]+" ");//用此可以将棋盘打印出来
int chessNum=chessNumArr[i][j];
if(chessNum==2) {
g.setColor(Color.white);
g.fillOval(i*SIZE+X-SIZE/2, j*SIZE+Y-SIZE/2,SIZE, SIZE);
}
else if(chessNum==1) {
g.setColor(Color.black);
g.fillOval(i*SIZE+X-SIZE/2, j*SIZE+Y-SIZE/2,SIZE, SIZE);
} else{
}
}
}
}
}
package com.xsx0813;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class WuZiqiListener implements MouseListener,GoBaangConfig,ActionListener {
public WuZiqi wzq;
public void setWuZiqi(WuZiqi wzq) {this.wzq=wzq;}
private Graphics g;
public void setGraphics(Graphics g) {this.g=g;}
//判断下棋颜色
int flag=0;
//设置行列坐标
int chessx;
int chessy;
//设置数组来存储棋盘信息
int chessNum=0;//设置棋盘数据表示棋盘布局黑棋:1、白棋:2、空:0
int[][] chessNumArr=new int[ROW+1][CLOUN+1];
//通过这个方法来传值
public int[][] getchessNumArr() {
return chessNumArr;
}
//用这个方法来存储下棋顺序
Chess[] chesslist=new Chess[ROW*CLOUN];
int chessIndex;
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String btner=e.getActionCommand();
if(btner.equals("悔棋")) {
System.out.println("撤回");
//从存储数据中取出这个棋子对象
Chess chess=chesslist[chessIndex-1];
if(chessNumArr[chess.row][chess.clun]==1) {
}else if(chessNumArr[chess.row][chess.clun]==2) {
}
//由这个棋子的行列值删除这个棋子在chessNumArr中的数据
chessNumArr[chess.row][chess.clun]=0;
//删去它在数据中的值
chesslist[chessIndex-1]=null;
chessIndex--;
wzq.repaint();
}
if(btner.equals("重新开始")) {
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
//获取落点坐标
int x=e.getX();
int y=e.getY();
//进行坐标转化
chessx=(x-X+SIZE/2)/SIZE;
chessy=(y-Y+SIZE/2)/SIZE;
//设置下棋边界,做到不能在棋盘外下棋
if(chessx<0||chessx>ROW) {
return;
}
if(chessy<0||chessy>CLOUN) {
return;
}
//设置不能重复下棋
if(chessNumArr[chessx][chessy]!=0) {
return;
}
//绘制棋子
if(flag==0) {
g.setColor(Color.white);
chessNum=2;
chessNumArr[chessx][chessy]=chessNum;
flag++;
}
else if(flag==1){
g.setColor(Color.black);
chessNum=1;
chessNumArr[chessx][chessy]=chessNum;
flag--;
}
g.fillOval(chessx*SIZE+X-SIZE/2, chessy*SIZE+Y-SIZE/2,SIZE, SIZE);
//用此来存储一个棋子对象
chesslist[chessIndex]=new Chess(chessIndex,chessNum,chessx,chessy);
chessIndex++;
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
package com.xsx0813;
public class StartGame {
public static void main(String[] args) {
WuZiqi wzq=new WuZiqi();
wzq.initUI();
}
}
-创建一个boolean类在里面写好判断代码,输入棋子信息然后输出true\false来判断胜负
-方法写成static可以直接使用类调用
package com.xsx0813;
import java.awt.Color;
public class WinChess {
public static boolean win(int[][] chessNumArr,int row,int cloun) {
if(winrow(chessNumArr,row,cloun)>=5||wincloun(chessNumArr,row, cloun)>=5
||winxie_1(chessNumArr,row,cloun)>=5||winxie_2(chessNumArr,row,cloun)>=5) {
return true;
}
else {
return false;
}
}
public static int winrow(int[][] chessNumArr,int row ,int cloun) {
int count=0;
for(int j=cloun;j<=15;j++) {
if(chessNumArr[row][j]==chessNumArr[row][cloun]) {
count++;
}
else {
break;
}
}
for(int j=cloun-1;j>=0;j--) {
if(chessNumArr[row][j]==chessNumArr[row][cloun]) {
count++;
}
else {
break;
}
}
return count;
}
public static int wincloun(int[][] chessNumArr,int row ,int cloun) {
int count=1;
for(int i=row+1;i<=15;i++) {
if(chessNumArr[i][cloun]==chessNumArr[row][cloun]) {
count++;
}
else {
break;
}
}
for(int i=row-1;i>=0;i--) {
if(chessNumArr[i][cloun]==chessNumArr[row][cloun]) {
count++;
}
else {
break;
}
}
return count;
}
public static int winxie_1(int[][] chessNumArr,int row ,int cloun) {
int count=1;
for(int j=cloun+1,i=row-1;i>=0&&j<=15;i--,j++) {
if(chessNumArr[i][j]==chessNumArr[row][cloun]) {
count++;
}
else {
break;
}
}
for(int j=cloun-1,i=row+1;i<=15&&j>=0;i++,j--) {
if(chessNumArr[i][j]==chessNumArr[row][cloun]) {
count++;
}
else {
break;
}
}
return count;
}
public static int winxie_2(int[][] chessNumArr,int row ,int cloun) {
int count=1;
for(int j=row+1,i=cloun+1;i<=15&&j<=15;i++,j++) {
if(chessNumArr[i][j]==chessNumArr[row][cloun]) {
count++;
}
else {
break;
}
}
for(int j=cloun-1,i=row-1;i>=0&&j>=0;i--,j--) {
if(chessNumArr[i][j]==chessNumArr[row][cloun]) {
count++;
}
else {
break;
}
}
return count;
}
}
if(WinChess.win(chessNumArr, chessx, chessy)==true) {
if(chessNumArr[chessx][chessy]==1) {
System.out.println("黑棋胜!");
}else {
System.out.println("白棋胜!");
}
}
}
- 将我们选择这个窗口可视化关闭再引用下一个窗口体建立的方法创建窗口体;
- 我们需要将窗口体对象传址进入监听器然后实现窗口体的可视化false;
-创建一个public static void mian(String[] args){}函数里面创建一个对象引用一个窗口体建立函数;这样其他类也可以调用啦
public static void main(String[] args) {
new StartUI().startUI();
}
-存档:将存档时的棋子信息传给设置好的数组已经计数对象;
-读档:再将存储好的值赋值给此时的棋盘数组和计数对象
-重绘
//全局变量记录存档信息
int countblack1=0;
int countwhite1=0;
int[][] chessRecord=new int[ROW+1][CLOUN+1];
//设置按钮信息
if(btner.equals("存档")) {
System.out.println("存档");
//应用后面的方法来存数据
for(int i=0;i<chessNumArr.length;i++) {
for(int j=0;j<chessNumArr[i].length;j++) {
chessRecord[i][j]=chessNumArr[i][j];
}
}
countblack1=countblack;
countwhite1=countwhite;
}
if(btner.equals("读档")){
System.out.println("读档");
for(int i=0;i<chessNumArr.length;i++) {
for(int j=0;j<chessNumArr[i].length;j++) {
chessNumArr[i][j]=chessRecord[i][j];
}
}
countblack=countblack1;
countwhite=countwhite1;
wzqjf.repaint();
}
package com.xsx0813;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class LoginUI extends JFrame {
private LoginListener ll=new LoginListener();
public void LoginUI() {
String text1;
String pwd;
//设置窗口
this.setTitle("66五子棋");
this.setSize(400, 400);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setIconImage(new ImageIcon("image\\saierda.png").getImage());
//设置按钮
JButton btn=new JButton();
JButton btn1=new JButton();
btn.setText("登录");
btn1.setText("注册");
btn.setFont(new Font("SimSun",1,15));
btn1.setFont(new Font("SimSun",1,15));
//设置标签
//设置图像
ImageIcon image=new ImageIcon("image\\wzq.png");
JLabel label=new JLabel(image);
//设置账号密码文字
JLabel j1=new JLabel();
JLabel j2=new JLabel("账号:");
JLabel j3=new JLabel("密码:");
j2.setFont(new Font("STFangsong",1,17));
j3.setFont(new Font("STFangsong",1,17));
//设置面板
JPanel jpp_1=new JPanel(new FlowLayout(FlowLayout.CENTER,20,15));
JPanel jpp_2=new JPanel(new FlowLayout(FlowLayout.CENTER,30,2));
jpp_1.setBackground(Color.gray);
jpp_2.setBackground(Color.gray);
//设置输入框
JTextField jt1=new JTextField();
JPasswordField jp1=new JPasswordField();
//设置尺寸大小
Dimension dim_1=new Dimension(100,50);//面板
Dimension dim_2=new Dimension(250,25);//输入框
Dimension dim_3=new Dimension(400,200);//图片标签
Dimension dim_4=new Dimension(100,25);//按钮
//图片标签大小
label.setPreferredSize(dim_3);
//输入框尺寸
jt1.setPreferredSize(dim_2);
jp1.setPreferredSize(dim_2);
//面板尺寸
jpp_1.setPreferredSize(dim_1);
jpp_2.setPreferredSize(dim_1);
//按钮尺寸
btn.setPreferredSize(dim_4);
btn1.setPreferredSize(dim_4);
//加入监听器
//按钮加上动作监听器
btn.addActionListener(ll);
btn1.addActionListener(ll);
//将内容加到面板上
//账号--面板1
jpp_1.add(j2);
jpp_1.add(jt1);
//密码--面板1
jpp_1.add(j3);
jpp_1.add(jp1);
//按钮--面板2
jpp_2.add(btn);
jpp_2.add(btn1);
//将面板和标签加入窗体
this.add(jpp_2,BorderLayout.SOUTH);//采用边界布局放在南边
this.add(jpp_1,BorderLayout.CENTER);
this.add(label,BorderLayout.NORTH);
//设置可视化
this.setVisible(true);
//引用本类
ll.setLoginUI(this);
ll.setJTextField(jt1);
ll.setJPasswordField(jp1);
this.setLocationRelativeTo(null);
}
}
package com.xsx0813;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class LoginListener implements ActionListener{
public LoginUI loginjf;
public JTextField jt1;
public JPasswordField jp1;
public void setLoginUI(LoginUI loginjf) {this.loginjf=loginjf;}
public void setJTextField(JTextField jt1) {this.jt1=jt1;}
public void setJPasswordField(JPasswordField jp1) {this.jp1=jp1;}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String btner=e.getActionCommand();
if(btner.equals("登录")) {
String text1;
String pwd;
//获取输入框文本
text1=jt1.getText();
pwd=jp1.getText();
//判断密码是否正确
if(text1.equals("123")&&pwd.equals("123")) {
loginjf.setVisible(false);
StartUI.main(null);
}else {
JOptionPane.showMessageDialog(null, "账号或密码错误");
}
}
}
}
package com.xsx0813;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class StartUI extends JFrame implements GoBaangConfig {
private StartListener sl=new StartListener();
public static void main(String[] args) {
new StartUI().startUI();
}
public void startUI(){
//创建窗体对象
this.setTitle("66五子棋");
this.setSize(400, 400);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setIconImage(new ImageIcon("image\\saierda.png").getImage());
//设置插入图片
ImageIcon image=new ImageIcon("image\\saierda.png");
//设置标签
JLabel label2=new JLabel(image);
//设置面板
JPanel jp1=new JPanel(new FlowLayout(FlowLayout.CENTER,100,10));
jp1.setBackground(Color.gray);
//设置按钮
JButton btn1=new JButton();
JButton btn2=new JButton();
JButton btn3=new JButton();
JButton btn4=new JButton();
//设置内容
btn1.setText("人机模式");
btn2.setText("双人模式");
btn3.setText("联机对战");
btn4.setText("返回登录");
btn1.setFont(new Font("STFangsong",1,15));
btn2.setFont(new Font("STFangsong",1,15));
btn3.setFont(new Font("STFangsong",1,15));
btn4.setFont(new Font("STFangsong",1,15));
//给按钮加监听器
btn1.addActionListener(sl);
btn2.addActionListener(sl);
btn3.addActionListener(sl);
btn4.addActionListener(sl);
//设置尺寸
Dimension dim1=new Dimension(150,30);
Dimension dim2=new Dimension(100,170);
Dimension dim3=new Dimension(200,200);
//设置按钮尺寸
btn1.setPreferredSize(dim1);
btn2.setPreferredSize(dim1);
btn3.setPreferredSize(dim1);
btn4.setPreferredSize(dim1);
//设置面板尺寸
jp1.setPreferredSize(dim2);
//设置标签尺寸
label2.setPreferredSize(dim3);
//将对象加到面板上
jp1.add(btn1);
jp1.add(btn2);
jp1.add(btn3);
jp1.add(btn4);
//加入窗体
this.add(jp1,BorderLayout.SOUTH);
this.add(label2,BorderLayout.NORTH);
//可视化与传地址
sl.setStartUI(this);
this.setVisible(true);
this.setLocationRelativeTo(null);
}
}
package com.xsx0813;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
public class StartListener implements ActionListener {
public StartUI startjf;
public void setStartUI(StartUI startjf) {this.startjf=startjf;}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String btner=e.getActionCommand();
if(btner.equals("双人模式")) {
startjf.setVisible(false);
WuZiqi.main(null);
}
}
}
package com.xsx0813;
import javax.swing.*;
import java.awt.*;
public class WuZiqi extends JFrame implements GoBaangConfig{
private static final Image bgimg=new ImageIcon("image\\yellow.png").getImage();
private static final Image bg1img=new ImageIcon("image\\背景板.png").getImage();
//创建监听器对象
private WuZiqiListener wl=new WuZiqiListener();
//创建这一个对象只调用一个函数,让其他类也可以使用这个方法
public static void main(String[] args) {
new WuZiqi().initUI();
}
//设置窗口体
public void initUI() {
//设置窗口体属性
this.setTitle("五子棋0");
this.setSize(1100, 1000);
this.setIconImage(new ImageIcon("image\\saierda.png").getImage());
//创建按钮面板对象
JPanel jp_1=new JPanel(new FlowLayout(FlowLayout.CENTER,20,10));
jp_1.setBackground(Color.gray);
JButton btn_1=new JButton();
JButton btn_2=new JButton();
JButton btn_3=new JButton();
JButton btn_4=new JButton();
JButton btn_5=new JButton();
JButton btn_6=new JButton();
JButton btn_7=new JButton();
JButton btn_8=new JButton();
//设置按钮内容
btn_1.setText("悔棋");
btn_2.setText("回放");
btn_3.setText("存档");
btn_4.setText("读档");
btn_5.setText("返回菜单");
btn_6.setText("开始录制");
btn_7.setText("停止录制");
btn_8.setText("重新开始");
btn_1.setFont(new Font("SimSun",1,15));
btn_2.setFont(new Font("SimSun",1,15));
btn_3.setFont(new Font("SimSun",1,15));
btn_4.setFont(new Font("SimSun",1,15));
btn_5.setFont(new Font("SimSun",1,15));
btn_6.setFont(new Font("SimSun",1,15));
btn_7.setFont(new Font("SimSun",1,15));
btn_8.setFont(new Font("SimSun",1,15));
Dimension dim_1=new Dimension(1000,70);
Dimension dim_2=new Dimension(100,40);
//设置按钮大小
btn_1.setPreferredSize(dim_2);
btn_2.setPreferredSize(dim_2);
btn_3.setPreferredSize(dim_2);
btn_4.setPreferredSize(dim_2);
btn_5.setPreferredSize(dim_2);
btn_6.setPreferredSize(dim_2);
btn_7.setPreferredSize(dim_2);
btn_8.setPreferredSize(dim_2);
//设置面板大小
jp_1.setPreferredSize(dim_1);
//在面板上加入按钮
jp_1.add(btn_1);
jp_1.add(btn_2);
jp_1.add(btn_3);
jp_1.add(btn_4);
jp_1.add(btn_5);
jp_1.add(btn_6);
jp_1.add(btn_7);
jp_1.add(btn_8);
//给按钮加上动作监听器
btn_1.addActionListener(wl);
btn_2.addActionListener(wl);
btn_3.addActionListener(wl);
btn_4.addActionListener(wl);
btn_5.addActionListener(wl);
btn_6.addActionListener(wl);
btn_7.addActionListener(wl);
btn_8.addActionListener(wl);
this.add(jp_1,BorderLayout.SOUTH);//将面板以边界布局加到窗口上
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
//使用方法传址给监听器
wl.setWuZiqi(this);
//获取画笔并将画笔传入监听器
wl.setGraphics(this.getGraphics());
//加上监听器
this.addMouseListener(wl);
}
public void endUI1() {
//设置窗口
JFrame winjf=new JFrame();
int [][] chessNumArr=wl.getchessNumArr();
int chessx=wl.getchessx();
int chessy=wl.getchessy();
if(chessNumArr[chessx][chessy]==1) {
winjf.setTitle("黑棋胜利");
}else if(chessNumArr[chessx][chessy]==2) {
winjf.setTitle("白棋胜利");
}
winjf.setSize(500,400);
winjf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
winjf.setIconImage(new ImageIcon("image\\saierda.png").getImage());
//设置标签以及图像
ImageIcon image=new ImageIcon("image\\6.PNG");
JLabel label=new JLabel(image);
//创建按钮和面板
JButton btn_2=new JButton();
JButton btn_3=new JButton();
JButton btn_4=new JButton();
JPanel jp_2=new JPanel(new FlowLayout(FlowLayout.CENTER,10,10));
// jp_2.setBackground(Color.gray);
//设置尺寸
Dimension dim_2=new Dimension(300, 200);
Dimension dim_3=new Dimension(100, 30);
Dimension dim_4=new Dimension(200, 80);
//设置面板标签尺寸
label.setPreferredSize(dim_2);
jp_2.setPreferredSize(dim_4);
//设置按钮
//设置按钮大小
btn_2.setPreferredSize(dim_3);
btn_3.setPreferredSize(dim_3);
btn_4.setPreferredSize(dim_3);
//设置按钮文字
btn_3.setText("再来一把");
btn_4.setText("回放");
btn_2.setText("退出游戏");
//将按钮加上动作监听器
btn_3.addActionListener(wl);
btn_4.addActionListener(wl);
btn_2.addActionListener(wl);
//将它加入面板
jp_2.add(btn_4);
jp_2.add(btn_3);
jp_2.add(btn_2);
winjf.add(jp_2,BorderLayout.SOUTH);
winjf.add(label,BorderLayout.NORTH);
winjf.setVisible(true);
winjf.setLocationRelativeTo(null);
wl.setwinjf(winjf);
}
public void paint(Graphics g) {
//调用父类的窗体绘制方法
super.paint(g);
//加入背景图并且设计它的尺寸
g.drawImage(bg1img, 0, 0, 1200, 920,null);
g.drawImage(bgimg, X-SIZE, Y-SIZE, ROW*SIZE+2*SIZE, ROW*SIZE+2*SIZE, null);
//设置棋盘网格
for(int i=0;i<=ROW;i++) {
g.drawLine(X, Y+i*SIZE, X+ROW*SIZE, Y+i*SIZE);
g.drawLine(X+i*SIZE, Y, X+i*SIZE, Y+CLOUN*SIZE);
}
//获取监听器的数组数据
int [][] chessNumArr=wl.getchessNumArr();
//利用棋盘布局来再次绘制棋盘
for(int i=0;i<chessNumArr.length;i++) {
for(int j=0;j<chessNumArr[i].length;j++) {
// System.out.print(chessNumArr[j][i]+" ");
int chessNum=chessNumArr[i][j];
if(chessNum==2) {
g.setColor(Color.white);
g.fillOval(i*SIZE+X-SIZE/2, j*SIZE+Y-SIZE/2,SIZE, SIZE);
}
else if(chessNum==1) {
g.setColor(Color.black);
g.fillOval(i*SIZE+X-SIZE/2, j*SIZE+Y-SIZE/2,SIZE, SIZE);
} else{
}
}
}
//重画计数器
int countblack=wl.getcountblack();
int countwhite=wl.getcountwhite();
//画字体
Font font=new Font("宋体",Font.BOLD,25);
Color color=new Color(255,255,255,255);
g.setColor(color.gray);
g.fillRect(900, 70, 200, 150);
g.setColor(color.ORANGE);
g.setFont(font);
g.drawString("黑棋:"+countblack, 960, 130);
g.drawString("白棋:"+countwhite, 960, 170);
g.setColor(color.gray);
g.fillRect(900, 220, 200, 150);
g.setColor(color.orange);
g.setFont(font);
g.drawString("下回合", 960, 280);
if(countblack==countwhite) {
g.setColor(color.orange);
g.setFont(font);
g.drawString("白棋", 960, 320);
}else if(countblack<countwhite) {
g.setColor(color.orange);
g.setFont(font);
g.drawString("黑棋", 960, 320);
}
}
}
}
package com.xsx0813;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class WuZiqiListener implements MouseListener,GoBaangConfig,ActionListener {
public WuZiqi wzqjf;
public void setWuZiqi(WuZiqi wzqjf) {this.wzqjf=wzqjf;}
public JFrame winjf;
public void setwinjf(JFrame winjf) {this.winjf=winjf;}
private Graphics g;
public void setGraphics(Graphics g) {this.g=g;}
//判断下棋颜色
int flag=0;
//设置行列坐标
int chessx;
int chessy;
//记录黑白棋子信息
int countblack=0;
int countwhite=0;
//记录存档信息
int countblack1=0;
int countwhite1=0;
int[][] chessRecord=new int[ROW+1][CLOUN+1];
//设置数组来存储棋盘信息
int chessNum=0;//设置棋盘数据表示棋盘布局黑棋:1、白棋:2、空:0
int[][] chessNumArr=new int[ROW+1][CLOUN+1];
//通过这个方法来传值
public int[][] getchessNumArr() {
return chessNumArr;
}
public int getchessx() {
return chessx;
}
public int getchessy() {
return chessy;
}
//用这个方法来存储下棋顺序
Chess[] chesslist=new Chess[ROW*CLOUN];
int chessIndex;
//传值黑白棋数
public int getcountwhite() {
return countwhite;
}
public int getcountblack() {
return countblack;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String btner=e.getActionCommand();
if(btner.equals("存档")) {
System.out.println("存档");
//应用后面的方法来存数据
for(int i=0;i<chessNumArr.length;i++) {
for(int j=0;j<chessNumArr[i].length;j++) {
chessRecord[i][j]=chessNumArr[i][j];
}
}
countblack1=countblack;
countwhite1=countwhite;
}
if(btner.equals("读档")){
System.out.println("读档");
for(int i=0;i<chessNumArr.length;i++) {
for(int j=0;j<chessNumArr[i].length;j++) {
chessNumArr[i][j]=chessRecord[i][j];
}
}
countblack=countblack1;
countwhite=countwhite1;
wzqjf.repaint();
}
if(btner.equals("返回菜单")) {
for(int i=0;i<chessNumArr.length;i++) {
for(int j=0;j<chessNumArr[i].length;j++) {
chessNumArr[i][j]=0;
}
}
countblack=0;
countwhite=0;
wzqjf.setVisible(false);
StartUI.main(null);
}
if(btner.equals("悔棋")) {
System.out.println("撤回");
//从存储数据中取出这个棋子对象
Chess chess=chesslist[chessIndex-1];
if(chessNumArr[chess.row][chess.clun]==1) {
countblack--;
}else if(chessNumArr[chess.row][chess.clun]==2) {
countwhite--;
}
//由这个棋子的行列值删除这个棋子在chessNumArr中的数据
chessNumArr[chess.row][chess.clun]=0;
//删去它在数据中的值
chesslist[chessIndex-1]=null;
chessIndex--;
wzqjf.repaint();
}
if(btner.equals("重新开始")) {
for(int i=0;i<chessNumArr.length;i++) {
for(int j=0;j<chessNumArr[i].length;j++) {
chessNumArr[i][j]=0;
}
}
countblack=0;
countwhite=0;
wzqjf.repaint();
}
if(btner.equals("再来一把")) {
for(int i=0;i<chessNumArr.length;i++) {
for(int j=0;j<chessNumArr[i].length;j++) {
chessNumArr[i][j]=0;
}
}
countblack=0;
countwhite=0;
wzqjf.repaint();
winjf.setVisible(false);
}
if(btner.equals("退出游戏")) {
System.exit(0);
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
//获取落点坐标
int x=e.getX();
int y=e.getY();
//进行坐标转化
chessx=(x-X+SIZE/2)/SIZE;
chessy=(y-Y+SIZE/2)/SIZE;
//设置下棋边界,做到不能在棋盘外下棋
if(chessx<0||chessx>ROW) {
return;
}
if(chessy<0||chessy>CLOUN) {
return;
}
//设置不能重复下棋
if(chessNumArr[chessx][chessy]!=0) {
return;
}
//绘制棋子
if(flag==0) {
g.setColor(Color.white);
chessNum=2;
chessNumArr[chessx][chessy]=chessNum;
countwhite++;
flag++;
}
else if(flag==1){
g.setColor(Color.black);
chessNum=1;
chessNumArr[chessx][chessy]=chessNum;
countblack++;
flag--;
}
g.fillOval(chessx*SIZE+X-SIZE/2, chessy*SIZE+Y-SIZE/2,SIZE, SIZE);
//用此来存储一个棋子对象
chesslist[chessIndex]=new Chess(chessIndex,chessNum,chessx,chessy);
chessIndex++;
// System.out.println("输赢:"+WinChess.win(chessNumArr, chessx, chessy));
if(WinChess.win(chessNumArr, chessx, chessy)==true) {
if(chessNumArr[chessx][chessy]==1) {
System.out.println("黑棋胜!");
wzqjf.endUI1();
}else {
System.out.println("白棋胜!");
wzqjf.endUI1();
}
}
//显示黑白棋数量
chesson(countblack,countwhite);
}
public void chesson(int countblack,int countwhite) {
Font font=new Font("宋体",Font.BOLD,25);
Color color=new Color(255,255,255,255);
g.setColor(color.gray);
g.fillRect(900, 70, 200, 150);
g.setColor(color.orange);
g.setFont(font);
g.drawString("黑棋:"+countblack, 960, 130);
g.drawString("白棋:"+countwhite, 960, 170);
g.setColor(color.gray);
g.fillRect(900, 220, 200, 150);
g.setColor(color.orange);
g.setFont(font);
g.drawString("下回合", 960, 280);
if(countblack==countwhite) {
g.setColor(color.orange);
g.setFont(font);
g.drawString("白棋", 960, 320);
}else if(countblack<countwhite) {
g.setColor(color.orange);
g.setFont(font);
g.drawString("黑棋", 960, 320);
}
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}