Java编写的24点纸牌游戏

所属分类: 软件编程 / java 阅读数: 95
收藏 0 赞 0 分享

任意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);
}

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

更多精彩内容其他人还在看

Java实现一个简单的文件上传案例示例代码

这篇文章主要介绍了Java实现一个简单的文件上传案例,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解在spring中使用JdbcTemplate操作数据库的几种方式

这篇文章主要介绍了详解在spring中使用JdbcTemplate操作数据库的几种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

java中的switch case语句使用详解

这篇文章主要介绍了java中的switch case语句使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

深入理解Java 线程池

这篇文章主要介绍了Java 线程池的相关资料,文中讲解非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解mybatis #{}和${}的区别、传参、基本语法

这篇文章主要介绍了mybatis #{}和${}的区别、传参、基本语法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

MyBatis 中 ${}和 #{}的正确使用方法(千万不要乱用)

这篇文章主要介绍了MyBatis 中 ${}和 #{}的正确使用方法,本文给大家提到了MyBatis 中 ${}和 #{}的区别,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Mybatis中的 ${} 和 #{}区别与用法

这篇文章主要介绍了Mybatis中的 ${} 和 #{}区别与用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Maven安装过程图文详解

这篇文章主要介绍了Maven安装过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

SpringBoot登录拦截配置详解(实测可用)

这篇文章主要介绍了SpringBoot登录拦截配置详解(实测可用),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

JAVA 内存溢出案例汇总

这篇文章主要介绍了JAVA 内存溢出案例的汇总,文中讲解非常细致,帮助各位工作学习时避免内存溢出,感兴趣的朋友可以了解下
收藏 0 赞 0 分享
查看更多