新组内成员:张月 李畅 袁明 梁杉杉 于子轩 孙晓彤
新GDC第一次合照
今日完成的功能,扫雷图形界面的实现已经完成,代码也已经完成,视频已经粗略完成,黄金点程序的代码已经完成。
明天将会细化代码运行说明视频还有扫雷代码也要细化
难点:计算方块周围雷数 当选中的位置为空,则翻开周围的地雷,标记地雷 随机分布雷,如何寻找离黄金点最近和最远的数字
扫雷游戏代码:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Frame extends JFrame { JTextField text; JLabel nowBomb, setBomb; int BombNum, BlockNum; // 当前雷数,当前方块数 int rightBomb, restBomb, restBlock; // 找到的地雷数,剩余雷数,剩余方块数 JButton start = new JButton(" 开始 "); JPanel MenuPamel = new JPanel();//菜单面板 JPanel bombPanel = new JPanel();//雷的面板 Bomb[][] bombButton;//扫雷按钮 JPanel c; BorderLayout borderLayout1 = new BorderLayout(); GridLayout gridLayout1 = new GridLayout(); public Frame() { try { //捕获异常 setDefaultCloseOperation(EXIT_ON_CLOSE); jbInit(); } catch (Exception exception) { exception.printStackTrace(); } } public void jbInit() throws Exception { c = (JPanel) getContentPane(); //中心面板 setTitle("GDC-扫雷"); // 面板标题 c.setBackground(Color.WHITE); //背景颜色:白色 MenuPamel.setBackground(Color.GRAY); //菜单栏面板颜色:灰色 c.setLayout(borderLayout1); setSize(new Dimension(800, 800)); //大小 setResizable(false); BlockNum = 144; //初始化空白按钮数 BombNum = 10; // 初始化雷的按钮数 text = new JTextField("10 ", 3); // 文本框默认数为10,最大允许3字节 nowBomb = new JLabel("当前雷数" + ":" + BombNum); //当前雷数 setBomb = new JLabel("设置地雷数"); // 设置的雷数 start.addActionListener(new Frame1_start_actionAdapter(this)); //事件监听 MenuPamel.add(setBomb); // 加入所需项 MenuPamel.add(text); MenuPamel.add(start); MenuPamel.add(nowBomb); c.add(MenuPamel, java.awt.BorderLayout.SOUTH); bombPanel.setLayout(gridLayout1); gridLayout1.setColumns( (int) Math.sqrt(BlockNum)); // 列 gridLayout1.setRows( (int) Math.sqrt(BlockNum)); // 行 bombButton = new Bomb[ (int) Math.sqrt(BlockNum)][ (int) Math.sqrt(BlockNum)]; //确定按钮位置 for (int i = 0; i < (int) Math.sqrt(BlockNum); i++) { for (int j = 0; j < (int) Math.sqrt(BlockNum); j++) { bombButton[i][j] = new Bomb(i, j); //bombButton[i][j].setSize(10, 10); //bombButton[i][j].setFont(new Font("", Font.PLAIN, 14));//设置字体大小 bombButton[i][j].setForeground(Color.white); bombButton[i][j].addMouseListener(new Bomb_mouseAdapter(this)); //鼠标监听器 bombButton[i][j].addActionListener(new Bomb_actionAdapter(this)); //时间监听器 bombPanel.add(bombButton[i][j]); } } c.add(bombPanel, java.awt.BorderLayout.CENTER); startBomb(); } /* 开始按钮 */ public void start_actionPerformed(ActionEvent e) { int num=Integer.parseInt(text.getText().trim()); if (num >= 5 && num < 50) { BombNum = num; startBomb(); } else if (num < 5) { JOptionPane.showMessageDialog(null, "您设置的地雷数太少了,请重设!", "错误",JOptionPane.ERROR_MESSAGE);//消息提醒框,只有一个确认按钮的消息提示框 num=10; BombNum = num; } else { JOptionPane.showMessageDialog(null, "您设置的地雷数太多了,请重设!", "错误",JOptionPane.ERROR_MESSAGE); num=10; BombNum = num; } } /* 开始,布雷 */ public void startBomb() { nowBomb.setText("当前雷数" + ":" + BombNum); for (int i = 0; i < (int) Math.sqrt(BlockNum); i++) { for (int j = 0; j < (int) Math.sqrt(BlockNum); j++) { bombButton[i][j].isBomb = false; //不是雷 bombButton[i][j].isClicked = false;//没被点击 bombButton[i][j].isRight = false; //没点击右键 bombButton[i][j].BombFlag = 0;//没被探雷标记 bombButton[i][j].BombRoundCount = 9;//统计轮数 bombButton[i][j].setEnabled(true); //可行的 bombButton[i][j].setText(""); //bombButton[i][j].setFont(new Font("", Font.PLAIN, 14));//设置字体大小 bombButton[i][j].setForeground(Color.BLUE); rightBomb = 0; restBomb = BombNum; restBlock = BlockNum - BombNum; } } for (int i = 0; i < BombNum; ) { int x = (int) (Math.random() * (int) (Math.sqrt(BlockNum) - 1)); //随机分布(行) int y = (int) (Math.random() * (int) (Math.sqrt(BlockNum) - 1)); //随机分布(列) if (bombButton[x][y].isBomb != true) //确定雷的位置 { bombButton[x][y].isBomb = true; i++; } } CountRoundBomb(); } /* 计算方块周围雷数 */ public void CountRoundBomb() { for (int i = 0; i < (int) Math.sqrt(BlockNum); i++) { for (int j = 0; j < (int) Math.sqrt(BlockNum); j++) { int count = 0; // 当需要检测的单元格本身无地雷的情况下,统计周围的地雷个数 if (bombButton[i][j].isBomb != true) { for (int x = i - 1; x < i + 2; x++) //确定左右 { for (int y = j - 1; y < j + 2; y++) //确定上下 { if ( (x >= 0) && (y >= 0)&& (x < ( (int) Math.sqrt(BlockNum))) && (y < ( (int) Math.sqrt(BlockNum)))) { if (bombButton[x][y].isBomb == true) { count++; } } } } bombButton[i][j].BombRoundCount = count; } } } } /* 是否挖完了所有的雷 */ public void isWin() { restBlock = BlockNum - BombNum; for (int i = 0; i < (int) Math.sqrt(BlockNum); i++) //空白格的循环 { for (int j = 0; j < (int) Math.sqrt(BlockNum); j++) { if (bombButton[i][j].isClicked == true) { restBlock--; } } } if (rightBomb == BombNum || restBlock == 0) //判断是否胜利 { JOptionPane.showMessageDialog(this, "您挖完了所有的雷,您胜利了!", "胜利",JOptionPane.INFORMATION_MESSAGE); startBomb(); } } /** 当选中的位置为空,则翻开周围的地图* */ public void isNull(Bomb ClickedButton) { int i, j; i = ClickedButton.num_x; j = ClickedButton.num_y; for (int x = i - 1; x < i + 2; x++) { for (int y = j - 1; y < j + 2; y++) { if ( ( (x != i) || (y != j)) && (x >= 0) && (y >= 0)&& (x < ( (int) Math.sqrt(BlockNum))) && (y < ( (int) Math.sqrt(BlockNum)))) { if (bombButton[x][y].isBomb == false&& bombButton[x][y].isClicked == false&& bombButton[x][y].isRight == false) { turn(bombButton[x][y]); } } } } } /* 翻开 */ public void turn(Bomb ClickedButton) { ClickedButton.setEnabled(false); ClickedButton.isClicked = true; if (ClickedButton.BombRoundCount > 0) { ClickedButton.setText(ClickedButton.BombRoundCount + ""); } else { isNull(ClickedButton); } } /* 左键点击 */ public void actionPerformed(ActionEvent e) { if ( ( (Bomb) e.getSource()).isClicked == false && ( (Bomb) e.getSource()).isRight == false) { if ( ( (Bomb) e.getSource()).isBomb == false) //调用源事件,获得点击的bomb { turn( ( (Bomb) e.getSource())); isWin(); } else { for (int i = 0; i < (int) Math.sqrt(BlockNum); i++) { for (int j = 0; j < (int) Math.sqrt(BlockNum); j++) { if (bombButton[i][j].isBomb == true) { bombButton[i][j].setText("b"); //显示b,即雷 } } } ( (Bomb) e.getSource()).setForeground(Color.RED); //红色 //( (Bomb) e.getSource()).setFont(new Font("", Font.BOLD, 20)); ( (Bomb) e.getSource()).setText("X"); //被点到雷的方块显示X JOptionPane.showMessageDialog(null, "你踩到地雷了,按确定重来", "踩到地雷",2); //弹出失败的消息提醒框 startBomb(); } } } /* 右键点击 */ public void mouseClicked(MouseEvent e) //鼠标事件 { Bomb bombSource = (Bomb) e.getSource(); boolean right = SwingUtilities.isRightMouseButton(e); //定义鼠标右键为right if ( (right == true) && (bombSource.isClicked == false)) { bombSource.BombFlag = (bombSource.BombFlag + 1) % 3; //判断点击几下 if (bombSource.BombFlag == 1) //点击一下方块显示“F” { if (restBomb > 0) { bombSource.setForeground(Color.RED); //颜色为红色 bombSource.setText("F"); //方块显示“F” bombSource.isRight = true; //若正确,雷的个数自减一 restBomb--; } else { bombSource.BombFlag = 0; //否则不标记 } } else if (bombSource.BombFlag == 2) //点击两下方块显示“Q” { restBomb++; //剩余雷数自加一 bombSource.setText("Q"); //方块显示“Q” bombSource.isRight = false; } else { bombSource.setText(""); } if (bombSource.isBomb == true) //若雷的位置判断正确 { if (bombSource.BombFlag == 1) { rightBomb++; } else if (bombSource.BombFlag == 2) { rightBomb--; } } nowBomb.setText("当前雷数" + ":" + restBomb); //文本框显示:当前雷数(还剩多少雷) isWin(); } } public static void main(String[] args) { Frame frame = new Frame(); frame.setVisible(true); } } class Frame1_start_actionAdapter implements ActionListener { public Frame adaptee; Frame1_start_actionAdapter(Frame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.start_actionPerformed(e); } } class Bomb extends JButton { int num_x, num_y; // 第几号方块 int BombRoundCount; // 周围雷数 boolean isBomb; // 是否为雷 boolean isClicked; // 是否被点击 int BombFlag; // 探雷标记 boolean isRight; // 是否点击右键 public Bomb(int x, int y) { num_x = x; num_y = y; BombFlag = 0; BombRoundCount = 9; isBomb = false; isClicked = false; isRight = false; } } class Bomb_actionAdapter implements ActionListener { public Frame adaptee; Bomb_actionAdapter(Frame adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.actionPerformed(e); } } class Bomb_mouseAdapter extends MouseAdapter { public Frame adaptee; Bomb_mouseAdapter(Frame adaptee) { this.adaptee = adaptee; } public void mouseClicked(MouseEvent e) { adaptee.mouseClicked(e); } }
黄金点代码:
import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.lang.*; public class GoldPoint{ public static double count(double d[],int size) { double sum=0.0; for (int i=0;i<size;i++) sum=sum+d[i]; return sum; } public static void goldpoint (){ System.out.println("欢迎参加这个黄金点的游戏!TVT"); System.out.println("请输入参加的游戏人数:"); String fileName = "E:\\javalian\\gd.txt"; try{//抓捕文本中数据异常和错误数据 Scanner cin = new Scanner(System.in); int peoplenum=cin.nextInt(); double [] shu=new double[peoplenum]; double [] b=new double[peoplenum]; double [] score=new double[peoplenum]; System.out.println("请输入你想玩多少轮:"); int rand=cin.nextInt(); BufferedReader in = new BufferedReader(new FileReader(fileName));//读入文件 for(int r=0;r<rand;r++) { System.out.println("开始第"+(r+1)+"轮"); for (int i=0;i<peoplenum;i++) { String m =in.readLine();//按行读取 shu[i]=Double.valueOf(m); System.out.println("请读入第"+(i+1)+"名参加者的数字:"+shu[i]); } double sum=GoldPoint.count(shu,peoplenum); double aver=sum/peoplenum; double g=aver*0.618; System.out.println("黄金点是:"+g); for (int i=0;i<peoplenum;i++) { b[i]=Math.abs(g-shu[i]); double latest=b[0]; double farthest=b[0]; for(int j=0;j<peoplenum;j++)//最近 { if (b[j]<latest) latest=b[j]; } for (int k=0;k<peoplenum;k++) { if (b[k]==latest) score[k]=peoplenum; else score[k]=0; } for (int n=0;n<peoplenum;n++)//最远 { if (b[n]>farthest) farthest=b[n]; } for (int m=0;m<peoplenum;m++) { if (b[m]==farthest) score[m]=-2; } } for (int num=0;num<peoplenum;num++) { System.out.println(score[num]); } System.out.println("本轮结束!"); } System.out.println("GAME OVER"); } catch(Exception e) { System.out.println("你的输入有误,请输入数字!"); GoldPoint.goldpoint(); } } public static void main (String args[]) { GoldPoint gd=new GoldPoint(); gd.goldpoint(); } }
今日工作
将黄金点和扫雷小程序完成,时间紧凑,完成的很粗略。进入了一个新的组员,他很认真,相处的不错,视频也下了软件也粗略录制完成。
学习总结
短时间内团队默契真的很重要,分工要很明确,,要不然细小且多的任务会让人忙的团团转,好在分工很明确,乱中有序,每个人都在赶着做,也是有条不紊。