问题描述
我为随机游戏创建了以下代码
它是一个随机显示的随机数字游戏
用户必须将数字放入序列中最小的击键次数,并且在用户按顺序显示之后,应显示消息,您已赢得游戏
,如果用户的击键次数少于记录,则应显示另一条消息,祝贺您已破坏应该询问记录和用户名
但是当我按顺序放置网格时它没有显示你赢得游戏的消息
帮我实现条件
代码为
i have created the following code for the shuffle game
its a game of shuffling numbers that are randomly displayed
the user have to put the numbers in the sequence with minimum keystrokes and after the user put it in sequence the message should be displayed you have won the game
and if the user have keystrokes less than the record then the another message should be displayed that congratulation u have broken the record and the user name should be asked
but when i put the grid in sequence its not showing the message that you have won the game
help me implement the condition
the code is as
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace Shuffle { public class Game { public static uint MoveCount=0; void DispGameStatus() { new Record().ReadBestRcord(Resource.PathOfSimple); }// displays the current game status 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(); #region 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; } #endregion Console.WriteLine("Enter your Move [U to Up,D to Down,L to Left,R to Right]"); } #region if (IsPass()) { Console.WriteLine("You won the game!"); FileStream fs = new FileStream(Resource.PathOfSimple,FileMode.OpenOrCreate,FileAccess.ReadWrite); StreamReader r = new StreamReader(fs); Record objOfRecord=new Record(); if (r.ReadLine() == null || objOfRecord.IsBreak(Resource.PathOfSimple))//If the game is the first player to start or break the record, then update records { r.Close(); fs.Close(); Console.WriteLine("Congratulations!You have broken the record."); Console.WriteLine("Now you are the keeper of the best record!"); Console.WriteLine("Please enter your name !"); objOfRecord.UpdateBestRecord(Resource.PathOfSimple); } r.Close(); fs.Close(); } #endregion }//Play void EndGame() { //To be improved }//End game public static void Main(string[] args) { Game objOfGame = new Game(); objOfGame.DispGameStatus(); objOfGame.PlayGame(); } }//Game public class Record { static string UserName; static uint KeyStrokes; public static void DisplayRecordInfor() { Console.WriteLine("Record Holder"+UserName); Console.WriteLine("Key Presses:" +KeyStrokes); Console.WriteLine("Crurrent Key Presses:" + Game.MoveCount); }//Information Display records public void ReadBestRcord(string fileName) { if (File.Exists(fileName)) { FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); StreamReader r = new StreamReader(fs); string str = r.ReadLine(); if (str != null) { Console.WriteLine(UserName=r.ReadLine()); Console.WriteLine(str); KeyStrokes = Convert.ToUInt32(str.Substring(str.IndexOf(':') + 1)); } r.Close(); fs.Close(); } }//Read Best Record public void UpdateBestRecord(string fileName) { FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite); StreamWriter w = new StreamWriter(fs); KeyStrokes = Game.MoveCount; UserName = Console.ReadLine(); w.WriteLine("Key Presses:"+KeyStrokes); w.WriteLine("Record Holder:" + UserName); Console.WriteLine("Crurrent Key Presses:" + Game.MoveCount); w.Flush(); w.Close(); fs.Close(); }//Update Best Record public bool IsBreak(string fileName) { if (KeyStrokes > Game.MoveCount) { return true; } else { return false; } } //Break the record to determine whether }//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[0]; a[1, 3] = temp[1]; a[1,5] = temp[2]; a[3, 1] = temp[3]; a[3, 3] = temp[4]; a[3,5] = temp[5]; a[5, 1] = temp[6]; a[5,3] = temp[7]; a[5,5] = temp[8]; }//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 const string PathOfSimple = "D:SRecord.txt"; const string PathOfNormal = "D:NRecord.txt"; const string PathOfHard = "D:NHard.txt"; public static char[,] arr1 ={ {'+','+','+','+','+','+','+'}, {'+','1','+','2','+','3','+'}, {'+','+','+','+','+','+','+'}, {'+','4','+','5','+','6','+'}, {'+','+','+','+','+','+','+'}, {'+','7','+','8','+','0','+'}, {'+','+','+','+','+','+','+'} }; }//Data Sources }
对不起英语
sorry for bad english
推荐答案
if (IsPass())
当你完成游戏时,它应该去那里吧?现在,当它发生时,应用程序将停止,您可以开始查看它正在做什么。执行指令,看看它的去向。如果,它是否进入?如果是这样,那么好,继续前进,看看它没有达到预期的效果。
如果没有,那么为什么不呢?再次运行它,这一次进入并通过 IsPass 方法 - 它在做什么?为什么?在你跨过它之前尝试找出一条线应该做什么,然后比较它做了什么以及你认为它应该做什么。如果它不匹配,请开始查看它不匹配的原因。
如果不出意外,它会让您更好地了解代码的位置比你现在失败了!
When you finish the game, it should go there, right? And now, when it does, the application will stop and you can start looking at what it is doing any why. Step the instruction, and see where it goes. Was it into the if? if so, then good, keep going and see where it doesn't do what you expect.
If it doesn't, then why not? run it again, and this time step your way into and through the IsPass method - what is it doing? Why? try to work out what a line should do before you step over it and compare what it did do with what you think it should. If it doesn't match, start looking at why it doesn't match.
If nothing else, it'll give you a better idea of where your code is failing than you have at the moment!
这篇关于如何显示...读入描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!