20135337朱荟潼——实验三之结对实验项目

游戏名称:答题小能手

结对小伙伴:20135317韩玉琪      

博客:http://www.cnblogs.com/hyq20135317

我的主要任务是:编写伪代码、完善代码、添加注释、TDD测试

 Git:http://git.shiyanlou.com/135337

实验内容:

主要包括的类:JFrame、JPanel

1、JPanel类  

JPanel类是主框架,程序的启动类。 

其中包括游戏界面的创建、按钮的设置、事件监听器设置  

2、JFrame类  

JFrame类主要实现的是界面的布局绘制、按钮的坐标、设置每个项目布局、实现游戏的开始、重来、下一题。  

伪代码:

创建一个容器f;

{

初始化界面;      

//文字:

"题目:" ——问题文本框;

"选项:"——四个选项按钮;

"你的得分:"——计分文本框;

//按钮

"开始"、"下一个题目"、"重新练习";

}

设置布局管理器之网格布局;//需要设置面板的具体方位(居中);

添加侦听器,使按按钮后,发生动作;

从指定的位置读取文件数据;//确保不在游戏本机下仍可以读取到文件;

判断正误;//对,加一分;否则,不变;

下一题;//顺序读取文件中的下一道题;

开始;//从第一题开始答题,累计加分;

重新联系;//从第一题开始答题,分数归零;

产品代码:

package game;

 

public class QuestionBank {

    String question;

    String choiceA, choiceB, choiceC, choiceD;

    String answer;

 

    QuestionBank(String question, String choiceA, String choiceB,

            String choiceC, String choiceD, String answer) {

        this.question = question;

        this.choiceA = choiceA;

        this.choiceB = choiceB;

        this.choiceC = choiceC;

        this.choiceD = choiceD;

        this.answer = answer;

    }

}

package game;

 

public class AnswerQuestion {

    public static void main(String args[]) {

        new Function();

    }

}

package game;

import javax.swing.*;

import java.awt.*;

import java.io.*;

import java.awt.event.*;

public class Function implements ActionListener {

    private static final int Q_Number = 100;

    JFrame f = new JFrame("答题小能手");//JFrame创建一个容器f,显示“答题小能手”

    JPanel p1 = new JPanel();

    JPanel p2 = new JPanel();

    JPanel p3 = new JPanel();

    JPanel p4 = new JPanel();

    int index = 0, total = 0;                //初始化题目下标和分数

    QuestionBank questions[] = new QuestionBank[Q_Number];

    JLabel questionString = new JLabel("题目:");               //创建对象显示“题目”

    JLabel choiceString = new JLabel("选项:");

    JLabel totalString = new JLabel("你的得分:");

    TextArea question = new TextArea(" 游戏规则: 1.点击“开始”进行答题,点击下一题继续 2.答题过程中点击“重新练习”,从第一道题作答,分数归0。 3.答题过程中点击“开始”从第一道题作答,分数累计. 现在,请单击<开始>进行游戏", 5, 60, 3);

    JTextField totaltf = new JTextField("0", 5);

    JRadioButton choiceA = new JRadioButton("a");            //创建一个单选按钮

    JRadioButton choiceB = new JRadioButton("b");

    JRadioButton choiceC = new JRadioButton("c");

    JRadioButton choiceD = new JRadioButton("d");

    ButtonGroup Group = new ButtonGroup();             //创建一个按钮分组框

    JButton beginbt = new JButton("开始");

    JButton nextbt = new JButton("下一个题目");

    JButton practiceAgainbt = new JButton("重新练习");

    File file = new File("src/Question_File/题目.txt");

    public Function() {    //对frame进行基本设置

       f.setSize(750, 600);          //设置大小width=750,height=600

       f.setLocation(250, 50);          //设置位置left=250,top=50

       f.setVisible(true);                 //可见

       f.setLayout(new GridLayout(4, 1, 15, 15));          //设置布局管理器之网格布局,显式定义组件的网格位置。

       p1.setLayout(new FlowLayout(FlowLayout.CENTER));             //组件剧中对齐

       p2.setLayout(new FlowLayout(FlowLayout.CENTER));

       p3.setLayout(new FlowLayout(FlowLayout.CENTER));

       p4.setLayout(new FlowLayout(FlowLayout.CENTER));

       Group.add(choiceA);            //将面板choiceA添加到radioGroup中

       Group.add(choiceB);

       Group.add(choiceC);

       Group.add(choiceD);

       p1.add(questionString) ;            //将"题目:"添加到p1中

       p1.add(question);           //将游戏问题添加到p1中

       p2.add(choiceString);          //将"选项:"添加到p2

       p2.add(choiceA);               //将A、B、C、D选择添加到p2

       p2.add(choiceB);

       p2.add(choiceC);

       p2.add(choiceD);

       p3.add(totalString);          //将"你的得分:"添加到p3

       p3.add(totaltf);           //将计分添加到p3

       p4.add(beginbt);         //将按钮"开始"添加到p4

       p4.add(nextbt);            //将按钮"下一个题目"添加到p4

       p4.add(practiceAgainbt);          //将按钮"重新练习"添加到p4

       //将面板1、2、3、4添加到容器f

       f.add(p1);

       f.add(p2);

       f.add(p3);

       f.add(p4);

       //添加侦听器,使按按钮后,发生动作

       nextbt.addActionListener(this);

       beginbt.addActionListener(this);

       practiceAgainbt.addActionListener(this);

       readFile();           //从指定的位置读取文件数据

    }

    public void readFile() {

       String s, question, choiceA, choiceB, choiceC, choiceD, answer;

       try {

           int count = 0;            //初始化计数

           FileInputStream in = new FileInputStream(file);            //创建流文件读入

           byte b[] = new byte[in.available()];          //先定义了一个byte类型的数组,数组长度为:文件读入的长度

           in.read(b);         //文件读取

           s = new String(b);                    //创建一个string对象,并将其返回给s

           String c[] = s.split("&&");               //用“&&”分割字符串

           for (int i = 0; i < c.length; i++) {                //通过循环,无论按下任何一个选项都将跳入下一个问题

              question = c[i++];

              choiceA = c[i++];

              choiceB = c[i++];

              choiceC = c[i++];

              choiceD = c[i++];

              answer = c[i];

              questions[count++] = new QuestionBank(question, choiceA,

                     choiceB, choiceC, choiceD, answer);

           }

       } catch (Exception e) {                //捕获异常

           e.printStackTrace();

       }

    }

    public void ifRight() {           //对答题正确与否进行打分

      

       if (choiceA.isSelected()) {

           if (questions[index].answer.equals(choiceA.getText())) {

              total++;             //如果与获取文件中的答案相等,则加1分

           }

       }

       if (choiceB.isSelected()) {

           if (questions[index].answer.equals(choiceB.getText())) {

              total++;

           }

       }

       if (choiceC.isSelected()) {

           if (questions[index].answer.equals(choiceC.getText())) {

              total++;

           }

       }

       if (choiceD.isSelected()) {

           if (questions[index].answer.equals(choiceD.getText())) {

              total++;

           }

       }

       totaltf.setText(Integer.toString(total));

    }

    public void setQuestion(int index) {           //读取各选项的内容

       question.setText(questions[index].question);

       choiceA.setText(questions[index].choiceA);

       choiceB.setText(questions[index].choiceB);

       choiceC.setText(questions[index].choiceC);

       choiceD.setText(questions[index].choiceD);

       Group.clearSelection();

    }

    public void actionPerformed(ActionEvent e) {

      

       if (e.getSource() == nextbt) {         //下一题

           ifRight();           //判断是否正确

           if (index < questions.length) {                   //如果“下一题”的下标在问题长度范围内,则进行下一题

              setQuestion(++index);

           }

       }

       if (e.getSource() == practiceAgainbt) {               //如果“重新开始”,从第一题开始

           totaltf.setText(Integer.toString(0));

           index = 0;

           total = 0;

           ifRight();

           if (index < questions.length) {

              setQuestion(index);

           }

       }

       if (e.getSource() == beginbt) {              //如果“开始”,从第一题开始

           index=0;

          

           if (total != 0) {

              ifRight();

              if (index < questions.length) {

                  setQuestion(index);

              }

           }else{

              setQuestion(0);

           }

       }

    }

}

运行截图:

                       

对答案正确与否的TDD测试:

import java.awt.event.ActionEvent;

import org.junit.Before;

import org.junit.Test;

import junit.framework.TestCase;

 

public class FunctionTest extends TestCase{

    public static Function function=new Function();

    @Before

    public void setUp() throws Exception {

    }

    @Test

    public void testIfRight() {

        function.ifRight();

        if(function.practiceAgainbt != null){

            assertEquals(0,function.total);

        }

    }

    @Test

    public void testreadFile(){

        function.readFile();

    }

    @Test

    public void testsetQuestion(){

 

    }

    @Test

    public void testactionPerformed(){

        if(function.practiceAgainbt != null){

            assertEquals(0,function.total);

        }

    }

}

  

TDD截图:

 

总结:

在这次结对编写游戏的实验当中,我们选择了答题游戏——“答题小能手”。首先,查找网络上的代码,对别人的代码进行了学习,了解开发者开发一个游戏的整体思路。然后,我们讨论了对自己的游戏的想法(主要是在游戏规则上面,发现了所查找的游戏代码存在很多的Bug)。最后,受到了所参考的代码的启发,同时也设定了自己的游戏规则,使这个游戏规则更加完善合理。

在这次结对实验当中。伪代码的编写,让我对这个游戏整体进程有了大致的了解。添加注释看上去是一项比较简单的工作,但是通过添加注释,我复习了JFrame、布局管理器的功能,以前学习课本敲代码时的思路比较“顺序化”,敲一行理解一行,而这次面对完整的代码在进行注释,开始时不太理解JFrame的一些功能调用,慢慢地往后发现在具体调用先前的功能(比如:p1.add(questionString);把“题目”放入“Panel1”)。让我对程序有了更深刻的学习以及对知识的再一次巩固。

原文地址:https://www.cnblogs.com/zzzz5/p/4554501.html