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;
}
}
Related
I have some months read into an array from an external text file and I need to convert the months to an array that holds the value equivalent of the months e.g. January = 1, February = 2 etc. So that they can then be put through Quicksort.
public static void Main(string[] args)
{
//Read external files into arrays.
string[] Month = File.ReadLines(#"Month.txt").ToArray();
string[] Year = File.ReadLines(#"Year.txt").ToArray();
//Convert arrays from string to double to be used in sort.
double[] YearSort = Array.ConvertAll(Year, double.Parse);
int UserInput1;
//Create new array that will hold selected array to be used in sort.
double[] data = new double[1022];
//Prompt user to select action.
Console.WriteLine("Press 1 to Sort by month or 2 to sort by year.");
UserInput1 = Convert.ToInt32(Console.ReadLine());
if(UserInput1 == 1)
{
Array.Copy(Month,data,1022);
QuickSort(data);
for (int i = 0; i < 1022; i++)
Console.WriteLine(data[i]);
Console.WriteLine();
}
else if (UserInput1 == 2)
{
Array.Copy(YearSort,data,1022);
QuickSort(data);
for (int i = 0; i < 1022; i++)
Console.WriteLine(data[i]);
Console.WriteLine();
}
else
{
Console.WriteLine("Please try again and select a valid option");
}
}
static int MonthToDouble( string Month )
{
int NewMonth = 0;
switch(Month)
{
case "January":
case "january":
NewMonth = 1;
break;
case "February":
case "february":
NewMonth = 2;
break;
case "March":
case "march":
NewMonth = 3;
break;
case "April":
case "april":
NewMonth = 4;
break;
case "May":
case "may":
NewMonth = 5;
break;
case "June":
case "june":
NewMonth = 6;
break;
case "July":
case "july":
NewMonth = 7;
break;
case "August":
case "august":
NewMonth = 8;
break;
case "September":
case "september":
NewMonth = 9;
break;
case "October":
case "october":
NewMonth = 10;
break;
case "November":
case "november":
NewMonth = 11;
break;
case "December":
case "december":
NewMonth = 12;
break;
}
return NewMonth;
}
static string DoubleToMonth(double Month)
{
string NewMonth = "";
switch ((int)Month)
{
case 1:
NewMonth = "January";
break;
case 2:
NewMonth = "February";
break;
case 3:
NewMonth = "March";
break;
case 4:
NewMonth = "April";
break;
case 5:
NewMonth = "May";
break;
case 6:
NewMonth = "June";
break;
case 7:
NewMonth = "July";
break;
case 8:
NewMonth = "August";
break;
case 9:
NewMonth = "September";
break;
case 10:
NewMonth = "October";
break;
case 11:
NewMonth = "November";
break;
case 12:
NewMonth = "December";
break;
}
return NewMonth;
}
//QuickSort for double data values are in ascending order.
public static void QuickSort(double[] data)
{
Quick_Sort(data, 0, data.Length - 1);
}
public static void Quick_Sort(double[] data, int left, int right)
{
int i, j;
double pivot, temp;
i = left;
j = right;
pivot = data[(left + right) / 2];
do
{
while ((data[i] < pivot) && (i < right)) i++;
while ((pivot < data[j]) && (j > left)) j--;
if (i <= j)
{
temp = data[i];
data[i] = data[j];
data[j] = temp;
i++;
j--;
}
} while (i <= j);
if (left < j) Quick_Sort(data, left, j);
if (i < right) Quick_Sort(data, i, right);
}
}
A DateTime object's Month property gives you the integer value of the month starting at 1 like you need. So you can use DateTime.ParseExact() to parse the string into a full DateTime object then grab the Month property:
int monthNumber = DateTime.ParseExact("January", "MMMM", CultureInfo.CurrentCulture).Month;
You just need to replace "January" with your month strings, and leave "MMMM"which is the Custom Format String for "The full name of the month".
All the above code does is simplify your MonthToDouble() method, which you are not even using for some reason (also it should return a double, not an int). Contrary to your title, you already have a method to "Convert months to number equivalent", you just aren't using it.
So, I assume the only thing you are missing is to replace this:
Array.Copy(Month,data,1022);
QuickSort(data);
With this:
double[] monthsAsDoubles = new double[Month.Length];
for (int i = 0; i < monthsAsDoubles.Length; i++)
{
monthsAsDoubles[i] = MonthToDouble(Month[i]);
}
QuickSort(monthsAsDoubles);
Also change the return value of MonthToDouble() from int to double (cast if you need to).
Edit: On second thought, Quantic's answer is simpler. I'll just leave this here as an alternative.
Your code can be simplified a lot by using the DateTimeFormatInfo.MonthNames property, along with a case-insensitive string comparison. This also has the advantage of being much easier to port to use different languages for the month names.
Here is a snippet:
using System;
using System.Globalization;
using System.Linq;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
var InputMonths = new List<string> { "January","march","sepTEmber","smarch" };
var MonthNames = new DateTimeFormatInfo().MonthNames.ToList();
var InputMonthNumbers = new List<int>();
foreach (var m in InputMonths)
{
//Find index of the month name, ignoring case
//Note if the input month name is invalid, FindIndex will return 0
int month_num = 1 + MonthNames.FindIndex(name => name.Equals(m, StringComparison.OrdinalIgnoreCase));
if (month_num > 0)
{
InputMonthNumbers.Add(month_num);
}
}
foreach (var n in InputMonthNumbers)
{
Console.WriteLine(n.ToString());
}
}
}
output:
1
3
9
I am trying to generate a text file of distinct 5-card poker hands, very similar to this question (which I actually posted an answer to). The program I pieced together from this answer to that question mostly works. However, there are two major problems: The number of hands is incorrect, and every hand has the 2 of Hearts in it. Perhaps if the number of hands was correct, the hands would be correct as well.
The person that posted the answer I was working from stated that he got the correct number by not seeding the initial card. However, if you remove the seed you get an Invalid Exception error when it tries to do the ranking down the line. Seeding it makes the program work, but returns only half of the hands.
What do I need to do to this code to get all distinct 5 card poker hands?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace PokerHands
{
struct Card
{
public int Suit { get; set; }
public int Rank { get; set; }
}
class Hand
{
public List<Card> Cards { get; set; }
public string HandString { get; set; }
}
class Program
{
static int ranks = 13;
static int suits = 4;
static int cardsInHand = 5;
static List<Hand> possibleHands = new List<Hand>();
static void Main(string[] args)
{
List<Card> cards = new List<Card>();
cards.Add(new Card() { Rank = 0, Suit = 0 });
int numHands = GenerateAllHands(cards);
int counter = 0;
Console.WriteLine(numHands);
Console.WriteLine(possibleHands.Count);
possibleHands.Shuffle();
using (System.IO.StreamWriter file = new System.IO.StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "Hands.txt"))
{
foreach (Hand hand in possibleHands)
{
counter += 1;
hand.Cards.Shuffle();
foreach (Card card in hand.Cards)
{
hand.HandString += GetCardName(card) + " ";
}
file.WriteLine(hand.HandString.Trim());
}
}
Console.ReadLine();
}
static string GetCardName(Card card)
{
string cardName;
string cardFace;
string cardSuit;
switch (card.Rank)
{
case 0:
cardFace = "2";
break;
case 1:
cardFace = "3";
break;
case 2:
cardFace = "4";
break;
case 3:
cardFace = "5";
break;
case 4:
cardFace = "6";
break;
case 5:
cardFace = "7";
break;
case 6:
cardFace = "8";
break;
case 7:
cardFace = "9";
break;
case 8:
cardFace = "10";
break;
case 9:
cardFace = "J";
break;
case 10:
cardFace = "Q";
break;
case 11:
cardFace = "K";
break;
default:
cardFace = "A";
break;
}
switch (card.Suit)
{
case 0:
cardSuit = "H";
break;
case 1:
cardSuit = "D";
break;
case 2:
cardSuit = "S";
break;
default:
cardSuit = "C";
break;
}
cardName = cardFace + cardSuit;
return cardName;
}
static int GenerateAllHands(List<Card> cards)
{
if (cards.Count == cardsInHand)
{
Hand hand = new Hand();
hand.Cards = cards;
possibleHands.Add(hand);
return 1;
}
List<Card> possibleNextCards = GetPossibleNextCards(cards);
int numSubHands = 0;
foreach (Card card in possibleNextCards)
{
List<Card> possibleNextHand = cards.ToList(); // copy list
possibleNextHand.Add(card);
numSubHands += GenerateAllHands(possibleNextHand);
}
return numSubHands;
}
static List<Card> GetPossibleNextCards(List<Card> hand)
{
int maxRank = hand.Max(x => x.Rank);
List<Card> result = new List<Card>();
// only use ranks >= max
for (int rank = maxRank; rank < ranks; rank++)
{
List<int> suits = GetPossibleSuitsForRank(hand, rank);
var possibleNextCards = suits.Select(x => new Card { Rank = rank, Suit = x });
result.AddRange(possibleNextCards);
}
return result;
}
static List<int> GetPossibleSuitsForRank(List<Card> hand, int rank)
{
int maxSuit = hand.Max(x => x.Suit);
// select number of ranks of different suits
int[][] card = GetArray(hand, rank);
for (int i = 0; i < suits; i++)
{
card[i][rank] = 0;
}
int[][] handRep = GetArray(hand, rank);
// get distinct rank sets, then find which ranks they correspond to
IEnumerable<int[]> distincts = card.Distinct(new IntArrayComparer());
List<int> possibleSuits = new List<int>();
foreach (int[] row in distincts)
{
for (int i = 0; i < suits; i++)
{
if (IntArrayComparer.Compare(row, handRep[i]))
{
possibleSuits.Add(i);
break;
}
}
}
return possibleSuits;
}
class IntArrayComparer : IEqualityComparer<int[]>
{
#region IEqualityComparer<int[]> Members
public static bool Compare(int[] x, int[] y)
{
for (int i = 0; i < x.Length; i++)
{
if (x[i] != y[i]) return false;
}
return true;
}
public bool Equals(int[] x, int[] y)
{
return Compare(x, y);
}
public int GetHashCode(int[] obj)
{
return 0;
}
#endregion
}
static int[][] GetArray(List<Card> hand, int rank)
{
int[][] cards = new int[suits][];
for (int i = 0; i < suits; i++)
{
cards[i] = new int[ranks];
}
foreach (Card card in hand)
{
cards[card.Suit][card.Rank] = 1;
}
return cards;
}
}
public static class ThreadSafeRandom
{
[ThreadStatic]
private static Random Local;
public static Random ThisThreadsRandom
{
get { return Local ?? (Local = new Random(unchecked(Environment.TickCount * 31 + Thread.CurrentThread.ManagedThreadId))); }
}
}
static class MyExtensions
{
public static void Shuffle<T>(this IList<T> list)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = ThreadSafeRandom.ThisThreadsRandom.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}
}
Change the start of GetPossibleNextCards() to:
static List<Card> GetPossibleNextCards(List<Card> hand)
{
int maxRank = (hand.Count == 0) ? 0 : hand.Max(x => x.Rank);
And remove the first line of GetPossibleSuitsForRank(), because you are not using the result. i.e. remove this line:
int maxSuit = hand.Max(x => x.Suit); // remove this
And also remove the line in Main() which is adding the 2H:
cards.Add(new Card() { Rank = 0, Suit = 0 }); // remove this
Can generate all distinct 5 card hard in 0.2 seconds.
You cannot read a file that fast.
Hand order does not matter - there are not 52! hands.
From combinations combin(52,5) there are 2,598,960 hands.
private byte[,] pokerHands;
public byte[,] PokerHands
{
get
{
if (pokerHands == null)
{
// easy to get rank and suite from a byte
// just store byte
// type can make a card class that you pass a byte constuctor
for (byte i = 0; i < 52; i++)
Debug.WriteLine("rank " + i % 13 + " suite " + i / 13);
Stopwatch sw = new Stopwatch();
sw.Start();
int counter = 0;
pokerHands = new byte[2598960, 5];
for (int i = 51; i >= 4; i--)
{
for (int j = i-1; j >= 3; j--)
{
for (int k = j-1; k >= 2; k--)
{
for (int m = k-1; m >= 1; m--)
{
for (int n = m-1; n >= 0; n--)
{
pokerHands[counter, 0] = (byte)i;
pokerHands[counter, 1] = (byte)j;
pokerHands[counter, 2] = (byte)k;
pokerHands[counter, 3] = (byte)m;
pokerHands[counter, 4] = (byte)n;
counter++;
}
}
}
}
}
sw.Stop();
System.Diagnostics.Debug.WriteLine("counter " + counter);
System.Diagnostics.Debug.WriteLine("sw " + sw.ElapsedMilliseconds.ToString("N0"));
}
return pokerHands;
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm developing a text-based RPG and I'm implementing a stat system. My code for the classes that interact with it is:
switch (charClass)
{
case "a":
Player.charC(0);
break;
case "b":
Player.charC(1);
break;
case "c":
Player.charC(2);
break;
}
public static void charC(int charPath)
{
switch (charPath)
{
case 0:
Player.stats(0);
break;
case 1:
Player.stats(1);
break;
case 2:
Player.stats(2);
break;
}
}
public static void stats(int stat)
{
Player p = new Player();
switch (stat)
{
case 0:
p.m_health = 200;
p.m_mana = 75;
p.fast = 7;
p.strng = 20;
p.smrt = 7;
p.move = 7;
p.level = 1;
break;
case 1:
p.m_health = 100;
p.m_mana = 200;
p.fast = 10;
p.strng = 7;
p.smrt = 15;
p.move = 7;
p.level = 1;
break;
case 2:
p.m_health = 100;
p.m_mana = 100;
p.fast = 10;
p.strng = 10;
p.smrt = 10;
p.move = 10;
p.level = 1;
break;
}
}
When I try to run it, it returns all the stats as 0.
Example: MAX HEALTH 0. MAX MANA 0. FAST 0. STRONG 0. SMART 0.
In the stats method you're not returning the player object. You create a new player, set its properties, but discard the object at the end of the method.
In short: You're not working on the object you think you're working on.
Why is all this static?
You need to remove the static-ness of stats and call it with the instance of the player
public void stats(int stat)
{
switch (stat)
{
case 0:
m_health = 200;
m_mana = 75;
fast = 7;
strng = 20;
smrt = 7;
move = 7;
break;
case 1:
m_health = 100;
m_mana = 200;
fast = 10;
strng = 7;
smrt = 15;
move = 7;
break;
case 2:
m_health = 100;
m_mana = 100;
fast = 10;
strng = 10;
smrt = 10;
move = 10;
break;
}
level = 1;
}
use the instance of player in both switches, i.e
switch (charClass)
{
case "a":
playerInstance.charC(0);
break;
case "b":
playerInstance.charC(1);
break;
case "c":
playerInstance.charC(2);
break;
}
public void charC(int charPath)
{
switch (charPath)
{
case 0:
this.stats(0);
break;
case 1:
this.stats(1);
break;
case 2:
this.stats(2);
break;
}
}
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.
I have written code to display many image (100 image) with PictureBox but when run the application, just one image has been shown...
please help me guys...
Here is my code:
Random r = new Random();
private int randomPoint()
{
return 1 + r.Next() % 15;
}
// x0, y0
private int[] initialLocation = new int[2];
private void setLocation(int i)
{
int x0 = 50, y0=50;
initialLocation[1] = y0;
switch (i)
{
case 1: initialLocation[0] = x0;
break;
case 2: initialLocation[0] = x0 + 50;
break;
case 3: initialLocation[0] = x0 + 100;
break;
case 4: initialLocation[0] = x0 + 150;
break;
case 5: initialLocation[0] = x0 + 200;
break;
case 6: initialLocation[0] = x0 + 250;
break;
case 7: initialLocation[0] = x0 + 300;
break;
case 8: initialLocation[0] = x0 + 350;
break;
case 9: initialLocation[0] = x0 + 400;
break;
case 10: initialLocation[0] = x0 + 450;
break;
}
}
public Form1()
{
InitializeComponent();
PictureBox[] p = new PictureBox[10];
for (int i=0; i<10;i++)
{
p[i] = new PictureBox();
p[i].ImageLocation = "1.png";
int[] l = new int[2];
// create random location for images
setLocation(randomPoint());
p[i].Location = new Point(initialLocation[0], initialLocation[1]);
this.Controls.Add(p[i]);
}
}
It's because you are declaring your random number generator every time you want an image:
private int randomPoint()
{
Random r = new Random();
return 1 + r.Next() % 15;
}
replace this with:
private Random r = new Random();
private int randomPoint()
{
return 1 + r.Next() % 15;
}
UPDATE
If I understand you correctly you want to display 15 images in a random order across the form.
To ensure that you don't get any repeats you need to make sure that you remove the picture you've picked from the list before picking the next. So (in pseudo code) you need something like this:
this.folderList = new List<string>();
// Populate it in an orderly manner
// Create temporary list to put the randomly selected items into
var radomisedList = new List<string>();
// Randomise the list.
var random = new Random();
int num = 0;
while (this.folderList.Count > 0)
{
num = random.Next(0, this.folderList.Count);
radomisedList.Add(this.folderList[num]);
this.folderList.RemoveAt(num);
}
This will ensure you get a random order and no repeats.