java實(shí)現(xiàn)24點(diǎn)紙牌游戲
本文題目為大家分享了java實(shí)現(xiàn)24點(diǎn)紙牌游戲的具體代碼,供大家參考,具體內(nèi)容如下
題目
24點(diǎn)游戲是經(jīng)典的紙牌益智游戲。
常見游戲規(guī)則:
從撲克中每次取出4張牌。使用加減乘除,第一個(gè)能得出24者為贏。(其中,J代表11,Q代表12,K代表13,A代表1),按照要求編程解決24點(diǎn)游戲。
基本要求: 隨機(jī)生成4個(gè)代表?yè)淇伺婆泼娴臄?shù)字字母,程序自動(dòng)列出所有可能算出24的表達(dá)式,用擅長(zhǎng)的語(yǔ)言(C/C++/Java或其他均可)實(shí)現(xiàn)程序解決問題。
分析
用窮舉法列出四個(gè)數(shù)加上三個(gè)運(yùn)算符號(hào)所構(gòu)成的表達(dá)式所有可能的結(jié)果
算法實(shí)現(xiàn)
import java.util.*;
public class point24 {
static int sum=0;
static int[] sum()//產(chǎn)生隨機(jī)數(shù)并顯示的方法
{
Random rand=new Random();
int r1=(1+rand.nextInt(13));
int r2=(1+rand.nextInt(13));
int r3=(1+rand.nextInt(13));
int r4=(1+rand.nextInt(13));
System.out.print("發(fā)出的牌為:");
switch(r1)
{
case 1 :System.out.print("A");break;
case 11:System.out.print("J");break;
case 12:System.out.print("Q");break;
case 13:System.out.print("K");break;
default:System.out.print(r1);break;
}
switch(r2)
{
case 1 :System.out.print(" A");break;
case 11:System.out.print(" J");break;
case 12:System.out.print(" Q");break;
case 13:System.out.print(" K");break;
default:System.out.print(" "+r2);break;
}
switch(r3)
{
case 1 :System.out.print(" A");break;
case 11:System.out.print(" J");break;
case 12:System.out.print(" Q");break;
case 13:System.out.print(" K");break;
default:System.out.print(" "+r3);break;
}
switch(r4)
{
case 1 :System.out.println(" A");break;
case 11:System.out.println(" J");break;
case 12:System.out.println(" Q");break;
case 13:System.out.println(" K");break;
default:System.out.println(" "+r4);break;
}
int [] s=new int[] {r1,r2,r3,r4};
return s;
}
static int js(int i,int j,int x)//兩個(gè)數(shù)之間計(jì)算符號(hào)確認(rèn)
{
int m=0;
switch(x)
{
case 0:m=i+j;break;
case 1:m=i-j;break;
case 2:m=i*j;break;
case 3:
m=i/j;if(i%j!=0) m=-1;//如果除不盡直接讓m=-1,以便直接結(jié)束此次循環(huán)
break;
}
return m;
}
static String prin(int s1,int s2,int s3,int s4,int x1,int x2,int x3)//打印解的表達(dá)式
{
String ch="";
switch(s1)
{
case 1 :System.out.print("A");ch="A";break;
case 11:System.out.print("J");ch="J";break;
case 12:System.out.print("Q");ch="Q";break;
case 13:System.out.print("K");ch="K";break;
default:System.out.print(s1);ch=(String)(s1+"0");break;
}
switch(x1)
{
case 0:System.out.print("+");ch+="+";break;
case 1:System.out.print("-");ch+="-";break;
case 2:System.out.print("*");ch+="*";break;
case 3:System.out.print("/");ch+="/";break;
}
switch(s2)
{
case 1 :System.out.print("A");ch+="A";break;
case 11:System.out.print("J");ch+="J";break;
case 12:System.out.print("Q");ch+="Q";break;
case 13:System.out.print("K");ch+="K";break;
default:System.out.print(s2);ch+=(String)(s2+"");break;
}
switch(x2)
{
case 0:System.out.print("+");ch+="+";break;
case 1:System.out.print("-");ch+="-";break;
case 2:System.out.print("*");ch+="*";break;
case 3:System.out.print("/");ch+="/";break;
}
switch(s3)
{
case 1 :System.out.print("A");ch+="A";break;
case 11:System.out.print("J");ch+="J";break;
case 12:System.out.print("Q");ch+="Q";break;
case 13:System.out.print("K");ch+="K";break;
default:System.out.print(s3);ch+=(String)(s3+"");break;
}
switch(x3)
{
case 0:System.out.print("+");ch+="+";break;
case 1:System.out.print("-");ch+="-";break;
case 2:System.out.print("*");ch+="*";break;
case 3:System.out.print("/");ch+="/";break;
}
switch(s4)
{
case 1 :System.out.println("A");ch+="A";break;
case 11:System.out.println("J");ch+="J";break;
case 12:System.out.println("Q");ch+="Q";break;
case 13:System.out.println("K");ch+="K";break;
default:System.out.println(s4);ch+=(String)(s4+"");break;
}
// System.out.println(" "+ch);
return ch;
}
static String[] con(int s[])//枚舉計(jì)算是否存在解
{
boolean flag=false;
String[] ch=new String [100];
int js1,js2,js3=0;
for(int i = 0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(i!=j)
{
for(int k=0;k<4;k++)
{
if(i!=j&&j!=k&&i!=k)
{
for(int l=0;l<4;l++)
{
if(i!=j&&j!=k&&k!=l&&i!=k&&i!=l&&j!=l)
{
for(int x1=0;x1<3;x1++)
{
for(int x2=0;x2<3;x2++)
{
for(int x3=0;x3<3;x3++)
{
js1=js(s[i],s[j],x1);
if(js1==-1) continue;
js2=js(js1,s[k],x2);
if(js2==-1) continue;
js3=js(js2,s[l],x3);
if(js3==-1) continue;
if(js3!=-1)
{
if(js3==24)
{ ch[sum]=prin(s[i],s[j],s[k],s[l],x1,x2,x3);
flag=true;
sum++;
}
}
}}}}}}}}}}
if(flag==false)
{
System.out.println("不存在一組解使其成為\"24點(diǎn)\"");
}
return ch;
}
public static void main(String[] args)
{
boolean f=false;
Scanner in=new Scanner(System.in);
while(!f)
{
int [] s=sum();//產(chǎn)生4個(gè)隨機(jī)紙牌
con(s);//求解并打印
System.out.println();
System.out.println("繼續(xù) (1)or結(jié)束(0)");
if(in.nextInt()==1)
{f=false;}
else
{f=true;}
}
}
}
運(yùn)行結(jié)果
隨機(jī)數(shù)產(chǎn)生測(cè)試:

運(yùn)行:

更多精彩游戲小代碼,請(qǐng)點(diǎn)擊《游戲?qū)n}》閱讀
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 利用Java編寫24點(diǎn)小游戲的實(shí)例代碼
- java實(shí)現(xiàn)24點(diǎn)游戲
- Java實(shí)現(xiàn)24點(diǎn)小游戲
- Java編寫的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)文章
IDEA新建javaWeb以及Servlet簡(jiǎn)單實(shí)現(xiàn)小結(jié)
這篇文章主要介紹了IDEA新建javaWeb以及Servlet簡(jiǎn)單實(shí)現(xiàn)小結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11
java實(shí)現(xiàn)簡(jiǎn)單年齡計(jì)算器
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單年齡計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
SpringBoot中@RestControllerAdvice注解實(shí)現(xiàn)全局異常處理類
這篇文章主要介紹了SpringBoot中@RestControllerAdvice注解全局異常處理類,springboot中使用@RestControllerAdvice注解,完成優(yōu)雅的全局異常處理類,可以針對(duì)所有異常類型先進(jìn)行通用處理后再對(duì)特定異常類型進(jìn)行不同的處理操作,需要的朋友可以參考下2024-01-01
String在棧中,StringBuffer在堆中!所以String是不可變的,數(shù)據(jù)是共享的。StringBuffer都是獨(dú)占的,是可變的(因?yàn)槊看味际莿?chuàng)建新的對(duì)象?。?/div> 2015-11-11
詳解基于MybatisPlus兩步實(shí)現(xiàn)多租戶方案
這篇文章主要介紹了詳解基于MybatisPlus兩步實(shí)現(xiàn)多租戶方案,本文分兩步,通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
解析SpringBoot中@Autowire注解的實(shí)現(xiàn)原理
在開發(fā)Java項(xiàng)目時(shí),依賴注入是一種常見的實(shí)現(xiàn)方式,SpringBoot框架通過@Autowired注解來實(shí)現(xiàn)依賴注入的功能,本文將介紹SpringBoot中 Autowired注解實(shí)現(xiàn)的原理2023-06-06最新評(píng)論

