public Chess[,] Road(int chozenX, int chozenY, Chess[,] Matrix) // 使棋子显示可行路径时,并使移动前的棋子不移动 { ProMod mod = new ProMod(); //实例化,方便使用该类里的方法 Chess[,] road = mod.SetRoad(); //用Chess类创建出一个[19,17]的road数组,并一个个实例,再初始化路径,即将Chess.Piecepath.not赋到每个road里 Chess[,] trans = new Chess[19, 17]; //用Chess类创建出一个[19,17]的trans数组,方便临时储存信息 bool cr; for (int i = 0; i < 19; i++) //遍历整个棋盘 { for (int j = 0; j < 17; j++) { trans[i, j] = new Chess(); //通过循环,一个个地具体实例化每个trans } } for (int i = 0; i < 19; i++) //遍历整个棋盘 { for (int j = 0; j < 17; j++) { if (i % 2 == 0) //因为x实际的坐标是输入坐标的两倍,即都为偶数的x才是棋盘上的的点 { trans[i, j].side = Matrix[i, j].side; //把每个位置的side属性一个个赋到trans上暂时储存 trans[i, j].type = Matrix[i, j].type; //把每个位置的type属性一个个赋到trans上暂时储存 trans[chozenX, chozenY].side = Matrix[chozenX, chozenY].side; //把当前选择的具体位置的side属性赋到trans暂时储存 trans[chozenX, chozenY].type = Matrix[chozenX, chozenY].type; //把当前选择的具体位置的type属性赋到trans暂时储存 cr = MovePiece(i, j, chozenX, chozenY, Matrix); //使用该方法依据棋子类型,通过遍历一个个检查当前选择的棋子能走的格子,通过返回true或false来形成路径 if (cr == true) //如果该格子能走 { road[i, j].path = Chess.Piecepath.yes; //把该格子的位置的path属性里赋上Chess.Piecepath.yes,即把之前初始化的not变成了yes } Matrix[i, j].side = trans[i, j].side; //每遍历了一个位置后及时把该位置的属性再赋回去,避免选择该棋子准备进行移动时棋子就已经提前移动影响整个函数的判断 Matrix[i, j].type = trans[i, j].type; Matrix[chozenX, chozenY].side = trans[chozenX, chozenY].side; Matrix[chozenX, chozenY].type = trans[chozenX, chozenY].type; } } } return(road); //返回road即可行路径 }
static void Main(string[] args) //主函数,懂得都懂 { bool result = true; bool turn; int player = 0; ProCon con = new ProCon(); ProMod mod = new ProMod(); ProgramView view = new ProgramView(); Chess[,] Matrix = mod.SetPosition(); Chess[,] road = mod.SetRoad(); while (result == true) { string[,] Board = mod.Piece(Matrix); view.Displaying(Matrix, road); view.Start(player); try { Console.ForegroundColor = ConsoleColor.DarkGray; Console.Write("============================================\n"); Console.Write(" X = "); int chozenX = Convert.ToInt32(Console.ReadLine()); Console.Write(" Y = "); int chozenY1 = Convert.ToInt32(Console.ReadLine()); int chozenY = chozenY1 * 2; int checkpiece = con.CheckPiece(chozenX * 2, chozenY, Matrix); if (checkpiece == 2) //检测是否有棋子 { road = con.Road(chozenX * 2, chozenY, Matrix); view.Displaying(Matrix, road); road = mod.SetRoad(); view.Check(checkpiece, Board, chozenX, chozenY); Console.Write(" X = "); int X = Convert.ToInt32(Console.ReadLine()); Console.Write(" Y = "); int Y = Convert.ToInt32(Console.ReadLine()); turn = con.SwitchPlayer(X * 2, Y * 2, chozenX * 2, chozenY, Matrix); player = view.Move(turn, player); result = con.Result(Matrix); } else if (checkpiece == 0) { view.Check(checkpiece, Board, chozenX, chozenY); } else { view.Check(checkpiece, Board, chozenX, chozenY); } } catch (Exception) //检测异常 { view.Wrong(); } } view.Displaying(Matrix, road); view.Win(player); //Console.ReadKey(); }