so as the title says my C# code is not running/debugging even though i have 0 errors being shown.
I begin the debugging and all that happens is the console screen flashes quickly then exits with 0 errors. Even the .exe in the bin\debug folder does the exact same. All i'm receiving is a wall of text in the output section.
Algorithms.vshost.exe' (CLR v4.0.30319: Algorithms.vshost.exe): Loaded C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Accessibility\v4.0_4.0.0.0__b03f5f7f11d50a3a\Accessibility.dll. Symbols loaded.
The thread 0x12b8 has exited with code 0 (0x0).
The thread 0x289c has exited with code 0 (0x0).
The program [15012] Algorithms.vshost.exe has exited with code 0 (0x0).
I hope this is understandable enough! I would appreciate any help, thanks!
Code as requested by some! I hope it's helpful! I appreciate all the answers and help so far!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AlgorithmsResit
{
class Program
{
private static void Main(string[] args)
{ }
public static void AlgorithmSortInt(int[] array)
{
int j = array.Length - 1;
int x, i, temp;
for (x = 1; x <= j; ++x)
{
temp = array[x];
for (i = x - 1; i >= 0; --i)
{
if (temp < array[i]) array[i + 1] = array[i];
else break;
}
array[1 + 1] = temp;
}
}
public static void AlgorithmSortDouble(double[] array)
{
int j = array.Length - 1;
int x, i;
double temp;
for (x = 1; x <= j; ++x)
{
temp = array[x];
for (i = x - 1; i >= 0; --i)
{
if (temp < array[i]) array[i + 1] = array[i];
else break;
}
array[i + 1] = temp;
}
}
public static void AlgorithmDateTime(DateTime[] array)
{
int j = array.Length - 1;
int x, i;
DateTime temp;
for (x = 1; x <= j; ++x)
{
temp = array[x];
for (i = x - 1; i >= 0; --i)
{
if (temp < array[i]) array[i + 1] = array[i];
else break;
}
array[i + 1] = temp;
}
}
public static void StringToSort(string[] array)
{
int x = array.Length - 1;
for (int j = 0; j < x; j++)
{
for (int i = x; i > j; i--)
{
if (((IComparable)array[i - 1]).CompareTo(array[i]) > 0)
{
var temp = array[i - 1];
array[i - 1] = array[i];
array[i] = temp;
}
}
}
}
static void main(string[] args)
{
string[] Day1 = System.IO.File.ReadAllLines(#"TextFiles/Day_1.txt");
List<int> Day1List = new List<int>();
foreach (string Day in Day1)
{
int Days = Convert.ToInt32(Day);
Day1List.Add(Days);
}
int[] Day1Arr = Day1List.ToArray();
string[] Depth1 = System.IO.File.ReadAllLines(#"TextFiles/Depth_1.txt");
List<double> Depth1List = new List<double>();
foreach (string Depth in Depth1)
{
double Depths = Convert.ToDouble(Depth);
Depth1List.Add(Depths);
}
double[] Depth1Arr = Depth1List.ToArray();
string[] IRISID1 = System.IO.File.ReadAllLines(#"TextFiles/IRIS_ID_1.txt");
List<int> Iris1List = new List<int>();
foreach (string IRIS in IRISID1)
{
int Iris = Convert.ToInt32(IRIS);
Iris1List.Add(Iris);
}
int[] Iris1Arr = Iris1List.ToArray();
string[] Latitude1 = System.IO.File.ReadAllLines(#"TextFiles/Latitude_1.txt");
List<double> Latitude1List = new List<double>();
foreach (string Lat in Latitude1)
{
double Latitude = Convert.ToDouble(Latitude1);
Latitude1List.Add(Latitude);
}
double[] Latitude1Arr = Latitude1List.ToArray();
string[] Longitude1 = System.IO.File.ReadAllLines(#"TextFiles/Longitude_1.txt");
List<double> Longitude1List = new List<double>();
foreach (string Longitude in Longitude1)
{
double Longitudes = Convert.ToDouble(Longitude1);
Longitude1List.Add(Longitudes);
}
double[] Longitude1Arr = Longitude1List.ToArray();
string[] Magnitude1 = System.IO.File.ReadAllLines(#"TextFiles/Magnitude_1.txt");
List<Double> Magnitude1List = new List<Double>();
foreach (string Magnitude in Magnitude1)
{
double Magnitudes = Convert.ToDouble(Magnitude1);
Magnitude1List.Add(Magnitudes);
}
double[] Magnitude1Arr = Magnitude1List.ToArray();
string[] Month1 = System.IO.File.ReadAllLines(#"TextFiles/Month_1.txt");
string[] Region1 = System.IO.File.ReadAllLines(#"TextFiles/Region_1.txt");
string[] Time1 = System.IO.File.ReadAllLines(#"TextFiles/Time_1.txt");
List<DateTime> Time1List = new List<DateTime>();
foreach (string Time in Time1)
{
DateTime Times = Convert.ToDateTime(Time);
Time1List.Add(Times);
}
DateTime[] Time1Arr = Time1List.ToArray();
string[] Timestamp1 = System.IO.File.ReadAllLines(#"TextFiles/Timestamp_1.txt");
List<int> Timestamp1List = new List<int>();
foreach (string Timestamp in Timestamp1)
{
int Timestamps = Convert.ToInt32(Timestamp);
Timestamp1List.Add(Timestamps);
}
int[] Timestamp1Arr = Timestamp1List.ToArray();
string[] Year1 = System.IO.File.ReadAllLines(#"TextFiles/Year_1.txt");
List<int> Year1List = new List<int>();
foreach (String Date in Year1)
{
int Dates = Convert.ToInt32(Date);
Year1List.Add(Dates);
}
int[] Year1Arr = Year1List.ToArray();
string UserArrayChoice, AscOrDescChoice;
int ArrayChoice;
int count = 0;
do
{
count++;
Console.Write("{0} ", Day1Arr[count]);
Console.Write("{0} ", Depth1[count]);
Console.Write("{0} ", IRISID1[count]);
Console.Write("{0} ", Latitude1Arr[count]);
Console.Write("{0} ", Longitude1Arr[count]);
Console.Write("{0} ", Magnitude1Arr[count]);
Console.Write("{0} ", Month1[count]);
Console.Write("{0} ", Region1[count]);
Console.Write("{0} ", Time1Arr[count]);
Console.Write("{0} ", Timestamp1Arr[count]);
Console.Write("{0}", Year1Arr[count]);
Console.Write("\n");
} while (count < Year1Arr.Length - 1);
Console.WriteLine("Enter the number of the file you would like to sort... \n1 ) Day_1.txt\n2 ) Depth_1.txt\n3 ) IRIS_ID_.txt\n4 ) Latitude_1.txt\n5 ) Longitude_1.txt\n6 ) Magnitude_1.txt\n7 ) Month_1.txt\n8 ) Region_1.txt\n9 ) Time_1.txt\n10 ) Timestamp_1.txt\n11 ) Year_1.txt\n12");
UserArrayChoice = Console.ReadLine();
ArrayChoice = Convert.ToInt32(UserArrayChoice);
switch (UserArrayChoice)
{
case "1":
UserArrayChoice = "Day_1.txt";
break;
case "2":
UserArrayChoice = "Depth_1.txt";
break;
case "3":
UserArrayChoice = "IRIS_ID_1.txt";
break;
case "4":
UserArrayChoice = "Latitude_1.txt";
break;
case "5":
UserArrayChoice = "Longitude_1.txt";
break;
case "6":
UserArrayChoice = "Magnitude_1.txt";
break;
case "7":
UserArrayChoice = "Month_1.txt";
break;
case "8":
UserArrayChoice = "Region_1.txt";
break;
case "9":
UserArrayChoice = "Time_1.txt";
break;
case "10":
UserArrayChoice = "Timestamp_1.txt";
break;
case "11":
UserArrayChoice = "Year_1.txt";
break;
default:
break;
}
Console.WriteLine("---------------------------------------------------------------------------");
Console.WriteLine("{0} Has been selected, would you like to sort by Ascending or Descending? ", UserArrayChoice);
Console.WriteLine("---------------------------------------------------------------------------");
AscOrDescChoice = Console.ReadLine();
if (AscOrDescChoice == "Ascending" | AscOrDescChoice == "ascending")
{
Console.WriteLine("---------------------------------");
Console.WriteLine("{0} Will sort in Ascending order!", UserArrayChoice);
Console.WriteLine("---------------------------------");
switch (ArrayChoice)
{
case 1:
AlgorithmSortInt(Day1Arr);
foreach (int temp in Day1Arr)
{
Console.WriteLine("{0} ", temp);
}
break;
case 2:
AlgorithmSortDouble(Depth1Arr);
foreach (double temp in Depth1Arr)
{
Console.WriteLine("{0}", temp);
}
break;
case 3:
AlgorithmSortInt(Iris1Arr);
foreach (int temp in Iris1Arr)
{
Console.WriteLine("{0}", temp);
}
break;
case 4:
AlgorithmSortDouble(Latitude1Arr);
foreach (double temp in Latitude1Arr)
{
Console.WriteLine("{0}", temp);
}
break;
case 5:
AlgorithmSortDouble(Longitude1Arr);
foreach (double temp in Longitude1Arr)
{
Console.WriteLine("{0}", temp);
}
break;
case 6:
AlgorithmSortDouble(Magnitude1Arr);
foreach (double temp in Magnitude1Arr)
{
Console.WriteLine("{0}", temp);
}
break;
case 7:
StringToSort(Month1);
foreach (string temp in Month1)
{
Console.WriteLine("{0}", temp);
}
break;
case 8:
StringToSort(Region1);
foreach (string temp in Region1)
{
Console.WriteLine("{0}", temp);
}
break;
case 9:
AlgorithmDateTime(Time1Arr);
foreach (DateTime temp in Time1Arr)
{
Console.WriteLine("{0}", temp);
}
break;
case 10:
AlgorithmSortInt(Timestamp1Arr);
foreach (int temp in Timestamp1Arr)
{
Console.WriteLine("{0}", temp);
}
break;
case 11:
AlgorithmSortInt(Year1Arr);
foreach (int temp in Year1Arr)
{
Console.WriteLine("{0}", temp);
}
break;
}
}
else if (AscOrDescChoice == "Descending" | AscOrDescChoice == "descending")
{
Console.WriteLine("----------------------------------");
Console.WriteLine("{0} Will sort in Descending order!", UserArrayChoice);
Console.WriteLine("----------------------------------");
switch (ArrayChoice)
{
case 1:
AlgorithmSortInt(Day1Arr);
Array.Reverse(Day1Arr);
foreach (int temp in Day1Arr)
{
Console.WriteLine("{0} ", temp);
}
break;
case 2:
AlgorithmSortDouble(Depth1Arr);
Array.Reverse(Depth1Arr);
foreach (double temp in Depth1Arr)
{
Console.WriteLine("{0}", temp);
}
break;
case 3:
AlgorithmSortInt(Iris1Arr);
Array.Reverse(Iris1Arr);
foreach (int temp in Iris1Arr)
{
Console.WriteLine("{0}", temp);
}
break;
case 4:
AlgorithmSortDouble(Latitude1Arr);
Array.Reverse(Latitude1Arr);
foreach (double temp in Latitude1Arr)
{
Console.WriteLine("{0}", temp);
}
break;
case 5:
AlgorithmSortDouble(Longitude1Arr);
Array.Reverse(Longitude1Arr);
foreach (double temp in Longitude1Arr)
{
Console.WriteLine("{0}", temp);
}
break;
case 6:
AlgorithmSortDouble(Magnitude1Arr);
Array.Reverse(Magnitude1Arr);
foreach (double temp in Magnitude1Arr)
{
Console.WriteLine("{0}", temp);
}
break;
case 7:
StringToSort(Month1);
Array.Reverse(Month1);
foreach (string temp in Month1)
{
Console.WriteLine("{0}", temp);
}
break;
case 8:
StringToSort(Region1);
Array.Reverse(Region1);
foreach (string temp in Region1)
{
Console.WriteLine("{0}", temp);
}
break;
case 9:
AlgorithmDateTime(Time1Arr);
Array.Reverse(Time1Arr);
foreach (DateTime temp in Time1Arr)
{
Console.WriteLine("{0}", temp);
}
break;
case 10:
AlgorithmSortInt(Timestamp1Arr);
Array.Reverse(Timestamp1Arr);
foreach (int temp in Timestamp1Arr)
{
Console.WriteLine("{0}", temp);
}
break;
case 11:
AlgorithmSortInt(Year1Arr);
Array.Reverse(Year1Arr);
foreach (int temp in Year1Arr)
{
Console.WriteLine("{0}", temp);
}
break;
}
}
else
{
Console.WriteLine("-------------------");
Console.WriteLine("Incorrect Response!");
Console.WriteLine("-------------------");
}
Console.WriteLine("Enter the number of the file you would like to search... \n1 ) Day_1.txt\n2 ) Depth_1.txt\n3 ) IRIS_ID_1\n4 ) Latitude_1\n5 ) Longitude_1\n6 ) Magnitude_1\n7 ) Month_1\n8 ) Region_1\n9 ) Time_1\n10 ) Timestamp_1\n11 ) Year_1\n12");
UserArrayChoice = Console.ReadLine();
ArrayChoice = Convert.ToInt32(UserArrayChoice);
switch (UserArrayChoice)
{
case "1":
UserArrayChoice = "Day_1.txt";
break;
case "2":
UserArrayChoice = "Depth_1.txt";
break;
case "3":
UserArrayChoice = "IRIS_ID_1.txt";
break;
case "4":
UserArrayChoice = "Latitude_1.txt";
break;
case "5":
UserArrayChoice = "Longitude_1.txt";
break;
case "6":
UserArrayChoice = "Magnitude_1.txt";
break;
case "7":
UserArrayChoice = "Month_1.txt";
break;
case "8":
UserArrayChoice = "Region_1.txt";
break;
case "9":
UserArrayChoice = "Time_1.txt";
break;
case "10":
UserArrayChoice = "Timestamp_1.txt";
break;
case "11":
UserArrayChoice = "Year_1.txt";
break;
default:
break;
}
Console.WriteLine("--------------------------------------------------------------------");
Console.WriteLine("{0} has been selected! Please enter what you would like to search - ", UserArrayChoice);
Console.WriteLine("--------------------------------------------------------------------");
string SearchTemp = Console.ReadLine();
bool Found = false;
int counter = 0;
switch (ArrayChoice)
{
case 1:
int SearchDay = Convert.ToInt32(SearchTemp);
for (int x = 0; x < Day1Arr.Length; x++)
{
if (SearchDay == Day1Arr[x])
{
Found = true;
}
}
if (Found == true) { Console.WriteLine("Successful! :-)"); }
else { Console.WriteLine("Unsuccessful! Sorry :-("); }
break;
case 2:
double DepthSearch = Convert.ToDouble(SearchTemp);
for (int x = 0; x < Depth1Arr.Length; x++)
{
if (DepthSearch == Depth1Arr[x])
{
Found = true;
}
}
if (Found == true) { Console.WriteLine("Successful!:-)"); }
else { Console.WriteLine("Unsuccessful! Sorry :-("); }
break;
case 3:
int SearchIris = Convert.ToInt32(SearchTemp);
for (int x = 0; x < Iris1Arr.Length; x++)
{
if (SearchIris == Iris1Arr[x])
{
Found = true;
}
}
if (Found == true) { Console.WriteLine("Successful! :-)"); }
else { Console.WriteLine("Unsuccessful! Sorry :-("); }
break;
case 4:
double SearchLatitude = Convert.ToDouble(SearchTemp);
for (int x = 0; x < Latitude1Arr.Length; x++)
{
if (SearchLatitude == Latitude1Arr[x])
{
Found = true;
}
}
if (Found == true) { Console.WriteLine("Successful! :-)"); }
else { Console.WriteLine("Unsuccessful! Sorry :-("); }
break;
case 5:
double SearchLong = Convert.ToDouble(SearchTemp);
for (int x = 0; x < Longitude1Arr.Length; x++)
{
if (SearchLong == Longitude1Arr[x])
{
Found = true;
}
}
if (Found == true) { Console.WriteLine("Successful! :-)"); }
else { Console.WriteLine("Unsuccessful! Sorry :-("); }
break;
case 6:
double SearchMag = Convert.ToDouble(SearchTemp);
for (int x = 0; x < Magnitude1Arr.Length; x++)
{
if (SearchMag == Magnitude1Arr[x])
{
Found = true;
}
}
if (Found == true) { Console.WriteLine("Successful! :-)"); }
else { Console.WriteLine("Unsuccessful! Sorry :-("); }
break;
case 7:
foreach (var Months in Month1)
{
if (Months.Contains(SearchTemp))
{
count++;
Found = true;
Console.Write("{0} ", Day1Arr[counter]);
Console.Write("{0} ", Depth1Arr[counter]);
Console.Write("{0} ", Iris1Arr[counter]);
Console.Write("{0} ", Latitude1Arr[counter]);
Console.Write("{0} ", Longitude1Arr[counter]);
Console.Write("{0} ", Magnitude1Arr[counter]);
Console.Write("{0} ", Month1[counter]);
Console.Write("{0} ", Region1[counter]);
Console.Write("{0} ", Time1Arr[counter]);
Console.Write("{0} ", Timestamp1Arr[counter]);
Console.Write("{0} ", Year1Arr[counter]);
Console.Write("\n");
}
}
if (Found == true) { Console.WriteLine("Successful! :-)"); }
else { Console.WriteLine("Unsuccessful! Sorry :-("); }
break;
case 8:
foreach (var Regions in Region1)
{
if (Regions.Contains(SearchTemp))
{
Found = true;
}
}
if (Found == true) { Console.WriteLine("Successful! :-)"); }
else { Console.WriteLine("Unsuccessful! Sorry :-("); }
break;
case 9:
DateTime SearchDate = Convert.ToDateTime(SearchTemp);
for (int x = 0; x < Time1Arr.Length; x++)
{
if (SearchDate == Time1Arr[x])
{
Found = true;
}
}
if (Found == true) { Console.WriteLine("Successful! :-)"); }
else { Console.WriteLine("Unsuccessful! Sorry :-("); }
break;
case 10:
int SearchTimestamp = Convert.ToInt32(SearchTemp);
for (int x = 0; x < Timestamp1Arr.Length; x++)
{
if (SearchTimestamp == Timestamp1Arr[x])
{
Found = true;
}
}
if (Found == true) { Console.WriteLine("Successful! :-)"); }
else { Console.WriteLine("Unsuccessful! Sorry :-("); }
break;
case 11:
int SearchYear = Convert.ToInt32(SearchTemp);
for (int x = 0; x < Year1Arr.Length; x++)
{
{
Found = true;
}
}
if (Found == true) { Console.WriteLine("Successful! :-)"); }
else { Console.WriteLine("Unsuccessful! Sorry :-("); }
break;
}
}
}
}
What I know that it is just debugging message. You can switch that off by right clicking into the output window and uncheck the thread ended message.
http://msdn.microsoft.com/en-us/library/bs4c1wda.aspx
(1)Please add a breakpoint in your app, and then debug it again.
Like this thread:
Keeping console window open when debugging.
(2)If you run it with “start without debugging”, it would not be closed. You could also add Console.ReadLine()in your app like this thread.
(3)If you just debug your app and get this messages, please also enable the Exceptions settings under Debugger->Windows menu, and make sure that no Exception.
Related
I am trying to program a game called NIM (https://plus.maths.org/content/play-win-nim). The subroutine 'ComputerMove();' does not execute for some reason, why is that? I am yet to finish but it is the ComputerMove subroutine that I am confused with. Apologies in advance if I am missing something obvious.
(Edit: There is no error message, just a blank console)
using System;
using System.Threading;
namespace THE_NIM
{
class Program
{
static char[,] Board = new char[7, 12];
static string underline = "\x1B[4m";
static string reset = "\x1B[0m";
static bool GameStatus = false;
static string firstMove;
static void Introduction()
{
Console.WriteLine("\t\t\t\t\t" + underline + "Welcome to NIM!\n" + reset);
Thread.Sleep(1000);
Console.WriteLine(underline + "The rules are as follows:\n" + reset);
Thread.Sleep(1000);
Console.WriteLine(" - Each player takes their turn to remove a certain number of 'blocks' from a stack, of which there are 7.");
Console.WriteLine(" - This happens until there is only 1 'block' remaining. With the winner being the one to remove the last 'block'.\n");
Thread.Sleep(1500);
}
static void PrintBoard()
{
Console.WriteLine();
Console.WriteLine();
for (int i = 0; i <= 6; i++)
{
Console.Write(" ");
for (int j = 0; j <= 11; j++)
{
Console.Write(Board[i, j]);
}
Console.WriteLine();
Console.WriteLine();
}
}
static void WhoGoesFirst()
{
string[] PossibleChoices = { "Computer", "You" };
Random WhoGoesFirst = new Random();
int WGFIndex = WhoGoesFirst.Next(0, 2);
firstMove = PossibleChoices[WGFIndex];
Console.WriteLine("Randomly selecting who goes first...");
Thread.Sleep(1000);
Console.WriteLine("{0} will go first!\n", firstMove);
Thread.Sleep(1000);
}
static void PlayerMove()
{
Console.Write("Which stack do you wish to remove from?: ");
int PlayerStackSelection = Convert.ToInt32(Console.ReadLine());
switch (PlayerStackSelection)
{
case(1):
PlayerStackSelection = 0;
break;
case (2):
PlayerStackSelection = 2;
break;
case (3):
PlayerStackSelection = 4;
break;
case (4):
PlayerStackSelection = 6;
break;
case (5):
PlayerStackSelection = 8;
break;
case (6):
PlayerStackSelection = 10;
break;
}
Console.Write("How many blocks do you wish to remove?: ");
int PlayerRemovedSelection = Convert.ToInt32(Console.ReadLine());
int spaces = 0;
int nearestBlock = 0;
for (int i = 0; i < 7; i++)
{
if (Board[i, PlayerStackSelection] == ' ')
{
spaces += 1;
}
}
for (int i = 0; i < 7; i++)
{
if (Board[i, PlayerStackSelection] == '█')
{
nearestBlock = i;
break;
}
}
if (spaces != 7)
{
for (int i = nearestBlock; i < (nearestBlock + PlayerRemovedSelection); i++)
{
Board[i, PlayerStackSelection] = ' ';
}
}
PrintBoard();
CheckIfBoardEmpty();
}
static void ComputerMove()
{
Random CompStack = new Random();
int CompStackSelection = CompStack.Next(1, 7);
int PreChangeStack = CompStackSelection;
switch (CompStackSelection)
{
case (1):
CompStackSelection = 0;
break;
case (2):
CompStackSelection = 2;
break;
case (3):
CompStackSelection = 4;
break;
case (4):
CompStackSelection = 6;
break;
case (5):
CompStackSelection = 8;
break;
case (6):
CompStackSelection = 10;
break;
}
Random CompRemoved = new Random();
int CompRemovedSelection = CompRemoved.Next(1, 7);
int spaces = 0;
int nearestBlock = 0;
for (int i = 0; i < 7; i++)
{
if (Board[i, CompStackSelection] == ' ')
{
spaces += 1;
}
}
while (spaces == 7)
{
CompRemovedSelection = CompRemoved.Next(1, 7);
CompStackSelection = CompStack.Next(1, 7);
switch (CompStackSelection)
{
case (1):
CompStackSelection = 0;
break;
case (2):
CompStackSelection = 2;
break;
case (3):
CompStackSelection = 4;
break;
case (4):
CompStackSelection = 6;
break;
case (5):
CompStackSelection = 8;
break;
case (6):
CompStackSelection = 10;
break;
}
for (int i = 0; i < 7; i++)
{
if (Board[i, CompStackSelection] == ' ')
{
spaces += 1;
}
}
}
for (int i = 0; i < 7; i++)
{
if (Board[i, CompStackSelection] == '█')
{
nearestBlock = i;
break;
}
}
if ((nearestBlock + CompRemovedSelection) !< 8)
{
while ((nearestBlock + CompRemovedSelection) !< 8)
{
CompRemovedSelection = CompRemoved.Next(1, 7);
}
}
if (spaces != 7)
{
for (int i = nearestBlock; i < (nearestBlock + CompRemovedSelection); i++)
{
Board[i, CompStackSelection] = ' ';
}
}
Console.WriteLine("Computer is making its move... ");
Thread.Sleep(1000);
Console.WriteLine("Computer has decided to remove {0} blocks from stack number {1}.\n", CompRemovedSelection, PreChangeStack);
Thread.Sleep(1000);
PrintBoard();
CheckIfBoardEmpty();
}
static void CheckIfBoardEmpty()
{
bool keepGoing = false;
for (int i = 0; i <= 6; i++)
{
for (int j = 0; j <= 11; j++)
{
if (Board[i,j] == '█')
{
GameStatus = false;
keepGoing = true;
break;
}
}
}
if (keepGoing != true)
{
Console.Write("BOARD EMPTY");
GameStatus = true;
}
}
static void Main(string[] args)
{
Introduction();
WhoGoesFirst();
for (int i = 0; i <= 6; i++)
{
for (int j = 0; j <= 11; j++)
{
if (j % 2 == 0)
{
Board[i, j] = '█';
}
else
{
Board[i, j] = ' ';
}
}
}
if (firstMove == "You")
{
while (GameStatus == false)
{
PlayerMove();
ComputerMove();
}
}
else
{
while (GameStatus == false)
{
ComputerMove();
PlayerMove();
}
}
}
}
}
I am making a os with Cosmos and working in a text editor
I copied the code from LiquidEditor but it does not seem to work very well with lines, so I tried to inplement a little line system, but it gives me an error: Enum.ToString() is not implemented.
Kernel code:
using System;
using System.Collections.Generic;
using System.Text;
using Sys = Cosmos.System;
using Filesystem = Cosmos.System.FileSystem.CosmosVFS;
using VFSFilesystem = Cosmos.System.FileSystem.VFS.VFSManager;
namespace LDCM
{
public class Kernel : Sys.Kernel
{
Filesystem filesystem = new Filesystem();
protected override void BeforeRun()
{
Console.Clear();
VFSFilesystem.RegisterVFS(filesystem, false);
}
protected override void Run()
{
LiquidEditor.Start(#"0:\");
Console.WriteLine(System.IO.File.ReadAllText(#"0:\0.txt"));
Console.ReadKey(true);
}
}
}
LiquidEditor code:
using System;
using System.Collections.Generic;
using System.Text;
namespace LDCM
{
public class LiquidEditor
{
public static String version = "0.2";
public static Char[] line = new Char[80]; public static int pointer = 0;
public static List<String> lines = new List<String>();
public static String[] final;
public static void Start(String currentdirectory)
{
Console.Clear();
Utils.WriteTextCentered("Liquid Editor by TheCool1James & valentinbreiz");
Utils.WriteTextCentered("Version " + version);
Console.Write("Filename: ");
String filename = Console.ReadLine();
Start(filename, currentdirectory);
}
public static void Start(String filename, String currentdirectory)
{
if (System.IO.File.Exists(currentdirectory + filename))
{
Console.Clear();
drawTopBar();
Console.SetCursorPosition(0, 1);
ConsoleKeyInfo c; cleanArray(line);
List<String> text = new List<String>();
text.Add(System.IO.File.ReadAllText(currentdirectory + filename));
String file = "";
foreach (String value in text)
{
file = file + value;
}
Console.Write(file);
while ((c = Console.ReadKey(true)) != null)
{
drawTopBar();
Char ch = c.KeyChar;
if (c.Key == ConsoleKey.Escape)
break;
else if (c.Key == ConsoleKey.F1)
{
Console.Clear();
Console.BackgroundColor = ConsoleColor.Gray;
Console.ForegroundColor = ConsoleColor.Black;
Utils.WriteTextCentered("Liquid Editor by TheCool1James & valentinbreiz");
Utils.WriteTextCentered("Version " + version);
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.Black;
lines.Add(new String(line).TrimEnd());
final = lines.ToArray();
String foo = Utils.ConcatString(final);
System.IO.File.Create(currentdirectory + filename);
System.IO.File.WriteAllText(currentdirectory + filename, file + foo);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("'" + filename + "' has been saved in '" + currentdirectory + "' !");
Console.ForegroundColor = ConsoleColor.White;
Console.ReadKey();
break;
}
else if (c.Key == ConsoleKey.F2)
{
Start(currentdirectory);
break;
}
switch (c.Key)
{
case ConsoleKey.Home: break;
case ConsoleKey.PageUp: break;
case ConsoleKey.PageDown: break;
case ConsoleKey.End: break;
case ConsoleKey.UpArrow:
if (Console.CursorTop > 1)
{
Console.CursorTop = Console.CursorTop - 1;
}
break;
case ConsoleKey.DownArrow:
if (Console.CursorTop < 24)
{
Console.CursorTop = Console.CursorTop + 1;
}
break;
case ConsoleKey.LeftArrow: if (pointer > 0) { pointer--; Console.CursorLeft--; } break;
case ConsoleKey.RightArrow: if (pointer < 80) { pointer++; Console.CursorLeft++; if (line[pointer] == 0) line[pointer] = ' '; } break;
case ConsoleKey.Backspace: deleteChar(); break;
case ConsoleKey.Delete: deleteChar(); break;
case ConsoleKey.Enter:
lines.Add(new String(line).TrimEnd()); cleanArray(line); Console.CursorLeft = 0; Console.CursorTop++; pointer = 0;
break;
default: line[pointer] = ch; pointer++; Console.Write(ch); break;
}
}
Console.Clear();
}
else
{
Console.Clear();
drawTopBar();
Console.SetCursorPosition(0, 1);
ConsoleKeyInfo c; cleanArray(line);
while ((c = Console.ReadKey(true)) != null)
{
drawTopBar();
Char ch = c.KeyChar;
if (c.Key == ConsoleKey.Escape)
break;
else if (c.Key == ConsoleKey.F1)
{
Console.Clear();
Console.BackgroundColor = ConsoleColor.Gray;
Console.ForegroundColor = ConsoleColor.Black;
Utils.WriteTextCentered("Liquid Editor by TheCool1James & valentinbreiz");
Utils.WriteTextCentered("Version " + version);
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.Black;
lines.Add(new String(line).TrimEnd());
final = lines.ToArray();
String foo = Utils.ConcatString(final);
System.IO.File.Create(currentdirectory + filename);
System.IO.File.WriteAllText(currentdirectory + filename, foo);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("'" + filename + "' has been saved in '" + currentdirectory + "' !");
Console.ForegroundColor = ConsoleColor.White;
Console.ReadKey();
break;
}
else if (c.Key == ConsoleKey.F2)
{
Start(currentdirectory);
break;
}
switch (c.Key)
{
case ConsoleKey.Home: break;
case ConsoleKey.PageUp: break;
case ConsoleKey.PageDown: break;
case ConsoleKey.End: break;
case ConsoleKey.UpArrow:
if (Console.CursorTop > 1)
{
Console.CursorTop = Console.CursorTop - 1;
}
break;
case ConsoleKey.DownArrow:
if (Console.CursorTop < 23)
{
Console.CursorTop = Console.CursorTop + 1;
}
break;
case ConsoleKey.LeftArrow: if (pointer > 0) { pointer--; Console.CursorLeft--; } break;
case ConsoleKey.RightArrow: if (pointer < 80) { pointer++; Console.CursorLeft++; if (line[pointer] == 0) line[pointer] = ' '; } break;
case ConsoleKey.Backspace: deleteChar(); break;
case ConsoleKey.Delete: deleteChar(); break;
case ConsoleKey.Enter:
lines.Add(new String(line).TrimEnd()); cleanArray(line); Console.CursorLeft = 0; Console.CursorTop++; pointer = 0;
break;
default: line[pointer] = ch; pointer++; Console.Write(ch); break;
}
}
Console.Clear();
}
}
public static void cleanArray(Char[] c)
{
for (int i = 0; i < c.Length; i++)
c[i] = ' ';
}
public static void drawTopBar()
{
int x = Console.CursorLeft, y = Console.CursorTop;
Console.SetCursorPosition(0, 0);
Console.BackgroundColor = ConsoleColor.Gray;
Console.ForegroundColor = ConsoleColor.Black;
Console.Write("Liquid Editor v" + version + " ");
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write("[F1]Save [F2]New [ESC]Exit\n");
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.Black;
Console.SetCursorPosition(x, y);
}
public static void deleteChar()
{
if ((Console.CursorLeft >= 1) && (pointer >= 1))
{
pointer--;
Console.CursorLeft--;
Console.Write(" ");
Console.CursorLeft--;
line[pointer] = ' ';
}
else if ((Console.CursorTop >= 2))
{
Console.CursorTop--;
Console.Write(new String(' ', lines[lines.Count - 1].Length - 1));
Console.CursorTop--;
lines.RemoveAt(lines.Count - 1);
line = lines[lines.Count - 1].ToCharArray();
}
}
public static void listCheck()
{
foreach (var s in lines)
Console.WriteLine(" List: " + s + "\n");
}
private String[] arrayCheck(String[] s)
{
foreach (var ss in s)
{
Console.WriteLine(" Line: " + ss + "\n");
}
return s;
}
}
}
Utils code:
using System;
using System.Collections.Generic;
using System.Text;
namespace LDCM
{
public static class Utils
{
public static void WriteTextCentered(String content)
{
Console.Write(new string(' ', (Console.WindowWidth - content.Length - 2) / 2));
Console.WriteLine(content);
}
public static String ConcatString(String[] s)
{
String t = "";
if (s.Length >= 1)
{
for (int i = 0; i < s.Length; i++)
{
if (!String.IsNullOrWhiteSpace(s[i]))
t = String.Concat(t, s[i].TrimEnd(), Environment.NewLine);
}
}
else
t = s[0];
t = String.Concat(t, '\0');
return t;
}
}
}
The code you mentioned works fine on my environment in this way:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
List<String> lines = new List<String>();
Char[] line = new Char[80];
lines.Add("Giorgi");
lines.Add("Davut");
Console.Write(new String('_', lines[lines.Count - 1].Length - 1));
lines.RemoveAt(lines.Count - 1);
line = lines[lines.Count - 1].ToCharArray();
}
}
}
I couldn't post this in comments to check if it is valued or not, but let me know if it is improveable.
I'm using this proximity card scanner software to read my card for testing. They provide an SDK but it covers everything except desktop scanners, even though their software is able to read it fine. I noticed their software was coded in .NET and I was able to decompile it using JetBrains dotPeek. I'm very close to what I'm needing. I was able to fix the decompiled code except for these 3 lines. I'm unfamiliar with how to handle the encoding:
string[] array = ((IEnumerable<string>) ConfigurationManager.AppSettings.AllKeys).Where<string>((Func<string, bool>) (stg => stg.StartsWith(\uE0001.\uE000(27359)))).ToArray<string>();
string str = ((IEnumerable<string>) ConfigurationManager.AppSettings.AllKeys).Any<string>((Func<string, bool>) (stg => stg == \uE0001.\uE000(28615))) ? ConfigurationManager.AppSettings[\uE0001.\uE000(28615)] : directoryName;
assyTypeKey = index2.Replace(\uE0001.\uE000(28428), \uE0001.\uE000(28439));
I know they're Unicode characters but it's looking very foreign to me with that declaration next to it in parentheses. I'm not sure how to handle this.
Here's one of the functions. I know it's ugly but it's decompiled:
public static void LoadSubServices()
{
//SubServicesManager._logger.Info((object) \uE0001.\uE000(28243));
string[] array = ((IEnumerable<string>) ConfigurationManager.AppSettings.AllKeys).Where<string>((Func<string, bool>) (stg => stg.StartsWith(\uE0001.\uE000(27359)))).ToArray<string>();
label_2:
int num1 = 1;
while (true)
{
switch (num1)
{
case 0:
//SubServicesManager._logger.Error((object) \uE0001.\uE000(28270));
num1 = 2;
continue;
case 1:
if (array.Length == 0)
{
num1 = 0;
continue;
}
goto label_7;
case 2:
goto label_31;
default:
goto label_2;
}
}
label_7:
string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
//SubServicesManager._logger.InfoFormat(\uE0001.\uE000(28582), (object) directoryName);
string str = ((IEnumerable<string>) ConfigurationManager.AppSettings.AllKeys).Any<string>((Func<string, bool>) (stg => stg == \uE0001.\uE000(28615))) ? ConfigurationManager.AppSettings[\uE0001.\uE000(28615)] : directoryName;
label_9:
int num2 = 1;
while (true)
{
switch (num2)
{
case 0:
//SubServicesManager._logger.ErrorFormat(\uE0001.\uE000(28621), (object) str);
num2 = 2;
continue;
case 1:
if (!Directory.Exists(str))
{
num2 = 0;
continue;
}
goto label_14;
case 2:
goto label_6;
default:
goto label_9;
}
}
label_6:
return;
label_14:
label_33:
for (int index1 = 0; index1 < array.Length; ++index1)
{
label_17:
int num3 = 2;
string appSetting1 = null;
string appSetting2 = null;
ILoadableServer loadableServer = null;
string index2 = null;
string assyTypeKey = null;
while (true)
{
switch (num3)
{
case 0:
//SubServicesManager._logger.InfoFormat(\uE0001.\uE000(28419), (object) appSetting1);
num3 = 1;
continue;
case 1:
loadableServer = SubServicesManager.LoadSubServiceAssembly(appSetting1, str, appSetting2);
num3 = 5;
continue;
case 2:
index2 = array[index1];
num3 = 3;
continue;
case 3:
assyTypeKey = index2.Replace(\uE0001.\uE000(28428), \uE0001.\uE000(28439));
num3 = 6;
continue;
case 4:
//SubServicesManager._logger.ErrorFormat(\uE0001.\uE000(28442), (object) appSetting1);
num3 = 9;
continue;
case 5:
if (loadableServer == null)
{
num3 = 4;
continue;
}
goto label_29;
case 6:
appSetting2 = ConfigurationManager.AppSettings[index2];
num3 = 7;
continue;
case 7:
if (((IEnumerable<string>) ConfigurationManager.AppSettings.AllKeys).Any<string>((Func<string, bool>) (stg => stg == assyTypeKey)))
{
num3 = 8;
continue;
}
goto label_30;
case 8:
appSetting1 = ConfigurationManager.AppSettings[assyTypeKey];
num3 = 0;
continue;
case 9:
goto label_33;
default:
goto label_17;
}
}
label_29:
SubServicesManager._loadableServices.Add(loadableServer);
//SubServicesManager._logger.InfoFormat(\uE0001.\uE000(28496));
continue;
label_30:
return;
//SubServicesManager._logger.WarnFormat(\uE0001.\uE000(28525), (object) index2, (object) appSetting2);
}
return;
label_31:;
}
Ok for my program I am trying to get it so that when I have a Key press on my program and say I Press L and it moves in the 4x4 2d array and I get to pit which I designated as P on the array I want it to know it hit the P and print out you have a hit a Pit or if I reach where it has G on the array I want it to print out you have found gold. Any help is appreciated.
using System;
namespace DragonCave
{
public struct DragonPlayer
{
public int X, Y;
public string CurrentMove;
}
public class DragonGameboard
{
public string[,] GameboardArray;
public DragonPlayer Player;
private Random r;
public DragonGameboard(){
GameboardArray = new string[4,4];
Player.CurrentMove = "";
r = new Random();
Player.X = r.Next(0, 4);
Player.Y = r.Next(0, 4);
GenerateRandomBoard();
}
private void GenerateRandomBoard()
{
//Put a dot in every spot
int row;
int col;
for (row = 0; row < 4; row++)
{
for (col = 0; col < 4; col++)
{
Console.Write(GameboardArray[row, col] = ".");
}
//Console.WriteLine();
}
//Randomly Places the entrance, dragon, pit and gold.
GameboardArray[r.Next(0,4), r.Next(0, 4)] = "E";
GameboardArray[r.Next(0,4), r.Next(0,4)] = "D";
GameboardArray[r.Next(0, 4), r.Next(0, 4)] = "P";
GameboardArray[r.Next(0, 4), r.Next(0, 4)] = "P";
GameboardArray[r.Next(0, 4), r.Next(0, 4)] = "P";
GameboardArray[r.Next(0, 4), r.Next(0, 4)] = "G";
}
public void PrintBoard()
{
int row;
int col;
for (row = 0; row < 4; row++)
{
for (col = 0; col < 4; col++)
{
Console.Write(GameboardArray[row, col] + "\t");
}
Console.WriteLine();
}
Console.WriteLine("Cheat you are in" + Player.X + "," + Player.Y);
//fill with room numbers
}
public void ProcessMove(string move)
{
switch (move)
{
case "F":
Console.WriteLine("You chose forward");
break;
case "L":
Player.X--;
Player.Y--;
//Console.WriteLine("You chose Left");
Console.WriteLine("A Breeze is in the air");
break;
case "R":
Player.X++;
Player.Y++;
if (GameboardArray)
Console.WriteLine("You chose Right");
break;
case "G":
Console.WriteLine("You chose Grab gold");
break;
case "S":
Console.WriteLine("You chose Shoot arrow");
break;
case "C":
Console.WriteLine("You chose Climb");
break;
case "Q":
Console.WriteLine("You chose Quit");
break;
case "X":
Console.WriteLine("You chose Cheat");
PrintBoard();
break;
default:
Console.WriteLine("Invalid move!!!!!!!!!!1");
break;
}
}
}
Just check what the string is at GameboardArray[player.x, player.y].
public void ProcessTileEvent()
{
if( Player.X >= 0 && Player.X < 4 && Player.Y >= 0 && Player.Y < 4 )
{
switch( GameboardArray[Player.X, Player.Y] )
{
case "G":
Console.WriteLine( "You found gold!" );
break;
case "P":
Console.WriteLine( "You fell in a pit!" );
break;
}
}
}
You'd probably want to do this every time the player moves.
This is my hangman code, and its almost good up to the point where it displays the guessed letters. It only displays the most recent guessed letter but I want it to continue on from whats left off. Such as if a person guess "A" and then "L" then Guessed letters are A, L.
I tried using a for loop after "Guessed letters are" but then it gives me the output
"A, A, A, A, A". What should I do to fix this problem?
class Hangman
{
public string[] words = new string[5] { "ARRAY", "OBJECT", "CLASS", "LOOP", "HUMBER" };
public string[] torture = new string[6] { "left arm", "right arm", "left leg", "right leg", "body", "head" };
public char[] guessed = new char[26];
int i;
public void randomizedWord()
{
Random random = new Random();
int index = random.Next(0, 5);
char[] hidden = new char[words[index].Length];
string word = words[index];
Console.WriteLine(words[index]);
Console.Write("The word is: ");
for (i = 0; i < hidden.Length; i++)
{
Console.Write('-');
hidden[i] = '-';
}
Console.WriteLine();
int lives = 6;
do
{
Console.WriteLine("Guess a letter: ");
char userinput = Console.ReadLine().ToCharArray()[0];
index++;
guessed[index] = userinput;
Console.WriteLine("Guessed letters are: " + guessed[index]);
bool foundLetter = false;
for (int i = 0; i < hidden.Length; i++)
{
if (word[i] == userinput)
{
hidden[i] = userinput;
foundLetter = true;
Console.WriteLine("You guessed right!");
}
}
for (int x = 0; x < hidden.Length; x++)
{
Console.Write(hidden[x]);
}
if (!foundLetter)
{
Console.WriteLine(" That is not a correct letter");
lives--;
if (lives == 5)
{
Console.WriteLine("You lost a " + torture[0]);
}
else if (lives == 4)
{
Console.WriteLine("You lost the " + torture[1]);
}
else if (lives == 3)
{
Console.WriteLine("You lost your " + torture[2]);
}
else if (lives == 2)
{
Console.WriteLine("You lost the " + torture[3]);
}
else if (lives == 1)
{
Console.WriteLine("You lost your " + torture[4]);
}
else
{
Console.WriteLine("You lost your " + torture[5]);
Console.WriteLine("You lose!");
break;
}
}
bool founddash = false;
for (int y = 0; y < hidden.Length; y++)
{
if (hidden[y] == '-')
{
founddash = true;
}
}
if (!founddash)
{
Console.WriteLine(" You Win! ");
break;
}
Console.WriteLine();
} while (lives != 0);
}
I was going to post on your other thread Hangman Array C#
Try changing this line
Console.WriteLine("Guessed letters are: " + guessed[index]);
To
Console.WriteLine("Guessed letters are: " + string.Join(" ", guessed).Trim());
I had a play with your hangman game and came up with this solution (while we are doing your homework). Although not related to the question.
public class HangCSharp
{
string[] words = new string[5] { "ARRAY", "OBJECT", "CLASS", "LOOP", "HUMBER" };
List<char> guesses;
Random random = new Random();
string word = "";
string current = "";
int loss = 0;
readonly int maxGuess = 6;
int highScrore = 0;
public void Run()
{
word = words[random.Next(0, words.Length)];
current = new string('-', word.Length);
loss = 0;
guesses = new List<char>();
while (loss < maxGuess)
{
Console.Clear();
writeHeader();
writeLoss();
writeCurrent();
var guess = Console.ReadKey().KeyChar.ToString().ToUpper()[0];
while (!char.IsLetterOrDigit(guess))
{
Console.WriteLine("\nInvalid Guess.. Please enter a valid alpha numeric character.");
Console.Write(":");
guess = Console.ReadKey().KeyChar;
}
while (guesses.Contains(guess))
{
Console.WriteLine("\nYou have already guessed {0}. Please try again.", guess);
Console.Write(":");
guess = Console.ReadKey().KeyChar;
}
guesses.Add(guess);
if (!isGuessCorrect(guess))
loss++;
else if (word == current)
break;
}
Console.Clear();
writeHeader();
writeLoss();
if (loss >= maxGuess)
writeYouLoose();
else
doYouWin();
Console.Write("Play again [Y\\N]?");
while (true)
{
var cmd = (Console.ReadLine() ?? "").ToUpper()[0];
if (cmd != 'Y' && cmd != 'N')
{
Console.WriteLine("Invalid Command. Type Y to start again or N to exit.");
continue;
}
else if (cmd == 'Y')
Run();
break;
}
}
bool isGuessCorrect(char guess)
{
bool isGood = word.IndexOf(guess) > -1;
List<char> newWord = new List<char>();
for (int i = 0; i < word.Length; i++)
{
if (guess == word[i])
newWord.Add(guess);
else
newWord.Add(current[i]);
}
current = string.Join("", newWord);
return isGood;
}
void writeCurrent()
{
Console.WriteLine("Enter a key below to guess the word.\nHint: {0}", current);
if (guesses.Count > 0)
Console.Write("Already guessed: {0}\n", string.Join(", ", this.guesses));
Console.Write(":");
}
void writeHeader()
{
Console.WriteLine("Hang-CSharp... v1.0\n\n");
if (highScrore > 0)
Console.WriteLine("High Score:{0}\n\n", highScrore);
}
void writeYouLoose()
{
Console.WriteLine("\nSorry you have lost... The word was {0}.", word);
}
void doYouWin()
{
Console.WriteLine("Congratulations you guessed the word {0}.", word);
int score = maxGuess - loss;
if (score > highScrore)
{
highScrore = score;
Console.WriteLine("You beat your high score.. New High Score:{0}", score);
}
else
Console.WriteLine("Your score:{0}\nHigh Score:{1}", score, highScrore);
}
void writeLoss()
{
switch (loss)
{
case 1:
Console.WriteLine(" C");
break;
case 2:
Console.WriteLine(" C{0} #", Environment.NewLine);
break;
case 3:
Console.WriteLine(" C\n/#");
break;
case 4:
Console.WriteLine(" C\n/#\\");
break;
case 5:
Console.WriteLine(" C\n/#\\\n/");
break;
case 6:
Console.WriteLine(" C\n/#\\\n/ \\");
break;
}
Console.WriteLine("\n\nLives Remaining {0}.\n", maxGuess - loss);
}
}
Can be run as new HangCSharp().Run();
The index variable is being set to a random number when the random word is selected in this line.
int index = random.Next(0, 5);
Then that index is being used to store the guesses in the do..while loop. However, since index starts from a random number between 0 and 5 you see unexpected behaviour.
Set index to 0 in the line before the do..while loop and the for loop (or the alternatives suggested elsewhere) should work e.g. as so
index = 0;
int lives = 6;
do
{
// rest of the code