TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Raj Chauhan
NA
1
778
i am not getting the proper output that i should get
Jul 1 2014 7:36 AM
hi people i am new here
i have written a code for shuffle game in C# console to shuffle numbers and get them in the right sequence
but as i shuffled the numbers in the right sequence i am not getting the message that "you have won" or the message that is written in the code
you may check the code
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
}
this code displays the numbers that are randomly arranged and the user has to solve this
but when it is solved it does not show the message that you have won or if the user has used minimum keystrokes then users name should be asked but its not asking help me out please
Reply
Answers (
0
)
FORMAT DATETIME IN LISTVIEW
C#6.0