Java編寫的24點(diǎn)紙牌游戲
任意4個(gè)1-13數(shù)字,加減乘除計(jì)算24點(diǎn)。
實(shí)現(xiàn)原理:
1)排列組合4個(gè)數(shù)字
2)計(jì)算每次排列組合的可能性
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);
}
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
- 利用Java編寫24點(diǎn)小游戲的實(shí)例代碼
- java實(shí)現(xiàn)24點(diǎn)紙牌游戲
- java實(shí)現(xiàn)24點(diǎn)游戲
- Java實(shí)現(xiàn)24點(diǎn)小游戲
- Java Swing實(shí)現(xiàn)坦克大戰(zhàn)游戲
- Java實(shí)戰(zhàn)之飛翔的小鳥小游戲
- Java實(shí)現(xiàn)五子棋游戲
- Java實(shí)現(xiàn)的迷宮游戲
- Java實(shí)戰(zhàn)入門之雙色球彩票小游戲
- Java實(shí)戰(zhàn)之貪吃蛇小游戲(源碼+注釋)
- 用Java實(shí)現(xiàn)24點(diǎn)游戲
相關(guān)文章
Java中實(shí)現(xiàn)WebSocket方法詳解
這篇文章主要介紹了Java中實(shí)現(xiàn)WebSocket方法詳解,WebSocket?是一種新型的網(wǎng)絡(luò)協(xié)議,它允許客戶端和服務(wù)器之間進(jìn)行雙向通信,可以實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)交互,需要的朋友可以參考下2023-07-07
mybatis-plus指定字段模糊查詢的實(shí)現(xiàn)方法
最近項(xiàng)目中使用springboot+mybatis-plus來實(shí)現(xiàn),所以下面這篇文章主要給大家介紹了關(guān)于mybatis-plus實(shí)現(xiàn)指定字段模糊查詢的相關(guān)資料,需要的朋友可以參考下2022-04-04
spring接口通過配置支持返回多種格式(xml,json,html,excel)
這篇文章主要給大家介紹了關(guān)于spring接口如何通過配置支持返回多種格式(xml,json,html,excel)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
Mybatis的parameterType造成線程阻塞問題分析
這篇文章主要詳細(xì)分析了Mybatis的parameterType造成線程阻塞問題,文中有詳細(xì)的解決方法,及相關(guān)的代碼示例,具有一定的參考價(jià)值,感興趣的朋友可以借鑒閱讀2023-06-06
mybatis-plus通用枚舉@JsonValue接收參數(shù)報(bào)錯(cuò)No enum constant
最近在使用mybatis-plus時(shí)用到了通用枚舉,遇到了問題,本文主要介紹了mybatis-plus通用枚舉@JsonValue接收參數(shù)報(bào)錯(cuò)No enum constant,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
Java中StringBuilder與StringBuffer使用及源碼解讀
我們前面學(xué)習(xí)的String就屬于不可變字符串,因?yàn)槔碚撋弦粋€(gè)String字符串一旦定義好,其內(nèi)容就不可再被改變,但實(shí)際上,還有另一種可變字符串,包括StringBuilder和StringBuffer兩個(gè)類,那可變字符串有什么特點(diǎn),又怎么使用呢,接下來就請(qǐng)大家跟我一起來學(xué)習(xí)吧2023-05-05

