欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java编写的24点纸牌游戏

程序员文章站 2024-03-02 17:44:22
任意4个1-13数字,加减乘除计算24点。 实现原理: 1)排列组合4个数字 2)计算每次排列组合的可能性 cal24.java import j...

任意4个1-13数字,加减乘除计算24点。

实现原理:

1)排列组合4个数字
2)计算每次排列组合的可能性

cal24.java

import java.util.hashset;
import java.util.set;
 
 
public class cal24 {
 
  private static final double precision = 0.00001;
  private static final int target = 24;
   
 
  public string[] execute(string[] inputs) {
    int[] digits = new int[4];
    for (int i = 0; i < inputs.length; i++) {
      digits[i] = integer.valueof(inputs[i]);
    }
    return new string[]{calc(digits)};
  }
 
   
   
  private string calc(final int data[]){
    final set<string> out = new hashset<string>();
    combination digit = new combination() {
       
      @override
      protected void handle(int[] result) {
        final int[] r = result; 
        combination oper = new combination(){
          @override
          protected void handle(int[] c) {
            double x = r[0];
            for (int i = 0; i < r.length - 1; i++) {
              x = docalculate(x, r[i + 1], c[i]);
            }
            if(math.abs(math.abs(x) - target) < precision || math.abs(math.abs(1/x) - target) < precision){
              stringbuilder sb = new stringbuilder();
              for (int j = 0; j < r.length; j++) {
                sb.append(r[j]);
                if(j != r.length - 1){
                  sb.append(getoperation(c[j]));
                }
              }
              out.add(sb.tostring());
            }
             
          }
        };
        oper.combine(new int[]{0, 1, 2, 3}, data.length - 1, true);
         
      }
    };
     
    digit.combine(data);
     
    stringbuilder sb = new stringbuilder();
    for (string string : out) {
      sb.append(string);
      sb.append("\n");
    }
    return sb.tostring();
  }
   
   
   
   
  private double docalculate(double x, double y, int operation){
    switch (operation) {
    case 0:
      return x + y;
    case 1:
      return x - y;
    case 2:
      return x * y;
    case 3:
      return x / y;
    default:
      return 0;
    }
  }
   
  private static string getoperation(int operation){
    switch (operation) {
    case 0:
      return "+";
    case 1:
      return "-";
    case 2:
      return "*";
    case 3:
      return "/";
    default:
      return "";
    }
  }
 
 
  public static void main(string[] args) {
    system.out.println(new cal24().calc(new int[]{1, 5, 5, 5}));
  }
}

combination.java

public abstract class combination {
  private boolean repeat;
  private int total = 0;
   
  public void combine(int data[]){
    combine(data, data.length, false);
  }
   
  public void combine(int data[], int count){
    combine(data, count, false);
  }
   
  public void combine(int data[], int count, boolean repeat){
    this.repeat = repeat;
    int times = data.length;
    int size = (int)math.pow(times, count);
    for (int i = 0; i < size; i++) {
      int[] result = toarray(data, i, count);
      if(result != null){
        handle(result);
        total ++;
      }
    }
  }
   
   
  private int[] toarray(int data[], int i, int count){
    int [] indices = new int[count];
    int times = data.length;
    for (int j = 0; j < count; j++) {
      int temp = 0;
      if(i > 0){
        temp = i%times;
        i = (i - temp)/times;
      }
      indices[j] = temp;
    }
     
     
    if(!repeat){
      //remove repetition
      for (int x = 0; x < count; x++) {
        for(int y = 0; y < count; y++){
          if(x != y){
            if(indices[x] == indices[y])
              return null;
          }
        }
      }
    }
     
    int [] result = new int[count];
    for (int x = 0; x < count; x++) {
      int selected = data[indices[x]];
      result[x] = selected;
    }
     
    return result;
  }
   
  public int gettotal() {
    return total;
  }
  protected abstract void handle(int[] result);
}

以上所述就是本文的全部内容了,希望大家能够喜欢。