本文介绍了如何使此代码尽可能简单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 你好。 保持良好的工作 我很快就会加入 使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Text; 使用 System.IO; 命名空间随机播放 { public class 游戏 { public static uint MoveCount = 0 ; bool IsPass() { for ( int i = 0 ; i < 7 ; i ++) { for ( int j = 0 ; j < 7 ; j ++) { if (GenerateArray.a [i,j]!= Resource.arr1 [i,j]) return false ; } } 返回 true ; } // 游戏是否 void PlayGame() { int row = 0 ,col = 0 ; GenerateArray objOfGen = new GenerateArray(); objOfGen.UpdateArray(); // 更新游戏 Record.DisplayRecordInfor( ); objOfGen.DisplayArray(); // 显示游戏画面 objOfGen。位置( out 行, out col); // 定位可移动网格坐标 Console.Write( 输入你的移动[U到Up,D到Down,L到左,R到右]); ConsoleKeyInfo cki = new ConsoleKeyInfo(); while (cki.Key!= ConsoleKey.Escape) // 按Esc退出 { cki = Console.ReadKey( true ); Console.Clear(); switch (cki.Key) { case ConsoleKey.D: case ConsoleKey.DownArrow: if (行< 2 ) { objOfGen.Swap(row,col,row ,col); } else { objOfGen.Swap(row,col,row - 2 ,col); row - = 2 ; ++ MoveCount; } Record.DisplayRecordInfor(); objOfGen.DisplayArray(); break ; case ConsoleKey.U: case ConsoleKey.UpArrow: if (row > 3 ) { objOfGen.Swap(row,col,row,col); } else { objOfGen.Swap(row,col,row + 2 ,col); row + = 2 ; ++ MoveCount; } Record.DisplayRecordInfor(); objOfGen.DisplayArray(); break ; case ConsoleKey.R: case ConsoleKey.RightArrow: if (col < 2 ) { objOfGen.Swap(row,col,row,col); } else { objOfGen.Swap(row,col,row,col - 2 ); col - = 2 ; ++ MoveCount; } Record.DisplayRecordInfor(); objOfGen.DisplayArray(); break ; case ConsoleKey.L: case ConsoleKey.LeftArrow: if (col > 3 ) { objOfGen.Swap(row,col,row,col); } else { objOfGen.Swap(row,col,row,col + 2 ); col + = 2 ; ++ MoveCount; } Record.DisplayRecordInfor(); objOfGen.DisplayArray(); break ; 默认: Console.Clear(); Record.DisplayRecordInfor(); objOfGen.DisplayArray(); break ; } Console.WriteLine( 输入你的移动[U to Up, D为向下,L为左,R为右); } if (IsPass()) { Console.WriteLine( 你赢了比赛!); } } // Play public void EndGame() { } // 结束游戏 public static void Main( string [] args) { Game objOfGame = new Game(); objOfGame.PlayGame(); } } // 游戏 public class 记录 { public static void DisplayRecordInfor() { Console.WriteLine( *** --- The Shuffle Game --- ***); Console.WriteLine( Crurrent Key Presses: + Game.MoveCount ); } // 信息显示记录 public void Updatecount() { Console.WriteLine( *** ---随机游戏--- ***); Console.WriteLine( Crurrent Key Presses: + Game.MoveCount ); } // 更新按下的键数 } // 记录类 public class GenerateArray { public static char [,] a = new char [ 7 , 7 ]; public GenerateArray() { for ( int i = 0 ; i < 7 ; i ++) { for ( int j = 0 ; j < 7 ; j ++) {a [i,j] = Resource.arr1 [i,j]; } } } // 初始化数组 public void UpdateArray() { Random RandomNext = new Random(); char [] temp = new char [ 9 ]; char ch = Convert.ToChar(RandomNext.Next( 0 , 9 )+ ' 0'); for ( int i = 0 ; i < 9 ; i ++) { temp [i ] = ch; } for ( int i = 0 ; i < 8 ;) { char RandomCh = Convert.ToChar(RandomNext.Next( 0 , 9 )+ ' 0'); for ( int j = 0 ; j < = i; j ++) { if ( temp [j] == RandomCh) break ; else if (i == j) { temp [i + 1 ] = RandomCh; i ++; break ; } } } a [ 1 , 1 ] = temp [ 1 ]; a [ 1 , 3 ] = temp [ 2 ]; a [ 1 , 5 ] = temp [ 3 ]; a [ 3 , 1 ] = temp [ 4 ]; a [ 3 , 3 ] = temp [ 5 ]; a [ 3 , 5 ] = temp [ 6 ]; a [ 5 , 1 ] = temp [ 7 ]; a [ 5 , 3 ] = temp [ 8 ]; a [ 5 , 5 ] = temp [ 0 ]; } // 更新数组(数组生成随机) public void 交换( int row1, int col1, int row2, int col2) { char temp = a [row1,col1]; a [row1,col1] = a [row2,col2]; a [row2,col2] = temp; } // 更改职位 public void DisplayArray() { for ( int i = 0 ; i < 7 ; i ++) { for ( int j = 0 ; j < 7 ; j ++) { Console.Write(a [i,j]); } Console.WriteLine(); } } // 显示数组 public void 排名( out int 行, out int col) { row = col = 0 ; for ( int i = 0 ; i < 7 ; i ++) { for ( int j = 0 ; j < 7 ; j ++) { if (a [i,j] == ' 0') { row = i; col = j; return ; } } } } } // 定位&处理数据 public class 资源 { public static char [,] arr1 = { {' +',' +',' +',' +',' +',' + ',' +'}, {' +',' 1',' +',' 2',' + ',' 3',' +'}, {' +',' +', ' +',' +',' +',' +',' +'} , {' +',' 4', ' +',' 5',' +',' 6',' +'} , {' +',' +',' +',' +',' +',' +',' +'}, {' +',' 7',' +',' 8',' +',' 0',' +'}, {' +',' +',' +',' +',' +',' +',' +'} }; } // 数据来源 } i希望将此代码呈现给C#的初学者,他们也可以用C#轻松制作游戏他们认为C和C ++比C#更好,他们不相信C#更好更容易解决方案 这取决于你如何定义简单,这与定义极为不同。让我们专注于如何使代码更好。审核您的代码需要花费太多时间,特别是没有关于它应该做什么的信息;并且转换后的代码几乎没有一行来自你的文字;所以它应该是重构的,充其量。 所以,我只会告诉你几个明显的步骤。 首先,删除所有立即常量。我是认真的。你所有的3,5,6,7。不是很明显吗?想象一下,您将需要使用一些阵列10x10而不是7x7(仅作为示例)。但是7在很多地方。你怎么能全部替换它们?此外,成像你有两个不同的七人组扮演不同的角色,因此只应改变其中一个。 0和1怎么样?有时您应该按原样使用它们(如果 counter == 0)或 sIndex = aIndex + 1 ;但有些情况下可以更改0或1。 将它们全部转换为显式定义的常量,带有名称。在其他地方,它们将作为功能参数出现。拥有名称将使您的代码清晰。不要像你一样使用单字母名称:i,j。所有名称都应正确拼写英文名称,尤其是那些具有广泛背景的名称。我甚至要解释原因吗? 并删除所有重复的代码。如果你有类似的循环在不同的地方做类似的工作,它应该是一个带参数的单独函数。编程是所有代码只编写一次的东西。这是一个至关重要的原则: http://en.wikipedia.org/wiki/Don %27t_repeat_yourself [ ^ ]。 如果你做得好,你的代码会发生巨大变化。它将变得清晰易懂,易于维护。 -SA 这不是一个问题:我们不会为你工作。 如果你想让别人写你的代码,你必须付钱 - 我建议你去VWorker.com并在那里问。 但要注意:你得到了你付出的代价。支付花生,买猴子。 hello peoplekeep up the good jobi'll be joining u'll soonusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;namespace Shuffle{ public class Game { public static uint MoveCount = 0; bool IsPass() { for (int i = 0; i < 7; i++) { for (int j = 0; j < 7; j++) { if (GenerateArray.a[i, j] != Resource.arr1[i, j]) return false; } } return true; }//Whether or not the game void PlayGame() { int row = 0, col = 0; GenerateArray objOfGen = new GenerateArray(); objOfGen.UpdateArray();//Update Game Record.DisplayRecordInfor(); objOfGen.DisplayArray();//Show the game screen objOfGen.Position(out row, out col);//Positioning the movable grid coordinates Console.Write("Enter your Move [U to Up,D to Down,L to Left,R to Right]"); ConsoleKeyInfo cki = new ConsoleKeyInfo(); while (cki.Key != ConsoleKey.Escape)//Press Esc to exit { cki = Console.ReadKey(true); Console.Clear(); switch (cki.Key) { case ConsoleKey.D: case ConsoleKey.DownArrow: if (row < 2) { objOfGen.Swap(row, col, row, col); } else { objOfGen.Swap(row, col, row - 2, col); row -= 2; ++MoveCount; } Record.DisplayRecordInfor(); objOfGen.DisplayArray(); break; case ConsoleKey.U: case ConsoleKey.UpArrow: if (row > 3) { objOfGen.Swap(row, col, row, col); } else { objOfGen.Swap(row, col, row + 2, col); row += 2; ++MoveCount; } Record.DisplayRecordInfor(); objOfGen.DisplayArray(); break; case ConsoleKey.R: case ConsoleKey.RightArrow: if (col < 2) { objOfGen.Swap(row, col, row, col); } else { objOfGen.Swap(row, col, row, col - 2); col -= 2; ++MoveCount; } Record.DisplayRecordInfor(); objOfGen.DisplayArray(); break; case ConsoleKey.L: case ConsoleKey.LeftArrow: if (col > 3) { objOfGen.Swap(row, col, row, col); } else { objOfGen.Swap(row, col, row, col + 2); col += 2; ++MoveCount; } Record.DisplayRecordInfor(); objOfGen.DisplayArray(); break; default: Console.Clear(); Record.DisplayRecordInfor(); objOfGen.DisplayArray(); break; } Console.WriteLine("Enter your Move [U to Up,D to Down,L to Left,R to Right]"); } if (IsPass()) { Console.WriteLine("You won the game!"); } }//Play public void EndGame() { }//End game public static void Main(string[] args) { Game objOfGame = new Game(); objOfGame.PlayGame(); } }//Game public class Record { public static void DisplayRecordInfor() { Console.WriteLine("***---The Shuffle Game---***"); Console.WriteLine("Crurrent Key Presses:" + Game.MoveCount); }//Information Display records public void Updatecount() { Console.WriteLine("***---The Shuffle Game---***"); Console.WriteLine("Crurrent Key Presses:" + Game.MoveCount); }//Updates number of keys pressed }//Record class public class GenerateArray { public static char[,] a = new char[7, 7]; public GenerateArray() { for (int i = 0; i < 7; i++) { for (int j = 0; j < 7; j++) { a[i, j] = Resource.arr1[i, j]; } } }//Initialize the array public void UpdateArray() { Random RandomNext = new Random(); char[] temp = new char[9]; char ch = Convert.ToChar(RandomNext.Next(0, 9) + '0'); for (int i = 0; i < 9; i++) { temp[i] = ch; } for (int i = 0; i < 8; ) { char RandomCh = Convert.ToChar(RandomNext.Next(0, 9) + '0'); for (int j = 0; j <= i; j++) { if (temp[j] == RandomCh) break; else if (i == j) { temp[i + 1] = RandomCh; i++; break; } } } a[1, 1] = temp[1]; a[1, 3] = temp[2]; a[1, 5] = temp[3]; a[3, 1] = temp[4]; a[3, 3] = temp[5]; a[3, 5] = temp[6]; a[5, 1] = temp[7]; a[5, 3] = temp[8]; a[5, 5] = temp[0]; }//Update array (array generates random) public void Swap(int row1, int col1, int row2, int col2) { char temp = a[row1, col1]; a[row1, col1] = a[row2, col2]; a[row2, col2] = temp; }//Changing positions public void DisplayArray() { for (int i = 0; i < 7; i++) { for (int j = 0; j < 7; j++) { Console.Write(a[i, j]); } Console.WriteLine(); } }//Display Array public void Position(out int row, out int col) { row = col = 0; for (int i = 0; i < 7; i++) { for (int j = 0; j < 7; j++) { if (a[i, j] == '0') { row = i; col = j; return; } } } } }//Locate & Manipulate data public class Resource { public static char[,] arr1 ={ {'+','+','+','+','+','+','+'}, {'+','1','+','2','+','3','+'}, {'+','+','+','+','+','+','+'}, {'+','4','+','5','+','6','+'}, {'+','+','+','+','+','+','+'}, {'+','7','+','8','+','0','+'}, {'+','+','+','+','+','+','+'} }; }//Data Sources}i want to present this code to some beginner students of C# that they can also make easy games with C# they think C and C++ are better than C# they dont believe that C# is much better and easier 解决方案 It depends on how would you define "simple", which is extremely different to define. Let's focus on how to make the code better. It would take too much time to review your code, especially having no information from you on what it should do; and the transformed code will hardly have a single line from your text; so it should be heavily refactored, at best.So, I'll only tell you just few apparent steps.First of all, remove all immediate constants. I mean it. All your 3, 5, 6, 7. Isn't it obvious? Imagine you will need to use some array 10x10 instead 7x7 (just for example). But 7 is in many places. How can you replace them all? Moreover, imaging you have two different sevens playing different roles, so only one of them should be changed. What about 0 and 1? Sometimes you should use them as is as in (if counter == 0) or sIndex = aIndex + 1; but there are cases when 0 or 1 could be changed.Turn them all into explicitly defined constants, with names. In other places, they will come as function parameters. Having names will make your code clear. Don't use single-letter name as you do: i, j. All the names should be correctly spelled English names, especially those with wide context. Do I even have to explain why?And remove all repeated code. If you have similar loop doing analogous job in different places, it should be a separate function with parameters. Programming is something where all pieces of code are written only once. This is a critically important principle:http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^].If you do it properly, your code will change dramatically. It will become clearly understandable and easy to maintain.—SAThis isn't a question: we do not do your work for you.If you want someone to write your code, you have to pay - I suggest you go to VWorker.com and ask there.But be aware: you get what you pay for. Pay peanuts, get monkeys. 这篇关于如何使此代码尽可能简单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 14:13