Trying to get GUI to print random card in deck using arrayList

后端 未结 3 1371
面向向阳花
面向向阳花 2021-01-17 04:59

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 f

相关标签:
3条回答
  • 2021-01-17 05:35

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

    Then just take the first two entries from the ArrayList.

    0 讨论(0)
  • 2021-01-17 05:40

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

    ArrayList list2= new ArrayList(
    <your list>.subList(0,1))
    
    0 讨论(0)
  • 2021-01-17 05:53

    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
    • 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);
    
            }
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题