1. 程式人生 > >骰子游戲(JAVA)

骰子游戲(JAVA)

importjava.util.Random;

importjava.util.*;

importjavax.swing.JOptionPane;

importjava.io.File;

importjava.util.HashMap;

importjava.util.Iterator;

importjava.util.Map;

importjava.util.Set;

importjava.util.Map.Entry;

publicclassDicerGame{

staticinta=0,b=0,c=0,d=0,e=0,f=0;

staticdoubleg=0;

Stringstr,str1,str2,str3;

intx[][]=newint[a][6];

inty=0;

intcount=0;

Stringz[]=newString[5];

Randomr=newRandom();

Map<String,Integer>map=newHashMap<String,Integer>();publicDicerGame(){

//if(o==1){

//給陣列x[i][j]增加a*6個面的值

for(inti=0;i<x.length;i++){

for(intj=0;j<x[0].length;j++){

b=b+1;

x[i][j]=b;

if(b==6){

b=0;

}

}

}

//Z[F]用於統計隨機抽出的骰子是不是全部一樣

for(intp=0;p<x.length;p++){

c=r.nextInt(6);

y=x[p][c]+y;

z[f]=String.valueOf(x[p][c]);

f++;

"+z[4];

2+"元");

你輸了");

"+z[4];}for(Stringstr:z){Integernum=map.get(str);num=null==num?1:num+1;map.put(str,num);}if(z.length!=map.size()){//System.out.println("存在相同的數字!");}Setset=map.entrySet();Iterator<Entry>it=set.iterator();while(it.hasNext()){Entry<String,Integer>entry=(Entry<String,Integer>)it.next();count++;}//判斷結果d=1,d=2,d=3,d=4四種情況下得輸贏if(d==1){str1="電腦的點數是"+y+"\n"+z[0]+""+z[1]+""+z[2]+""+z[3]+"if(y>((a*6)/2)){JOptionPane.showMessageDialog(null,str1+"\n"+"你贏了"+g*}elseif(y<=((a*6)/2)){JOptionPane.showMessageDialog(null,str1+"\n"+"你輸了");}elseif(count==5){JOptionPane.showMessageDialog(null,str1+"\n"+"圍骰,通殺"+"}}if(d==2){str1="電腦的點數是"+y+"\n"+z[0]+""+z[1]+""+z[2]+""+z[3]+"

if(y>=((a*6)/2)){

JOptionPane.showMessageDialog(null,str1+"\n"+"你輸了");

}elseif(y<((a*6)/2)){

JOptionPane.showMessageDialog(null,str1+"\n"+"你贏了"

+g*2+"元");

}

elseif(count==5){

JOptionPane.showMessageDialog(null,str1+"\n"+"圍骰,通殺"+"

你輸了");

}

}

if(d==3){

str1="電腦的點數是"+y+"\n"+z[0]+""+z[1]+""+z[2]+""+z[3]+"

"+z[4];

if(y>((a*6)/2)){

JOptionPane.showMessageDialog(null,str1+"\n"+"你輸了");

}elseif(y<((a*6)/2)){

JOptionPane.showMessageDialog(null,str1+"\n"+"你輸了");

}elseif(y==((a*6)/2)){

JOptionPane.showMessageDialog(null,str1+"\n"+"你贏了"+g*

4+"元");

}

elseif(count==5){

JOptionPane.showMessageDialog(null,str1+"\n"+"圍骰,通殺"+"

你輸了");

}

}

if(d==4){

str1="電腦的點數是"+y+"\n"+z[0]+""+z[1]+""+z[2]+""+z[3]+"

"+z[4];

if(count==5){

JOptionPane.showMessageDialog(null,str1+"\n"+"你贏了"+g*

10

+"元");

}else

JOptionPane.showMessageDialog(null,str1+"\n"+"你輸了");

}

}

publicstaticvoidmain(String[]args){

//e=Integer.valueOf(JOptionPane.showInputDialog("遊戲型別(1:賭大小)"));

a=Integer.valueOf(JOptionPane.showInputDialog("親,輸入骰子個數(最大個數為5,最小個數為2):"));

if(a>5||a<=1){

JOptionPane.showMessageDialog(null,"親,骰子個數不符合規定哦,請退出");System.exit(1);


}

g=Double.valueOf(JOptionPane.showInputDialog("親,投注金額:"));

d=Integer.valueOf(JOptionPane.showInputDialog("親,1:大,2:小,3:和4:圍骰"));

if(d>4||d<0){

JOptionPane.showMessageDialog(null,"親,這不可以買噢,請退出");

System.exit(1);

}

DicerGamedicer=newDicerGame();

}