你好同学书呆子我有一个问题! 我正在开展一个项目,这个项目将于周三到期,我被困在中途。该游戏是双人游戏,其中每个玩家从16个节点的列表中选择转弯,当点击时,将隐藏值添加到玩家得分。该项目给出了一个标签,上面写着"玩家1"默认情况下,这部分作业要求我们更改文字以表示当前是谁。当前玩家猜到一个正方形后,转弯会发生变化。还有一个" Hold"按钮使当前玩家可以跳过转弯(有一个" Bust"方形隐藏,将当前玩家的得分设置为零)。我想在完成这部分之后我可以想出那部分内容。你能帮助我指点正确的方向吗?请不要为我解决。我真的想学习这些东西,我只需要一些方向/建议。
以下是它运行时的样子: Image
感谢您的帮助,这个网站太棒了! 以下是相关代码:
public class GuiCodeBehind {
private ViewModel theViewModel;
private List<Button> buttons;
private List<Node> boardNodes;
@FXML
private GridPane boardGridPane;
@FXML
private Button holdButton;
@FXML
private Label gameStatusLabel;
@FXML
private TextField player1ScoreTextField;
@FXML
private TextField player2ScoreTextField;
public GuiCodeBehind() {
this.theViewModel = new ViewModel();
this.createButtons();
}
/**
* Initializes the GUI components, binding them to the view model properties
* and setting their event handlers.
*/
@FXML
public void initialize() {
this.bindGuiComponentsToViewModel();
this.addButtonsToBoard();
this.boardNodes = this.boardGridPane.getChildren();
this.setEventActions();
}
private void createButtons() {
this.buttons = new ArrayList<Button>();
for (int i = 0; i < 16; i++) {
this.buttons.add(this.createButtonWithId(i));
}
}
private Button createButtonWithId(int id) {
String buttonId = "" + (id + 1);
Button aButton = new Button(" ? ");
aButton.setMaxWidth(Double.MAX_VALUE);
aButton.setId(buttonId);
return aButton;
}
private void addButtonsToBoard() {
for (int row = 0; row < 4; row++) {
this.addARow(row);
}
}
private void addARow(int row) {
for (int column = 0; column < 4; column++) {
this.boardGridPane.add(this.buttons.remove(0), column, row);
}
}
private void bindGuiComponentsToViewModel() {
this.player1ScoreTextField.textProperty().
bindBidirectional(this.theViewModel.player1ScoreProperty());
this.player2ScoreTextField.textProperty().
bindBidirectional(this.theViewModel.player2ScoreProperty());
}
private void setEventActions() {
for (Node aButton: this.boardNodes) {
aButton.setOnMouseClicked(event -> this.handleBoardButtonClick(aButton));
}
}
private void handleBoardButtonClick(Node aButton) {
aButton.setDisable(true);
this.holdButton.requestFocus();
this.theViewModel.play(aButton.getId());
((Button) aButton).setText(this.theViewModel.selectedSquareDescription());
}
}
ViewModel:
/**
* ViewModel defines the view model for the play-or-hold application.
*/
public final class ViewModel {
private String descriptionOfSelectedSquare;
private final Player player1;
private final Player player2;
private final GameBoard theBoard;
private final GameController theGameController;
private StringProperty player1Score;
private StringProperty player2Score;
private StringProperty gameStatus;
/**
* Creates a new ViewModel instance.
*
* @precondition none
* @postcondition the object and its properties exist
*/
public ViewModel() {
this.descriptionOfSelectedSquare = "";
this.player1 = new RegularPlayer();
this.player2 = new PrivilegedPlayer();
GameBoardFactory factory = new OneDoublePointsBoardFactory(true);
this.theBoard = factory.getGameBoard();
this.theGameController = new GameController(this.theBoard, this.player1, this.player2);
this.player1Score = new SimpleStringProperty("0");
this.player2Score = new SimpleStringProperty("0");
}
/**
* Returns the property that represents the score for player 1.
*
* @precondition none
* @return the property
*/
public StringProperty player1ScoreProperty() {
return this.player1Score;
}
/**
* Returns the property that represents the score for player 2.
*
* @precondition none
* @return the property
*/
public StringProperty player2ScoreProperty() {
return this.player2Score;
}
/**
* Tells the game controller to carry out the current
* player's move.
*
* @precondition 1 <= gameSquareId <= 16
* @postcondition the current player's score reflects this move, &&
* currentMoveValue() returns the value of the
* @param gameSquareId the ID of the selected control in the GUI
*/
public void play(String gameSquareId) {
int squareIndex = Integer.parseInt(gameSquareId) - 1;
this.theGameController.play(squareIndex);
this.descriptionOfSelectedSquare = this.theBoard.getSquare(squareIndex).getDescription();
this.player1Score.setValue("" + this.player1.getScore());
this.player2Score.setValue("" + this.player2.getScore());
}
/**
* Returns the value of the current "move".
*
* @precondition none
* @return the value of the selected game board item
*/
public String selectedSquareDescription() {
return this.descriptionOfSelectedSquare;
}
public StringProperty gameStatusStringProperty() {
return this.gameStatus;
}
}