Compare commits

...

63 Commits
fmj ... main

Author SHA1 Message Date
pjz6esrxm 99422eb668 Delete 'doc/项目自评报告.xlsx'
1 year ago
pjz6esrxm 3c54de0cb4 ADD file via upload
1 year ago
p2cyq7sv3 59f155961d ADD file via upload
1 year ago
p2cyq7sv3 59eaa7a23b Delete 'log/项目自评报告-2022310040511-李啊俊.xlsx'
1 year ago
p2cyq7sv3 709588fbdd ADD file via upload
1 year ago
p2cyq7sv3 b936236896 Delete 'log/日志4-011-李啊俊.docx'
1 year ago
p2cyq7sv3 1ac3107e29 ADD file via upload
1 year ago
p2cyq7sv3 e875bf0d1a ADD file via upload
1 year ago
p2cyq7sv3 e93ea21130 Delete 'doc/课程设计汇报.pptx'
1 year ago
p4zfh75bw 6f4cc4e692 ADD file via upload
1 year ago
p4zfh75bw fd5531e49d Delete 'doc/面向对象课程设计报告.docx'
1 year ago
p2cyq7sv3 55002afbd7 Delete 'doc/项目自评报告.xlsx'
1 year ago
pjz6esrxm 957af93525 ADD file via upload
1 year ago
p2cyq7sv3 937e1e8e7e ADD file via upload
1 year ago
p2cyq7sv3 67c2e9548f ADD file via upload
1 year ago
p4zfh75bw 239018c480 ADD file via upload
1 year ago
p4zfh75bw f9d1dc853c Delete 'doc/文档模板-课程设计的汇报模板.pptx'
1 year ago
p2cyq7sv3 72c2650177 ADD file via upload
1 year ago
p4zfh75bw 08e68c7ccf ADD file via upload
1 year ago
p4zfh75bw 8d83db25ce ADD file via upload
1 year ago
p2cyq7sv3 148750e1de ADD file via upload
1 year ago
p2cyq7sv3 7871eb5b47 Delete 'log/日志-011-李啊俊.docx'
1 year ago
pjz6esrxm 4addf9ce0a ADD file via upload
1 year ago
pjz6esrxm 4611b7d69e ADD file via upload
1 year ago
p4zfh75bw eb1b35bbd7 ADD file via upload
1 year ago
p4zfh75bw 796ed613b5 ADD file via upload
1 year ago
p4zfh75bw b58e694fb7 ADD file via upload
1 year ago
p4zfh75bw 7574256d6a Delete '日志3-014-费敏洁.docx'
1 year ago
p4zfh75bw 81d86170f6 ADD file via upload
1 year ago
p2cyq7sv3 4bb7a03c57 ADD file via upload
1 year ago
pjz6esrxm 64446bd182 ADD file via upload
1 year ago
p2cyq7sv3 816dece296 ADD file via upload
1 year ago
p2cyq7sv3 1be907f0df ADD file via upload
1 year ago
p2cyq7sv3 05151fd631 ADD file via upload
1 year ago
p2cyq7sv3 c0b4303c04 ADD file via upload
1 year ago
p2cyq7sv3 f3efb98e91 ADD file via upload
1 year ago
pjz6esrxm 72f6abcb5b Delete 'model/绘图2.vsdx'
1 year ago
pjz6esrxm 8c77fd336b ADD file via upload
1 year ago
p2cyq7sv3 0f98e93fe9 ADD file via upload
1 year ago
p2cyq7sv3 f6f511fc66 Delete 'model/软件需求模型.vsdx'
1 year ago
p2cyq7sv3 0dbacb6123 ADD file via upload
1 year ago
p2cyq7sv3 98f4359968 ADD file via upload
1 year ago
p2cyq7sv3 a42fc522bc Delete 'model/软件需求模型.vsdx'
1 year ago
p2cyq7sv3 bda2e1d5e1 ADD file via upload
1 year ago
p2cyq7sv3 03c785d4f1 Delete 'doc/软件需求构思及描述.docx'
1 year ago
p4zfh75bw bb168edb81 ADD file via upload
1 year ago
pjz6esrxm fb09da66a7 ADD file via upload
1 year ago
p2cyq7sv3 fb733705fb ADD file via upload
1 year ago
p2cyq7sv3 2f74d75e4f ADD file via upload
1 year ago
pjz6esrxm ae640088fb ADD file via upload
1 year ago
pjz6esrxm 0eed0532e4 Delete 'log/log-段志伟.docx'
1 year ago
p2cyq7sv3 366eadae45 ADD file via upload
1 year ago
p2cyq7sv3 d7d0347975 Delete 'log/日志-014-费敏洁.docx'
1 year ago
p2cyq7sv3 6f5fab2aa1 ADD file via upload
1 year ago
p2cyq7sv3 095c4733bc Delete 'log/日志-011-李啊俊.docx'
1 year ago
p2cyq7sv3 8c6c58d2a9 ADD file via upload
1 year ago
p2cyq7sv3 fb815e0d3a Delete 'log/日志-014-费敏洁.docx'
1 year ago
p2cyq7sv3 21c2e100e6 ADD file via upload
1 year ago
p2cyq7sv3 d41b0629fd Delete 'log/hj'
1 year ago
p2cyq7sv3 359d765c70 ADD file via upload
1 year ago
pjz6esrxm 0e5ea3b4e9 ADD file via upload
1 year ago
p4zfh75bw fccb0eb0d2 Merge pull request '合并' (#3) from fmj into main
1 year ago
p4zfh75bw 0c279c21c8 Merge pull request 'log合并' (#2) from fmj into main
1 year ago

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -0,0 +1,25 @@
package ;
import java.awt.Color;
public class Chess {
private int x;//棋子的x坐标索引
private int y;//棋子的y坐标索引
private Color color;//棋子颜色
public static int DIAMETER=30;//直径
public Chess(int x,int y,Color color){//棋子构造函数
this.x=x;
this.y=y;
this.color=color;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Color getColor() {
return color;
}
}

@ -0,0 +1,179 @@
package ;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ChessBord extends JPanel implements MouseListener{//继承面板类和鼠标事件接口
public static int MARGIN=30;//定义边距
public static int ROWS=15;//定义行数
public static int COLS=15;//定义列数
public static int GRID_SPAN=35;//网格间距
Chess[] chessList=new Chess[(ROWS+1)*(COLS+1)];//定义一个棋子数组
String[][] board=new String[MARGIN*2+GRID_SPAN*COLS][MARGIN*2+GRID_SPAN*COLS];//声明一个字符串数组,用来判断输赢
int chessCount;//棋子数目
int xindex,yindex;//棋子的坐标索引
boolean start=true;//开始默认黑子先下
boolean GameOver=false;//定义是否游戏结束
public ChessBord() {//棋盘类构造函数
setBackground(Color.LIGHT_GRAY);//设置背景颜色
addMouseListener(this);//将棋盘类添加到鼠标事件监听器
addMouseMotionListener(new MouseMotionListener() {//匿名内部类
@Override
public void mouseMoved(MouseEvent e) {//根据鼠标的移动所在的坐标来设置鼠标光标形状
int x1=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN;//对鼠标光标的x坐标进行转换
int y1=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN;//对鼠标光标的y坐标进行转换
if(x1<0||x1>ROWS||y1<0||y1>COLS||GameOver||findchess(x1, y1)) {
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));//设置鼠标光标为默认形状
}else {
setCursor(new Cursor(Cursor.HAND_CURSOR));//设置鼠标光标为手型
}
}
@Override
public void mouseDragged(MouseEvent e) {
}
});
for(int i=0;i<MARGIN*2+GRID_SPAN*COLS;i++) {//对board[][]赋初值
for (int j = 0; j < MARGIN*2+GRID_SPAN*COLS; j++) {
board[i][j]="0";
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {//鼠标点击事件
if(GameOver)//游戏结束,不能按
return ;
String colorName=start?"黑棋":"白棋";//判断是什么颜色的棋子
xindex=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN;//得到棋子x坐标
yindex=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN;//得到棋子y坐标
board[xindex][yindex]=colorName;//以棋子x坐标y坐标做索引将棋子的颜色添加到board中
if(xindex<0||xindex>ROWS||yindex<0||yindex>COLS) {//棋子在棋盘外不能下,
return ;
}else if(findchess( xindex, yindex)) {//所下位置已有棋子,不能下
return ;
}
Chess po=new Chess(xindex,yindex,start?Color.black:Color.WHITE);//对棋子对象进行初始化
chessList[chessCount++]=po;//将棋子对象添加到棋子数组中
repaint();//重画图型
if(win( xindex,yindex,start)) {//判断是否胜利
String msg=String.format("恭喜 %s赢了",colorName);
JOptionPane.showMessageDialog(this, msg);
//gameOver=true;
GameOver=true;
}else if(chessCount==(COLS+1)*(ROWS+1)) {//判断是否全部下满
String msg=String.format("恭喜 %s赢了",colorName);
JOptionPane.showMessageDialog(this, msg);
GameOver=true;
}
start=!start;//改变棋子先下棋状态
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
protected void paintComponent(Graphics g) {//画棋盘和棋子
super.paintComponent(g);
for(int i=0;i<=ROWS;i++) {//画横线
g.drawLine(MARGIN, MARGIN+i*GRID_SPAN, MARGIN+COLS*GRID_SPAN, MARGIN+i*GRID_SPAN);
}
for(int j=0;j<=COLS;j++) {//画竖线
g.drawLine(MARGIN+j*GRID_SPAN, MARGIN, MARGIN+j*GRID_SPAN, MARGIN+ROWS*GRID_SPAN);
}
for(int i=0;i<chessCount;i++) {//画棋子
int xpos=chessList[i].getX()*GRID_SPAN+MARGIN;//得到棋子x坐标
int ypos=chessList[i].getY()*GRID_SPAN+MARGIN;//得到棋子y坐标
g.setColor(chessList[i].getColor());//设置棋子颜色
g.fillOval(xpos-Chess.DIAMETER/2, ypos-Chess.DIAMETER/2, Chess.DIAMETER, Chess.DIAMETER);//画棋子
if(i==chessCount-1){
g.setColor(Color.red);//标记最后一个棋子为红色
g.drawRect(xpos-Chess.DIAMETER/2, ypos-Chess.DIAMETER/2, Chess.DIAMETER, Chess.DIAMETER);
}
}
}
private boolean findchess(int index,int yindex) {//查找所在位置是否有棋子
for (Chess c : chessList) {
if(c!=null&&c.getX()==xindex&&c.getY()==yindex)
return true;
}
return false;
}
private boolean win(int x,int y,boolean start) {//对棋子输赢的判断
String str=start?"黑棋":"白棋";
//棋子所在行和列是否有五子相连的情况
for(int i=0;i<16;i++){
if((board[x][i].equals(str)&&board[x][i+1].equals(str)&&board[x][i+2].equals(str)&&board[x][i+3].equals(str)&&board[x][i+4].equals(str))||(board[i][y].equals(str)&&board[i+1][y].equals(str)&&board[i+2][y].equals(str)&&board[i+3][y].equals(str)&&board[i+4][y].equals(str)))
return true;
}
//棋子所在撇行是否有五子相连的情况
if(x+y>=4&&x+y<=30){
int i=(x+y<=19)?x+y:x+y-20;
if(x+y<=19){
for(int k=0;k<=i-4;k++){
if(board[k][i-k].equals(str)&&board[k+1][i-k-1].equals(str)&&board[k+2][i-k-2].equals(str)&&board[k+3][i-k-3].equals(str)&&board[k+4][i-k-4].equals(str))
return true;
}
}else{
for(int k=i;k<=15;k++){
if(board[k][20-k].equals(str)&&board[k+1][20-k-1].equals(str)&&board[k+2][20-k-2].equals(str)&&board[k+3][20-k-3].equals(str)&&board[k+4][20-k-4].equals(str))
return true;
}
}
}
//棋子所在捺行是否有五子相连的情况
if(y-x<=15&&x-y<=15){
int i=(x<y)?y-x:x-y;
if(x<y){
for(int k=0;k<=19-4-i;k++){
if(board[k][i+k].equals(str)&&board[k+1][i+k+1].equals(str)&&board[k+2][i+k+2].equals(str)&&board[k+3][i+k+3].equals(str)&&board[k+4][i+k+4].equals(str))
return true;
}
}else{
for(int k=i;k<=15;k++){
if(board[k][i+k].equals(str)&&board[k+1][i+k+1].equals(str)&&board[k+2][i+k+2].equals(str)&&board[k+3][i+k+3].equals(str)&&board[k+4][i+k+4].equals(str))
return true;
}
}
}
return false;
}
public void goback() {//悔棋函数
if(chessCount==0) {
return ;
}
chessList[chessCount-1]=null;
chessCount--;
if(chessCount>0) {
xindex=chessList[chessCount-1].getX();
yindex=chessList[chessCount-1].getY();
}
start=!start;
repaint();
}
public void restartGame() {//重新开始函数
for(int i=0;i<chessList.length;i++)//设置为初始状态
chessList[i]=null;
for(int i=0;i<MARGIN*2+GRID_SPAN*COLS;i++) {
for (int j = 0; j < MARGIN*2+GRID_SPAN*COLS; j++) {
board[i][j]="0";
}
}
start=true;
GameOver=false;
chessCount=0;
repaint();
}
public Dimension getPreferredSize(){//画矩形
return new Dimension(MARGIN*2+GRID_SPAN*COLS,MARGIN*2+GRID_SPAN*ROWS);
}
}

@ -0,0 +1,61 @@
package ;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ChessJFrame extends JFrame {
private ChessBord chessbord;//声明一个棋盘对象
private Panel tool; //声明一个面板对象
private Button StartButton;//声明开始按钮
private Button BackButton;//声明悔棋按钮
private Button exitButton;//声明退出按钮
public ChessJFrame() {//构造函数
setTitle("单机版五子棋");//设置标题
MyButtonLister mb=new MyButtonLister();//按钮事件处理对象
tool=new Panel();//面板对象
chessbord=new ChessBord();//棋盘对象
StartButton=new Button("Reopen");//设置开始按钮
BackButton=new Button("Regret");//设置悔棋按钮
//exitButton=new Button("EXIT");//设置退出游戏按钮
tool.setLayout(new FlowLayout(FlowLayout.CENTER ));//流式布局
tool.add(StartButton);
tool.add(BackButton);
// tool.add(exitButton);//将三个按钮添加到面板对象
StartButton.addActionListener(mb);
BackButton.addActionListener(mb);
// exitButton.addActionListener(mb);//将三个按钮添加到事件监听
add(tool,BorderLayout.SOUTH);//按钮所在的位置
add(chessbord);//添加棋盘对象
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭
pack();//自适应
}
private class MyButtonLister implements ActionListener {
//按钮处理事件类
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object obj=e.getSource();//获取事件源
if(obj==StartButton) {//事件源是重新开始按钮
System.out.println("重新开始");
chessbord.restartGame();
}
else if(obj==BackButton) {//事件源是悔棋按钮
System.out.println("悔棋!");
chessbord.goback();
}
else if(obj==exitButton) {//事件源是退出按钮
System.exit(0);
}
}
}
public static void main(String[] args) {
ChessJFrame jf = new ChessJFrame();//声明框架对象
jf.setLocationRelativeTo(null);//居中显示
jf.setVisible(true);//设置为可见
}
}
Loading…
Cancel
Save