我正在使用JavaFX创建一个Set游戏,但遇到了一些问题。
当我尝试在第4栏中选择卡片时(或者如果我使用add 3功能并尝试点击其中一张卡片),我当前遇到了越界异常
此外,当我点击三张卡并且它们不是一组时,我想将背景重置为白色,并且可以重新开始/选择三张新卡
如果是一套,我想从电路板上取下这些卡并用三张新卡取代
任何帮助将不胜感激!我一直如此坚持,无法找到任何资源指出我正确的方向。
GameFX
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.*;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.transform.Rotate;
import javafx.scene.text.*;
import javafx.scene.image.Image;
import javafx.scene.paint.ImagePattern;
import java.util.ArrayList;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
public class GameFX extends Application {
public Game g;
private Pane pane;
private Board b;
//int rows = g.getBoard().numRows();
//int cols = g.getBoard().numCols();
public static void main(String [] args) {
Application.launch((String[]) args);
}
public void start(Stage stage) throws Exception {
// Variables
g = new Game();
b = g.getBoard();
pane = new Pane();
int cardsLeft = g.deck.deck.size();
BackgroundFill grayBG = new BackgroundFill(Color.LIGHTGRAY, new CornerRadii(1), new Insets(0.0, 0.0, 0.0, 0.0));
BackgroundFill darkGrayBG = new BackgroundFill(Color.DARKGRAY, new CornerRadii(1), new Insets(0.0, 0.0, 0.0, 0.0));
BorderPane main = new BorderPane();
Scene scene = new Scene(main);
Label label = new Label("Set Game");
Label instructionsLabel = new Label("Click on cards to select them, Double-click to deselect them, type 'A' to add three cards, type 'E' to end the game");
Label actionsLabel = new Label("(A)dd 3 cards, (E)nd Game, (N)ew Game");
Label cardsRemaining = new Label("There are " + cardsLeft + " cards remaining in the deck");
GridPane topPane = new GridPane();
GridPane centerPane = new GridPane();
GridPane rightPane = new GridPane();
GridPane bottomPane = new GridPane();
int rows = g.getBoard().numRows();
int cols = g.getBoard().numCols();
boolean stop = false;
// Configure pane
pane.setPrefHeight(500);
pane.setPrefWidth(1000);
centerPane.setPrefHeight(500);
centerPane.setPrefWidth(700);
rightPane.setPrefWidth(300);
rightPane.setPrefHeight(500);
pane.setBackground(new Background(grayBG));
rightPane.setBackground(new Background(darkGrayBG));
// Create initial board
updateBoard(b, centerPane);
// Configure label
label.setTextFill(Color.RED);
// Add content to topPane
topPane.add(label, 0, 0);
topPane.add(instructionsLabel, 0, 1);
topPane.add(actionsLabel, 0, 2);
topPane.add(cardsRemaining, 0, 3);
// Set panes in BorderPane layout
main.setCenter(centerPane);
main.setTop(topPane);
//main.setRight(rightPane);
// Request focus and configure window
stage.setTitle("Set");
stage.setScene(scene);
stage.show();
centerPane.requestFocus();
centerPane.setOnKeyPressed(e -> {
switch (e.getCode()) {
case A: {
System.out.println("A was pressed");
if (b.numCols() < 6) {
g.add3();
}
updateBoard(b, centerPane);
break;
}
case E: {
Platform.exit();
break;
}
case N: {
System.out.println( "New Game!" );
stage.close();
Game g2 = new Game();
Board b2 = g2.getBoard();
updateBoard(b2, centerPane);
stage.setScene(scene);
stage.show();
}
}
});
}
public Pane createCard(int shape, int fill, int color, int number, int rows, int cols) {
//int cardRow = cardRow;
//int cardColumn = cardColumn;
Pane cardPane = new Pane();
Pane shapePane = new Pane();
ArrayList<Rectangle> squares = new ArrayList<Rectangle>();
ArrayList<Circle> circles = new ArrayList<Circle>();
ArrayList<Rectangle> diamonds = new ArrayList<Rectangle>();
String stripesRedURL = "stripesRed.png";
String stripesPurpleURL = "stripesPurple.png";
String stripesGreenURL = "stripesGreen.png";
Image stripesRed = new Image(stripesRedURL);
Image stripesPurple = new Image(stripesPurpleURL);
Image stripesGreen = new Image(stripesGreenURL);
BackgroundFill cardBG = new BackgroundFill(Color.WHITE,
new CornerRadii(1),
new Insets(0.0, 0.0, 0.0, 0.0));
if (shape == 0) {
for (int i=0; i < number; i++) {
circles.add(new Circle(50,20 + (i * 30),10));
}
for(Circle circle: circles) {
if (fill == 0) {
if (color == 0) {
circle.setFill(Color.RED);
} else if (color == 1) {
circle.setFill(Color.PURPLE);
} else if (color == 2) {
circle.setFill(Color.GREEN);
}
} else if (fill == 1) {
if (color == 0) {
circle.setFill(new ImagePattern(stripesRed, 0, 0, 40, 40, false));
} else if (color == 1) {
circle.setFill(new ImagePattern(stripesPurple, 0, 0, 40, 40, false));
} else if (color == 2) {
circle.setFill(new ImagePattern(stripesGreen, 0, 0, 40, 40, false));
}
} else if (fill == 2) {
if (color == 0) {
circle.setStroke(Color.RED);
circle.setFill(Color.WHITE);
} else if (color == 1) {
circle.setStroke(Color.PURPLE);
circle.setFill(Color.WHITE);
} else if (color == 2) {
circle.setStroke(Color.GREEN);
circle.setFill(Color.WHITE);
}
}
}
//System.out.println(circles);
for(Circle circle: circles) {
shapePane.getChildren().add(circle);
}
} else if (shape == 1) {
for (int i=0; i < number; i++) {
squares.add(new Rectangle(40, 10 + (i * 30), 20, 20));
}
for(Rectangle square: squares) {
if (fill == 0) {
if (color == 0) {
square.setFill(Color.RED);
} else if (color == 1) {
square.setFill(Color.PURPLE);
} else if (color == 2) {
square.setFill(Color.GREEN);
}
} else if (fill == 1) {
if (color == 0) {
square.setFill(new ImagePattern(stripesRed, 0, 0, 40, 40, false));
} else if (color == 1) {
square.setFill(new ImagePattern(stripesPurple, 0, 0, 40, 40, false));
} else if (color == 2) {
square.setFill(new ImagePattern(stripesGreen, 0, 0, 40, 40, false));
}
} else if (fill == 2) {
if (color == 0) {
square.setStroke(Color.RED);
square.setFill(Color.WHITE);
} else if (color == 1) {
square.setStroke(Color.PURPLE);
square.setFill(Color.WHITE);
} else if (color == 2) {
square.setStroke(Color.GREEN);
square.setFill(Color.WHITE);
}
}
}
//System.out.println(squares);
for(Rectangle square: squares) {
shapePane.getChildren().add(square);
}
} else if (shape == 2) {
for (int i=0; i < number; i++) {
diamonds.add(new Rectangle(40, 10 + (i * 30), 20, 20));
}
for(Rectangle diamond: diamonds) {
diamond.getTransforms().add(new Rotate(45,50,50));
if (fill == 0) {
if (color == 0) {
diamond.setFill(Color.RED);
} else if (color == 1) {
diamond.setFill(Color.PURPLE);
} else if (color == 2) {
diamond.setFill(Color.GREEN);
}
} else if (fill == 1) {
if (color == 0) {
diamond.setFill(new ImagePattern(stripesRed, 0, 0, 40, 40, false));
} else if (color == 1) {
diamond.setFill(new ImagePattern(stripesRed, 0, 0, 40, 40, false));
} else if (color == 2) {
diamond.setFill(new ImagePattern(stripesRed, 0, 0, 40, 40, false));
}
} else if (fill == 2) {
if (color == 0) {
diamond.setStroke(Color.RED);
diamond.setFill(Color.WHITE);
} else if (color == 1) {
diamond.setStroke(Color.PURPLE);
diamond.setFill(Color.WHITE);
} else if (color == 2) {
diamond.setStroke(Color.GREEN);
diamond.setFill(Color.WHITE);
}
}
}
//System.out.println(diamonds);
for(Rectangle diamond: diamonds) {
shapePane.getChildren().add(diamond);
}
}
cardPane.setBackground(new Background(cardBG));
cardPane.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, BorderWidths.DEFAULT)));
cardPane.setPrefHeight(100);
cardPane.setPrefWidth(100);
cardPane.getChildren().addAll(shapePane);
//If card is selected, background changes color
cardPane.setOnMouseClicked(new EventHandler<MouseEvent>(){
//int selected = 0;
//ArrayList<String> selected = new ArrayList<String>();
//g.getSelected();;
@Override
public void handle(MouseEvent me) {
g.board.getBoardSquare(rows,cols).setSelected(!g.board.getBoardSquare(rows,cols).isSelected());
if(me.getClickCount() == 1){
cardPane.setBackground(new Background(new BackgroundFill(Color.LIGHTGRAY, CornerRadii.EMPTY, Insets.EMPTY)));
g.addToSelected(rows, cols);
}
else if(me.getClickCount() == 2){
cardPane.setBackground(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY)));
g.removeSelected(rows, cols);
}
if(g.numSelected()==3){
g.testSelected();
if(g.testSelected() == true){
System.out.println("Remove and replace");
}
else if(g.testSelected() == false){
System.out.println("Background to white");
}
//entire back board to white
//cardPane.setBackground(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY)));
}
}
});
return cardPane;
}
public void updateBoard(Board b, GridPane pane) {
ArrayList<ArrayList<BoardSquare>> boardSquares = b.getBoardSquares();
for (ArrayList<BoardSquare> boardRow: boardSquares) {
for (BoardSquare boardSquare: boardRow) {
Card boardCard = boardSquare.getCard();
pane.add(createCard(boardCard.getShape(), boardCard.getShading(), boardCard.getColor(), boardCard.getNumber() + 1, boardSquare.getRow(), boardSquare.getColumn()), boardSquare.getRow(), boardSquare.getColumn());
}
}
}
}
游戏
import java.util.ArrayList;
public class Game{
//intialize variable, deck and board, arraylist
private int selectedCards;
Deck deck;
Board board;
private ArrayList <BoardSquare> selected;
/*
Constructor
creates a new game with a new shuffled deck
and board, selected starts empty
*/
public Game(){
selectedCards = 0;
deck = new Deck();
deck.shuffle();
board = new Board(deck);
selected = new ArrayList<BoardSquare>(3);
}
/*
method outOfCards determines if the
board is out of new cards
@return boolean out
*/
public boolean outOfCards(){
boolean out;
if(deck.isEmpty() == true){
out = true;
}
else
{
out = false;
}
return out;
}
/*
method addToSelected
takes the BoardSquare the user selects and saves it to compare later
@param row
@param column
*/
public void addToSelected(int row, int col){
//add card to an arraylist of selected
selected.add(board.getBoardSquare(row, col));
selectedCards++;
if (selectedCards == 3){
testSelected();
}
}
/*
method numSelected returns the number of selected cards being held
@return selectedCards
*/
public int numSelected(){
return selectedCards;
}
/*
method testSelected
tests whether the three cards in the selected ArrayList
are a Set
*/
public boolean testSelected(){
boolean set = false;
//test if the cards from selected are a Set, method is in Card Class
if (selected.size() == 3 && !selected.isEmpty()){
//boolean set = false;
//BoardSquare bs1 = selected.get(0);
//BoardSquare bs2 = selected.get(1);
//BoardSquare bs3 = selected.get(2);
//Card.isSet(bs1.getCard(), bs2.getCard(), bs2.getCard());
if (Card.isSet(selected.get(0).getCard(), selected.get(1).getCard(), selected.get(2).getCard())){
board.replaceCard(deck.getTopCard(), selected.get(0).getRow(), selected.get(0).getColumn());
board.replaceCard(deck.getTopCard(), selected.get(1).getRow(), selected.get(1).getColumn());
board.replaceCard(deck.getTopCard(), selected.get(2).getRow(), selected.get(2).getColumn());
//print SET
System.out.print("It's a SET! Pick another\n");
//empty ArrayList
for (int i = 0; i < selected.size();) {
selectedCards = 0;
selected.remove(i);
}
set = true;
}
else{
//print NOT a SET
System.out.print("Not a SET! Try again\n");
//empty ArrayList
for (int i = 0; i < selected.size();) {
selectedCards = 0;
selected.remove(i);
}
set = false;
}
//return set;
}
return set;
}
/*
method removeSelected
de-selects a boardsquare from the arraylist
@param row
@param column
*/
public void removeSelected(int row, int col){
boolean status = false;
if (selected.get(0).getRow() == row && selected.get(0).getColumn() == col){
selected.remove(0);
selectedCards--;
}
else if (selected.get(1).getRow() == row && selected.get(1).getColumn() == col){
selected.remove(1);
selectedCards--;
}
else if (selected.get(2).getRow() == row && selected.get(2).getColumn() == col){
selected.remove(2);
selectedCards--;
}
else{
status = false;
}
}
/*
method getSelected
returns the cards that have been selected
@return selected
*/
public ArrayList<BoardSquare> getSelected(){
return selected;
}
/*
method add3
adds three cards to the board
*/
public void add3(){
board.add3(deck);
}
/*
method to String
converts the game to a string for printing
*/
public String toString(){
return board.toString();
}
public Board getBoard() {
return board;
}
}
板
import java.util.ArrayList;
public class Board{
//initialize arraylist and variables
private ArrayList<ArrayList<BoardSquare>> board;
public final static int INITIAL_ROWS = 3;
public final static int INITIAL_COLS = 4;
//Constructor that accepts a Deck object and populates
//The ArrayLists
public Board(Deck deck){
board = new ArrayList<ArrayList<BoardSquare>>();
ArrayList<Card> boardsquare;
for(int i=0; i<3; i++){
board.add(new ArrayList<BoardSquare>());
for(int j=0; j<4; j++){
board.get(i).add(new BoardSquare(deck.getTopCard(),i,j));
}
}
}
/*
method replaceCard will replace a card that has been removed
@param card - card object
@param row - row of the card
@param column - column of card
*/
public void replaceCard(Card card, int row, int column){
board.get(row).get(column).setCard(card);
}
/*
method getBoardSquare returns the card at a specified location
@param row
@param column
@return reference to a board square location
*/
public BoardSquare getBoardSquare(int row, int column){
return board.get(row).get(column);
}
public ArrayList<ArrayList<BoardSquare>> getBoardSquares() {
return board;
}
/*
method add3 puts one card into each row if no sets are available
@param deck object
*/
public void add3(Deck deck){
for(int i=0; i<3; i++) {
board.get(i).add(new BoardSquare(deck.getTopCard(),i,board.get(i).size()));
}
}
/*
method getCard returns a reference to a card at a location
@param row
@param column
@return reference to a card
*/
public Card getCard(int row, int column){
return board.get(row).get(column).getCard();
}
/*
method numRows returns amount of rows on the board
@return rows
*/
public int numRows(){
return INITIAL_ROWS;
}
/*
method numCols gets how many columns in each row, intially 4
@return columns aka size of the column array
*/
public int numCols(){
return board.get(0).size();
}
/*
method toString returns a String representation of the Board
@return str
*/
public String toString(){
String str = "";
for(int i=0; i<3; i++){
for(int j=0; j<numCols(); j++){
str += (board.get(i).get(j).getCard().toString() + "\t");
}
str += "\n";
}
return str;
}
}
BoardSquare
public class BoardSquare{
private int column;//column of the card
private int row; //row of the card
private Card card;
private boolean selected=false;
/*Constructor
@param card, a card object
@param column, column of the card
@param row, row of the card
*/
public BoardSquare(Card card, int column, int row){
this.column = column;
this.row = row;
this.card = card;
}
/*
method getColumn returns column
@return column
*/
public int getColumn(){
return column;
}
/*
method setColumn updates the column
@param c
*/
public void setColumn(int c){
column = c;
}
/*
method getRow returns row
@return row
*/
public int getRow(){
return row;
}
/*
method setRow updates the row
@param r
*/
public void setRow(int r){
row = r;
}
/*
method getCard returns card
@return card
*/
public Card getCard(){
return card;
}
/*
method setCard updates the card
@param c
*/
public void setCard(Card c){
card = c;
}
/*
method isSelected
@returns boolean selected
*/
public boolean isSelected(){
return selected;
}
public void setSelected(boolean s){
selected = s;
}
}
甲板
import java.util.ArrayList;
import java.util.Random;
public class Deck{
//initialize deck size, create ArrayList, and initialize count
public final static int CARDS_IN_DECK = 81;
public ArrayList<Card> deck;
private int ct;
/*
Constructor that represents a fresh deck
*/
public Deck(){
freshDeck();
}
/*
Copy constructor that creates all cards in a deck
*/
public void freshDeck(){
deck = new ArrayList<Card>(CARDS_IN_DECK);
for (int number=0; number<3; number++){
for (int shape=0; shape<3; shape++){
for (int shading=0; shading<3; shading++) {
for (int color=0; color<3; color++) {
deck.add(new Card(number, shape, shading, color));
}
}
}
}
}
/*
method shuffle randomizes the order of the cards
*/
public void shuffle()
{
int random;
Card temp;
Random r = new Random();
for (int i = 0; i < deck.size(); i++)
{
random = r.nextInt(deck.size());
temp = deck.get(i);
deck.set(i,deck.get(random));
deck.set(random,temp);
}
}
/*
method isEmpty determines if the deck has cards in it
*/
public boolean isEmpty()
{
return (deck.size() == 0);
}
/*
method getTopCard
deals a card, returns it, and removes it from the deck
@return c
*/
public Card getTopCard()
{
Card c = deck.remove(deck.size()-1);
return c;
}
/*
method toString
turns the deck into a string and returns it
@return str
*/
public String toString(){
String str="Current Deck:\n";
for (int i=0; i < deck.size() ; i++){
str+= deck.get(i).toString();
}
return str;
}
public ArrayList getCards() {
return deck;
}
}