JAVA——五子棋
五子棋小游戏
具体功能
(1)显示一个十五行十五列的围棋棋盘。
(2)在棋盘上面在对应位置通过点击鼠标下子,棋盘上将在相应的位置上显示棋子,并且棋子在横纵交点上。
(3)可以自动判断游戏是否结束,是否黑方或者是白方已经胜利,并且跳出对话框来提示玩家。
(4)在游戏棋盘上方会有一个游戏信息,这个游戏信息是用来提示当前是黑白双方的哪一方下子,玩家可以根据这个提示来相应的下棋,而不会导致不清楚轮到哪一方下棋。
(5)可以重新开始游戏,并且有认输功能,在下棋提示上提示哪方时点认输,哪一方就输了,并且有悔棋功能。
(6)还可以对游戏时间进行设置,判断是否超出规定的时间,如果超出时间则自动判断为输。
1.分段解析
1.1 各变量定义
private static final long serialVersionUID = 1L;
//设置游戏界面
// 屏幕分辨率求法:
// int w = f.getToolkit().getScreenSize().width;//宽度
// int h = f.getToolkit().getScreenSize().height;//高度
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
int x,y; // 定义鼠标的坐标
int[][] allChess = new int[15][15]; // 用数组来保存棋子,0表示无子,1表示黑子,2表示白子
boolean isblack = true; //用来表示黑子还是白子, true表示黑子 false表示白子
boolean canPlay = true; // 用来表示当前游戏是否结束
String message = "黑方先行";//展示当前执子方
String blackMessage = "无限制";//默认
String whiteMessage = "无限制";
//保存棋谱,记录双方每一步落子的位置
int[] chessX = new int[255];//将每一步存入一个数组中
int[] chessY = new int[255];
int countX,countY;//第几步
//时间限制
int maxTime = 0; //保存最大时间
int blackTime = 0;
int whiteTime = 0; //保存黑白方所剩余的时间
//做倒计时的线程类
Thread t=new Thread(this);
1.2 绘制一个棋盘
//画棋盘界面
public void paint(Graphics g){
//双缓冲技术
BufferedImage buf = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
Graphics g1 = buf.createGraphics(); // 创建画笔
g1.setColor(new Color(0xA9B9BF));
g1.fill3DRect(0,0,500,500,true);
g1.setColor(new Color(0xA9A600));
g1.fill3DRect(43, 60, 350, 350, true);
for (int i = 0; i <= 15; i++) {
g1.setColor(Color.WHITE);
g1.drawLine(43, 60+i*25, 350+43, 60+i*25); //画棋盘横线
g1.drawLine(43+i*25, 60, 43+i*25, 350+60); //画棋盘竖线
}
//标记五个点
g1.setColor(Color.BLACK);
g1.fillOval(43+25*7-5, 60+25*7-5, 10, 10);//中心点
g1.drawOval(43+25*7-6, 60+25*7-6, 12, 12);
g1.fillOval(43+25*3-5, 60+25*3-5, 10, 10);
g1.fillOval(43+25*11-5, 60+25*3-5, 10, 10);
g1.fillOval(43+25*3-5, 60+25*11-5, 10, 10);
g1.fillOval(43+25*11-5, 60+25*11-5, 10, 10);
g1.setColor(Color.BLACK);
g1.setFont(new Font("黑体",Font.BOLD,20));
g1.drawString("游戏信息:"+message,50,50);
g1.drawRect(30, 440, 180, 40);
g1.drawRect(250, 440, 180, 40); //画黑方时间与白方时间字符串的边框
g1.setFont(new Font("楷体",3,15));
g1.drawString("黑方时间: "+blackMessage,40,465);//显示倒计时
g1.drawString("白方时间: "+whiteMessage,260,465);
g1.drawRect(425,66,60,20);
g1.setColor(new Color(0x25558E));
g1.fill3DRect(425, 66, 60, 20, true);
g1.setColor(Color.white);
g1.drawString("重新开始",425,80); //重新开始按钮
g1.drawRect(425,106,60,20);
g1.setColor(new Color(0x25558E));
g1.fill3DRect(425, 106, 60, 20, true);
g1.setColor(Color.white);
g1.drawString("时间设置",425,120); //时间设置按钮
g1.drawRect(425,146, 60, 20);
g1.setColor(new Color(0x25558E));
g1.fill3DRect(425, 146, 60, 20, true);
g1.setColor(Color.white);
g1.drawString("游戏说明", 425, 160); // 游戏说明按钮
g1.drawRect(425, 186, 60, 20);
g1.setColor(new Color(0x25558E));
g1.fill3DRect(425, 186, 60, 20, true);
g1.setColor(Color.white);
g1.drawString("退出游戏", 425, 200); // 退出游戏按钮
g1.drawRect(425, 246, 60, 20);
g1.setColor(new Color(0x25558E));
g1.fill3DRect(425, 246, 60, 20, true);
g1.setColor(Color.white);
g1.drawString("悔棋", 435, 260); // 悔棋
g1.drawRect(425, 286, 60, 20);
g1.setColor(new Color(0x25558E));
g1.fill3DRect(425, 286, 60, 20, true);
g1.setColor(Color.white);
g1.drawString("认输", 435, 300); // 认输
for(int i=0; i < 15; i++){
for (int j = 0; j < 15; j++) {
//画实心黑子
if(allChess[i][j] == 1){
int tempX = i*25+43;
int tempY = j*25+60;
g1.setColor(Color.BLACK);
g1.fillOval(tempX-8, tempY-8, 16, 16);
g1.setColor(Color.BLACK);
g1.drawOval(tempX-8, tempY-8, 16, 16);
}
//画实心白子
if(allChess[i][j] == 2){
int tempX = i*25+43;
int tempY = j*25+60;
g1.setColor(Color.WHITE);
g1.fillOval(tempX-8, tempY-8, 16, 16);
g1.setColor(Color.WHITE);
g1.drawOval(tempX-8, tempY-8, 16, 16);
}
}
}
g.drawImage(buf, 0, 0,this);
}
通过该函数,对棋盘以及各个棋子进行绘制
allChess[i][j]是下棋的位置, 对这个数组进行判断 如果为1 那就代表是黑棋,2就是白棋
tempX,tempY 是用来确定棋子所在的位置区域的顶点坐标。
1.3 对鼠标点击进行监控
1.3.1 判断鼠标落点位置
判定鼠标点击究竟算在哪一个位置上,从而到时候在对应的位置进行落子
鼠标点击超过半格则算在下一个交叉点上,否则算在前一个交叉点处
if(canPlay){
x=e.getX();
y=e.getY(); // 用来获取鼠标坐标
System.out.println("x="+x+"y="+y);//测试
if(x>=43 && x<= 393 && y>=60 && y<=410){
//让鼠标在棋盘范围内
if((x-43)%25>12){
x=(x-43)/25 + 1;
}else {
x = (x-43)/25;
}
if((y-60)%25>12){
y=(y-60)/25 + 1;
}else {
y=(y-60)/25;
}
1.3.2 实现落子
当该位置为空,首先countX++,countY++用来计算步数增加,并且将每一步记录进相应的X、Y数组之中
判断此刻究竟是哪一方落子,将存放该位置数组置换对应的落子(黑或白),切换显示信息,同时刷新棋盘
然后进行输赢的判定
if(allChess[x][y] == 0){
chessX[countX++] = x;
chessY[countY++] = y;
if(isblack){
allChess[x][y] = 1;
isblack = false;
message = "白方下子";
}else {
allChess[x][y] = 2;
isblack = true;
message = "黑方下子";
}
this.repaint();
if(this.isWin()){
if(allChess[x][y] == 1){
JOptionPane.showMessageDialog(this, "游戏结束,黑方胜利");
}else {
JOptionPane.showMessageDialog(this, "游戏结束,白方胜利");
}
this.canPlay = false; //表示游戏结束
}
1.3.3 各个按钮点击事件
前面已经在各个位置化出按钮框,当点击按钮框对应位置时,实现对应事件
1.3.3.1 重新开局
//重新开始游戏
if(e.getX() >=430 && e.getY() <= (428+55) && e.getY() >= 66
&& e.getY() <= (66+20) ){
int result = JOptionPane.showConfirmDialog(this, "是否重新开始游戏?");
if(result == 0){
restarGame();
}
}
实现重新开局
1.清空棋盘上所有的棋子
2.赋予限制时间
3.黑棋先行
4.游戏开始判断
5.刷新棋盘
public void restarGame(){
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
allChess[i][j] = 0; //清空棋盘的棋子
}
}
//清空下棋棋子坐标的记录
for (int i = 0; i < 15; i++) {
chessX[i] = 0;
chessY[i] = 0;
}
countX =0;
countY =0;
message = "黑方先行";
blackTime = maxTime;
whiteTime = maxTime;
if(maxTime>0){
blackMessage = maxTime/3600+":"+(maxTime/60-maxTime/3600*60)
+":"+(maxTime-maxTime/60*60);
whiteMessage = maxTime/3600+":"+(maxTime/60-maxTime/3600*60)
+":"+(maxTime-maxTime/60*60);
t.resume();
}else{
blackMessage="无限制";
whiteMessage="无限制";
}
isblack = true;
canPlay = true;
this.repaint();
}
1.3.3.2 游戏说明
//游戏说明
if(e.getX() >= 425 && e.getY() <= (425+60) && e.getY() >=146
&& e.getY() <= (146+20) ){
JOptionPane.showMessageDialog(this, "规则:横竖斜先连成五子者获胜!");
}
1.3.3.3 退出游戏
//退出游戏
if(e.getX() >=425 && e.getX() <= (425+60) && e.getY() >=186
&& e.getY() <= (186+20)){
int result = JOptionPane.showConfirmDialog(this, "是否退出游戏?");
if(result == 0){
System.exit(0);
}
}
1.3.3.4 悔棋
1.弹出对话框进行黑白方判断,及是否同意提示
2.判断此刻棋盘上有无棋子
3.如果有,将第N步进行回退,N-1,同时对应Xn,Yn处的位置数组置零
4.回退显示信息
5.刷新棋盘
//悔棋
if(e.getX() >= 425 && e.getX() <= (425+60) && e.getY() >= 246
&& e.getY() <= (246+20)){
int result = JOptionPane.showConfirmDialog(this,
(isblack == true ? "白方悔棋,黑方是否同意?" :"黑方悔棋,白方是否同意?"));
// result = 0为悔棋
if(result == 0){
if(countX==0&&countY==0){
JOptionPane.showMessageDialog(this,"提示:此时棋盘上没有任何棋子!");
}else {
allChess[chessX[--countX]][chessY[--countY]]=0;
if(isblack == true ){
isblack = false;
}else {
isblack = true;
}
}
this.repaint(); //重绘棋盘
}
}
1.3.3.5 游戏限制时间
1.弹出对话框进行读取String,然后将String转化为数字
2.设置成功后,提示是否进行重新开局
【该时间设置将在重新开局后生效】
//时间设置
if(e.getX()>=425&&e.getX()<=(425+60)&&e.getY()>=106
&&e.getY()<=(106+20)){
//显示一个可输入的对话框,返回一个String类型的值
String input = JOptionPane.showInputDialog("请输入游戏的最大时间:(分钟)\n" +
"如果输入的数字<=0,表示没有时间限制");
try{
maxTime=Integer.parseInt(input)*60;
if(maxTime<=0){
JOptionPane.showMessageDialog(this,"输入信息有误,时间无限制!");
}else{
int result= JOptionPane.showConfirmDialog(this,"设置完成!是否重新开始游戏?");
if(result==0){
restarGame();
}
}
} catch (NumberFormatException e1){
JOptionPane.showMessageDialog(this,"请正确输入信息!");
//如果输入字母
}
}
}
线程的run();
当有时间限制时,分别进行黑白方的倒计时,当超时,判断游戏结束,重新开局
@Override
public void run() {
if(maxTime>0){//判断是否有时间限制
while(true){
if(isblack){
whiteTime=maxTime;
blackTime--;
if(blackTime==0){
JOptionPane.showMessageDialog(this,"黑方超时,游戏结束!");
JOptionPane.showMessageDialog(this,"游戏重新开始!");
restarGame();
}
}else{
blackTime=maxTime;
whiteTime--;
if(whiteTime==0){
JOptionPane.showMessageDialog(this,"白方超时,游戏结束!");
JOptionPane.showMessageDialog(this,"游戏重新开始!");
restarGame();
}
}
blackMessage = blackTime/3600+":"+(blackTime/60-blackTime/3600*60)
+":"+(blackTime-blackTime/60*60);
whiteMessage = whiteTime/3600+":"+(whiteTime/60-whiteTime/3600*60)
+":"+(whiteTime-whiteTime/60*60);
this.repaint();
try{
Thread.sleep(1000);//休息一秒钟
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println(blackTime+"___"+whiteTime);
}
}
}
1.3.3.6 认输
1.判断是谁认输
2.重新开局
//认输
if(e.getX()>=425 && e.getX()<=(425+60) && e.getY()>=286
&& e.getY()<=(286+20)){
int result=JOptionPane.showConfirmDialog(this, "是否认输?");
if(result==0){
JOptionPane.showMessageDialog(this,
"游戏结束,"+(isblack==true ? "黑方认输,白方获胜!" : "白方认输,黑方获胜!"));
restarGame();
}
}
1.3.4 相关函数
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
1.4 判断输赢规则
/**
* 判断输赢规则
* @return
*/
public boolean isWin(){
boolean flag = false;
int count = 1; //用来保存共有相同颜色多少棋子相连,初始值为1
int color = allChess[x][y]; //color = 1 (黑子) color = 2(白子)
//判断横向是否有5个棋子相连,特点:纵坐标是相同,即allChess[x][y] 中y值是相同
count = this.checkCount(1,0,color);
if(count >= 5){
flag = true;
}else {
//判断纵向
count = this.checkCount(0,1,color);
if(count >= 5){
flag = true;
}else {
//判断右上,左下
count = this.checkCount(1,-1,color);
if(count >= 5){
flag = true;
}else {
//判断右下,左上
count = this.checkCount(1,1,color);
if(count >= 5){
flag = true;
}
}
}
}
return flag;
}
/**
* 检查棋盘中的五子棋是否连城五子
* @param xChange
* @param yChenge
* @param color
* @return
*/
public int checkCount(int xChange , int yChenge ,int color){
int count = 1;
int tempX = xChange;
int tempy = yChenge; //保存初始值
//全局变量x,y最初为鼠标点击的坐标,
//经下棋方法已经将x,y的范围变成0-15(遍历整个棋盘,寻找相同颜色的棋子)
while(x + xChange >=0 && x+xChange <15 && y+yChenge >=0 &&
y+yChenge < 15 && color == allChess[x+xChange][y+yChenge]){
count++;
if(xChange != 0) xChange++;
if(yChenge != 0 ){
if(yChenge != 0){
if(yChenge > 0) {
yChenge++;
}else {
yChenge--;
}
}
}
}
xChange = tempX;
yChenge = tempy; // 恢复初始值
while(x-xChange >=0 && x-xChange <15 && y-yChenge >=0 &&
y-yChenge <15 && color == allChess[x-xChange][y-yChenge]){
count++;
if(xChange != 0){
xChange++;
}
if(yChenge != 0){
if (yChenge > 0) {
yChenge++;
}else {
yChenge--;
}
}
}
return count;
}
2.源代码
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.sql.SQLOutput;
public class FiveChessFrame extends JFrame implements MouseListener,Runnable{
private static final long serialVersionUID = 1L;
//设置游戏界面
// 屏幕分辨率求法:
// int w = f.getToolkit().getScreenSize().width;//宽度
// int h = f.getToolkit().getScreenSize().height;//高度
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
int x,y; // 定义鼠标的坐标
int[][] allChess = new int[15][15]; // 用数组来保存棋子,0表示无子,1表示黑子,2表示白子
boolean isblack = true; //用来表示黑子还是白子, true表示黑子 false表示白子
boolean canPlay = true; // 用来表示当前游戏是否结束
String message = "黑方先行";//展示当前执子方
String blackMessage = "无限制";//默认
String whiteMessage = "无限制";
//保存棋谱,记录双方每一步落子的位置
int[] chessX = new int[255];//将每一步存入一个数组中
int[] chessY = new int[255];
int countX,countY;//第几步
//时间限制
int maxTime = 0; //保存最大时间
int blackTime = 0;
int whiteTime = 0; //保存黑白方所剩余的时间
//做倒计时的线程类
Thread t=new Thread(this);
public FiveChessFrame(){
this.setTitle("五子棋小游戏");
this.setSize(500,500);
this.setLocation((width - 500) / 2 , (height - 500) / 2 );
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false); //设置窗口不可改变,固定窗口大小
this.setVisible(true);
t.start();//启动线程
t.suspend();//线程挂起
this.repaint(); //java里repaint()是重绘component的方法;
this.addMouseListener(this);//鼠标监听器
}
//画棋盘界面
public void paint(Graphics g){
//双缓冲技术
BufferedImage buf = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
Graphics g1 = buf.createGraphics(); // 创建画笔
g1.setColor(new Color(0xA9B9BF));
g1.fill3DRect(0,0,500,500,true);
g1.setColor(new Color(0xA9A600));
g1.fill3DRect(43, 60, 350, 350, true);
for (int i = 0; i <= 15; i++) {
g1.setColor(Color.WHITE);
g1.drawLine(43, 60+i*25, 350+43, 60+i*25); //画棋盘横线
g1.drawLine(43+i*25, 60, 43+i*25, 350+60); //画棋盘竖线
}
//标记五个点
g1.setColor(Color.BLACK);
g1.fillOval(43+25*7-5, 60+25*7-5, 10, 10);//中心点
g1.drawOval(43+25*7-6, 60+25*7-6, 12, 12);
g1.fillOval(43+25*3-5, 60+25*3-5, 10, 10);
g1.fillOval(43+25*11-5, 60+25*3-5, 10, 10);
g1.fillOval(43+25*3-5, 60+25*11-5, 10, 10);
g1.fillOval(43+25*11-5, 60+25*11-5, 10, 10);
g1.setColor(Color.BLACK);
g1.setFont(new Font("黑体",Font.BOLD,20));
g1.drawString("游戏信息:"+message,50,50);
g1.drawRect(30, 440, 180, 40);
g1.drawRect(250, 440, 180, 40); //画黑方时间与白方时间字符串的边框
g1.setFont(new Font("楷体",3,15));
g1.drawString("黑方时间: "+blackMessage,40,465);//显示倒计时
g1.drawString("白方时间: "+whiteMessage,260,465);
g1.drawRect(425,66,60,20);
g1.setColor(new Color(0x25558E));
g1.fill3DRect(425, 66, 60, 20, true);
g1.setColor(Color.white);
g1.drawString("重新开始",425,80); //重新开始按钮
g1.drawRect(425,106,60,20);
g1.setColor(new Color(0x25558E));
g1.fill3DRect(425, 106, 60, 20, true);
g1.setColor(Color.white);
g1.drawString("时间设置",425,120); //时间设置按钮
g1.drawRect(425,146, 60, 20);
g1.setColor(new Color(0x25558E));
g1.fill3DRect(425, 146, 60, 20, true);
g1.setColor(Color.white);
g1.drawString("游戏说明", 425, 160); // 游戏说明按钮
g1.drawRect(425, 186, 60, 20);
g1.setColor(new Color(0x25558E));
g1.fill3DRect(425, 186, 60, 20, true);
g1.setColor(Color.white);
g1.drawString("退出游戏", 425, 200); // 退出游戏按钮
g1.drawRect(425, 246, 60, 20);
g1.setColor(new Color(0x25558E));
g1.fill3DRect(425, 246, 60, 20, true);
g1.setColor(Color.white);
g1.drawString("悔棋", 435, 260); // 悔棋
g1.drawRect(425, 286, 60, 20);
g1.setColor(new Color(0x25558E));
g1.fill3DRect(425, 286, 60, 20, true);
g1.setColor(Color.white);
g1.drawString("认输", 435, 300); // 认输
for(int i=0; i < 15; i++){
for (int j = 0; j < 15; j++) {
//画实心黑子
if(allChess[i][j] == 1){
int tempX = i*25+43;
int tempY = j*25+60;
g1.setColor(Color.BLACK);
g1.fillOval(tempX-8, tempY-8, 16, 16);
g1.setColor(Color.BLACK);
g1.drawOval(tempX-8, tempY-8, 16, 16);
}
//画实心白子
if(allChess[i][j] == 2){
int tempX = i*25+43;
int tempY = j*25+60;
g1.setColor(Color.WHITE);
g1.fillOval(tempX-8, tempY-8, 16, 16);
g1.setColor(Color.WHITE);
g1.drawOval(tempX-8, tempY-8, 16, 16);
}
}
}
g.drawImage(buf, 0, 0,this);
}
public void mousePressed(MouseEvent e){
if(canPlay){
x=e.getX();
y=e.getY(); // 用来获取鼠标坐标
System.out.println("x="+x+"y="+y);//测试
if(x>=43 && x<= 393 && y>=60 && y<=410){
//让鼠标在棋盘范围内
if((x-43)%25>12){
x=(x-43)/25 + 1;
}else {
x = (x-43)/25;
}
if((y-60)%25>12){
y=(y-60)/25 + 1;
}else {
y=(y-60)/25;
}
//落子
if(allChess[x][y] == 0){
chessX[countX++] = x;
chessY[countY++] = y;
if(isblack){
allChess[x][y] = 1;
isblack = false;
message = "白方下子";
}else {
allChess[x][y] = 2;
isblack = true;
message = "黑方下子";
}
this.repaint();
if(this.isWin()){
if(allChess[x][y] == 1){
JOptionPane.showMessageDialog(this, "游戏结束,黑方胜利");
}else {
JOptionPane.showMessageDialog(this, "游戏结束,白方胜利");
}
this.canPlay = false; //表示游戏结束
}
}
}
}
//重新开始游戏
if(e.getX() >=430 && e.getY() <= (428+55) && e.getY() >= 66
&& e.getY() <= (66+20) ){
int result = JOptionPane.showConfirmDialog(this, "是否重新开始游戏?");
if(result == 0){
restarGame();
}
}
//游戏说明
if(e.getX() >= 425 && e.getY() <= (425+60) && e.getY() >=146
&& e.getY() <= (146+20) ){
JOptionPane.showMessageDialog(this, "规则:横竖斜先连成五子者获胜!");
}
//退出游戏
if(e.getX() >=425 && e.getX() <= (425+60) && e.getY() >=186
&& e.getY() <= (186+20)){
int result = JOptionPane.showConfirmDialog(this, "是否退出游戏?");
if(result == 0){
System.exit(0);
}
}
//悔棋
if(e.getX() >= 425 && e.getX() <= (425+60) && e.getY() >= 246
&& e.getY() <= (246+20)){
int result = JOptionPane.showConfirmDialog(this,
(isblack == true ? "白方悔棋,黑方是否同意?" :"黑方悔棋,白方是否同意?"));
// result = 0为悔棋
if(result == 0){
if(countX==0&&countY==0){
JOptionPane.showMessageDialog(this,"提示:此时棋盘上没有任何棋子!");
}else {
allChess[chessX[--countX]][chessY[--countY]]=0;
if(isblack == true ){
isblack = false;
}else {
isblack = true;
}
}
this.repaint(); //重绘棋盘
}
}
//认输
if(e.getX()>=425 && e.getX()<=(425+60) && e.getY()>=286
&& e.getY()<=(286+20)){
int result=JOptionPane.showConfirmDialog(this, "是否认输?");
if(result==0){
JOptionPane.showMessageDialog(this,
"游戏结束,"+(isblack==true ? "黑方认输,白方获胜!" : "白方认输,黑方获胜!"));
restarGame();
}
}
//时间设置
if(e.getX()>=425&&e.getX()<=(425+60)&&e.getY()>=106
&&e.getY()<=(106+20)){
//显示一个可输入的对话框,返回一个String类型的值
String input = JOptionPane.showInputDialog("请输入游戏的最大时间:(分钟)\n" +
"如果输入的数字<=0,表示没有时间限制");
try{
maxTime=Integer.parseInt(input)*60;
if(maxTime<=0){
JOptionPane.showMessageDialog(this,"输入信息有误,时间无限制!");
}else{
int result= JOptionPane.showConfirmDialog(this,"设置完成!是否重新开始游戏?");
if(result==0){
restarGame();
}
}
} catch (NumberFormatException e1){
JOptionPane.showMessageDialog(this,"请正确输入信息!");
//如果输入字母
}
}
}
public void restarGame(){
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
allChess[i][j] = 0; //清空棋盘的棋子
}
}
//清空下棋棋子坐标的记录
for (int i = 0; i < 15; i++) {
chessX[i] = 0;
chessY[i] = 0;
}
countX =0;
countY =0;
message = "黑方先行";
blackTime = maxTime;
whiteTime = maxTime;
if(maxTime>0){
blackMessage = maxTime/3600+":"+(maxTime/60-maxTime/3600*60)
+":"+(maxTime-maxTime/60*60);
whiteMessage = maxTime/3600+":"+(maxTime/60-maxTime/3600*60)
+":"+(maxTime-maxTime/60*60);
t.resume();
}else{
blackMessage="无限制";
whiteMessage="无限制";
}
isblack = true;
canPlay = true;
this.repaint();
}
/**
* 判断输赢规则
* @return
*/
public boolean isWin(){
boolean flag = false;
int count = 1; //用来保存共有相同颜色多少棋子相连,初始值为1
int color = allChess[x][y]; //color = 1 (黑子) color = 2(白子)
//判断横向是否有5个棋子相连,特点:纵坐标是相同,即allChess[x][y] 中y值是相同
count = this.checkCount(1,0,color);
if(count >= 5){
flag = true;
}else {
//判断纵向
count = this.checkCount(0,1,color);
if(count >= 5){
flag = true;
}else {
//判断右上,左下
count = this.checkCount(1,-1,color);
if(count >= 5){
flag = true;
}else {
//判断右下,左上
count = this.checkCount(1,1,color);
if(count >= 5){
flag = true;
}
}
}
}
return flag;
}
/**
* 检查棋盘中的五子棋是否连城五子
* @param xChange
* @param yChenge
* @param color
* @return
*/
public int checkCount(int xChange , int yChenge ,int color){
int count = 1;
int tempX = xChange;
int tempy = yChenge; //保存初始值
//全局变量x,y最初为鼠标点击的坐标,
//经下棋方法已经将x,y的范围变成0-15(遍历整个棋盘,寻找相同颜色的棋子)
while(x + xChange >=0 && x+xChange <15 && y+yChenge >=0 &&
y+yChenge < 15 && color == allChess[x+xChange][y+yChenge]){
count++;
if(xChange != 0) xChange++;
if(yChenge != 0 ){
if(yChenge != 0){
if(yChenge > 0) {
yChenge++;
}else {
yChenge--;
}
}
}
}
xChange = tempX;
yChenge = tempy; // 恢复初始值
while(x-xChange >=0 && x-xChange <15 && y-yChenge >=0 &&
y-yChenge <15 && color == allChess[x-xChange][y-yChenge]){
count++;
if(xChange != 0){
xChange++;
}
if(yChenge != 0){
if (yChenge > 0) {
yChenge++;
}else {
yChenge--;
}
}
}
return count;
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
new FiveChessFrame();
}
@Override
public void run() {
if(maxTime>0){//判断是否有时间限制
while(true){
if(isblack){
whiteTime=maxTime;
blackTime--;
if(blackTime==0){
JOptionPane.showMessageDialog(this,"黑方超时,游戏结束!");
JOptionPane.showMessageDialog(this,"游戏重新开始!");
restarGame();
}
}else{
blackTime=maxTime;
whiteTime--;
if(whiteTime==0){
JOptionPane.showMessageDialog(this,"白方超时,游戏结束!");
JOptionPane.showMessageDialog(this,"游戏重新开始!");
restarGame();
}
}
blackMessage = blackTime/3600+":"+(blackTime/60-blackTime/3600*60)
+":"+(blackTime-blackTime/60*60);
whiteMessage = whiteTime/3600+":"+(whiteTime/60-whiteTime/3600*60)
+":"+(whiteTime-whiteTime/60*60);
this.repaint();
try{
Thread.sleep(1000);//休息一秒钟
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println(blackTime+"___"+whiteTime);
}
}
}
}
以上参考博客:https://blog.csdn.net/qq_40595913/article/details/81563301
本文地址:https://blog.csdn.net/LZM647/article/details/112251780