寻找导致24的算术运算

Joh*_*Doe 6 java javafx combinatorics playing-cards

我正在编写一个程序来评估4张扑克牌的整数值(数字1-13),并显示一个等于24的解决方案.我有一个很大的if语句,我已经为此编写并且意识到它只是太多的解决方案来添加它们.我正在寻找有关如何将其浓缩为更优化版本的建议.代码运行正常,没有错误,这是我的整个代码:

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.VBox;
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;

   private int a;
   private int b;
   private int c;
   private int d;
   private int e=0;
   private int f=0;
   boolean par=false;


   @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<>("");

      Collections.shuffle(deck);

      BorderPane pane = new BorderPane();

      HBox top = new HBox(10);
      Label display = new Label(result.toString());
      Button btShuffle = new Button("Shuffle");
      Button fiSolution = new Button("Find Solution");
      TextField solfield = new TextField();
      VBox bottomBox = new VBox();

      top.getChildren().add(fiSolution);
      top.getChildren().add(solfield);
      top.getChildren().add(btShuffle);

      HBox center = new HBox(10);

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

      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);


      //String str1 = solfield.setText();

      fiSolution.setOnAction(
            (ActionEvent e) -> {

               a = card1.CardValue();
               b = card2.CardValue();
               c = card3.CardValue(); 
               d = card4.CardValue();



              if (a+b+c+d == 24)
                  solfield.setText(a+"+"+b+"+"+c+"+"+d);
               else if (a+b+c-d == 24)
                  solfield.setText(a+"+"+b+"+"+c+"-"+d);
               else if (a-b+c+d == 24)
                  solfield.setText(a+"+-"+b+"+"+c+"+"+d);   
               else if (a+b-c+d == 24)
                  solfield.setText(a+"+"+b+"-"+c+"+"+d);
               else if ((((a+b)-c)*d)==24)
                  solfield.setText(a+"+"+b+"-"+c+"*"+d);
               else if ((((a+b)-c)/d)==24)
                  solfield.setText(a+"+"+b+"-"+c+"/"+d);
               else if ((((a+b)/c)-d)==24)
                  solfield.setText(a+"+"+b+"/"+c+"-"+d);
               else if ((((a+b)/c)*d)==24)
                  solfield.setText(a+"+"+b+"/"+c+"*"+d);
               else if ((((a+b)*c)/d)==24)
                  solfield.setText(a+"+"+b+"*"+c+"/"+d);
               else if ((((a+b)*c)-d)==24)
                  solfield.setText(a+"+"+b+"*"+c+"-"+d);
               else if ((((a-b)+c)*d)==24)
                  solfield.setText(a+"-"+b+"+"+c+"*"+d);
               else if ((((a-b)+c)/d)==24)
                  solfield.setText(a+"-"+b+"+"+c+"/"+d);
               else if ((((a-b)/c)+d)==24)
                  solfield.setText(a+"-"+b+"/"+c+"+"+d);
               else if ((((a-b)/c)*d)==24)
                  solfield.setText(a+"-"+b+"/"+c+"*"+d);
               else if ((((a-b)*c)/d)==24)
                  solfield.setText(a+"-"+b+"*"+c+"/"+d);
               else if ((((a-b)*c)+d)==24)
                  solfield.setText(a+"-"+b+"*"+c+"+"+d);
               else if ((((a*b)+c)/d)==24)
                  solfield.setText(a+"*"+b+"+"+c+"/"+d);
               else if ((((a*b)+c)-d)==24)
                  solfield.setText(a+"*"+b+"+"+c+"-"+d);
               else if ((((a*b)-c)/d)==24)
                  solfield.setText(a+"*"+b+"-"+c+"/"+d);
               else if ((((a*b)-c)+d)==24)
                  solfield.setText(a+"*"+b+"-"+c+"+"+d);
               else if ((((a*b)/c)+d)==24)
                  solfield.setText(a+"*"+b+"/"+c+"+"+d);
               else if ((((a*b)/c)-d)==24)
                  solfield.setText(a+"*"+b+"/"+c+"-"+d);
               else if ((((a/b)+c)*d)==24)
                  solfield.setText(a+"/"+b+"+"+c+"*"+d);
               else if ((((a/b)+c)-d)==24)
                  solfield.setText(a+"/"+b+"+"+c+"-"+d);
               else if ((((a/b)-c)+d)==24)
                  solfield.setText(a+"/"+b+"-"+c+"+"+d);
               else if ((((a/b)-c)*d)==24)
                  solfield.setText(a+"/"+b+"-"+c+"*"+d);
               else if ((((a/b)*c)-d)==24)
                  solfield.setText(a+"/"+b+"*"+c+"-"+d);
               else if ((((a/b)*c)+d)==24)
                  solfield.setText(a+"/"+b+"*"+c+"+"+d);


               f=c+d;
               if (((a/b)*f)==24)
                  solfield.setText(a+"/"+b+"*("+c+"+"+d+")");
               else if (((a/b)-f)==24)
                  solfield.setText(a+"/"+b+"-("+c+"+"+d+")");
               else if (((a*b)-f)==24)
                  solfield.setText(a+"*"+b+"-("+c+"+"+d+")");
               else if (((a*b)/f)==24)
                  solfield.setText(a+"*"+b+"/("+c+"+"+d+")");
               else if (((a-b)*f)==24)
                  solfield.setText(a+"-"+b+"*("+c+"+"+d+")");
               else if (((a-b)/f)==24)
                  solfield.setText(a+"-"+b+"/("+c+"+"+d+")");

               f=c-d;
               if (((a/b)*f)==24)
                  solfield.setText(a+"/"+b+"*("+c+"-"+d+")");
               else if (((a/b)+f)==24)
                  solfield.setText(a+"/"+b+"+("+c+"-"+d+")");
               else if (((a*b)+f)==24)
                  solfield.setText(a+"*"+b+"+("+c+"-"+d+")");
               else if (((a*b)/f)==24)
                  solfield.setText(a+"*"+b+"/("+c+"-"+d+")");
               else if (((a+b)*f)==24)
                  solfield.setText(a+"+"+b+"*("+c+"-"+d+")");
               else if (((a+b)/f)==24)
                  solfield.setText(a+"+"+b+"/("+c+"-"+d+")");

               f=c*d;
               if (((a/b)-f)==24)
                  solfield.setText(a+"/"+b+"*("+c+"*"+d+")");
               else if (((a/b)+f)==24)
                  solfield.setText(a+"/"+b+"+("+c+"*"+d+")");
               else if (((a-b)+f)==24)
                  solfield.setText(a+"-"+b+"+("+c+"*"+d+")");
               else if (((a-b)/f)==24)
                  solfield.setText(a+"-"+b+"/("+c+"*"+d+")");
               else if (((a+b)-f)==24)
                  solfield.setText(a+"+"+b+"-("+c+"*"+d+")");
               else if (((a+b)/f)==24)
                  solfield.setText(a+"+"+b+"/("+c+"*"+d+")");

               f=c/d;
               if (((a-b)*f)==24)
                  solfield.setText(a+"-"+b+"*("+c+"/"+d+")");
               else if (((a-b)+f)==24)
                  solfield.setText(a+"-"+b+"+("+c+"/"+d+")");
               else if (((a*b)+f)==24)
                  solfield.setText(a+"*"+b+"+("+c+"/"+d+")");
               else if (((a*b)-f)==24)
                  solfield.setText(a+"*"+b+"-("+c+"/"+d+")");
               else if (((a+b)*f)==24)
                  solfield.setText(a+"+"+b+"*("+c+"/"+d+")");
               else if (((a+b)-f)==24)
                  solfield.setText(a+"+"+b+"-("+c+"/"+d+")");

               f=b*c;
               if (((a-f)/d)==24)
                  solfield.setText(a+"-("+b+"*"+c+")/"+d);
               else if (((a-f)+d)==24)
                  solfield.setText(a+"-("+b+"*"+c+")+"+d);
               else if (((a/f)+d)==24)
                  solfield.setText(a+"/("+b+"*"+c+")+"+d);
               else if (((a/f)-d)==24)
                  solfield.setText(a+"/("+b+"*"+c+")-"+d);
               else if (((a+f)/d)==24)
                  solfield.setText(a+"+("+b+"*"+c+")/"+d);
               else if (((a+f)-d)==24)
                  solfield.setText(a+"+("+b+"*"+c+")-"+d);

               f=b-c;
               if (((a*f)/d)==24)
                  solfield.setText(a+"*("+b+"-"+c+")/"+d);
               else if (((a*f)+d)==24)
                  solfield.setText(a+"*("+b+"-"+c+")+"+d);
               else if (((a/f)+d)==24)
                  solfield.setText(a+"/("+b+"-"+c+")+"+d);
               else if (((a/f)*d)==24)
                  solfield.setText(a+"/("+b+"-"+c+")*"+d);

               f=b/c;
               if (((a-f)*d)==24)
                  solfield.setText(a+"-("+b+"/"+c+")*"+d);
               else if (((a-f)+d)==24)
                  solfield.setText(a+"-("+b+"/"+c+")+"+d);
               else if (((a*f)+d)==24)
                  solfield.setText(a+"*("+b+"/"+c+")+"+d);
               else if (((a*f)-d)==24)
                  solfield.setText(a+"*("+b+"/"+c+")-"+d);
               else if (((a+f)*d)==24)
                  solfield.setText(a+"+("+b+"/"+c+")*"+d);
               else if (((a+f)-d)==24)
                  solfield.setText(a+"+("+b+"/"+c+")-"+d);

               f=b+c;
               if (((a*f)/d)==24)
                  solfield.setText(a+"*("+b+"+"+c+")/"+d);
               else if (((a*f)-d)==24)
                  solfield.setText(a+"*("+b+"+"+c+")-"+d);
               else if (((a/f)-d)==24)
                  solfield.setText(a+"/("+b+"+"+c+")-"+d);
               else if (((a/f)*d)==24)
                  solfield.setText(a+"/("+b+"+"+c+")*"+d);






            });

      btShuffle.setOnAction(
            e -> {
               center.getChildren().clear();  
               Collections.shuffle(deck);
               card1 = new Card(deck.get(0));
               card2 = new Card(deck.get(1));
               card3 = new Card(deck.get(2));
               card4 = new Card(deck.get(3));

               center.getChildren().add(card1);
               center.getChildren().add(card2);           
               center.getChildren().add(card3);
               center.getChildren().add(card4);

            });

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

      TextField tfExpress = new TextField();

      String str = tfExpress.getText();       

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

      bottomBox.getChildren().add(bottom);
      bottomBox.getChildren().add(display);

      btVerify.setOnAction(
            (ActionEvent e) -> 
            {               
               String regex = ("[^0-9]+");
               String[] inputIntegers = tfExpress.getText().split(regex);

              // expInput.removeIf(p-> p.equals(signs));
               ArrayList<Integer> temp = new ArrayList<>();
               temp.add(new Integer(card1.CardValue()));
               temp.add(new Integer(card2.CardValue()));                    
               temp.add(new Integer(card3.CardValue()));          
               temp.add(new Integer(card4.CardValue())); 

               if(inputIntegers.length != 0)
               {
                  if (inputIntegers.length != 0) {
                     for (String s : inputIntegers) {
                        if (!s.equals(""))
                           temp.remove(new Integer(Integer.valueOf(s)));
                     }
                  }        
               }

               if(temp.isEmpty())
               {
                  if(evaluateExpression(tfExpress.getText()) == 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(bottomBox);


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

   public boolean Twentyfour(boolean par){
   //some people play the game with or without parentheses.
      this.par=par;

      return par;
   }

   /** Evaluate an expression */
   public static int evaluateExpression(String expression) {
    // Create operandStack to store operands
      Stack<Integer> operandStack = new Stack<Integer>();

    // Create operatorStack to store operators
      Stack<Character> operatorStack = new Stack<Character>();

    // Insert blanks around (, ), +, -, /, and *
      expression = insertBlanks(expression);

    // Extract operands and operators
      String[] tokens = expression.split(" ");

    // Phase 1: Scan tokens
      for (String token: tokens) {
         if (token.length() == 0) // Blank space
            continue; // Back to the while loop to extract the next token
         else if (token.charAt(0) == '+' || token.charAt(0) == '-') {
         // Process all +, -, *, / in the top of the operator stack 
            while (!operatorStack.isEmpty() &&
            (operatorStack.peek() == '+' || 
            operatorStack.peek() == '-' ||
            operatorStack.peek() == '*' ||
            operatorStack.peek() == '/')) {
               processAnOperator(operandStack, operatorStack);
            }

         // Push the + or - operator into the operator stack
            operatorStack.push(token.charAt(0));
         }
         else if (token.charAt(0) == '*' || token.charAt(0) == '/') {
         // Process all *, / in the top of the operator stack 
            while (!operatorStack.isEmpty() &&
            (operatorStack.peek() == '*' ||
            operatorStack.peek() == '/')) {
               processAnOperator(operandStack, operatorStack);
            }

         // Push the * or / operator into the operator stack
            operatorStack.push(token.charAt(0));
         }
         else if (token.trim().charAt(0) == '(') {
            operatorStack.push('('); // Push '(' to stack
         }
         else if (token.trim().charAt(0) == ')') {
         // Process all the operators in the stack until seeing '('
            while (operatorStack.peek() != '(') {
               processAnOperator(operandStack, operatorStack);
            }

            operatorStack.pop(); // Pop the '(' symbol from the stack
         }
         else { // An operand scanned
         // Push an operand to the stack
            operandStack.push(new Integer(token));
         }
      }

    // Phase 2: process all the remaining operators in the stack 
      while (!operatorStack.isEmpty()) {
         processAnOperator(operandStack, operatorStack);
      }

    // Return the result
      return operandStack.pop();
   }

  /** Process one operator: Take an operator from operatorStack and
   *  apply it on the operands in the operandStack */
   public static void processAnOperator(
      Stack<Integer> operandStack, Stack<Character> operatorStack) {
      char op = operatorStack.pop();
      int op1 = operandStack.pop();
      int op2 = operandStack.pop();
      if (op == '+') 
         operandStack.push(op2 + op1);
      else if (op == '-') 
         operandStack.push(op2 - op1);
      else if (op == '*') 
         operandStack.push(op2 * op1);
      else if (op == '/') 
         operandStack.push(op2 / op1);
   }

   public static String insertBlanks(String s) {
      String result = "";

      for (int i = 0; i < s.length(); i++) 
      {
         if (s.charAt(i) == '(' || s.charAt(i) == ')' || 
          s.charAt(i) == '+' || s.charAt(i) == '-' ||
          s.charAt(i) == '*' || s.charAt(i) == '/')
            result += " " + s.charAt(i) + " ";
         else
            result += s.charAt(i);
      }

      return result;
   }

   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);
   }
}
Run Code Online (Sandbox Code Playgroud)

Pim*_*mgd 0

9216 个不同的方程 - 6 个括号位置(给定 a 1 b 2 c 3 d,运算以 6 种不同的顺序发生)、64 (4x4x4) 种不同的运算符情况和 24 种不同的数字顺序。

显然你不能手写这些。

然而,您可以做的是手动定义每个类并具有三个 for 循环,每个循环遍历其操作列表。

对于实际实施,我认为策略模式是最好的,因为它允许您将问题分成小块。

...虽然这是一个蛮力解决方案,但我认为一定有更好的东西...