将数值设置为卡值

时间:2015-05-04 01:12:30

标签: java arraylist javafx integer character

这就是我现在所拥有的:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.scene.image.ImageView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.concurrent.atomic.AtomicReference;
import java.util.*;

public class Main extends Application {


   private Card card1;
   private Card card2;
   private Card card3;
   private Card card4;

   @Override
   public void start(Stage primaryStage) {


      ArrayList<Integer> deck;
      deck = new ArrayList<>();
      int i = 1;
      while(i < 52){
         deck.add(i);
         i++;
      }
      final AtomicReference<String> result = new AtomicReference<>("go.");

      Collections.shuffle(deck);

      BorderPane pane = new BorderPane();

      HBox top = new HBox(10);
      Label display = new Label(result.toString());
      Button btShuffle = new Button("Shuffle");

      top.getChildren().add(display);
      top.getChildren().add(btShuffle);




      HBox center = new HBox(10);

      card1 = new Card(deck.get(0));
      center.getChildren().add(card1);

      Card card2;
      card2 = new Card(deck.get(1));
      center.getChildren().add(card2);


      card3 = new Card(deck.get(3));
      center.getChildren().add(card3);


      card4 = new Card(deck.get(4));
      center.getChildren().add(card4);



      btShuffle.setOnAction(
            e -> {
                  center.getChildren().clear();  
                   Collections.shuffle(deck);


              center.getChildren().add(new Card(deck.get(0)));
              center.getChildren().add(new Card(deck.get(1)));
              center.getChildren().add(new Card(deck.get(3)));
              center.getChildren().add(new Card(deck.get(4)));

            });





      HBox bottom = new HBox(10);
      Label expression = new Label("Please Enter the expression: ");

      TextField tfExpress = new TextField();
      ArrayList<Character> signs = new ArrayList<>();
      signs.add('/');
      signs.add('+');
      signs.add('(');
      signs.add(')');
      signs.add('-');
      signs.add('^');
      signs.add('*');
      signs.add('%');
      String str = tfExpress.getText();


      int express = (str != null && !"".equals(str)) ? Integer.parseInt(str) : 0;      



     // expInput.removeIf(p-> p.equals(signs));

      Button btVerify = new Button("Verify");
      bottom.getChildren().add(expression);
      bottom.getChildren().add(tfExpress);
      bottom.getChildren().add(btVerify);

      btVerify.setOnAction(
            (ActionEvent e) -> 
            {
               LinkedList<Character> expInput = new LinkedList<Character>();
            for(char c: tfExpress.getText().toCharArray()){
                expInput.add(c);
                }
            String[] inputIntegers = tfExpress.getText().split("[^0-9]+");

               expInput.removeIf(p-> p.equals(signs));

            ArrayList<Integer> temp = new ArrayList<>();
            for(String s:inputIntegers)
            {
               temp.add(new Integer(Integer.valueOf(s)));
            } 
            temp.remove(new Integer(card1.CardValue()));
            temp.remove(new Integer(card2.CardValue()));                    
            temp.remove(new Integer(card3.CardValue()));          
            temp.remove(new Integer(card4.CardValue()));          


               if(temp.isEmpty() /*card1.CardValue() == Character.getNumericValue(deck.get(0))
               && card2.CardValue() == Character.getNumericValue(deck.get(1))
               && card3.CardValue() == Character.getNumericValue(deck.get(2))
               && card4.CardValue() == Character.getNumericValue(deck.get(3))*/)
               {
                  if(express == 24){
                     display.setText("Correct");
                  }
                  else
                     display.setText("Incorrect");

               }
               else
                  display.setText("The numbers in the expression don't "
                     + "match the numbers in the set.");
            });



      pane.setTop(top);
      pane.setCenter(center);
      pane.setBottom(bottom);

      Scene scene = new Scene(pane);
      primaryStage.setTitle("24 card game");
      primaryStage.setScene(scene);
      primaryStage.show();
   }



   public class Card extends Pane {
      public int cardVal;
      Card(int card){
         Image cardImage;
         cardImage = new Image("card/"+ card +".png");
         getChildren().add(new ImageView(cardImage));
         cardVal = card;

      }

      public int CardValue(){
         int card = 0;

         if(cardVal <= 13){
            card = cardVal;
         }
         else if(cardVal > 13 && cardVal <= 26){
            card = cardVal - 13;
         }
         else if(cardVal > 26 && cardVal <= 39){
            card = cardVal - 26;
         }
         else if(cardVal > 39 && cardVal <= 52){
            card = cardVal - 39;
         }


         return card;
      }




   }



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

}

在我的:

 btShuffle.setOnAction(
            e -> {
                  center.getChildren().clear();  
                   Collections.shuffle(deck);


              center.getChildren().add(new Card(deck.get(0)));
              center.getChildren().add(new Card(deck.get(1)));
              center.getChildren().add(new Card(deck.get(3)));
              center.getChildren().add(new Card(deck.get(4)));

            });

我想将这些值更改为card1-4,但是当我这样做时,它现在不会随机播放。有谁知道这是为什么?此外,我做了一个扫描仪,并确定卡值实际上正确地分配给卡片,这只是现在正确评估它们的问题。

2 个答案:

答案 0 :(得分:0)

我不确定你的问题是什么,但似乎你假设card1 == 0card2 == 1等等。这不是真的,因为你正在初始化你的卡片。甲板,你设置从1开始。你应该从0开始你的牌组以获得所需的效果(目前你在牌组中只有51张牌。)

 for (int i = 0; i < 52; i++) {
     deck.add(i);
 }

编辑:您使用card2收到错误的原因是因为您在与lambda调用相同的方法中在本地声明它(start()方法) 。从您的Card card2;方法中删除语句start(),它应该有效。

答案 1 :(得分:0)

在你提到的另一个问题中,你想保留你的代码&#34;浓缩&#34;。请不要这样做。你现在应该意识到你的&#34;浓缩&#34;代码会引导你解决很多问题,这些问题会把你带到这里。

以下是一个更好的例子:

  • 创建一个名为
  • 的套装枚举
  • 使用名称和值或您拥有的任何其他属性创建一个Rank枚举
  • 创建一个由Suit和Rank组成的卡片类,它根据名称加载图像(是的,你必须正确地重命名图像)
  • 通过迭代Suit和Rank的所有组合来创建你的牌

除此之外别无其他。这是代码:

public class CardsDemo extends Application {

    /**
     * All the cards in the game. Loaded only once at game start.
     */
    private List<Card> deck;

    /**
     * The cards to be dealt. Created initially from the deck.
     */
    private List<Card> stock;

    @Override
    public void start(Stage primaryStage) {

        // create cards
        deck = createDeck();

        Group root = new Group();
        Scene scene = new Scene(root, 300, 250);

        primaryStage.setScene(scene);
        primaryStage.show();


        stock = new ArrayList<>( deck);

        // shuffle cards
        Collections.shuffle(stock);

        // deal cards
        while( !stock.isEmpty()) {

            Card card = stock.remove(0);

            System.out.println( "Dealing: " + card);
        }

    }

    private List<Card> createDeck() {

        List<Card> cards = new ArrayList<>();

        for( Suit suit: EnumSet.allOf(Suit.class)) {

            for( Rank rank: EnumSet.allOf( Rank.class)) {

                Card card = new Card( suit, rank);
                cards.add( card);
            }

        }

        return cards;

    }

    public static class Card {
        Suit suit;
        Rank rank;

        public Card( Suit suit, Rank rank) {
            this.suit = suit;
            this.rank = rank;

            // TODO: create imageview
            String filename = "card/" + suit.getName() + "_" + rank.getName() + ".png";

            System.out.println( "Filename: " + filename);

        }

        public Suit getSuit() {
            return suit;
        }

        public Rank getRank() {
            return rank;
        }

        public String toString() {
            return "Suit: " + suit + ", Rank: " + rank + ", Value: " + rank.getValue(); 
        }
    }

    public enum Rank {

        ACE( "A", 1),
        _2( "2", 2),
        _3("3", 3),
        _4("4", 4),
        _5("5", 5),
        _6("6", 6),
        _7("7", 7),
        _8("8", 8),
        _9("9", 9),
        _10("10", 10),
        JACK("J", 11),
        QUEEN("Q", 12),
        KING("K", 13)
        ;

        String name;
        int value;

        Rank( String name, int value) {
            this.name = name;
            this.value = value;
        }

        public String getName() {
            return name;
        }

        public int getValue() {
            return value;
        }
    }

    public enum Suit {

        SPADES( "spade"),
        HEARTS( "heart"),
        CLUBS( "club"),
        DIAMONDS("diamond"),
        ;

        String name;

        Suit( String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }

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

this question has an answer关于如何处理卡片问题。你没有使用&#34; get&#34;而是使用&#34;删除&#34;用于访问卡。只需使用&#34; get&#34;只会一遍又一遍地给你同一张卡片。你也需要删除它。但是,删除也会为您提供已移除的卡,因此删除就足够了。