/** ClientControl.java
*
* Created on 2007年10月2日, 下午2:02
* 此类是客户端中的关键类,此类专门负责整个程序的
* 流程和表现,负责对外通讯
* To change this template, choose Tools | Template Manager
* and open the template in the editor.*/packagecom.hadeslee.apple.client;/***
*@authorlbf*/importcom.hadeslee.apple.common.Bet;importcom.hadeslee.apple.common.INFO;importjava.applet.Applet;importjava.applet.AudioClip;importjava.awt.BorderLayout;importjava.awt.DisplayMode;importjava.awt.GraphicsDevice;importjava.awt.GraphicsEnvironment;importjava.awt.Image;importjava.awt.MediaTracker;importjava.awt.Toolkit;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;importjava.util.EnumMap;importjava.util.Iterator;importjava.util.Set;importjava.util.Vector;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;importjavax.swing.JFrame;importstaticcom.hadeslee.apple.common.Constants.*;publicclassClientControlextendsThreadimplementsKeyListener {privateClientView cv;//表示视图的类privateClientModel cm;//表示模型的类privateExecutorService es;//表示一个线程池的对象privateAudioClip[] betAu;//表示下注时的一个音频对象privateAudioClip onceMore;privateAudioClip xiu;privateAudioClip loop;privateAudioClip runA;privateAudioClip runB;privateAudioClip win;privateAudioClip lose;privateBet bet;//缓存的上一局的下注记录privatePP p=PP.p1;privatevolatilebooleancanBet=true;privatevolatilebooleanisRunning;privatevolatilebooleanisBetting;privatevolatilebooleanisBetted;privatevolatilebooleancanMove=true;//表示此时是否可以下注,是否正在附加赌privateImage[] pkA;privateImage[] pkB;//表示两个人物的图像数组privateintresult;//表示PK的结果,定义在Constants类里面privateControl con;//处理控制的类privateintallBet=1;privateintallWin=1;//表示此人所有下注,所有赢的数,此数用于控制,不能赋0,除法会异常privateAudioClip temp;//中间变量,用于放歌用的privatevolatilebooleango;privatevolatilebooleanshowMoney;privatevolatilebooleanblinkRatio;//用以控制闪动线程的privatevolatilebooleanisFullScreen;//指示此时是否是全屏privateJFrame jf;//表示此游戏的窗口/**Creates a new instance of ClientControl*/publicClientControl() {
}publicClientControl(intid,intallMoney) {
cm=newClientModel();
cv=newClientView(cm);
cm.setId(id);
cm.setAllMoney(allMoney);
cm.setInfo(newINFO());try{
cm.getInfo().setBigBonus(10000);
cm.getInfo().setSmallBonus(1000);
cm.getInfo().setTip("欢迎使用单机版!!谢谢!");
}catch(Exception exe) {
exe.printStackTrace();
}
initModel();
initWindow();
initOther();
doShowAllMoney();
}//初始化其它东西,比如声音片断privatevoidinitOther() {
con=newControl();
betAu=newAudioClip[10];for(inti=1; i<10; i++) {
betAu[i]=Applet.newAudioClip(this.getClass().getResource("sound/bet/bet"+i+".au"));
}//初始化所有要使用的声音片段loop=Applet.newAudioClip(this.getClass().getResource("sound/loop.au"));
runA=Applet.newAudioClip(this.getClass().getResource("sound/runA.au"));
runB=Applet.newAudioClip(this.getClass().getResource("sound/runB.au"));
onceMore=Applet.newAudioClip(this.getClass().getResource("sound/wu.au"));
xiu=Applet.newAudioClip(this.getClass().getResource("sound/oncemore.au"));
win=Applet.newAudioClip(this.getClass().getResource("sound/win/win.au"));
lose=Applet.newAudioClip(this.getClass().getResource("sound/win/lose.au"));
bet=newBet();
}//初始化全屏privatevoidinitWindow() {
jf=newJFrame("苹果机");
jf.getContentPane().add(cv, BorderLayout.CENTER);
jf.setSize(800,600);
jf.setUndecorated(true);
jf.setVisible(true);
jf.setAlwaysOnTop(true);
jf.setLocationRelativeTo(null);//一条简便的语句,设置窗口居中显示jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.addKeyListener(this);
}//初始化模型privatevoidinitModel() {
initImage();
initModelOther();
}//初始化图像privatevoidinitImage() {
MediaTracker mt=newMediaTracker(cv);intindex=0;
Image[] number=newImage[10];
Image[] winNumber=newImage[10];for(inti=0; i<10; i++) {
number[i]=Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("pic/game/number1/"+i+".png"));
winNumber[i]=Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("pic/game/number2/"+i+".png"));
mt.addImage(number[i], index++);
mt.addImage(winNumber[i], index++);
}
Image bg=Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("pic/game/bg.jpg"));
Image table=Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("pic/game/table.png"));
Image pkBg=Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("pic/game/pk.jpg"));
mt.addImage(bg, index++);
mt.addImage(table, index++);
mt.addImage(pkBg, index++);
pkA=newImage[3];
pkB=newImage[3];for(inti=0; i<3; i++) {
pkA[i]=Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("pic/game/pk/A"+i+".png"));
pkB[i]=Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("pic/game/pk/B"+i+".png"));
mt.addImage(pkA[i], index++);
mt.addImage(pkB[i], index++);
}try{
mt.waitForAll();
}catch(Exception exe) {
exe.printStackTrace();
}
cm.setBg(bg);
cm.setNumber(number);
cm.setWinNumber(winNumber);//cm.setRunA(PP.p1.other);cm.setTable(table);
cm.setPkA(pkA[0]);
cm.setPkB(pkB[0]);
cm.setPKBG(pkBg);
}//初始化模型其它部份privatevoidinitModelOther() {
Bet bet=newBet();
cm.setBet(bet);
cm.addP(PP.p1);//初始化线程池es=Executors.newCachedThreadPool();
}//专门起一个线程用于更新余额的方法privatevoiddoShowAllMoney() {
es.execute(newRunnable() {publicvoidrun() {
showMoney=true;while(showMoney) {try{
Thread.sleep(80);
cv.repaint(10,661,73,107,65);
}catch(Exception exe) {
exe.printStackTrace();
}
}
}
});
}//专门处理下注的方法privatesynchronizedvoiddoBet(inti) {if(!isBetted) {
isBetted=true;
canMove=false;
cm.getBet().clearWin();//doShowAllMoney();}
cm.setAllMoney(cm.getAllMoney()-1);intbet=cm.getBet().getBet(i);
cm.getBet().setBet(i, bet+1);
betAu[i].play();
}//专门用来设置赔率的方法privatevoidsetRatio() {intrad=(int) (Math.random()*100)+1;if(rad>95) {
cm.setRatioA(40);
}elseif(rad>85) {
cm.setRatioA(30);
}else{
cm.setRatioA(20);
}
rad=(int) (Math.random()*100)+1;if(rad>95) {
cm.setRatioB(20);
}elseif(rad>85) {
cm.setRatioB(15);
}else{
cm.setRatioB(10);
}
}//专门处理开始跑的方法privatevoiddoRun() {try{//此时先把赔率设置好setRatio();
isRunning=true;
canBet=false;
canMove=false;
showMoney=false;
INFO inf=cm.getInfo();
inf.setBigBonus(cm.getBet().getTotal()/50+inf.getBigBonus());
inf.setSmallBonus(cm.getBet().getTotal()/100+inf.getSmallBonus());
cv.repaint();
es.execute(newRunnable() {publicvoidrun() {
runRun();
}
});
}catch(Exception exe) {
exe.printStackTrace();
}
}publicvoidkeyPressed(KeyEvent ke) {intallMoney=cm.getAllMoney();intcode=ke.getKeyCode();//先看看是不是CTRL+F,如果是的话,就表示全屏或者退出全屏了if(KeyEvent.VK_F==code&&(ke.getModifiersEx()&KeyEvent.CTRL_DOWN_MASK)==KeyEvent.CTRL_DOWN_MASK) {if(isFullScreen) {
GraphicsDevice gd=GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();try{
gd.setFullScreenWindow(null);
isFullScreen=false;
}catch(Exception exe) {
exe.printStackTrace();
}
}else{//得到图形设备,也就是显示器GraphicsDevice gd=GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();try{
gd.setFullScreenWindow(jf);
gd.setDisplayMode(newDisplayMode(800,600,16,60));
isFullScreen=true;
}catch(Exception exe) {
exe.printStackTrace();
}
}return;
}//如果可以下注并且余额大于0才处理键盘事件if(allMoney>0&&canBet) {if(KeyEvent.VK_1==code) {
doBet(1);
cv.repaint(24,566,64,31);
}elseif(KeyEvent.VK_2==code) {
doBet(2);
cv.repaint(129,566,64,31);
}elseif(KeyEvent.VK_3==code) {
doBet(3);
cv.repaint(197,566,64,31);
}elseif(KeyEvent.VK_4==code) {
doBet(4);
cv.repaint(264,566,64,31);
}elseif(KeyEvent.VK_5==code) {
doBet(5);
cv.repaint(369,566,64,31);
}elseif(KeyEvent.VK_6==code) {
doBet(6);
cv.repaint(474,566,64,31);
}elseif(KeyEvent.VK_7==code) {
doBet(7);
cv.repaint(542,566,64,31);
}elseif(KeyEvent.VK_8==code) {
doBet(8);
cv.repaint(610,566,64,31);
}elseif(KeyEvent.VK_9==code) {
doBet(9);
cv.repaint(712,566,64,31);
}
}if(KeyEvent.VK_LEFT==code&&canMove) {if(cm.getAllMoney()>0) {if(temp!=null) {
temp.stop();
}
go=false;
canBet=false;
cm.setAllMoney(cm.getAllMoney()-1);
cm.setWinMoney(cm.getWinMoney()+1);
cv.repaint(30,75,107,65);
}
}elseif(KeyEvent.VK_RIGHT==code&&canMove) {if(cm.getWinMoney()>0) {if(temp!=null) {
temp.stop();
}
go=false;
cm.setAllMoney(cm.getAllMoney()+1);
cm.setWinMoney(cm.getWinMoney()-1);
cv.repaint(30,75,107,65);if(cm.getWinMoney()==0) {
canBet=true;
cm.setBet(newBet());
cm.setBetting(false);
isBetting=false;
cv.repaint(0,30,800,600);
}
}
}
}publicvoidkeyTyped(KeyEvent ke) {
}publicvoidkeyReleased(KeyEvent ke) {intcode=ke.getKeyCode();if(KeyEvent.VK_ESCAPE==code) {
System.exit(0);
}//如果是回车键则分为几种可能,只分两种可能,有分和没分,有分就下分,没分就跑if(KeyEvent.VK_ENTER==code) {if(isRunning) {
ke.consume();
}else{
doEnter();
}
}elseif(KeyEvent.VK_NUMPAD1==code||KeyEvent.VK_Q==code) {if(cm.getWinMoney()>0&&isBetting) {
isBetting=false;
result=A_WIN;
}
}elseif(KeyEvent.VK_NUMPAD3==code||KeyEvent.VK_P==code) {if(cm.getWinMoney()>0&&isBetting) {
isBetting=false;
result=B_WIN;
}
}//要按键能出来PK界面,必须赢的钱要大于0,否则赌的没意义if(KeyEvent.VK_B==code) {if(cm.isBetting()) {//如果此时在PK,则无条件不显示cm.setBetting(false);
isBetting=false;
cv.repaint(0,30,800,600);
}elseif(cm.getWinMoney()>0&&canMove) {//设置为可以PKif(temp!=null) {
temp.stop();
}
go=false;
cm.setBetting(true);
doPK();
cv.repaint(0,30,800,600);
}
}
}//定义一个同步方法以防止两个线程共同起动跑privatesynchronizedvoiddoEnter() {//当前的可用分为0时,按回车就跑result=0;
cm.setBetting(false);
isBetting=false;
go=false;if(temp!=null) {
temp.stop();
}if(cm.getWinMoney()==0) {
cm.clearP();
resetPP();//无论怎么样,都要在开始把PP的记录重置//如果已下注了,那就取下注的为准if(canBet) {if(isBetted) {
bet=cm.getBet();
doRun();
isBetted=false;
}else{if(cm.getAllMoney()>=bet.getTotal()&&bet.getTotal()>0) {
bet.clearWin();
cm.setBet(bet);
cm.setAllMoney(cm.getAllMoney()-bet.getTotal());
doRun();
}
}
cv.repaint(0,30,800,600);
}
}else{
allWin+=cm.getWinMoney();
allBet+=cm.getBet().getTotal();
cm.setAllMoney(cm.getAllMoney()+cm.getWinMoney());
cm.setWinMoney(0);
cm.setBet(newBet());
canBet=true;
cv.repaint(0,30,800,600);
}
}//起PK线程,并且设置相当的布尔量//使之可以PKprivatevoiddoPK() {
isBetting=true;//此时才可以将这个设为真,因为不会再跑了,所以可以押大小了//doShowAllMoney();cm.setBetting(true);
es.execute(newRunnable() {publicvoidrun() {
pkRun();
}
});
}//处理PK的线程体privatevoidpkRun() {intindexA=0;intindexB=0;while(isBetting) {try{
Thread.sleep(80);intrand=(int) (Math.random()*100);if(rand%2==0) {if(indexA
cm.setPkA(pkA[++indexA]);
}else{
indexA=0;
cm.setPkA(pkA[0]);
}
}else{if(indexB
cm.setPkB(pkB[++indexB]);
}else{
indexB=0;
cm.setPkB(pkB[0]);
}
}
cv.repaint(269,274,254,132);
}catch(Exception exe) {
exe.printStackTrace();
}
}if(Math.random()*10>1) {while(indexA==indexB) {
indexA=(int) (Math.random()*3);
}
}
cm.setPkA(pkA[indexA]);
cm.setPkB(pkB[indexB]);
cv.repaint(238,258,330,152);
handleResult(indexA, indexB);
}//处理结果的一个方法,针对传进来的A和B的下标,判断谁赢谁输privatevoidhandleResult(inta,intb) {intcurrent=0;//当A为布时if(a==0) {if(b==0) {
current=AB_TIE;
}elseif(b==1) {
current=A_WIN;
}elseif(b==2) {
current=B_WIN;
}
}elseif(a==1) {if(b==0) {
current=B_WIN;
}elseif(b==1) {
current=AB_TIE;
}elseif(b==2) {
current=A_WIN;
}
}elseif(a==2) {if(b==0) {
current=A_WIN;
}elseif(b==1) {
current=B_WIN;
}elseif(b==2) {
current=AB_TIE;
}
}//当两者相等,则说明猜中了if(result==current||current==AB_TIE) {
win.play();
cm.setWinMoney(cm.getWinMoney()*2);
cv.repaint(30,75,107,65);try{
Thread.sleep(1000);
isBetting=true;
result=0;
pkRun();
}catch(Exception exe) {
exe.printStackTrace();
}
}elseif(result==0) {//如果此时等于0,则说明没有猜拳//showMoney=false;}else{
canBet=true;
lose.play();
cm.setWinMoney(0);//showMoney=false;try{
Thread.sleep(1000);
cm.setBetting(false);
isBetting=false;
cm.setBet(newBet());
cv.repaint(0,30,800,600);
}catch(Exception exe) {
exe.printStackTrace();
}
}
result=0;
}//计算当前的路径出发总共跑SUM步得到的结果privatePP fromIndex(PP from,intsum,booleanisRun) {if(isRun) {intall=sum;
PP[] ps=PP.values();intindex=(all+p.ordinal())%26;returnps[index];
}else{intall=5+7+sum;
PP[] ps=PP.values();intindex=(all+p.ordinal())%26;returnps[index];
}
}privatePP fromIndex(PP from,intsum) {intall=5+7+sum;
PP[] ps=PP.values();intindex=(all+p.ordinal())%26;returnps[index];
}//跑大四喜的实现privatevoiddoDaSiXi() {
es.execute(newRunnable() {publicvoidrun() {
runDaSiXi();
}
});
}//跑大四喜的方法体实现privatevoidrunDaSiXi() {
}//随机跑的时间用到的线程执行体privatevoidrunRun() {
doBlinkRatio();
RatioA ra=RatioA.A_20;
RatioB rb=RatioB.B_10;inttotal=con.getSteps(AUTO_CONTROL);intsum=0;//表示已经跑的数//PP p=PP.p1;//初始开始跑的位置cm.setRunning(true);//把模型设为正在跑的模式cm.setP(p);//p.setImage(yellow);intsleepTime=100;try{//先跑五个点做为起步runA.play();for(inti=1; i<6; i++) {
Thread.sleep(200);
p=p.next();
ra=ra.next();
rb=rb.next();
cm.setRa(ra);
cm.setRb(rb);
cm.setP(p);//p.setImage(yellow);//cv.repaint(178,50,465,413);cv.repaint(p.x-100, p.y-100,230,230);
}
loop.loop();while(sum++
Thread.sleep(20);
p=p.next();
cm.setP(p);
ra=ra.next();
rb=rb.next();
cm.setRa(ra);
cm.setRb(rb);//p.setImage(yellow);//cv.repaint(178,50,465,413);cv.repaint(p.x-100, p.y-100,230,230);
}
loop.stop();
runB.play();//后面七个点为停步for(inti=0; i<7; i++) {
p=p.next();
cm.setP(p);//p.setImage(yellow);Thread.sleep(sleepTime);
sleepTime+=10;
ra=ra.next();
rb=rb.next();
cm.setRa(ra);
cm.setRb(rb);//cv.repaint(178,50,465,413);cv.repaint(p.x-100, p.y-100,230,230);
}
PP pps=p;//doBlinkRun(pps);doRunOver(p);
}catch(Exception exe) {
exe.printStackTrace();
}
}//根据赔率得到当前的位置privateRatioA getRa(intratio) {
RatioA[] ras=RatioA.values();for(RatioA rs : ras) {if(rs.ratio==ratio) {returnrs;
}
}returnnull;
}privateRatioB getRb(intratio) {
RatioB[] rbs=RatioB.values();for(RatioB rs : rbs) {if(rs.ratio==ratio) {returnrs;
}
}returnnull;
}//处理根据国数得到当前的统一陪率privateintgetRatio(intcountry) {if(COUNTRY_A==country) {returncm.getRatioA();
}elseif(COUNTRY_B==country) {returncm.getRatioB();
}else{thrownewRuntimeException("出了个不该出的错误!");
}
}//确实是否亮赔率的灯privatevoiddoCheckBlinkRatio() {if(cm.isAWin()) {
cm.setRa(getRa(cm.getRatioA()));
}else{
cm.setRa(null);
}if(cm.isBWin()) {
cm.setRb(getRb(cm.getRatioB()));
}else{
cm.setRb(null);
}
cv.repaint(0,30,800,600);
}//处理跑完的场景,根据中得不同的下标,比如,跑火车,中彩金等privatevoiddoRunOver(PP p)throwsException {
doShowAllMoney();//如果下标大于0,则表示中了正规的注if(p.getIndex()>0) {
canMove=true;
doBlinkRun(p);
doCheckBlinkRatio();
cm.getBet().setWin(p.getIndex(),true);intwinMoney=0;if(p.country>0) {
winMoney=bet.getBet(p.getIndex())*p.country;
}else{
winMoney=bet.getBet(p.getIndex())*getRatio(p.country);
}
cm.setWinMoney(winMoney);
cv.repaint(30,75,107,65);
isRunning=false;
p.audio.play();
Thread.sleep(800);if(cm.getWinMoney()>0) {
playMusic();//doPK();}else{if(!isRunning) {
cm.setBet(newBet());
canBet=true;
}
}
cv.repaint(0,30,800,600);
}else{//如果下标等于ONCE_MORE,则表示开火车if(p.getIndex()==ONCE_MORE) {
doOnceMore();
}elseif(p.getIndex()==SONG_DENG) {
cm.setRa(getRa(cm.getRatioA()));
cm.setRb(getRb(cm.getRatioB()));
p.audio.play();
Thread.sleep(1000);
onceMore.play();
Thread.sleep(1500);
doSongDeng(p);
}elseif(p.getIndex()==SMALL_BONUS) {
p.audio.play();
Thread.sleep(1000);
cm.getInfo().setSmallBonus(0);
cv.repaint();
}elseif(p.getIndex()==BIG_BONUS) {
p.audio.play();
Thread.sleep(1000);
cm.getInfo().setBigBonus(0);
cv.repaint();
}//下面的语句是为了调试程序而用的//isBetting=true;}
}//专门用来播声音的方法privatevoidplayMusic() {intNO=(int) (Math.random()*10)+1;if(isBetting) {
}else{if(temp!=null) {
temp.stop();
}
temp=Applet.newAudioClip(this.getClass().getResource("sound/music/music"+NO+".au"));
temp.loop();
}
}//专起一个线程用于更新赔率的闪灯privatevoiddoBlinkRatio() {
es.execute(newRunnable() {publicvoidrun() {
blinkRatio=true;while(!canMove) {try{
Thread.sleep(40);
cv.repaint(141,496,518,32);if(!blinkRatio) {break;
}
}catch(Exception exe) {
exe.printStackTrace();
}
}
}
});
}//专门用以跑开火车的方法privatevoiddoOnceMore()throwsException {//doBlinkRatio();//blinkRatio=false;p.audio.play();
PP startP=p;
RatioA ra=RatioA.A_20;
RatioB rb=RatioB.B_10;inttotal=(int) (Math.random()*3)+3;//随机产生火车的节数finalAudioClip wu=Applet.newAudioClip(this.getClass().getResource("sound/oncemore/wu.au"));finalAudioClip run=Applet.newAudioClip(this.getClass().getResource("sound/oncemore/run.au"));finalAudioClip stop=Applet.newAudioClip(this.getClass().getResource("sound/oncemore/stop.au"));//先处理一个一个的出来for(inti=0; i
Thread.sleep(1000);
stop.play();
cm.addP(startP=startP.next());
cv.repaint(178,50,465,413);
}
es.execute(newRunnable() {publicvoidrun() {try{
wu.play();
Thread.sleep(3500);
run.loop();
}catch(Exception exe) {
exe.printStackTrace();
}
}
});intsteps=(int) (Math.random()*26)+60;intstepAll=steps;//在此控制火车的头尾,也就是说不能中到50-100倍的那个灯PP pto=fromIndex(p, steps+total,true);
PP pwe=fromIndex(p, steps,true);//当总长度大于15时,必须会一头一尾的溢出if(total>15) {while(!(pto.ordinal()<15&&pwe.ordinal()>17)) {
steps++;
pto=fromIndex(p, steps+total,true);
pwe=fromIndex(p, steps,true);
}
}else{while(!(pto.ordinal()<15||pwe.ordinal()>17)) {
steps++;
pto=fromIndex(p, steps+total,true);
pwe=fromIndex(p, steps,true);
}
}while(steps>0) {//p.setImage(yellow);Thread.sleep(200);
Vectorv=cm.getP();
cm.clearP();
ra=ra.next();
rb=rb.next();
cm.setRa(ra);
cm.setRb(rb);if(stepAll/2==steps) {
wu.play();
}for(inti=0; i
cm.addP(v.get(i).next());
}
steps--;
cv.repaint(178,50,465,413);
}
blinkRatio=false;
cm.setRa(getRa(cm.getRatioA()));
cm.setRb(getRb(cm.getRatioB()));
cv.repaint(141,496,518,32);
run.stop();//先只留一个车头,然后一个一个地出来Vectorv=cm.getP();
cm.clearP();for(inti=v.size()-1; i>=0; i--) {
PP p=v.get(i);if(p.index>0) {
cm.getBet().setWin(p.index,true);
stop.play();intwinMoney=0;if(p.country>0) {
winMoney=bet.getBet(p.getIndex())*p.country;
}else{
winMoney=bet.getBet(p.getIndex())*getRatio(p.country);
}
cm.setWinMoney(winMoney+cm.getWinMoney());
}
cm.addP(p);//doBlinkRun(p);//doCheckBlinkRatio();cv.repaint(0,30,800,600);
Thread.sleep(1000);
}for(PP pp : v) {
doBlinkRun(pp);
}
p=v.get(v.size()-1);
canMove=true;if(cm.getWinMoney()>0) {
playMusic();//doPK();}else{
cm.setBet(newBet());
canBet=true;
cv.repaint(0,30,800,600);
}
isRunning=false;
}//专门用以跑送灯的方法privatevoiddoSongDeng(PP ps) {
p=ps;
blinkRatio=false;//即使中到火车也不一定跑,跑的概率有70%if(Math.random()*100>30) {inttotal=(int) (Math.random()*3)+2;while(total>0) {try{intsteps=(int) (Math.random()*20)+20;
xiu.play();for(inti=0; i
Thread.sleep(20);
p=p.next();
cm.setP(p);
cv.repaint(p.x-100, p.y-100,230,230);
}//如果是已经中过的,或者大奖,或者彩金等等,皆不中while(p.isWin()||p.index<0||p.index==5) {
steps++;
Thread.sleep(20);
p=p.next();
cm.setP(p);
cv.repaint(p.x-100, p.y-100,230,230);
}finalPP pps=p;//doBlinkRun(pps);cm.addP(p);
p.setWin(true);if(p.index>0) {
cm.getBet().setWin(p.index,true);
p.audio.play();intwinMoney=0;if(p.country>0) {
winMoney=bet.getBet(p.getIndex())*p.country;
}else{
winMoney=bet.getBet(p.getIndex())*getRatio(p.country);
}
cm.setWinMoney(winMoney+cm.getWinMoney());
cv.repaint(0,30,800,600);
}
total--;
Thread.sleep(1500);
}catch(Exception exe) {
exe.printStackTrace();
}
}for(PP pp : cm.getP()) {
doBlinkRun(pp);
}//doCheckBlinkRatio();if(cm.getWinMoney()>0) {
playMusic();//doPK();}else{
cm.setBet(newBet());
canBet=true;
cv.repaint(0,30,800,600);
}
}else{
cm.setBet(newBet());
canBet=true;
cv.repaint(0,30,800,600);
}
canMove=true;
isRunning=false;
}//把PP里面的元素全部还原privatevoidresetPP() {
PP[] ps=PP.values();for(PP p : ps) {
p.setWin(false);
p.setImage(p.other);
}
}//闪动的线程方法实现privatevoiddoBlinkRun(finalPP p) {
es.execute(newRunnable() {publicvoidrun() {
blinkRun(p);
}
});
}//终点闪动的线程体privatevoidblinkRun(PP p) {inttotal=0;
go=true;while(go) {try{
Thread.sleep(300);
total++;
p.setImage(total%2==0?null: p.other);
cv.repaint(p.x, p.y,60,60);
}catch(Exception exe) {
exe.printStackTrace();
}
}
p.setImage(p.other);
cv.repaint(0,30,800,600);
}//专门定义一个类来,把一些控制的机制封装在里面privateclassControl {publicControl() {
}//通过传入参数的值得到该返回多少步//此方法是对外的统一接口publicintgetSteps(intstyle) {if(COMMON==style) {returngetCommon();
}elseif(WIN_MIN==style) {returngetWinMin();
}elseif(WIN_MAX==style) {returngetWinMax();
}elseif(OPEN_MAX==style) {returngetOpenMax();
}elseif(OPEN_MIN==style) {returngetOpenMin();
}elseif(OPEN_BIG_BONUS==style) {returngetOpenBigBonus();
}elseif(OPEN_SMALL_BONUS==style) {returngetOpenSmallBonus();
}elseif(OPEN_SONG_DENG==style) {returngetOpenSongDeng();
}elseif(OPEN_ONCE_MORE==style) {returngetOpenOnceMore();
}elseif(AUTO_CONTROL==style) {returngetAutoControl();
}elseif(OPEN_LOSE==style) {returngetOpenLose();
}else{return(int) (Math.random()*26)+10;
}
}//内部方法,实现普通跑的步数privateintgetCommon() {inttotal=(int) (Math.random()*26)+50;//如果此时随机跑中得是大奖,则不一定给if(fromIndex(p, total).index==5) {if(Math.random()*100>1) {
total+=3;
}
}//如果此时中了大小彩金,则也不一定给if(fromIndex(p, total).index==BIG_BONUS||fromIndex(p, total).index==SMALL_BONUS) {//if(Math.random()*10000>1){total++;//}}returntotal;
}//实现取赢最小的方案privateintgetWinMin() {
Bet bet=cm.getBet();intindex=0;intwin=Integer.MAX_VALUE;int[] wins=newint[10];for(inti=1; i<10; i++) {
PP p=PP.p1;while(p.index!=i) {
p=p.next();
}if(p.country>0) {
wins[i]=bet.getBet(p.getIndex())*p.country;
}else{
wins[i]=bet.getBet(p.getIndex())*getRatio(p.country);
}if(wins[i]<=win) {if(wins[i]==win) {if(Math.random()*10>5) {
win=wins[i];
index=i;
}
}else{
win=wins[i];
index=i;
}
}
}returngetTotal(p, index);
}//实现赢得最多方案privateintgetWinMax() {
Bet bet=cm.getBet();intindex=0;intwin=Integer.MIN_VALUE;int[] wins=newint[10];for(inti=1; i<10; i++) {
PP p=PP.p1;while(p.index!=i) {
p=p.next();
}if(p.country>0) {
wins[i]=bet.getBet(p.getIndex())*p.country;
}else{
wins[i]=bet.getBet(p.getIndex())*getRatio(p.country);
}if(wins[i]>win) {
win=wins[i];
index=i;
}
}returngetTotal(p, index);
}//实现开最大的那个方案(50或100倍)privateintgetOpenMax() {returngetTotal(p,5);
}//实现开最小的那个方案(2或5倍)privateintgetOpenMin() {intrad=(int) (Math.random()*10);if(rad>5) {returngetTotal(p,1);
}else{returngetTotal(p,9);
}
}//实现只要让玩家输即可,不一定让玩得输最多publicintgetOpenLose() {intbetAll=cm.getBet().getTotal();//用一个哈希表来存储输的下标和输的金额EnumMaplose=newEnumMap(PP.class);for(inti=1; i<10; i++) {intwins=0;
PP p=PP.p1;while(p.index!=i) {
p=p.next();
}if(p.country>0) {
wins=bet.getBet(p.getIndex())*p.country;
}else{
wins=bet.getBet(p.getIndex())*getRatio(p.country);
}if(wins
}
}
PP last=PP.p1;//最后要得到的位置Setkeys=lose.keySet();
System.out.println("会输的PP的个数是:"+keys.size());//分三种方式去取想要的位置intMyRad=(int) (Math.random()*100)+1;if(MyRad>85) {//第一种方式用权重去取到底开什么System.out.println("用权重去取开什么牌了");doublevalue=0;
Iteratorit=keys.iterator();while(it.hasNext()) {
PP now=it.next();doublemyValue=lose.get(now)*1.0/now.random;if(myValue>=value) {
value=myValue;
last=now;
}
}
}elseif(MyRad>30) {//第二种是概率去取到底开什么System.out.println("用概率去取什么牌了");intrand=(int) (Math.random()*100)+1;//产生一个1-100的随机数System.out.println("产生的随机数是:"+rand);//然后把要开的位置按概率从小到大排,尽量满足概率小的开PP[] ps=keys.toArray(newPP[keys.size()]);for(inti=0; ips[j].random) {
PP temp=ps[i];
ps[i]=ps[j];
ps[j]=temp;
}
}
}
System.out.println("排列前的顺序是:"+keys);
System.out.println("排列后的顺序是:");for(PP now : ps) {
System.out.print(now+",");
}
System.out.println();//双重循环,已经把概率小的排在前面了booleanisFind=false;//一个布尔量,看有没有当前随机数匹配的for(PP now : ps) {if(now.random>rand) {
System.out.println("匹配到的是:"+now+"它的概率是:"+now.random*2);
last=now;
isFind=true;break;
}
}if(!isFind) {
System.out.println("没有匹配到,随机找一个");intindex=(int) (Math.random()*keys.size());
last=ps[index];
}
}else{//第三种是直接以概率来取System.out.println("直接取概率");
PP[] ps=keys.toArray(newPP[keys.size()]);intindex=(int) (Math.random()*keys.size());
last=ps[index];
}
System.out.println("===============================================");returngetTotal(p, last.index);
}//表示开大彩金的实现privateintgetOpenBigBonus() {returngetTotal(p, BIG_BONUS);
}//表示开小彩金的实现privateintgetOpenSmallBonus() {returngetTotal(p, SMALL_BONUS);
}//表示开出送灯的实现privateintgetOpenSongDeng() {returngetTotal(p, SONG_DENG);
}//表示开出跑火车的实现privateintgetOpenOnceMore() {returngetTotal(p, ONCE_MORE);
}//实现到传入的位置,应该全速跑的数量totalprivateintgetTotal(PP from,intindex) {intsum=(int) (Math.random()+26)+30;while(fromIndex(from, sum).index!=index) {
sum++;
}returnsum;
}//实现智能自动控privateintgetAutoControl() {
System.out.println("总下注是:"+allBet);
System.out.println("总赢 是:"+allWin);intrad=(int) (Math.random()*100);
System.out.println("随机数是:"+rad);//输了九成了,这个时候送个大的if(allBet/allWin>10) {
System.out.println("输了九成了");if(rad>95) {returngetSteps(WIN_MAX);
}elseif(rad>90) {returngetSteps(OPEN_SONG_DENG);
}elseif(rad>80) {returngetSteps(OPEN_ONCE_MORE);
}elseif(rad>30) {returngetSteps(COMMON);
}else{returngetSteps(OPEN_LOSE);
}
}elseif(allBet/allWin>5) {
System.out.println("输了八成了");if(rad>98) {returngetSteps(WIN_MAX);
}elseif(rad>95) {returngetSteps(OPEN_SONG_DENG);
}elseif(rad>90) {returngetSteps(OPEN_ONCE_MORE);
}elseif(rad>30) {returngetSteps(COMMON);
}else{returngetSteps(OPEN_LOSE);
}
}elseif(allBet>allWin) {
System.out.println("输了。。。。。。");if(rad>98) {returngetSteps(WIN_MAX);
}elseif(rad>95) {returngetSteps(OPEN_SONG_DENG);
}elseif(rad>90) {returngetSteps(OPEN_ONCE_MORE);
}elseif(rad>10) {returngetSteps(COMMON);
}else{returngetSteps(OPEN_LOSE);
}
}elseif(allBet
System.out.println("赢了。。。。。。");if(rad>50) {returngetSteps(OPEN_LOSE);
}else{returngetSteps(COMMON);
}
}else{
System.out.println("出鬼了。。。。");returngetSteps(COMMON);
}
}
}
}