一、实验目标
1)体验敏捷开发中的两人合作。
+ 2)进一步提高个人编程技巧与实践。
二 、实验内容
1)根据以下问题描述,练习结对编程(pair programming)实践;
2)要求学生两人一组,自由组合。每组使用一台计算机,二人共同编码,完成实验要求。
3)要求在结对编程工作期间,两人的角色至少切换 4 次;
4)编程语言不限,版本不限。建议使用 Python 或 JAVA 进行编程。
博客内容应该包括:
1、代码规范
2、程序的总体设计(附图说明模块之间的关系)
3、程序结对编程过程(附图)及功能实现情况(附代码和图)
4、项目github地址(附图)
5、实验总结
实验的总体设计
通过cell.java文件家里细胞信息和游戏机制,InitMarix.java定义了其他情况,即报错机制.这两个模板提供给games.java建立游戏环境,但是没有初始值的自动建立
所以运行程序之前需要首先运行first.java文件,生成几个初始文件,main函数通过games.java板块运行就能得到最终结果
程序结对编程过程
功能实现情况
代码
cell.java代码
package GaLi;
import java.util.Arrays;
public class cell {
/**
* 矩阵高度
*/
private int height;
/**
* 矩阵宽度
*/
private int width;
/**
* 动画速度,每两个状态之间的毫秒数
*/
private int duration;
/**
* 总的变化次数
*/
private int transfromNum=0;
/**
* 矩阵状态,1表示活,0表示死
*/
private int[][] matrix;
public cell(int height, int width, int duration, int transfromNum, int[][] matrix) {
this.height = height;
this.width = width;
this.duration = duration;
this.transfromNum = transfromNum;
this.matrix = matrix;
}
/**
* 上一个状态到下一个状态的转移
* 根据规则可以总结得出两条规则:
* 1. 对于周围活着的细胞为3的情况,下一个状态该细胞总是为活
* 2. 对于周围活着的细胞为2的情况,下一个状态与上一状态相同
*/
public void transform(){
int[][] nextMatrix=new int[height][width];
for (int y = 0; y < matrix.length; y++) {
for (int x = 0; x < matrix[0].length; x++) {
nextMatrix[y][x]=0;
int nearNum= findLifedNum(y,x);
//等于3,则下一状态总是活
if(nearNum==3){
nextMatrix[y][x]=1;
}
//等于2,则与上一状态一样
else if(nearNum==2){
nextMatrix[y][x]=matrix[y][x];
}
}
}
matrix=nextMatrix;
}
/**
* 统计每个细胞周围活着的个数
* @param x 横坐标
* @param y 纵坐标
* @return
*/
public int findLifedNum(int y, int x){
int num=0;
//左边
if(x!=0){
num+=matrix[y][x-1];
}
//左上角
if(x!=0&&y!=0){
num+=matrix[y-1][x-1];
}
//上边
if(y!=0){
num+=matrix[y-1][x];
}
//右上
if(x!=width-1&&y!=0){
num+=matrix[y-1][x+1];
}
//右边
if(x!=width-1){
num+=matrix[y][x+1];
}
//右下
if(x!=width-1&&y!=height-1){
num+=matrix[y+1][x+1];
}
//下边
if(y!=height-1){
num+=matrix[y+1][x];
}
//左下
if(x!=0&&y!=height-1){
num+=matrix[y+1][x-1];
}
return num;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < matrix.length; i++) {
sb.append(Arrays.toString(matrix[i]) + "\n");
}
return sb.toString();
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public int[][] getMatrix() {
return matrix;
}
public int getTransfromNum() {
return transfromNum;
}
public int getDuration() {
return duration;
}
}
first.java代码
package GaLi;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
/**
* 随机生成case
*/
public class first {
/**
* 生成文件数量
*/
public static final int FILE_NUM=5;
public static void main(String[] args) {
for (int i = 0; i < FILE_NUM; i++) {
createCaseFile();
System.out.println(i+1);
}
System.out.println("成功生成5个case文件");
}
/**
* 创建测试案例
*/
private static void createCaseFile() {
Random random = new Random();
int rows = 1 + random.nextInt(100);
int cols = 1 + random.nextInt(100);
int duration = 200;
int num = 300;
File file = new File(cols+"_"+rows+"_"+System.nanoTime() + ".txt");
PrintWriter writer = null;
try {
writer = new PrintWriter(new FileWriter(file));
StringBuilder sb = new StringBuilder(cols + " " + rows + " " + duration + " " + num);
writer.write(sb.append("\n").toString());
//开始逐行初始化
for (int y = 0; y < rows; y++) {
sb = new StringBuilder();
for (int x = 0; x < cols; x++) {
if (random.nextInt(3) % 3 == 0) {
sb.append("1 ");
} else {
sb.append("0 ");
}
}
sb.deleteCharAt(sb.length()-1).append("\n");
writer.write(sb.toString());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
writer.close();
}
}
}
}
games.java
package GaLi;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.TimeUnit;
public class games extends JFrame {
private JButton openFileBtn = new JButton("选择文件");
private JButton startGameBtn = new JButton("开始游戏");
private JLabel durationPromtLabel = new JLabel("动画间隔设置(ms为单位)");
private JTextField durationTextField = new JTextField();
/**
* 游戏是否开始的标志
*/
private boolean isStart = false;
/**
* 游戏结束的标志
*/
private boolean stop = false;
private cell cellMatrix;
private JPanel buttonPanel = new JPanel(new GridLayout(2, 2));
private JPanel gridPanel = new JPanel();
private JTextField[][] textMatrix;
/**
* 动画默认间隔200ms
*/
private static final int DEFAULT_DURATION = 200;
//动画间隔
private int duration = DEFAULT_DURATION;
public games() {
setTitle("生命游戏");
openFileBtn.addActionListener(new OpenFileActioner());
startGameBtn.addActionListener(new StartGameActioner());
buttonPanel.add(openFileBtn);
buttonPanel.add(startGameBtn);
buttonPanel.add(durationPromtLabel);
buttonPanel.add(durationTextField);
buttonPanel.setBackground(Color.WHITE);
getContentPane().add("North", buttonPanel);
this.setSize(1000, 1200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class OpenFileActioner implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fcDlg = new JFileChooser(".");
fcDlg.setDialogTitle("请选择初始配置文件");
int returnVal = fcDlg.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
isStart = false;
stop = true;
startGameBtn.setText("开始游戏");
String filepath = fcDlg.getSelectedFile().getPath();
cellMatrix = InitMatrix.initMatrixFromFile(filepath);
initGridLayout();
showMatrix();
gridPanel.updateUI();
}
}
}
private void showMatrix() {
int[][] matrix = cellMatrix.getMatrix();
for (int y = 0; y < matrix.length; y++) {
for (int x = 0; x < matrix[0].length; x++) {
if (matrix[y][x] == 1) {
textMatrix[y][x].setBackground(Color.BLACK);
} else {
textMatrix[y][x].setBackground(Color.WHITE);
}
}
}
}
/**
* 创建显示的gridlayout布局
*/
private void initGridLayout() {
int rows = cellMatrix.getHeight();
int cols = cellMatrix.getWidth();
gridPanel = new JPanel();
gridPanel.setLayout(new GridLayout(rows, cols));
textMatrix = new JTextField[rows][cols];
for (int y = 0; y < rows; y++) {
for (int x = 0; x < cols; x++) {
JTextField text = new JTextField();
textMatrix[y][x] = text;
gridPanel.add(text);
}
}
add("Center", gridPanel);
}
private class StartGameActioner implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (!isStart) {
//获取时间
try {
duration = Integer.parseInt(durationTextField.getText().trim());
} catch (NumberFormatException e1) {
duration = DEFAULT_DURATION;
}
new Thread(new GameControlTask()).start();
isStart = true;
stop = false;
startGameBtn.setText("暂停游戏");
} else {
stop = true;
isStart = false;
startGameBtn.setText("开始游戏");
}
}
}
private class GameControlTask implements Runnable {
@Override
public void run() {
while (!stop) {
cellMatrix.transform();
showMatrix();
try {
TimeUnit.MILLISECONDS.sleep(duration);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
}
InitMarix.java
package GaLi;
import java.io.*;
public class InitMatrix {
/**
* 从文件路径初始化CellMatrix对象
*
* @param path
* @return
*/
public static cell initMatrixFromFile(String path) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
String line = reader.readLine();
String[] array = line.split(" ");
int width = Integer.parseInt(array[0]);
int height = Integer.parseInt(array[1]);
int duration = Integer.parseInt(array[2]);
int totalNum = Integer.parseInt(array[3]);
int[][] matrix = new int[height][width];
for (int i = 0; i < height; i++) {
line = reader.readLine();
array = line.split(" ");
for (int j = 0; j < array.length; j++) {
matrix[i][j] = Integer.parseInt(array[j]);
}
}
cell cellMatrix = new cell(height, width, duration, totalNum, matrix);
return cellMatrix;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
main.java
package GaLi;
public class Main {
public static void main(String[] args) {
new games();
}
}
功能实现情况
项目地址
附链接 https://github.com/3170701118/-
实验总结:
此次试验比较完整的复习了java语言,更加熟练地掌握了java的使用,但是此次实验最大的收获还是结对编程的完成和GitHub网页的熟练使用,结对编程可以让很多人的智慧总结到一处,这样更能发挥每个人的长处,让自己的作品能更好的完善。同时,我也意识到多和别人讨论,写好相关代码的注释是非常重要的,在GitHub上学习进步也应该是今后目标之一。