Displaying Contents of a List

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 384 Views - Last Post: 03 May 2013 - 03:13 PM Rate Topic: -----

#16 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7629
  • View blog
  • Posts: 12,863
  • Joined: 19-March 11

Re: Displaying Contents of a List

Posted 03 May 2013 - 12:07 PM

You've got two almost-right ways of doing it bundled up into one wrong way.

either do a nested loop - deal one card to each hand, N times - or do your multiply and then take the modulo. Doing both is not correct.

        for(int i = 0; i < N.Hands; i++)
            hand[i].add(deck.remove(0));
            if( i == N.Hands)
                i = 0;
        }



The usual way to take the modulo is

i = i%numberOfHands;



But notice that doing it this way means your loop never terminates. Instead, you'd do

        for(int i = 0; i < numberOfHands*cardsPerHand; i++)
            hand[i%numberOfHands].add(deck.remove(0));
        }



Except you don't want to have that invariant calculated each time through the loop, it's wasteful. So:

  
        int cardsToDeal = numberOfHands*cardsPerHand;
        for(int i = 0; i < cardsToDeal ; i++)
            hand[i%numberOfHands].add(deck.remove(0));
        }


Was This Post Helpful? 0
  • +
  • -

#17 mkiser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 22-February 12

Re: Displaying Contents of a List

Posted 03 May 2013 - 12:15 PM

Will your way make so that hand[i] loops through 5 players and then return to the first one so it can add its sexond card? or will it just loop till 5 players
Was This Post Helpful? 0
  • +
  • -

#18 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,075
  • Joined: 05-April 11

Re: Displaying Contents of a List

Posted 03 May 2013 - 12:18 PM

What is the reason for dealing the 1st card to player1, 2nd card to player2 etc. ? You are creating an application, it is not like in real life
They won't be able to see if you deal the first or second card from the deck
..Unless you are ordering the cards in some special way of course

I still think you should add a Deck class
Make it simple, not more complex :)/>
public void dealCards(Deck deck, Player[] players, int cardsPerPlayer) {
	for (Player player : players) {
		Card[] cards = deck.take(cardsPerPlayer);
		player.dealCards(cards);
	}
}



Following your weird dealing 1st to player1 bla bla, I would still do like in my first post
public void dealCards(Deck deck, Player[] players, int cardsPerPlayer) {
	int playerIndex = 0;
	for (int i=0; i<cardsPerPlayer*players.length; i++) {
		Card card = deck.pull();
		players[playerIndex].dealCard(card);
		playerIndex = (playerIndex + 1) % players.length
	}
}



Edit: lets just add more weird solutions cause why not
public void dealCards(Deck deck, Player[] players, int cardsPerPlayer) {
	Card[] cards = deck.take(cardsPerPlayer * players.length);
	
	for (int i=0; i<cards.length; i++)
		players[i % players.length].dealCard(cards[i]);
}


This post has been edited by CasiOo: 03 May 2013 - 12:30 PM

Was This Post Helpful? 0
  • +
  • -

#19 mkiser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 22-February 12

Re: Displaying Contents of a List

Posted 03 May 2013 - 12:20 PM

the deck must stay in order. so it has to be like real life dealing.
Was This Post Helpful? 0
  • +
  • -

#20 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7629
  • View blog
  • Posts: 12,863
  • Joined: 19-March 11

Re: Displaying Contents of a List

Posted 03 May 2013 - 12:24 PM

View Postmkiser, on 03 May 2013 - 02:15 PM, said:

Will your way make so that hand[i] loops through 5 players and then return to the first one so it can add its sexond card? or will it just loop till 5 players



You should be able to satisfy yourself on that point. Examine the code and see what you think it does, step by step.
Was This Post Helpful? 0
  • +
  • -

#21 mkiser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 22-February 12

Re: Displaying Contents of a List

Posted 03 May 2013 - 03:13 PM

Thanks a lot evrybody. I will test out what I got from you guys later tonight.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2