0

EDIT2: Sorry all... I believe it is due to the lack of understanding of question that cause this misconception. After reading through, I think what they want is for the return value of getWinningPoint() be the biggest number among the players and yet still <=21. so that in the game output, can loop each player to get their card point again and compare it to this winningpoint. I thank all of your input and help. Moderator or Admin can close this thread. Thanks again.

I would like to find out how to access the particular object in the arraylist so that i can cast the method on it. In a overall view, I am able to make method that apply to all items in the arraylist of players (distributeCardsToPlayer). But my 2nd method of getWinningPoints() is a int that sum up all the cards the particular player in arraylist players have. The winningPoint is a individual result which will ultimately be used in printWinners() method. I'm only familiar with accessing a obj with "Player player = players.get(0);" but in this case the "player" itself will be calling getWinningPoints() to check their own result.

P.S - I am not sure how to put it properly,and hopefully someone can point me to the right direction.

import java.util.*;
public class ModifiedBlackJack
{
protected ArrayList<Card> gameDeck;
protected ArrayList<Player> players;

public ModifiedBlackJack(ArrayList<Player> players)
{
    this.players=players;
}

public void distributeCardsToPlayers()
{
    Scanner console = new Scanner(System.in);
    for (Player player : players)
    {
        player.drawACard(getACardFromDeck());
        player.drawACard(getACardFromDeck());
        System.out.println(player.getName()+": " + player.toString()); 
        System.out.print("Draw another card? (y/n): ");
        char input = console.nextLine().toLowerCase().charAt(0);
        if(input == 'y')
        {
            player.drawACard(getACardFromDeck());
        }
    }

EDIT2: After reading through, I think what they want is for the return value of getWinningPoint() be the biggest number among the players and yet still <=21. so that in the game output, can loop each player to get their card point again and compare it to this winningpoint.

public int getWinningPoints()
{
    int wp=0;;
    int point=0;
    for (Player player:players)
    {
        point = player.getCardsPoints();
        if (point>=21 && point>wp)
        {
            wp=point;
        }
    }
    return wp;
}

In the Player class, there is a function for summing up all the cards point

public int getCardsPoints()
{
    int point=0;
    for (Card c: cards)
    {
        point=point+c.getPoints();
    }
    return point;
}

I am new to java and any help or guidance is very much appreciated. Thank You

2
  • To clarify: your goal is to print out the points for all players at the end of the game, correct? Commented Mar 25, 2017 at 4:53
  • in printWinners(int min) it will print out the summed point. but to get the summed point is through the getWinningPoint() method. but i am unsure how to build the getWinningPoint() method because I don't have a clear object to reference the method from my Player class Commented Mar 25, 2017 at 5:01

2 Answers 2

0

You may be overthinking this, and the method getWinningPoints isn't entirely required.

Because you already have getCardsPoints declared in Player, and you already have an instance of Player to work with in your loop, the only thing you realistically need to do is...invoke it.

System.out.println(player.getName() + "Chips: " + player.getChips() + "[]" + player.getCardsPoints());

Whatever conditions you need to satisfy the min parameter should be done inside of this loop; that is, conditionally print the values that are larger than min.

Sign up to request clarification or add additional context in comments.

2 Comments

unfortunately the method getWinningPoints() is part of the requirement... but yes, I agree entirely with you. That is what I would have work towards/done if there is no requirement for the getWinningPoints() method.
thank you, it seems i really over think. getWinningPoint() should only get me a int value to compare with player.getCardsPoints() in my loop. And if they are equal, my noOfWinner will ++.
0

If you want to invoke a Player class method you need to have a player object to call a method that it "owns".

Pass the player object to the method and accept the player object in getWinningPoints().

Call

getWinningPoints(player)

Declaration

public int getWinningPoints(Player localPlayer)
{
   return localPlayer.getCardsPoints();
}

3 Comments

base on the requirements, theres no input for the method. so i assume in the main app output will go something similar to "int result = player.getWinningPoints()" so im trying to figure out if it is possible for the method to self access base on who call it.
You have access to the players arraylist. So you could loop through the players arraylist in getWinningPoints() so you don't have to pass anything to it..
the looping of player through the arraylist will be done else where. so when the method is called, it will be the particular player calling. but how will the method know which player is using it? like in distribute method, there is a loop, so all player will get the cards. but in winningPoint, only 1 player will be calling it at a time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.