以下代码仅为部分代码举例,具体请见项目。
1. 在静态类constant中定义所有数字常量,数字常量名全部为大写字母。
static class constant
{
//空地/墙/箱子
public const int BLANK = 0,WALL = 1,BOX = 3, DESTINATION = 0, PEOPLE = 6;
//箱子与目的地重合/人与目的地重合
public const int BOX_DES = 7,PEO_DES=9;
public const int EDGE = 30;
}
不要“从天上掉下来一个数”,修改前后的部分代码对比:
修改前
pictureBox1.Width = myArray.GetLength(1) * 30;
if (myArray[i, j] == 1)
{
image = new Bitmap("wall.png");
g.DrawImage(image, j * 30, i * 30, 30, 30);
}
修改后
pictureBox1.Width = myArray.GetLength(1) * constant.EDGE;
if (myArray[i, j] == constant.WALL)
{
image = new Bitmap("wall.png");
g.DrawImage(image, j * constant.EDGE, i * constant.EDGE, constant.EDGE, constant.EDGE);
}
if (myArray[next_row, next_col] == constant.BLANK)
{
myArray[next_row, next_col] = constant.WORKER;
if (myArray[now_row, now_col] == constant.WORKER)
{
myArray[now_row, now_col] = constant.BLANK;
}
else
{
myArray[now_row, now_col] = constant.DESTINATION;
}
}
else if (myArray[next_row, next_col] == constant.BOX && myArray[to_row, to_col] == constant.BLANK)
{
myArray[next_row, next_col] = constant.WORKER;
myArray[to_row, to_col] = constant.BOX;
if (myArray[now_row, now_col] == constant.WORKER)
{
myArray[now_row, now_col] = constant.BLANK;
}
else
{
myArray[now_row, now_col] = constant.DESTINATION;
}
2.重复代码的修改
利用逻辑上的规律,给新的变量赋不同的,使用相同的语句。
修改前
if (myArray[i, j] == 3)
{
image = new Bitmap("box.png");
g.DrawImage(image, j * 30, i * 30, 30, 30);
}
if (myArray[i, j] == 0)
{
image = new Bitmap("land.png");
g.DrawImage(image, j * 30, i * 30, 30, 30);
}
if (myArray[i, j] == 4)
{
image = new Bitmap("dest.png");
g.DrawImage(image, j * 30, i * 30, 30, 30);
}
if (myArray[i, j] == 9)
{
image = new Bitmap("WoD.png");
g.DrawImage(image, j * 30, i * 30, 30, 30);
}
if (myArray[i, j] == 7)
{
image = new Bitmap("BoD.png");
g.DrawImage(image, j * 30, i * 30, 30, 30);
}
修改后
不使用数字常量,直接将数组中所存数字转换成字符串,修改图片地址
public static string imgName = "1";
public static string imgFileName = imgName+".png";
Image image = new Bitmap(imgFileName);
for (int i = 0; i < myArray.GetLength(0); i++)
{
for (int j = 0; j < myArray.GetLength(1); j++)
{
imgName = myArray[i, j].ToString();
imgFileName = imgName + ".png";
image = new Bitmap(imgFileName);
g.DrawImage(image, j * constant.EDGE, i * constant.EDGE, constant.EDGE, constant.EDGE);
}
}
修改前
4个case 几乎重复分代码
myArray[i, j] = 4;
}
}
break;
case Keys.Left:
if (myArray[i, j - 1] == 0)
{
myArray[i, j - 1] = 6;
if (myArray[i, j] == 6)
{
myArray[i, j] = 0;
}
else
{
myArray[i, j] = 4;
}
}
else if (myArray[i, j - 1] == 3 && myArray[i, j - 2] == 0)
{
myArray[i, j - 1] = 6;
myArray[i, j - 2] = 3;
if (myArray[i, j] == 6)
{
myArray[i, j] = 0;
}
else
修改后
int i = 0, j = 0;
int now_row = 0, now_col = 0,next_row=0,next_col=0,to_row=0,to_col=0;
//得到人的坐标
for (int x = 0; x < row_num; x++)
{
for (int y = 0; y < col_num; y++)
{
if (myArray[x, y] == constant.WORKER || myArray[x, y] == constant.WOR_DES)
{
i = x;
j = y;
}
}
}
//接收按键
switch (e.KeyCode)
{
case Keys.Up: { now_row = i; next_row = i - 1;to_row = i - 2; next_col=now_col = to_col = j; break; }
case Keys.Left: { now_row=next_row=to_row = i; now_col = j; next_col = j - 1;to_col = j - 2; break; }
case Keys.Down: { now_row = i; next_row = i + 1; to_row = i + 2; next_col = now_col = to_col = j; break; }
case Keys.Right: { now_row = next_row = to_row = i; now_col = j; next_col = j + 1; to_col = j + 2; break; }
}
if (myArray[next_row, next_col] == 0)
{
myArray[next_row, next_col] = 6;
if (myArray[now_row, now_col] == 6)
{
myArray[now_row, now_col] = 0;
}
else
{
myArray[now_row, now_col] = 4;
}
}
else if (myArray[next_row, next_col] == 3 && myArray[to_row, to_col] == 0)
{
myArray[next_row, next_col] = 6;
myArray[to_row, to_col] = 3;
if (myArray[now_row, now_col] == 6)
{
myArray[now_row, now_col] = 0;
}
else
{
myArray[now_row, now_col] = 4;
}
}
else if (myArray[next_row, next_col] == 4)
{
myArray[next_row, next_col] = 9;
if (myArray[now_row, now_col] == 6)
{
myArray[now_row, now_col] = 0;
}
else
{
myArray[now_row, now_col] = 4;
}
}
else if (myArray[next_row, next_col] == 7 && myArray[to_row, to_col] == 0)
{
myArray[next_row, next_col] = 9;
myArray[to_row, to_col] = 3;
if (myArray[now_row, now_col] == 6)
{
myArray[now_row, now_col] = 0;
}
else
{
myArray[now_row, now_col] = 4;
}
}
else if (myArray[next_row, next_col] == 3 && myArray[to_row, to_col] == 4)
{
myArray[next_row, next_col] = 6;
myArray[to_row, to_col] = 7;
if (myArray[now_row, now_col] == 6)
{
myArray[now_row, now_col] = 0;
}
else
{
myArray[now_row, now_col] = 4;
}
}
else if (myArray[next_row, next_col] == 7 && myArray[to_row, to_col] == 4)
{
myArray[next_row, next_col] = 9;
myArray[to_row, to_col] = 7;
if (myArray[now_row, now_col] == 6)
{
myArray[now_row, now_col] = 0;
}
else
{
myArray[now_row, now_col] = 4;
}
3.变量命名规范化,将i、j等循环中使用的变量改为row、col,增强代码可读性
修改后
for (int row = 0; row < myArray.GetLength(0); row++)
{
for (int col = 0; col < myArray.GetLength(1); col++)
{
}
}
4.使用卫语句
抛出异常
try { txt = File.ReadAllLines(FileName); }
catch (FileNotFoundException q)
{
MessageBox.Show("找不到地图文件", "提示");
Close();
return;
}
init_data();
5.修改变量作用域
全局变量放在最前面,仅在某方法内使用的放在方法体内。
//图片文件名
public static string imgName = "1";
public static string imgFileName = imgName + ".png";
//地图文件名
public static string FileName = ".\\map\\configurations.txt";
private string[] txt = File.ReadAllLines(FileName);
//全局变量
private int[,] myArray;//存放地图的数组
private int row_num = 0, col_num = 0; //第n关的行数和列数
public int row_start = 0, row_end = 0;//第n关的开始和结束行数
private string[] txt_n;
internal int flag_last = 0;
private int Level = 1;//关卡数
6.增加必要注释
补充一些必要的注释,删减部分无用注释。