Yop
Yop

Reputation: 71

While Loop And Condition C#

I'm having trouble with something that should seem so simple. I'm using a conditional and on a pretest loop While and it doesn't seem to even execute one because the conditions are met but they're not.

I have but the loop never seems be met/ when I break on it. It just skips over it

int sorter = random.Next(0, 10);
bool player1full = false;
bool player2full = false;

while (player1full && player2full == false)
{
    if (chuckcards[sorter] != null)
    {
         while (player1full != true)
         {
              if (player1.Count != 5)
              {
                   player1.Enqueue(chuckcards[sorter]);
                   chuckcards[sorter] = null;
              }
              else
              {
                   player1full = true;
              }

              sorter = random.Next(0, 10);
         }

         while (player2full != true)
         {
              if (chuckcards[sorter] != null)
              {
                   if (player2.Count != 5)
                   {
                        player2.Enqueue(chuckcards[sorter]);
                        chuckcards[sorter] = null;
                   }
                   else
                   {
                        player2full = true;
                   }

                   sorter = random.Next(0, 10);
               }
          }
     }
     else
     {
          sorter = random.Next(0, 10);
     }
}

My logic maybe slightly off and I'm just wanting someone to point me in the right direction/see my error.

Thank you

Upvotes: 0

Views: 522

Answers (5)

p.s.w.g
p.s.w.g

Reputation: 149000

It will never enter the loop because here:

bool player1full = false;
bool player2full = false;
while (player1full && player2full == false)

This will test the Boolean value of player1full, and if it's true, then test the Boolean value of player2full == false. Since player1full is false, it stops right there and never enters the loop. I think what you want is:

while (player1full == false && player2full == false)

Or equivalently

while (!player1full && !player2full)

Or even (by De Morgan's Law):

while (!(player1full || player2full))

However, it seems like the entire outer loop is unnecessary. I can't be entirely sure without known the full context of your program (and that's out of scope for this question), but it could be rewritten as:

int sorter;
while (player1.Count != 5)
{
    sorter = random.Next(0, 10);
    if (chuckcards[sorter] != null)
    {
        player1.Enqueue(chuckcards[sorter]);
        chuckcards[sorter] = null;
    }
}

while (player2.Count != 5)
{
    sorter = random.Next(0, 10);
    if (chuckcards[sorter] != null)
    {
        player2.Enqueue(chuckcards[sorter]);
        chuckcards[sorter] = null;
    }
}

Upvotes: 8

YoryeNathan
YoryeNathan

Reputation: 14502

It seems that everyone is thinking that you want to loop as long as both are false. But according to the logic, it looks to me that you only want one of them to be not-full.

If that is indeed the case, the condition should be !player1full || !player2full.

The reason I think that you might want only one to be not-full is that one may be full but the other one still needs handling. Not sure though. Depends on how you randomly distribute the cards to players, or whatever... And you seem to have edited that part out.

By the way, your shuffling method is horrible. Here is a neat and simple example:

List<string> cards = new List<string> { "1", "2", "3", ... , "10", "J", "Q", "K" };
List<string> shuffled = cards.Select(x => new { X = x, Y = random.Next() })
                             .OrderBy(x => x.Y)
                             .Select(x => x.X).ToList();

This isn't tested, since I don't have VS now, but the idea is to match every card to a random number, sort according to the random numbers, and then lose the extra information (that random number) so you have a shuffled list of cards.

So bottom line, you can just have a list of a player's cards, and shuffle it. Or you could have a full deck, shuffle, and take top 5 for each player. Whichever you choose, you want to shuffle them nicely.

Example for dealing cards after full-deck shuffling:

for (var i = 0; i < CARDS_PER_PLAYER; i++)
{
    foreach (var player in players)
    {
        // Shouldn't happen, if code is carefully planned
        if (cards.Count == 0) throw new ApplicationException("Out of cards!");

        // Deal the top card
        player.Enqueue(cards[0]);
        // Remove from deck, doh! Card can't be in deck AND in player's hand anyways
        cards.RemoveAt(0);
    }
}

Upvotes: 2

Obama
Obama

Reputation: 2612

You should write :

while(!player1full && !player2full)

good luck!

Upvotes: 0

gareththegeek
gareththegeek

Reputation: 2418

This will never be true:

bool player1full = false;
bool player2full = false;
while (player1full && player2full == false)

Maybe you meant:

while (player1full == false && player2full == false)

Upvotes: 1

The problem is here:

bool player1full = false;
bool player2full = false;
while (player1full && player2full == false)

that is not equivalent to

while(player1full == false && player2full == false)

the == operator results in a new boolean, so thus the following is valid:

bool myBool = num1 == num2

What your condition essentially breaks down to is

(false && (false == false))

which reduces to

(false && true)

which reduces to

(false)

Upvotes: 3

Related Questions