试图让GUI使用arrayList在deck中打印随机卡

时间:2022-03-25 09:25:48

So I'm currently working on a Card War Game in java. I'm trying to get the GUI screen to print 2 random cards from a set of card images using an arrayList (must use this for assignment). The card image files are named 1.png,2.png,...52.png and stored in a image/card directory. My question is how do I get two cards to appear randomly? Thanks here's my code

所以我目前正在研究java中的卡片战争游戏。我正试图让GUI屏幕使用arrayList从一组卡片图像中打印2张随机卡片(必须使用它进行分配)。卡图像文件命名为1.png,2.png,... 52.png并存储在图像/卡目录中。我的问题是如何让两张牌随机出现?谢谢,这是我的代码

Also, just to note, I'm a relatively new programmer

另外,请注意,我是一个相对较新的程序员

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.paint.Color;
import javafx.scene.text.FontWeight;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import java.util.ArrayList;

public class CardWar extends Application{

public static void main(String args[]){
Application.launch(args);


}

@Override
public void start(Stage primaryStage){


ArrayList<Integer> cardList = new ArrayList<>();

for(int i = 1; i <=52;i++){
cardList.add(i);
}


java.util.Collections.shuffle(cardList);
System.out.println(cardList.get(0));




//Label label1 = new Label ("Welcome to the Card War Game!");

//layout for first scene
StackPane layout1 = new StackPane();
Button welcome = new Button("Click to Play");
Text text1 = new Text(20,20, "Welcome to The Game");
layout1.getChildren().add(welcome);



//layout for second scene
//Button button2 = new Button("You are here!");
Pane layout2 = new HBox (457);
//layout2.getChildren().add(button2);
Image image = new Image("image/card/1.png");
Image image2 = new Image("image/card/2.png");

ImageView imageView2 = new ImageView(image);
imageView2.setFitHeight(100);
imageView2.setFitWidth(100);

layout2.getChildren().add(new ImageView (image));
layout2.getChildren().add(new ImageView (image2));



Scene scene = new Scene (layout1,600,300);
Scene scene2 = new Scene(layout2,600,300);

welcome.setOnAction(e -> primaryStage.setScene(scene2));

primaryStage.setScene(scene);
primaryStage.setTitle("War Game");
primaryStage.show();

}
} 

3 个解决方案

#1


2  

Here's a modified version of your code. This is what you should do:

这是您的代码的修改版本。这是你应该做的:

  • create a dedicated Card class, an object which you can use. Don't use integers
  • 创建一个专用的Card类,一个可以使用的对象。不要使用整数
  • load the cards into a list (the deck). The cards are loaded only once
  • 将卡片加载到列表中(卡片组)。这些卡只加载一次
  • create the stock, i. e. a list out of the deck which you can modify
  • 创造股票,我。即甲板上可以修改的列表
  • shuffle the stock
  • 洗牌股票
  • put stock on the table
  • 把股票放在桌子上
  • pick a card from the stock and add it to the tableau until there are no more cards available in the stock
  • 从库存中挑选一张卡片并将其添加到画面中,直到库存中没有可用的卡片为止

The code:

代码:

public class CardWar extends Application {

    /**
     * List of all available cards. Loaded once at game start.
     */
    List<Card> deck;

    /**
     * List of cards in the game.
     */
    List<Card> stock;

    /**
     * Cards to be dealt
     */
    Pane stockPane;

    /**
     * Cards which are already dealt
     */
    FlowPane tableauPane;

    public static void main(String args[]) {
        Application.launch(args);

    }

    @Override
    public void start(Stage primaryStage) {

        // load all cards into a list
        loadCards();

        // create playfield: table with toolbar
        BorderPane playfield = new BorderPane();
        playfield.setStyle("-fx-background-color:green");

        // simple flowpane to align one card next to the other 
        FlowPane tableauPane = new FlowPane();

        // pick 2 cards and add them to the table
        Button button = new Button( "Pick Cards");
        button.setOnAction( e -> {

            // abort if we don't have cards in the deck
            if( stock.size() == 0)
                return;

            Card card;

            // pick top card and add it to the table
            card = stock.remove( stock.size()-1);
            stockPane.getChildren().remove(card);
            tableauPane.getChildren().add(card);

            // pick top card and add it to the table
            card = stock.remove( stock.size()-1);
            stockPane.getChildren().remove(card);
            tableauPane.getChildren().add(card);

        });

        playfield.setCenter(tableauPane);

        // talon contains all cards of the deck
        stockPane = new Pane();
        playfield.setBottom(stockPane);

        // toolbar with a button
        HBox toolbar = new HBox();
        toolbar.getChildren().add( button);

        playfield.setTop(toolbar);

        Scene scene = new Scene(playfield, 1600, 900, Color.GREEN);

        primaryStage.setScene(scene);
        primaryStage.setTitle("War Game");
        primaryStage.show();

        // create deck for game, shuffle cards
        startGame();

    }

    private void startGame() {

        // create stock from deck
        stock = new ArrayList<>( deck);

        // shuffle stock
        Collections.shuffle(stock);

        // put cards on stock pane
        for( int i=0; i < stock.size(); i++) {

            // get card from stock
            Card card = stock.get(i);

            // set card position
            card.setLayoutX(i * 20);

            // put card on stock pane
            stockPane.getChildren().add( card);

        }
    }

    private void loadCards() {

        deck = new ArrayList<>();

        for (int i = 1; i <= 52; i++) {
            deck.add( new Card( i));
        }
    }

    private static class Card extends ImageView {

        public Card( int id) {

            setImage(new Image( getClass().getResource("image/card/" + id + ".png").toExternalForm()));

            setFitHeight(100);
            setFitWidth(100);

        }

    }
}

#2


0  

You could use the Collections.shuffle(...) method to shuffle the ArrayList.

您可以使用Collections.shuffle(...)方法来重新排列ArrayList。

Then just take the first two entries from the ArrayList.

然后从ArrayList中获取前两个条目。

#3


0  

You could create a new list like this using .subList(fromIndex, toIndex)

您可以使用.subList(fromIndex,toIndex)创建这样的新列表

ArrayList list2= new ArrayList(
<your list>.subList(0,1))

#1


2  

Here's a modified version of your code. This is what you should do:

这是您的代码的修改版本。这是你应该做的:

  • create a dedicated Card class, an object which you can use. Don't use integers
  • 创建一个专用的Card类,一个可以使用的对象。不要使用整数
  • load the cards into a list (the deck). The cards are loaded only once
  • 将卡片加载到列表中(卡片组)。这些卡只加载一次
  • create the stock, i. e. a list out of the deck which you can modify
  • 创造股票,我。即甲板上可以修改的列表
  • shuffle the stock
  • 洗牌股票
  • put stock on the table
  • 把股票放在桌子上
  • pick a card from the stock and add it to the tableau until there are no more cards available in the stock
  • 从库存中挑选一张卡片并将其添加到画面中,直到库存中没有可用的卡片为止

The code:

代码:

public class CardWar extends Application {

    /**
     * List of all available cards. Loaded once at game start.
     */
    List<Card> deck;

    /**
     * List of cards in the game.
     */
    List<Card> stock;

    /**
     * Cards to be dealt
     */
    Pane stockPane;

    /**
     * Cards which are already dealt
     */
    FlowPane tableauPane;

    public static void main(String args[]) {
        Application.launch(args);

    }

    @Override
    public void start(Stage primaryStage) {

        // load all cards into a list
        loadCards();

        // create playfield: table with toolbar
        BorderPane playfield = new BorderPane();
        playfield.setStyle("-fx-background-color:green");

        // simple flowpane to align one card next to the other 
        FlowPane tableauPane = new FlowPane();

        // pick 2 cards and add them to the table
        Button button = new Button( "Pick Cards");
        button.setOnAction( e -> {

            // abort if we don't have cards in the deck
            if( stock.size() == 0)
                return;

            Card card;

            // pick top card and add it to the table
            card = stock.remove( stock.size()-1);
            stockPane.getChildren().remove(card);
            tableauPane.getChildren().add(card);

            // pick top card and add it to the table
            card = stock.remove( stock.size()-1);
            stockPane.getChildren().remove(card);
            tableauPane.getChildren().add(card);

        });

        playfield.setCenter(tableauPane);

        // talon contains all cards of the deck
        stockPane = new Pane();
        playfield.setBottom(stockPane);

        // toolbar with a button
        HBox toolbar = new HBox();
        toolbar.getChildren().add( button);

        playfield.setTop(toolbar);

        Scene scene = new Scene(playfield, 1600, 900, Color.GREEN);

        primaryStage.setScene(scene);
        primaryStage.setTitle("War Game");
        primaryStage.show();

        // create deck for game, shuffle cards
        startGame();

    }

    private void startGame() {

        // create stock from deck
        stock = new ArrayList<>( deck);

        // shuffle stock
        Collections.shuffle(stock);

        // put cards on stock pane
        for( int i=0; i < stock.size(); i++) {

            // get card from stock
            Card card = stock.get(i);

            // set card position
            card.setLayoutX(i * 20);

            // put card on stock pane
            stockPane.getChildren().add( card);

        }
    }

    private void loadCards() {

        deck = new ArrayList<>();

        for (int i = 1; i <= 52; i++) {
            deck.add( new Card( i));
        }
    }

    private static class Card extends ImageView {

        public Card( int id) {

            setImage(new Image( getClass().getResource("image/card/" + id + ".png").toExternalForm()));

            setFitHeight(100);
            setFitWidth(100);

        }

    }
}

#2


0  

You could use the Collections.shuffle(...) method to shuffle the ArrayList.

您可以使用Collections.shuffle(...)方法来重新排列ArrayList。

Then just take the first two entries from the ArrayList.

然后从ArrayList中获取前两个条目。

#3


0  

You could create a new list like this using .subList(fromIndex, toIndex)

您可以使用.subList(fromIndex,toIndex)创建这样的新列表

ArrayList list2= new ArrayList(
<your list>.subList(0,1))