Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Given this sample code

import java.util.ArrayList;
import blackjack.model.items.Card;

public class BlackJackPlayer extends Player {

    private double bet;

    private Hand hand01 = new Hand();
    private Hand hand02 = new Hand();


    public void addCardToHand01(Card c) {
        hand01.addCard(c);
    }

    public void addCardToHand02(Card c) {
        hand02.addCard(c);
    }

    public void bustHand01() {
        hand01.setBust(true);
    }

    public void bustHand02() {
        hand02.setBust(true);
    }

    public void standHand01() {
        hand01.setStand(true);
    }

    public void standHand02() {
        hand02.setStand(true);
    }

    public boolean isHand01Bust() {
        return hand01.isBust();
    }

    public boolean isHand02Bust() {
        return hand02.isBust();
    }

    public boolean isHand01Standing() {
        return hand01.isStanding();
    }

    public boolean isHand02Standing() {
        return hand02.isStanding();
    }

    public int  getHand01Score(){ return hand01.getCardScore(); }

    public int  getHand02Score(){ return hand02.getCardScore(); }

}

Is this considered as a repetitive code? providing that each method is operating a seperate field but doing the same implementation ? Note that hand01 and hand02 should be distinct.

if this is considered as repetitive code, how would I address this? providing that each hand is a seperate entity

share|improve this question

1 Answer 1

up vote 8 down vote accepted

Your mixing the game code with the player class. You should have a class for the player and one for the game(BlackJack). You need to start with something like this:

public class Player 
{
    private double bet;
    private string name;

    public Hand hand = new Hand();

    public Player(string name)
    {
         this.name = name'
    }

    public Hand getHand(){
        return hand;
    }

}

public class BlackJack
{
     private List<Player> players = new ArrayList<Player>(); ;

     public BlackJack(Player player1, Player player2)
     {
         players.add(player1);
         players.add(player2);
     }

     public void bustHand(int playerNum) throws IndexOutOfBoundsException 
     {
         players.get(playerNum).getHand().setBust(true); 
     }     

}

I am coding on the spot so it will not working 100% but should give you an idea

share|improve this answer
    
I have another class does this exact same thing, in this class I made the user have two hands just in case he is eligible for split. –  KyelJmD Oct 22 '12 at 2:36
1  
You can add a list of hands to the player class like list<Hands> hands; To do that. –  hbrock Oct 22 '12 at 2:42
    
so it's better to have it as a list? –  KyelJmD Oct 22 '12 at 2:43
3  
Yes, writing the same code over and over is real bad. –  hbrock Oct 22 '12 at 2:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.