C# card game


















The simulation allows the game to be played in two different modes, manual and automatic. You can choose to play the game manually, conducting each battle one at a time and seeing the results. Alternately, you can choose to have the program conduct all battles until a winner is determined. The abstract base class, CardGame , contains generic fields, properties, and methods, which could be reused to create many types of card games.

CardGame class contains methods for creating a new deck of 52 cards, shuffling a set of cards see code below and dealing cards to each player. The WarCardGame class inherits the CardGame class, and adds additional fields, properties, and methods, specific to the game of War. WarCardGame class adds methods for starting a game, executing a battle see code below , and determining the type of battles — single, double, etc.

LINQ class. During the battle, cards are moved between the three temporary Dictionaries. Template hum, cpu, round ; GameFlow. LastWords hum, cpu ; while! WriteLine "Would you like to play again?

Enter y for yes or n for no. ToLower ; if answer! WriteLine "Thats not a valid option you idiot! WriteLine "Press enter to continue.

ReadLine ; Console. Clear ; Words. WriteLine "Thanks for playing! WriteLine "Press enter to exit. WriteLine "" ; if int hum. WriteLine ; Console. WriteLine "You win this one! WriteLine "Press enter to keep going. You suck! WriteLine "Its a draw!

WriteLine "Hello , welcome to war! WriteLine "The rules are simple. Its you vs a cpu" ; Console. WriteLine "You each pull 1 card from your decks and see which is highest. WriteLine "The one with the most points at the end wins all the glory! WriteLine "Understand? WriteLine "Press enter if you're ready for the showdown.

WriteLine ; hum. PrintCard ; Console. Write " vs " ; cpu. WriteLine "Thats all the cards! WriteLine "Oh my! Its a draw. You both are equal warriors of luck. Improve this question. Yummy Yummy 2 2 gold badges 6 6 silver badges 9 9 bronze badges. Thanks for posting. Add a comment. Active Oldest Votes. This is what I consider to be one of the best web pages on the entire internet, and it has a section on shuffling that you might find interesting: Visualizing Algorithms by Mike Bostock Mike has also created this resource: Will it shuffle?

It will show you that the shuffle is quite biased: If that weren't bad enough, you have accidentally added an additional bias. For example, you might want to just create Card objects to represent the cards. Then you can display them in PictureBoxes later. It sets the Parent to the Panel control named panCards.

Later the program can use the Tag object to see what card the PictureBox represents. Next it makes a rectangle that fills the new bitmap. It also makes rectangles that identifies the location where the image should be copied from the big bitmap containing all of the card images. This method simply loops through the PictureBox controls in the Pics array and positions the controls in rows and columns within the Panel.

The final pieces of the program include the following event handlers, which execute when the mouse enters or leaves a PictureBox. It then gets the control's Tag property and converts it into a Card object. The event handler finishes by displaying the Card object's rank plus 1 and suit in the form's title bar.

The Card class and the technique of storing Card objects in a PictureBox control's Tag property should help you get started making a card game. See, I want you, my dear readers who think they need a bit more practice designing and implementing complex applications, to attempt to model War in C first, and then come read this series of posts. That way, you get both the experience of working through and modeling this problem yourself, and a bit of a sanity-check in comparing your solution to mine.

You might even find a better solution than I do, and if you do so, I want to know about it, so share in the comments! War is a two-player card game, played with a standard card deck. Each player starts with a shuffled half of that deck, and they do not look through their deck or know what card is on top of it. On each turn, each player flips over their top card. If both players flip over a card with the same value, a War is declared. In a War, each player places three cards face down on the table, and a fourth card face up.

Often this sequence is remembered using the mnemonic "1, 2, 3, 4, I De-Clare War" with a card being placed on the table for each of the last four syllables. Whichever player has the higher value of this fourth card gets all the cards on the table, including the face-down ones.



0コメント

  • 1000 / 1000