For my .Net programming class, I have been asked to create my own program that i
ID: 3668334 • Letter: F
Question
For my .Net programming class, I have been asked to create my own program that is supposed to showcase what techniques i have learned in the class. I have to use C# for the programming, I have to use visual basic as my IDE (and currently have installed Visual Studio 2012).
What i settled on was creating the card game "War". seemed easy to me. Just display two cards, and one card is higher it goes to the winner.
I needed to show off some techniques that i have leared, so i chose the ability to save the game and load the game (FILE PROGRAMING), a LOOP that keeps the game going until all the cards are gone from one players hand, one or two ARRAYS that will log the cards that the players have, and finally different CLASSES and METHODS.
But my trouble is I do not know how to start this. How does one start a program? In VB, i created my display screen, so it has a window that shows the face of the card, a button to deal the card, a button to shuffle, a list window that shows you what cards you have and a window that shows your current card count vs players. But how do i put this down? How do i start?
Explanation / Answer
Hi, Below i have explained how to work with War Card games using C#.
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. Fields and properties in the base class include Dictionary<> objects to store the player’s cards, and player, battle, and game stats.
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. The WarCardGame class’s fields store values such as war tallies and number of cards to be placed face down in war. One might argue that more of theWarCardGame class’s fields, properties, and methods could be placed in the base class and overridden if necessary.
The instance of the WarCardGame object manages three sets of card during game play: Player One’s cards, Player Two’s cards, and the ‘cards on the table’ (the players have laid down during the battle). The card sets are stored in Dictionary<string, int> object fields. The Dictionary’s KeyValuePair elements represent individual cards. The Key represents the card as a string, for example the ‘9 ?’. The Valuerepresents the value of the card as an integer, for example ‘9’.
During a battle, card sets are copied to one of three temporary Dictionaries using LINQ expressions (System.LINQ class). During the battle, cards are moved between the three temporary Dictionaries. After the battle, LINQ is used again to copy the cards back to the appropriate Dictionary fields. LINQ is also used to reverse the order of the cards and to concatenate the contents of Dictionaries. The temporary Dictionaries represent the state of the decks during the battle, while the original Dictionary fields represent the state of the decks prior to and proceeding a battle. I felt using this approach helped maintain the ‘state’ of the game, easier.
After each battle, and at the end of the game, the fields and properties that track the game’s status are updated to reflect the current game state — number on tricks, player wins, number of wars, etc. Statistics are returned to the UI from DisplayResults(). Contents of each deck are displayed as a comma-delimited string usingDisplayDeck(Dictionary< string, int >). The optional DataToClipboard() can be called to return the game’s statistics as a tab-delimited string for easy export to the clipboard and onto Excel for testing and statistic analysis.
Deck Weight
After completing the simulation, I read the PREDICTABILITY IN THE GAME OF WAR, an article by Jacob Haqq-Misra, which appeared online in Science Creative Quarterly. Reading Jocob’s article, I decided to add the deck weighting method he described to the simulation. The weight range for a player’s initial 26-card deck will be between +84 and -84. A deck with an ideal maximum weighting of +84 would have to contain only the highest values cards possible: 4 Aces, 4 Kings, 4 Queens, etc. down to and including 2 Eights. The weighting theory suggests that the higher a player’s deck weight, the higher the probability that player will win the game.
Interestingly (and logically), the opposite player’s deck (1/2 the original deck) will always have the inverse weight of the opposite player, since the weight of the entire 52-card deck is always 0 (zero). If Player One has a weight +25, Player Two’s deck will always have a weight of -25. This inverse rule is true even though the cards were shuffled, making each player’s decks totally random.
In the simulation, the DeckWeight(Dictionary<string, int>) method accepts a set of cards, to which it assigns individual cards, weights between -8 and +8, depending on the card’s face value (see code below). The DeckWeight(Dictionary<string, int>) method returns an integer representing the deck's weight — the sum of all the individual card’s weight.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.