2013-06-11 23 views
0
的球員數組

我需要做的是能夠在我的視圖類中調用播放器數組,打印輪到誰和他們的分數。當他們輪到結束時,轉到下一位玩家並打印該玩家得分。當它回到玩家1的轉身時,它會帶回他們的總分並加入到這一點中。Java - 類

最終目標: 玩家和模子類是一個數組。我的死亡和評分方法相當不錯。這是貪婪/ farkle的遊戲。一名玩家擲骰6死,並以此爲基礎得分。未使用/未加芯的模具然後滾動。在球員回合結束時,我們必須打印出他們的回合得分和總得分。轉移到下一位玩家,並重新進行。當某個玩家達到某個分數時,遊戲結束,該人是贏家。

現在我只有一個if語句循環4個玩家。這不是目標。我需要將玩家類放入一個數組中,並使用該類來循環玩家,並可能使用它來報告他們的遊戲得分。

public class Player { 

/** player id */ 
private int id; 

/** player name */ 
private String name; 

/** player's score in the game */ 
private int gameScore; 

/** 
* Constructs a new Player with the specified id. 
* 
* @param id player's id 
*/ 
public Player(int id) { 
    this.id = id; 
} 

/** 
* Returns the id of player. 
* 
* @return player's id 
*/ 
public int getId() { 
    return id; 
} 

/** 
* Returns the name of player. 
* 
* @return player's name 
*/ 
public String getName() { 
    return name; 
} 

/** 
* Sets the name of player using the given parameter value. 
* 
* @param name value used to set the name of player 
*/ 
public void setName(String name) { 
    this.name = name; 
} 

/** 
* Returns the player's score in the game. 
* 
* @return player's score in the game 
*/ 
public int getGameScore() { 
    return gameScore; 
} 

/** 
* Sets the game score of a player. 
* 
* @param score value used to set the game score of player 
*/ 
public void setGameScore(int score) { 
    this.gameScore = score; 
} 

/** 
* Returns a String representing a player. 
* 
* @return string form of this player 
*/ 
public String toString() { 
    return id + ""; 
} 

}

public class Die { 

/** maximum face value */ 
private static final int MAX = 6; 

/** current value showing on the die */ 
private int faceValue; 

/** 
* Constructs a Die instance with a face value of 1. 
*/ 
public Die() { 
    faceValue = 1; 
} 

/** 
* Computes a new face value for this die and returns the result. 
* 
* @return face value of die 
*/ 
public int roll() { 
    faceValue = (int) (Math.random() * MAX) + 1; 
    return faceValue; 
} 

/** 
* Sets the face value of the die. 
* 
* @param value an int indicating the face value of the die 
*/ 
public void setFaceValue(int value) { 
    if (value > 0 && value <= MAX) { 
     faceValue = value; 
    } 
} 

/** 
* Returns the face value of the die. 
* 
* @return the face value 
*/ 
public int getFaceValue() { 
    return faceValue; 
} 

}

import java.util.*; 

import model.Die; 
import model.Player; 
import model.GreedGame; 

public class GreedCLI { 

private int whoInt; 
private int farkleScore; 
private Player playerClass; 
private GreedGame farkle; 
private Scanner scan = new Scanner(System.in); 
private final int max = 4; 
private final int min = 2; 
private final int minStarter = 1; 
private final int minScore = 1000; 
private final int maxScore = 10000; 
int toWin, totalPlayers, player; 
public GreedCLI() { 
    startUp(); 
    gameOn(); 
} 

public void startUp() { 
    System.out.println("Welcome to Farkle! \n \n"); 

    totalPlayers = getPlayerTotal("Please enter total number of player (2-4): "); 
    toWin = getScoreTotal("Please enter total points needed to win: "); 
    player = getStartPlayer(); 

    System.out.println("\nGood Luck!\n"); 
    farkle = new GreedGame(totalPlayers, player, toWin); 
} 

private int getStartPlayer() { 
    int who; 
    while (true) { 
     try { 
      System.out.print("Which player will start the game(1-" 
        + totalPlayers + ")?: "); 
      who = Integer.parseInt(scan.nextLine()); 

      if (who < minStarter || who > totalPlayers) { 
       System.out.println("Error - values outside parameter."); 
      } else { 
       break; 
      } 
     } catch (InputMismatchException ex) { 
      scan.next(); 
      System.out.println("Error - input must be an integer value."); 
     } 
    } 
    return who; 
} 

private int getPlayerTotal(String enter) { 
    int playerTotal; 
    while (true) { 
     try { 
      System.out.print(enter + ""); 
      playerTotal = Integer.parseInt(scan.nextLine()); 
      if (playerTotal < min || playerTotal > max) { 
       System.out.println("Error - values outside parameter."); 
      } else { 
       break; 
      } 
     } catch (InputMismatchException ex) { 
      scan.next(); 
      System.out.println("Error - input must be an integer value."); 
     } 
    } 
    return playerTotal; 
} 

private int getScoreTotal(String enter) { 
    int scoreTotal; 
    while (true) { 
     try { 
      System.out.print(enter + ""); 
      scoreTotal = Integer.parseInt(scan.nextLine()); 
      if (scoreTotal < minScore || scoreTotal > maxScore) { 
       System.out.println("Error - values outside parameter."); 
      } else { 
       break; 
      } 
     } catch (InputMismatchException ex) { 
      scan.next(); 
      System.out.println("Error - input must be an integer value."); 
     } 
    } 
    return scoreTotal; 
} 

// public int scoreTotal() { 
//  int playerScore = playerClass.getGameScore() + farkleScore; 
//  return playerScore; 
// } 

private void gameOn() { 
    boolean over = false; 
    boolean endTurn = false; 
    String answer; 
    char answerChar = 'Y'; 

    String roll; 

    System.out.println("Player " + farkle.getPlayers() + "'s turn!"); 

    while (!over) { 

     while (!endTurn) { 
      roll = farkle.toString(); 
      farkleScore = farkle.score(); 
      System.out.println("Player " + farkle.getPlayers() + " rolls " 
        + roll + " worth " + farkleScore); 

      if (farkleScore < 1) { 
       System.out 
         .println("Sorry, you rolled a 0. Moving on to the next Player!"); 
       endTurn = true; 
      } 
      if (farkle.availableDie() < 1) { 
       System.out 
         .println("Sorry, you are out of dice. Moving on to the next Player!"); 
       endTurn = true; 
      } 
      if (farkle.availableDie() > 1 && farkleScore > 1) { 
       System.out 
         .print("Would you like to keep rolling? You have " 
           + farkle.availableDie() 
           + " die remaining (Y or N): "); 
       answer = scan.nextLine().trim().toUpperCase(); 
       answerChar = answer.charAt(0); 
       if (answerChar == 'N') { 
        endTurn = true; 
       } 
      } 

     } 
     while (endTurn) { 
      System.out.println("\nNow it is the next player's turn."); 
      farkle.passDie(); 
      farkle.nextPlayer(); 
      endTurn = false; 
     } 
    } 
} 

public static void main(String[] args) { 
    new GreedCLI(); 
} 

}

package model; 

import java.util.*; 

public class GreedGame { 

/** int for total die remaining */ 
private int remainingDie = 6; 

/** counts number of times number appears */ 
private int[] numFreq; 

/** array for players */ 
private int players = 1; 

/** call player class */ 
private Player playerFromClass; 

/** array for die */ 
private int[] die; 


private Player[] who; 
private int whoInt; 

/** total players */ 
private int totalPlayers; 

/** starting player */ 
private int currentPlayer; 

/** total number of points needed */ 
private int winningPoints; 

/** calls player method to get turn */ 
private Player turn; 

/** score for the turn */ 
private int turnScore = 0; 

/** score for the turn */ 
private int totalScore; 

/** string for the roll result for the toString */ 
private String rollResult; 

/** calls class to roll the die */ 
Die dieRoll = new Die(); 


/***************************************************************** 
* Default constructor, sets the values of the instance variables 
* 
* @param players 
*   and winning points pulled from CLI 
*****************************************************************/ 

public GreedGame(int totalPlayers, int firstPlayer, int winningPoints) { 
    super(); 
    this.totalPlayers = totalPlayers; 
    this.currentPlayer = firstPlayer; 
    this.winningPoints = winningPoints; 
} 

public Player playerClass() { 
    return playerFromClass; 
} 

public int getPlayers() { 
    return players; 
} 

/* private Player[] getStartPlayerClass() { 
    for(int i = 0; i < totalPlayers; i++){ 
     i = this.currentPlayer++; 
    } 
    return who; 
}*/ 

public void nextPlayer(){ 
    if(players < 1){ 
     players = 1; 
    } 
    else if(players < 4){ 
     players++; 
    } 
    else{ 
     players = 1; 
    } 
} 

/*private int getStartPlayerInt(){ 
    whoInt = who.getId(); 
    return whoInt; 
}*/ 

public Player getTurn(){ 
    return turn; 
} 

public void setPlayers(int players) { 
    this.players = players; 
} 

/***************************************************************** 
* calculates remaining die 
*****************************************************************/ 

public int availableDie() { 
    return this.remainingDie; 
} 

/***************************************************************** 
* boolean to passDie 
*****************************************************************/ 

public void passDie() { 
    this.remainingDie = 6; 
} 

/***************************************************************** 
* array to roll the remaining dice 
*****************************************************************/ 

public int[] rollDie() { 
    this.die = new int[this.remainingDie]; 

    for (int i = 0; i < this.die.length; i++) { 
     this.die[i] = dieRoll.roll(); 
    } 

    return this.die; 
} 

/***************************************************************** 
* toString for the cli to call, puts roll in string. 
*****************************************************************/ 

public String toString() { 
    rollResult = Arrays.toString(rollDie()); 
    return rollResult; 
} 

/***************************************************************** 
* score method to add up total points and can be called elsewhere 
*****************************************************************/ 

public int score() { 
    rollCheck(); 
    turnScore = 0; 
    return straight() + threePairs() + sixOfAKind() + fiveOfAKind() 
      + fourOfAKind() + threeOfAKind() + eachFive() + eachOne(); 
} 

/***************************************************************** 
* array to roll the remaining dice 
*****************************************************************/ 

public int[] rollCheck() { 
    availableDie(); 
    this.numFreq = new int[6]; 
    for (int i = 0; i < 6; i++) { // set to zero 
     this.numFreq[i] = 0; 
    } 

    for (int i = 0; i < this.remainingDie; i++) { 

     if (die[i] == 1) { 
      numFreq[0] += 1; 
     } 
     if (die[i] == 2) { 
      numFreq[1] += 1; 
     } 
     if (die[i] == 3) { 
      numFreq[2] += 1; 
     } 
     if (die[i] == 4) { 
      numFreq[3] += 1; 
     } 
     if (die[i] == 5) { 
      numFreq[4] += 1; 
     } 
     if (die[i] == 6) { 
      numFreq[5] += 1; 
     } 
    } 
    return this.numFreq; 
} 

/***************************************************************** 
* scoring method for rolling a single or two 1's 
*****************************************************************/ 
private int eachOne() { 

    if (straight() == 0 && sixOfAKind() == 0 && threePairs() == 0 
      && this.numFreq[0] < 3) { 
     if (this.numFreq[0] == 1) { 
      turnScore = 100; 
      this.remainingDie--; 
      return turnScore; 
     }else if (this.numFreq[0] == 2) { 
      turnScore = 200; 
      this.remainingDie -= 2; 
      return turnScore; 
     } else { 
      return 0; 
     } 
    } else { 
     return 0; 
    } 
} 

/***************************************************************** 
* scoring method for rolling a single or two 5's 
*****************************************************************/ 

private int eachFive() { 

    if (straight() == 0 && sixOfAKind() == 0 && threePairs() == 0 
      && this.numFreq[4] < 3) { 
     if (this.numFreq[4] == 1) { 
      turnScore = 50; 
      this.remainingDie--; 
      return turnScore; 
     }else if (this.numFreq[4] == 2) { 
      turnScore = 100; 
      this.remainingDie -= 2; 
      return turnScore; 
     } else { 
      return 0; 
     } 
    } else { 
     return 0; 
    } 
} 

/***************************************************************** 
* scoring method for rolling 3 of a kind 
*****************************************************************/ 
private int threeOfAKind() { 

    if (sixOfAKind() == 0 && fiveOfAKind() == 0 && fourOfAKind() == 0 
      && straight() == 0) { 
     if (this.numFreq[0] == 3) { 
      turnScore += 1000; 
      this.remainingDie -= 3; 
      return turnScore; 
     } 
     if (this.numFreq[1] == 3) { 
      turnScore += 200; 
      this.remainingDie -= 3; 
      return turnScore; 
     } 
     if (this.numFreq[2] == 3) { 
      turnScore += 300; 
      this.remainingDie -= 3; 
      return turnScore; 
     } 
     if (this.numFreq[3] == 3) { 
      turnScore += 400; 
      this.remainingDie -= 3; 
      return turnScore; 
     } 
     if (this.numFreq[4] == 3) { 
      turnScore += 500; 
      this.remainingDie -= 3; 
      return turnScore; 
     } 
     if (this.numFreq[5] == 3) { 
      turnScore += 600; 
      this.remainingDie -= 3; 
      return turnScore; 
     } else { 
      return 0; 
     } 
    } else { 
     return 0; 
    } 

} 

/***************************************************************** 
* scoring method for rolling four of a kind 
*****************************************************************/ 
private int fourOfAKind() { 

    if (sixOfAKind() == 0 && fiveOfAKind() == 0 && straight() == 0) { 
     if (this.numFreq[0] == 4) { 
      turnScore += 2000; 
      this.remainingDie -= 4; 
      return turnScore; 
     } 
     if (this.numFreq[1] == 4) { 
      turnScore += 400; 
      this.remainingDie -= 4; 
      return turnScore; 
     } 
     if (this.numFreq[2] == 4) { 
      turnScore += 600; 
      this.remainingDie -= 4; 
      return turnScore; 
     } 
     if (this.numFreq[3] == 4) { 
      turnScore += 800; 
      this.remainingDie -= 4; 
      return turnScore; 
     } 
     if (this.numFreq[4] == 4) { 
      turnScore += 1000; 
      this.remainingDie -= 4; 
      return turnScore; 
     } 
     if (this.numFreq[5] == 4) { 
      turnScore += 1200; 
      this.remainingDie -= 4; 
      return turnScore; 
     } else { 
      return 0; 
     } 
    } else { 
     return 0; 
    } 
} 

/***************************************************************** 
* scoring method for rolling 5 of a kind 
*****************************************************************/ 
private int fiveOfAKind() { 

    if (sixOfAKind() == 0 && straight() == 0) { 
     if (this.numFreq[0] == 5) { 
      turnScore += 4000; 
      this.remainingDie -= 5; 
      return turnScore; 
     } 
     if (this.numFreq[1] == 5) { 
      turnScore += 800; 
      this.remainingDie -= 5; 
      return turnScore; 
     } 
     if (this.numFreq[2] == 5) { 
      turnScore += 1200; 
      this.remainingDie -= 5; 
      return turnScore; 
     } 
     if (this.numFreq[3] == 5) { 
      turnScore += 1600; 
      this.remainingDie -= 5; 
      return turnScore; 
     } 
     if (this.numFreq[4] == 5) { 
      turnScore += 2000; 
      this.remainingDie -= 5; 
      return turnScore; 
     } 
     if (this.numFreq[5] == 5) { 
      turnScore += 2400; 
      this.remainingDie -= 5; 
      return turnScore; 
     } else { 
      return 0; 
     } 
    } else { 
     return 0; 
    } 
} 

/***************************************************************** 
* scoring method for rolling 6 of a kind 
*****************************************************************/ 
private int sixOfAKind() { 

    if (this.numFreq[0] == 6) { 
     turnScore += 8000; 
     this.remainingDie -= 6; 
     return turnScore; 
    } 
    if (this.numFreq[1] == 6) { 
     turnScore += 1600; 
     this.remainingDie -= 6; 
     return turnScore; 
    } 
    if (this.numFreq[2] == 6) { 
     turnScore += 2400; 
     this.remainingDie -= 6; 
     return turnScore; 
    } 
    if (this.numFreq[3] == 6) { 
     turnScore += 3200; 
     this.remainingDie -= 6; 
     return turnScore; 
    } 
    if (this.numFreq[4] == 6) { 
     turnScore += 4000; 
     this.remainingDie -= 6; 
     return turnScore; 
    } 
    if (this.numFreq[5] == 6) { 
     turnScore += 4800; 
     this.remainingDie -= 6; 
     return turnScore; 
    } else { 
     return 0; 
    } 
} 

/***************************************************************** 
* scoring method for rolling 3 pairs 
*****************************************************************/ 
private int threePairs() { 
    int pairs = 0; 
    if (this.numFreq[0] == 2) { 
     pairs++; 
    } 
    if (this.numFreq[1] == 2) { 
     pairs++; 
    } 
    if (this.numFreq[2] == 2) { 
     pairs++; 
    } 
    if (this.numFreq[3] == 2) { 
     pairs++; 
    } 
    if (this.numFreq[4] == 2) { 
     pairs++; 
    } 
    if (this.numFreq[5] == 2) { 
     pairs++; 
    } 
    if (pairs == 3) { 
     turnScore += 800; 
     this.remainingDie -= 6; 
     return turnScore; 
    } else { 
     return 0; 
    } 
} 

/***************************************************************** 
* scoring method for rolling a straight 
*****************************************************************/ 
private int straight() { 
    if (this.numFreq[0] == 1 && this.numFreq[1] == 1 
      && this.numFreq[2] == 1 && this.numFreq[3] == 1 
      && this.numFreq[4] == 1 && this.numFreq[5] == 1) { 
     turnScore += 1200; 
     this.remainingDie -= 6; 
     return turnScore; 
    } else { 
     return 0; 
    } 
} 

}

+1

- 如果這只是使用數據結構來保持列表並循環遍歷列表中的Java集合或定義數組的問題。您可以將循環索引與for循環解耦,並有條件地增加循環索引。您也可以在for循環中調用方法或在下一個玩家設置之前添加需要的邏輯。 – myqyl4

+0

僅供參考玩家標籤不屬於「玩家」類。 – arshajii

+0

我想要做的是設置一個for循環,它接受int的輸入,將該玩家遞增到下一個數字,並返回該數字。但是我不能返回一個int,需要成爲一個Player對象,那就是我卡住的地方。 – Enormosaurus

回答

0

我有很難理解你想要做什麼,所以糾正我,如果這不是你所需要的,但使大小Player類型的數組n你會做類似如下

Player[] playerArray = new Player[n]; 

for(int i=0; i<playerArray.length; i++) 
{ 
    // If you need your player id to be different than i just say so in the comments 
    playerArray[i] = new Player(i); 
} 

,這就是關於它。

我有一個鬼鬼祟祟的懷疑,你的要求比這個要複雜一點,但很難準確地告訴你要求什麼,所以如果你需要不同的東西,就這麼說吧。

+0

對不起,我用一個明確的目標更新了我的帖子,需要完成。 – Enormosaurus

+0

@Enormosaurus是否存在'Player []'? –

+0

我有一個實例變量「private Player [] who;」 – Enormosaurus

0

因此,如果我理解正確,您希望應用程序等待一個玩家做出移動,然後繼續玩下一個玩家?

你必須寫一個事件playerMoved。當事件發生時,您手動增加您的索引並等待下一位玩家進行移動。