Displaying Contents of a List

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 mkiser  Icon User is offline

  • New D.I.C Head

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

Displaying Contents of a List

Posted 03 May 2013 - 08:02 AM

Hi,

I need help with this algorithm that I am banging my head on.
I have a list containg 52 cards from a deck of cards. I have multiple hands that I need to deal the cards to. What I want to do is deal the first card to the first hand, deal the second card to the second hand and so on. I cannot seem to accomplish this.

I have tried skipping cards in my list and going back to the start, but that seems ugly.

Any tips for this?

Thank you,

M

Is This A Good Question/Topic? 0
  • +

Replies To: Displaying Contents of a List

#2 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 - 08:08 AM

Just someore info. The deck needs to be not shuffled so I really need the fisrt card to the first player and the second card to the second player and so on.
Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1428
  • View blog
  • Posts: 3,172
  • Joined: 05-April 11

Re: Displaying Contents of a List

Posted 03 May 2013 - 08:17 AM

You need to deal just one card to each player or the whole deck?
If it is the whole deck, you can do something like
public void dealCards() {
	int playerIndex = 0;
	while (!deck.isEmpty()) {
		Card card = deck.pull();
		players[playerIndex].addCard(card);
		playerIndex = (playerIndex + 1) % players.length
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 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 - 08:19 AM

its 5 cards to each player. So its one card to each player.
Was This Post Helpful? 0
  • +
  • -

#5 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Displaying Contents of a List

Posted 03 May 2013 - 08:21 AM

Quote

its 5 cards to each player. So its one card to each player.

I accidentally my fish when I read that. Thanks a lot, pal.

I'm guessing that you have classes for the cards, and for the players. You could always arrange the cards in an Array structure, and as you hand them out to players, remove them from the array (deck).

List<Card> deck = new ArrayList<Card>();
List<Player> players = new ArrayList<Player>();

public void deal(List<Card> deck, List<Player> players)
{
    outOfCards:
    while(deck.size() > 0)
    {
        for(int i = 0; i < players.size(); i++)
        {
            try {
                player.dealCard(deck.remove(0));
            } catch (Exception e) {
                //e.printStackTrace();
                break outOfCards:
            }
        }
    }
}


This post has been edited by Gungnir: 03 May 2013 - 08:23 AM

Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1428
  • View blog
  • Posts: 3,172
  • Joined: 05-April 11

Re: Displaying Contents of a List

Posted 03 May 2013 - 08:23 AM

View Postmkiser, on 03 May 2013 - 03:19 PM, said:

its 5 cards to each player. So its one card to each player.


Then it is even more simple
One loop iterating over the players will do
for (Player player : players)
    player.addCard(deck.pull());



Edit: wait what...

Quote

its 5 cards to each player

Quote

So its one card to each player.

I'm confused

This post has been edited by CasiOo: 03 May 2013 - 08:25 AM

Was This Post Helpful? 0
  • +
  • -

#7 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 - 08:28 AM

I am new to Java. Ove used C++ for a while and Im just confused on how you print what each player has in their hand?

Itis the whole deck, but each player can only have 5 cards.
Was This Post Helpful? 0
  • +
  • -

#8 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 - 08:39 AM

View PostCasiOo, on 03 May 2013 - 08:17 AM, said:

You need to deal just one card to each player or the whole deck?
If it is the whole deck, you can do something like
public void dealCards() {
	int playerIndex = 0;
	while (!deck.isEmpty()) {
		Card card = deck.pull();
		players[playerIndex].addCard(card);
		playerIndex = (playerIndex + 1) % players.length
	}
}


Hows the .pull() implemented? Is it one of the methods for a list?
Was This Post Helpful? 0
  • +
  • -

#9 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1428
  • View blog
  • Posts: 3,172
  • Joined: 05-April 11

Re: Displaying Contents of a List

Posted 03 May 2013 - 08:46 AM

View Postmkiser, on 03 May 2013 - 03:39 PM, said:

View PostCasiOo, on 03 May 2013 - 08:17 AM, said:

You need to deal just one card to each player or the whole deck?
If it is the whole deck, you can do something like
public void dealCards() {
	int playerIndex = 0;
	while (!deck.isEmpty()) {
		Card card = deck.pull();
		players[playerIndex].addCard(card);
		playerIndex = (playerIndex + 1) % players.length
	}
}


Hows the .pull() implemented? Is it one of the methods for a list?


No, I just supposed your Deck class has a method to take one card from the deck :)
Was This Post Helpful? 0
  • +
  • -

#10 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Displaying Contents of a List

Posted 03 May 2013 - 08:49 AM

Why not set up a few methods so that you can call that information in a print call?
for(Player player : players)
{
    System.out.println(player.getName() + "\'s hand: ");
    for(Card card : player.getHand())
        System.out.print(card.toString());
    System.out.println();
}


Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8037
  • View blog
  • Posts: 13,753
  • Joined: 19-March 11

Re: Displaying Contents of a List

Posted 03 May 2013 - 09:44 AM

View Postmkiser, on 03 May 2013 - 10:28 AM, said:

Itis the whole deck, but each player can only have 5 cards.


I'm getting more confused, not less.
Was This Post Helpful? 0
  • +
  • -

#12 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Displaying Contents of a List

Posted 03 May 2013 - 09:54 AM

A 52 card deck, an undisclosed number of players, and each player can have 5 cards in their hand. Correct?
Was This Post Helpful? 0
  • +
  • -

#13 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 - 10:20 AM

yeap
Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8037
  • View blog
  • Posts: 13,753
  • Joined: 19-March 11

Re: Displaying Contents of a List

Posted 03 May 2013 - 10:30 AM

And what is the Deck like? If it's a list, you would just pop cards off it one at a time. List.remove(0) will remove the first item from a list and return it.
Was This Post Helpful? 0
  • +
  • -

#15 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 - 11:47 AM

I dont have a compiler available to me right now but what do you guys think about this.


List<Hand> hand = ArrayList<Hand>();

public void dealCards()
{
	int j= 0;
	while(j < N.Hands * N.Cards.Per.Hand)
	{
		for(int i = 0; i < N.Hands; i++)
		{
			hand[i].add(deck.remove(0));
			if( i == N.Hands)
				i = 0;
		}
	j++;
	}
}


That should give me the top card to the first hand, the second card to the second hand and so on. And this will return to the first hand to give its second card. It will finish when 5 cards per player have been delt.


What do you guys think?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2