The Form has 4 classes Card, Deck , Player and Game, game being the class that runs everything
The form has 3 TextBox named textName, textBooks and textProgress , ListBox named listHand, 2 buttons named buttonStart and buttonAsk
Card
classCard{publicValuesValue{ get;set;}publicSuitsSuit{get;set;}publicCard(Suits suit,Values value){this.Suit= suit;this.Value= value;}public string Name{
get {returnValue.ToString()+" of "+Suit.ToString();}}public override string ToString(){returnName;}publicstatic string Plural(Values value){if(value ==Values.Six)return"Sixes";elsereturn value.ToString()+"s";}}enumSuits{Spades,Clubs,Hearts,Diamonds,}enumValues{Ace=1,Two=2,Three=3,Four=4,Five=5,Six=6,Seven=7,Eight=8,Nine=9,Ten=10,Jack=11,Queen=12,King=13,}
Deck
classDeck{privateList<Card> cards;privateRandom random =newRandom();publicDeck(){
cards =newList<Card>();for(int suit =0; suit <=3; suit++)for(int value =1; value <=13; value++)
cards.Add(newCard((Suits)suit,(Values)value));}publicDeck(IEnumerable<Card> initialCards){
cards =newList<Card>(initialCards);}publicintCount{ get {return cards.Count;}}publicvoidAdd(Card cardToAdd){
cards.Add(cardToAdd);}publicCardDeal(int index){CardCardToDeal= cards[index];
cards.RemoveAt(index);returnCardToDeal;}publicvoidShuffle(){List<Card>NewCards=newList<Card>();while(cards.Count>0){intCardToMove= random.Next(cards.Count);NewCards.Add(cards[CardToMove]);
cards.RemoveAt(CardToMove);}
cards =NewCards;}publicIEnumerable<string>GetCardNames(){
string[]CardNames=new string[cards.Count];for(int i =0; i < cards.Count; i++)CardNames[i]= cards[i].Name;returnCardNames;}publicvoidSort(){
cards.Sort(newCardComparer_bySuit());}publicCardPeek(int cardNumber){return cards[cardNumber];}publicCardDeal(){returnDeal(0);}publicboolContainsValue(Values value){
foreach (Card card in cards)if(card.Value== value)returntrue;returnfalse;}publicDeckPullOutValues(Values value){Deck deckToReturn =newDeck(newCard[]{});for(int i = cards.Count-1; i >=0; i--)if(cards[i].Value== value)
deckToReturn.Add(Deal(i));return deckToReturn;}publicboolHasBook(Values value){intNumberOfCards=0;
foreach (Card card in cards)if(card.Value== value)NumberOfCards++;if(NumberOfCards==4)returntrue;elsereturnfalse;}publicvoidSortByValue(){
cards.Sort(newCardComparer_byValue());}}classCardComparer_bySuit:IComparer<Card>{publicintCompare(Card x,Card y){if(x.Suit> y.Suit)return1;if(x.Suit< y.Suit)return-1;if(x.Value> y.Value)return1;if(x.Value< y.Value)return-1;return0;}}classCardComparer_byValue:IComparer<Card>{publicintCompare(Card x,Card y){if(x.Value< y.Value)return-1;if(x.Value> y.Value)return1;if(x.Suit< y.Suit)return-1;if(x.Suit> y.Suit)return1;return0;}}
Player
classPlayer{private string name;public string Name{ get {return name;}}privateRandom random;privateDeck cards;privateTextBox textBoxOnForm;publicPlayer(String name,Random random,TextBox textBoxOnForm){this.name = name;this.random = random;this.textBoxOnForm = textBoxOnForm;this.cards =newDeck(newCard[]{});
textBoxOnForm.Text+= name +"has joined the game"+Environment.NewLine;}publicIEnumerable<Values>PullOutBooks(){List<Values> books =newList<Values>();for(int i =1; i<=13; i++){Values value =(Values)i;int howMany =0;for(int card =0; card < cards.Count; card++)if(cards.Peek(card).Value== value)
howMany++;if(howMany ==4){
books.Add(value);for(int card = cards.Count-1; card >=0; card--)
cards.Deal(card);}}return books;}publicValuesGetRandomValue(){Card randomCard = cards.Peek(random.Next(cards.Count));return randomCard.Value;}publicDeckDoYouHaveAny(Values value){Deck cardsIHave = cards.PullOutValues(value);
textBoxOnForm.Text+=Name+"has"+ cardsIHave.Count+""+Card.Plural(value)+Environment.NewLine;return cardsIHave;}publicvoidAskForACard(List<Player> players,int myIndex,Deck stock){Values randomValue =GetRandomValue();AskForACard(players, myIndex, stock, randomValue);}publicvoidAskForACard(List<Player>players,int myIndex,Deck stock,Values value){
textBoxOnForm.Text+=Name+"asks if anyone has a "+ value +Environment.NewLine;int totalCardsGiven =0;for(int i =0; i<players.Count; i++){if(i != myIndex){Player player = players[i];DeckCardsGiven= player.DoYouHaveAny(value);
totalCardsGiven +=CardsGiven.Count;while(CardsGiven.Count>0)
cards.Add(CardsGiven.Deal());}}if(totalCardsGiven ==0){
textBoxOnForm.Text+=Name+"must draw from teh stock."+Environment.NewLine;
cards.Add(stock.Deal());}}publicintCardCount{ get {return cards.Count;}}publicvoidTakeCard(Card card){ cards.Add(card);}publicIEnumerable<string>GetCardNames(){return cards.GetCardNames();}publicCardPeek(int cardNumber){return cards.Peek(cardNumber);}publicvoidSortHand(){ cards.SortByValue();}}
Game
classGame{privateList<Player> players;privateDictionary<Values,Player> books;privateDeck stock;privateTextBox textBoxOnForm;publicGame(string playerName,IEnumerable<string> opponentNames,TextBox textBoxOnForm){Random random =newRandom();this.textBoxOnForm = textBoxOnForm;
players =newList<Player>();
players.Add(newPlayer(playerName, random, textBoxOnForm));
foreach (string player in opponentNames)
players.Add(newPlayer(player, random, textBoxOnForm));
books =newDictionary<Values,Player>();
stock =newDeck();Deal();
players[0].SortHand();}privatevoidDeal(){
stock.Shuffle();for(int i =0; i <5; i++)
foreach (Player player in players)
player.TakeCard(stock.Deal());
foreach (Player player in players)PullOutBooks(player);}publicboolPlayOneRound(int selectedPlayerCard){Values cardToAskFor = players[0].Peek(selectedPlayerCard).Value;for(int i =0; i < players.Count; i++){if(i ==0)
players[0].AskForACard(players,0, stock, cardToAskFor);else
players[i].AskForACard(players, i, stock);if(PullOutBooks(players[i])){
textBoxOnForm.Text+= players[i].Name+"drew a new hand"+Environment.NewLine;int card =1;while(card <=5&& stock.Count>0){
players[i].TakeCard(stock.Deal());
card++;}}
players[0].SortHand();if(stock.Count==0){
textBoxOnForm.Text="The stock is out of cards.Game over!"+Environment.NewLine;returntrue;}}returnfalse;}publicboolPullOutBooks(Player player){IEnumerable<Values> booksPulled = player.PullOutBooks();
foreach (Values value in booksPulled)
books.Add(value, player);if(player.CardCount==0)returntrue;returnfalse;}public string DescribeBooks(){
string whoHasWhichBooks ="";
foreach (Values value in books.Keys)
whoHasWhichBooks += books[value].Name+"has a book of"+Card.Plural(value)+Environment.NewLine;return whoHasWhichBooks;}public string GetWinnerName(){Dictionary<string,int> winners =newDictionary<string,int>();
foreach (Values value in books.Keys){
string name = books[value].Name;if(winners.ContainsKey(name))
winners[name]++;else
winners.Add(name,1);}int mostBooks =0;
foreach (string name in winners.Keys)if(winners[name]> mostBooks)
mostBooks = winners[name];bool tie =false;
string winnerList ="";
foreach(string name in winners.Keys)if(winners[name]== mostBooks){if(!string.IsNullOrEmpty(winnerList)){
winnerList +="and";
tie =true;}
winnerList += name;}
winnerList +="with"+ mostBooks +"books";if(tie)return"A tie between"+ winnerList;elsereturn winnerList;}publicIEnumerable<string>GetPlayerCardNames(){return players[0].GetCardNames();}public string DescribePlayerHands(){
string description ="";for(int i=0; i < players.Count; i++){
description += players[i].Name+"hase"+ players[i].CardCount;if(players[i].CardCount==1)
description +="card."+Environment.NewLine;else
description +="cards."+Environment.NewLine;}
description +="The stock has"+ stock.Count+"cards left.";return description;}}
The Program
public partial classForm1:Form{publicForm1(){InitializeComponent();}privateGame game;privatevoid buttonStart_Click(object sender,EventArgs e){if(String.IsNullOrEmpty(textName.Text)){MessageBox.Show("Please enter your name"," Can't start the game yet");return;}
game =newGame(textName.Text,newList<string>{"Joe","Bob"}, textProgress);
buttonStart.Enabled=false;
textName.Enabled=false;
buttonAsk.Enabled=true;UpdateForm();}privatevoidUpdateForm(){
listHand.Items.Clear();
foreach (String cardName in game.GetPlayerCardNames())
listHand.Items.Add(cardName);
textBooks.Text= game.DescribeBooks();
textProgress.Text+= game.DescribePlayerHands();
textProgress.SelectionStart= textProgress.Text.Length;
textProgress.ScrollToCaret();}privatevoid buttonAsk_Click(object sender,EventArgs e){
textProgress.Text="";if(listHand.SelectedIndex<0){MessageBox.Show("Please select a card");return;}if(game.PlayOneRound(listHand.SelectedIndex)){
textProgress.Text+="The winner is..."+ game.GetWinnerName();
textBooks.Text= game.DescribeBooks();
buttonAsk.Enabled=false;}elseUpdateForm();}}
The Game runs fine, but my question here is , I can't understand how the method GetWinnerName( ) in the class Game is working
In this loop
foreach (Values value in books.Keys)
{
string name = books[value].Name;
if (winners.ContainsKey(name)) //What is ContainsKey ?
winners[name]++; // What is happening here ?
else
winners.Add(name, 1); // What is happening here ?
}
Question
Ch33f
The Form has 4 classes Card, Deck , Player and Game, game being the class that runs everything
The form has 3 TextBox named textName, textBooks and textProgress , ListBox named listHand, 2 buttons named buttonStart and buttonAsk
Card
Deck
Player
Game
The Program
The Game runs fine, but my question here is , I can't understand how the method GetWinnerName( ) in the class Game is working
In this loop
foreach (Values value in books.Keys)
{
string name = books[value].Name;
if (winners.ContainsKey(name)) //What is ContainsKey ?
winners[name]++; // What is happening here ?
else
winners.Add(name, 1); // What is happening here ?
}
Link to comment
https://www.neowin.net/forum/topic/1328866-having-problems-understanding-how-dictionary-is-working-in-this-program/Share on other sites
2 answers to this question
Recommended Posts