Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

logic problem with While loop and method in it

Status
Not open for further replies.

Triacona

Technical User
Jun 11, 2009
462
GB
Dear All,
Thanks for a great forum [smile]

I have the following problem at understanding some logic, it seems simple from just looking at the while loop, but the IsCardAlreadyDealt method is throwing a spanner in the
while (this.IsCardAlreadyDealt(suit, value))
{
value = (Value)randomCardSelector.Next(CardsPerSuit);​
}

Please see my class below.
Basically I want to know why the while (above) creates a random value, based on the IsCardAlreadDealt method.
As far as I understand it the IsCardAlreadDealt method returns !result
so if the card does exist (given by the exists method a bool which if exists will be true
the ! would convert that to a false, therefore card not dealt??
so if it does exist the IsAlreadyDealtMethod returns a false and then the while does not execute...
but if it does not exist returns true and while executes why??
I thought the while was there to generate a new random card if the card did exist in the pack...

Any help would be greatly appreciated [bigsmile]
Thank you [wavey3][pc2]

and the while would not execute and .'. not generate a random card...and .'. no you'd have the card already dealt and not a new one.


using System;
using System.Collections.Generic;

namespace Cards
{
class Pack
{
public const int NumSuits = 4;
public const int CardsPerSuit = 13;
// creating a Dictionary set of Suits and PlayingCards
// in the form of a enum Suit as the Key and a List of PlayingCard as value
// value is the list of cards in that suit


private Dictionary<Suit,List<PlayingCard>> cardPack;
private Random randomCardSelector = new Random();

// constructor
public Pack()
{
// so if you use the Suit key you will get the List of cards associated with it
// the cardPack will have 4 suits in it and a list of cards for each suit

this.cardPack = new Dictionary<Suit, List<PlayingCard>>(NumSuits);

// for loop: sets the suit variable to the lowest value (enums can determine which is the lowest in the List
// while suit <= Suit.Spades (highest value) the suit variable will go up 1 each time the loop is iterated
// enums can also be incremented with an int
// once the inner loop has iterated 13 times, for the Suit.Clubs the loop
// will move to the next suit Suit.Diamonds etc.

for (Suit suit = Suit.Clubs; suit <= Suit.Spades; suit++)
{
// declares a new list of PlayingCard, and there are 13 playing cards
// list (cardsInSuit) now has methods (add,remove etc)

List<PlayingCard> cardsInSuit = new List<PlayingCard>(CardsPerSuit);

// for loop: Value value enum is assigned Value.Two (lowest value)
// while value <= Value.Ace (highest) value increments as per enum explaination above
// this loop will iterate through all card values for the suit it the above for loop
// .'. two till ace for suit of clubs and so on

for (Value value = Value.Two; value <= Value.Ace; value++)
{
// to add a new PlayingCard object new keyword needed
// the PlayingCard class has a constructor that takes a
// suit and value parameters
// which at first iteration will be clubs and two
// this will then eventually fill the cardsInSuit List with 13 PlayingCard
// with suit and value for each

cardsInSuit.Add(new PlayingCard(suit, value));
}
// this adds the key of Suit.Clubs in the first iteration as well as the cardsInSuit List which has
// a list of PlayingCard with their suit (clubs 1st iteration) and value (two till ace)

this.cardPack.Add(suit, cardsInSuit);

}
}

public PlayingCard DealCardFromPack()
{
// adds a random value to a suit using a cast for randomCardSelector which is of type Random
// this cycles through the suit types randomly using NumSuits(4) to specify the maximum random number

Suit suit = (Suit)randomCardSelector.Next(NumSuits);

// while the suit is empty checks if the card has been dealt
// if it has it returns a false which means it has not used this card and it can be used
// if true then the random card selector is run again to select another random card
// which is checked to see if the card has been dealt (method within IsSuitEmpty)
// if true then exits, because card is random and not been used, if false cycle begins again

while (this.IsSuitEmpty(suit))
{
suit = (Suit)randomCardSelector.Next(NumSuits);
}

// adds a random value to a value variable using the random card selector
// restricts the max value to 13 (cardsPerSuit)

Value value = (Value)randomCardSelector.Next(CardsPerSuit);

// checks whether the card is already dealt takes the random suit variable created and the random
// value already created are the parameters for IsCardAlreadyDealt
// the method checks if the suit and card match values that have been dealt
// the IsCardReadyDealt takes the random suit and value
// creates a List<PlayingCard> based on the cardPack[suit] postion
// it then checks if the card exists and returns a true or false this true/false has ! operator applied
// in the IsCardAlreadyDealt method
// so if card exists !true(false) | if does not exist !false(true)
// while true (card dealt) creates random value // while executes until statement false

while (this.IsCardAlreadyDealt(suit, value))
{
value = (Value)randomCardSelector.Next(CardsPerSuit);
}
// creates a List of PlayingCard that gets assigned a List from the random suit within the
// Dictionary<suit,List<PlayingCard>> cardPack
// you can use array notation to access this list using the suit as the key (this being the random suit)

List<PlayingCard> cardsInSuit = this.cardPack[suit];
// create a PlayingCard obj card to take the found value of the card
PlayingCard card = cardsInSuit.Find((PlayingCard c) => { return c.CardValue == value; } );
// this card is then removed from cardsInSuit
cardsInSuit.Remove(card);
// this returns the found card (PlayingCard obj)
return card;
}

private bool IsSuitEmpty(Suit suit)
{
bool result = true;

// the below if is iterated trough 13 times (all values of cards)
// this checks to see if the suit and suit card value have already been dealt

for (Value value = Value.Two; value <= Value.Ace; value++)
{
// if IsCardAlreadyDealt returns a false it will make true .'. return a false value
// if IsCardAlreadyDealt returns a true it will make false .'. return a true value
// so if the card does exist (as below) then the IsCardAlready dealt returns true
// .'. if(true) result = false, or card does not exist if(false) result = true

if (!IsCardAlreadyDealt(suit, value))
{
result = false;
break;
}
}

return result;

}

private bool IsCardAlreadyDealt(Suit suit, Value value)
{
// creates a List of PlayingCard cardsInSuit at position suit in the cardPack Dictionary obj
// which has a List of PlayingCard according to the key (suit)

List<PlayingCard> cardsInSuit = this.cardPack[suit];

// returns if not exists if PlayingCard c c.CardSuit == suit and c.CardValue == value
// returns that the card does not exist
// lambada expression returns a true false value based on suit and value
// if suit and value match that PlayingCard c then returns true if not false
// the ! creates the oposite of the true/false so if true the card exists then it returns false else true
// so if card does exist returns false if does not exist returns true

return (!cardsInSuit.Exists((PlayingCard c) => {return c.CardSuit == suit && c.CardValue == value; }));
}
}
}

Thank you,

Kind regards

Triacona
 
Is it:
while(card does not exist)
{
create random value
}
??
Thanks [smile]

Thank you,

Kind regards

Triacona
 
Is the while then meant only to check if there is no card and if there is create a new random one?
.'. not looking to create a random value if the card has already been dealt?
Thanks [smile]

Thank you,

Kind regards

Triacona
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top