Trying to write out a program - c#

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.

Related

Why doesn't the 'ComputerMove' subroutine get executed?

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();
}
}
}
}
}

Controlling menu with the arrow keys and enter

I was trying to create an RPG. I have a problem with the menu where I can choose a class.
I was trying to create an menu where you can control the directions with the arrow keys to the specific class which then get highlighted with the foreground color red, like in a real game when you want to choose something you just use the arrow keys and the thing you are on gets highlighted.
My problem is I can't specify the location of the arrow keys when I press the arrow key. I can only go to the first location and another problem is when I highlight the rpg class to show the user where he is, all rpg classes get the foreground color. I used Console.Read to separate them but now I always have to press Enter to change the color.
Here is the code. I think after you opened the code u will understand my problem.
Best regards Csharpnoob61.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Enter_Eingabe
{
class Program
{
static void Main(string[] args)
{
//ints
int char_HP_Current = 20;
int char_HP_Full = 100;
double char_Exp_Current = 10;
double char_Exp_Full = 100;
int char_Level_Current = 1;
int GameOver = 0;
int char_Move_Left_Right = 0;
int char_Move_Up_Down = 8;
int Error = 0;
int Resting_Time = 0;
int Attack_Bonus = 0;
int Speech_Bonus = 0;
int Sneak_Bonus = 0;
int Armor_Bonus = 0;
int Casting_Bonus = 0;
//Strings
string char_Name = "";
string Current_Command;
string char_Status = "";
string char_Class;
string test;
Console.Clear();
Console.SetCursorPosition(0, 8);
do
{
string text = "Guardian";
Console.SetCursorPosition(15, 8);
Console.WriteLine(text);
Console.SetCursorPosition(45, 8);
Console.WriteLine("Paladin");
Console.SetCursorPosition(30, 8);
Console.WriteLine("Outlaw");
ConsoleKeyInfo KeyInfo;
KeyInfo = Console.ReadKey(true);
switch (KeyInfo.Key)
{
//Player Controlls
case ConsoleKey.RightArrow:
Console.SetCursorPosition(0, 8);
if (char_Move_Left_Right < 78)
{
char_Move_Left_Right+=14;
Console.SetCursorPosition(char_Move_Left_Right, char_Move_Up_Down);
Console.WriteLine("_");
Console.SetCursorPosition(char_Move_Left_Right - 1, char_Move_Up_Down);
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine("_");
Console.ForegroundColor = ConsoleColor.White;
if (char_Move_Left_Right == 14)
{
if (char_Move_Up_Down == 8)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.SetCursorPosition(15, 8);
Console.WriteLine(text);
Console.Read();
}
Console.ForegroundColor = ConsoleColor.White;
}
}
if (char_Move_Left_Right == 29)
{
if (char_Move_Up_Down == 8)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.SetCursorPosition(30,8);
Console.WriteLine("Outlaw");
Console.Read();
}
Console.ForegroundColor = ConsoleColor.White;
}
if (char_Move_Left_Right == 44)
{
if (char_Move_Up_Down == 8)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.SetCursorPosition(45, 8);
Console.WriteLine("Paladin");
Console.ReadLine();
}
Console.ForegroundColor = ConsoleColor.White;
}
break;
case ConsoleKey.LeftArrow:
if (char_Move_Left_Right > 1)
{
char_Move_Left_Right--;
Console.SetCursorPosition(char_Move_Left_Right, char_Move_Up_Down);
Console.WriteLine("_");
Console.SetCursorPosition(char_Move_Left_Right + 1, char_Move_Up_Down);
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine("_");
Console.ForegroundColor = ConsoleColor.White;
}
else { }
break;
case ConsoleKey.UpArrow:
if (char_Move_Up_Down > 3)
{
char_Move_Up_Down--;
Console.SetCursorPosition(char_Move_Left_Right, char_Move_Up_Down);
Console.WriteLine("_");
Console.SetCursorPosition(char_Move_Left_Right, char_Move_Up_Down + 1);
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine("_");
Console.ForegroundColor = ConsoleColor.White;
}
else { }
break;
case ConsoleKey.DownArrow:
if (char_Move_Up_Down < 21)
{
char_Move_Up_Down++;
Console.SetCursorPosition(char_Move_Left_Right, char_Move_Up_Down);
Console.WriteLine("_");
Console.SetCursorPosition(char_Move_Left_Right, char_Move_Up_Down - 1);
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine("_");
Console.ForegroundColor = ConsoleColor.White;
}
else { }
break;
}
}while (Error == 0);
}
}
}
Instead of writing every combination by hand, here is what I would do: Use a helper class for common tasks like these kind of multiple-choice actions, pass a set of options and wait for the user to make a selection.
public class ConsoleHelper
{
public static int MultipleChoice(bool canCancel, params string[] options)
{
const int startX = 15;
const int startY = 8;
const int optionsPerLine = 3;
const int spacingPerLine = 14;
int currentSelection = 0;
ConsoleKey key;
Console.CursorVisible = false;
do
{
Console.Clear();
for (int i = 0; i < options.Length; i++)
{
Console.SetCursorPosition(startX + (i % optionsPerLine) * spacingPerLine, startY + i / optionsPerLine);
if(i == currentSelection)
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(options[i]);
Console.ResetColor();
}
key = Console.ReadKey(true).Key;
switch (key)
{
case ConsoleKey.LeftArrow:
{
if (currentSelection % optionsPerLine > 0)
currentSelection--;
break;
}
case ConsoleKey.RightArrow:
{
if (currentSelection % optionsPerLine < optionsPerLine - 1)
currentSelection++;
break;
}
case ConsoleKey.UpArrow:
{
if (currentSelection >= optionsPerLine)
currentSelection -= optionsPerLine;
break;
}
case ConsoleKey.DownArrow:
{
if (currentSelection + optionsPerLine < options.Length)
currentSelection += optionsPerLine;
break;
}
case ConsoleKey.Escape:
{
if (canCancel)
return -1;
break;
}
}
} while (key != ConsoleKey.Enter);
Console.CursorVisible = true;
return currentSelection;
}
}
The one above for example can be used like this:
int selectedClass = ConsoleHelper.MultipleChoice(true, "Warrior", "Bard", "Mage", "Archer",
"Thief", "Assassin", "Cleric", "Paladin", "etc.");
selectedClass will simply be the index of the selected option when the function returns (or -1 if the user pressed escape). You might want to add additional parameters like a banner text ("Select class") or formatting options.
Should look something like this:
You can of course add additional marks like _these_ or > those < to further highlight the current selection.

C# code not building/running even though i have no errors

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.

Cursor overwrites text in console application

I am building an ascii version of TicTacToe as a class asigment and I ran into a problem.The cursor doverwrites the board and the signs.
while (pressed == false)
{
ConsoleKeyInfo cki;
cki = Console.ReadKey();
switch (cki.Key)
{
case ConsoleKey.RightArrow:
if(sides+1>Console.WindowWidth)
break;
if(sides+1==17||sides+1==8)
sides+=2;
else
sides += 1;
Console.SetCursorPosition(sides, ver);
break;
case ConsoleKey.LeftArrow:
if (sides - 1 < 0)
break;
if (sides -1 == 17 || sides -1 == 8)
sides -= 2;
else
sides -= 1;
Console.SetCursorPosition(sides, ver);
break;
case ConsoleKey.DownArrow:
if (ver + 1 > Console.WindowHeight)
break;
if (ver+1==8||ver+1==17)
ver += 2;
else
ver += 1;
Console.SetCursorPosition(sides, ver);
break;
case ConsoleKey.UpArrow:
if (ver - 1 < 0 )
break;
if (ver - 1 == 8 || ver - 1 == 17)
ver-=2;
else
ver -= 1;
Console.SetCursorPosition(sides, ver);
break;
case ConsoleKey.X:
case ConsoleKey.O:
br.PutSign(ver,sides,ch);
pressed = true;
break;
default:
break;
}
}
}
I fixed the problme that the cursor overwrites the board But I still cant find a way to protect the sings(the X and the O)
Thats the board class for refernce:
public char[,] board = new char[27, 27];
public Board()
{
for (int i = 0; i < board.GetLength(0); i++)
{
for (int j = 0; j < board.GetLength(1); j++)
{
if (i==8||i==17)
board[i, j] = '_';
if (j==8||j==17)
board[i, j] = '|';
}
}
}
public void PrintB()
{
for (int i = 0; i < board.GetLength(0); i++)
{
for (int j = 0; j < board.GetLength(1); j++)
{
Console.Write(board[i, j]);
}
Console.WriteLine("");
}
}
public void PutSign(int row, int col, char ch)
{
board[row, col] = ch;
}

C# card shuffle in card deck 52 cards

there is a project for Windows application that I'm still working on and is about a set of card deck. The application utilizes 52 cards which consist of 4 suits and 13 face values such as 2 of Clubs, Jack of Hearts, and so forth. The part that I'm working is that I also have to use five pictureboxes to display each random card so I click on a "Deal" button. I'm aware that I would have to use a "Random" keyword along with using a for-loop to do the shuffle.
Therefore, I'm not too sure how would I display each picturebox with different random cards and display each card's name accordingly.
Beneath of this contain screenshots of what the windows application looks like and my code for the project.
List<PlayingCard> cardDeckList = new List<PlayingCard>();
private void buttonDeal_Click(object sender, EventArgs e)
{
int integer = 0;
Random picRandom = new Random();
int n = 0;
integer = picRandom.Next(0, imageListCards.Images.Count);
for( n = 0; n < cardDeckList.Count; n++)
{
pictureBox_Card1.Image = cardDeckList[integer].CardImage;
pictureBox_Card2.Image = cardDeckList[integer].CardImage;
pictureBox_Card3.Image = cardDeckList[integer].CardImage;
pictureBox_Card4.Image = cardDeckList[integer].CardImage;
pictureBox_Card5.Image = cardDeckList[integer].CardImage;
}
listBoxOutput.Items.Add(cardDeckList[integer].ToString());
}
private void FormShuffleCardDeck_Load(object sender, EventArgs e)
{
try
{
string[] suitList = { "Clubs", "Diamonds", "Hearts", "Spades" };
string[] faceList = new string[13];
List<int> pointValues = new List<int>();
pointValues.Add(2);
pointValues.Add(3);
pointValues.Add(4);
pointValues.Add(5);
pointValues.Add(6);
pointValues.Add(7);
pointValues.Add(8);
pointValues.Add(9);
pointValues.Add(10);
pointValues.Add(10);
pointValues.Add(11);
string suit = "";
string face = "";
int counter = 0;
int i = 0;
int k = 0;
for (i = 0; i < 4; i++)
{
suit = i.ToString();
switch (suit)
{
case "0":
suit = "Clubs";
break;
case "1":
suit = "Diamonds";
break;
case "2":
suit = "Hearts";
break;
case "3":
suit = "Spades";
break;
}
for (k = 0; k < 13; k++)
{
face = k.ToString();
switch (k)
{
case 0:
face = "2";
break;
case 1:
face = "3";
break;
case 2:
face = "4";
break;
case 3:
face = "5";
break;
case 4:
face = "6";
break;
case 5:
face = "7";
break;
case 6:
face = "8";
break;
case 7:
face = "9";
break;
case 8:
face = "10";
break;
case 9:
face = "Ace";
break;
case 10:
face = "King";
break;
case 11:
face = "Jack";
break;
case 12:
face = "Queen";
break;
}
cardDeckList.Add(new PlayingCard(suit, face, imageListCards.Images[counter],2));
counter++;
}
}
//for (int l = 0; l < cardDeckList.Count; l++)
//{
// listBoxOutput.Items.Add(cardDeckList[l].ToString());
// //MessageBox.Show(cardDeckList.Count.ToString());
//}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Windows App Program
I'm not sure how your PlayingCard class is coded, but it seems like a large portion of your problems stem from its design. Take this implementation for example:
PlayingCard Class
public class PlayingCard : IComparable<PlayingCard>
{
private int value;
private int suit;
private Bitmap cardImage;
public int Value => value;
public string ValueName => ValueToName(value);
public int Suit => suit;
public string SuitName => SuitToName(suit);
public Bitmap CardImage => cardImage;
public PlayingCard(int value, int suit, Bitmap cardImage)
{
this.value = value;
this.suit = suit;
this.cardImage = cardImage;
}
private string ValueToName(int n)
{
switch (n)
{
case 0:
return "Ace";
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
return (n+1).ToString();
case 10:
return "Jack";
case 11:
return "Queen";
case 12:
return "King";
default:
throw new ArgumentException("Unrecognized card value.");
}
}
private string SuitToName(int s)
{
switch (s)
{
case 0:
return "Clubs";
case 1:
return "Diamonds";
case 2:
return "Spades";
case 3:
return "Hearts";
default:
throw new ArgumentException("Unrecognized card suit");
}
}
public int CompareTo(PlayingCard other)
{
int result = this.Suit.CompareTo(other.Suit);
if (result != 0)
return result;
return this.Value.CompareTo(other.Value);
}
public override string ToString()
{
return String.Format("{0} of {1}", ValueName, SuitName);
}
}
It has all the comparison and value conversion coded within it, so you don't have to worry about creating highly specialized methods to do extraneous conversions. You can use it in a deck like so:
Array Implementation
// How to initialize deck
PlayingCard[] deck = Enumerable.Range(0, 52)
.Select(x => new PlayingCard(x % 13, x / 13, imageListCards[x]))
.ToArray();
// How to shuffle deck
Random r = new Random();
Array.Sort(deck, (a, b) => r.Next(0, 2) == 0 ? -1 : 1);
// How to reset deck
Array.Sort(deck);
// How to display top five cards
pictureBox_Card1.Image = deck[0].CardImage;
pictureBox_Card2.Image = deck[1].CardImage;
pictureBox_Card3.Image = deck[2].CardImage;
pictureBox_Card4.Image = deck[3].CardImage;
pictureBox_Card5.Image = deck[4].CardImage;
List Implementation
// How to initialize deck
List<PlayingCard> deck = Enumerable.Range(0, 52)
.Select(x => new PlayingCard(x % 13, x / 13, imageListCards[x]))
.ToList();
// How to shuffle deck
Random r = new Random();
deck.Sort((a, b) => r.Next(0, 2) == 0 ? -1 : 1);
// How to reset deck
deck.Sort();
// How to display top five cards
pictureBox_Card1.Image = deck[0].CardImage;
pictureBox_Card2.Image = deck[1].CardImage;
pictureBox_Card3.Image = deck[2].CardImage;
pictureBox_Card4.Image = deck[3].CardImage;
pictureBox_Card5.Image = deck[4].CardImage;
EDIT:
Manual Shuffling
If you want to do a shuffle manually, there's a simple algorithm called the Fisher-Yates Shuffle that will do the trick:
private static Random r = new Random();
static void Shuffle<T>(T[] array)
{
for (int i = 0; i < array.Length; i++)
{
int idx = r.Next(i, array.Length);
T temp = array[idx];
array[idx] = array[i];
array[i] = temp;
}
}
(List Implementation)
private static Random r = new Random();
static void Shuffle<T>(List<T> list)
{
for (int i = 0; i < list.Count; i++)
{
int idx = r.Next(i, list.Count);
T temp = list[idx];
list[idx] = list[i];
list[i] = temp;
}
}

Categories