i'm trying write program shuffle 13 playing cards ace king. deal 2 cards out , add value that's assigned each card. ace = 11, king = 10, jack = 10, queen = 10, ten = 10, 9 = 9, 8 = 8 , on... sort of blackjack.
so far i'm able shuffle cards , print out 2 cards randomly don't know how assign value each card, add them , print out. example, if 2 random cards king , 8 want program print out..
king
eight
18
here's got...
static void main(string[] args) { string[] cards = new string[] {"two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", "ace"}; (int x = 0; x < 100; x++) // looping shuffle 100 times maximize randomness { (int = cards.length; > 0; i--) //shuffle { string temp; random random = new random(); int r = random.next(i); temp = cards[r]; cards[r] = cards[i-1]; cards[i-1] = temp; } } console.writeline(cards[0]); //first random card console.writeline(cards[1]); //second random card console.readkey(); }
for great example of programming card game, google search karlicards book beginning visual c# 2012 (you can find full pdf, won't post link because i'm not sure if it's legal). has lots of stuff how use regular operators (+) on things classes or structs.
anyway, you're looking enum. it's similar dictionary(string)(int) suggested rob (i'm not sure how write triangle brackets). here's example of how works:
enum cardvalue { 1 = 1, 2 = 2, 3 = 3, 4 = 4 } static void main(string[] args) { int myint = (int)cardvalue.one; console.writeline("output should 1: " + myint); int mysum = (int)cardvalue.one + (int)cardvalue.three; console.writeline("output should 4: " + mysum); console.readkey(); } my first language perl, tend see struct instead of class. there's more 1 way it....
public enum cardsuits { clubs, spades, hearts, diamonds } public enum cardvalues { ace = 1, 2 = 2, 3 = 3, 4 = 4 } public struct card { public cardvalues value; // card.value = cardvalues.ace public cardsuits suit; // card.suit = cardsuits.spades public override string tostring() { // card.tostring() == "ace of spades" return string.format(value + " of " + suit); } public string tostringasinteger() { // card.tostringasinteger() == "1 of spades" return string.format((int)value + " of " + suit); } } static void main(string[] args) { card aceofspades = new card(); aceofspades.value = cardvalues.ace; aceofspades.suit = cardsuits.spades; card twoofclubs = new card(); twoofclubs.value = cardvalues.two; twoofclubs.suit = cardsuits.clubs; int mysum = (int)aceofspades.value + (int)twoofclubs.value; console.writeline("sum of ace (1) , 2 (2) is: " + mysum); // should 3 console.writeline("output of aceofspades.tostring() is: " + aceofspades.tostring()); console.writeline("output of aceofspades.tostringasinteger is: " + aceofspades.tostringasinteger()); console.readkey(); }
Comments
Post a Comment