/*
* ClientView.java
*
* Created on 2007年10月2日, 下午2:00
* 此类专门负责视图的实现,此类中须定义从模型中
* 取出数据并重绘的方法
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author lbf
*/
public class ClientView extends JPanel {
private ClientModel cm; //模型类的一个对象
private volatile boolean isStar;
private Image starA; //表示当前星星的图片
private Image[] star; //星星的数组
private int x1;
private int y1; //星星的座标
private Image bg2; //表示底衬的那层
private Image ratio; //赔率底衬的那层
private int x;
private int length; //表示跑马灯的位置
/** Creates a new instance of ClientView */
public ClientView(ClientModel cm) {
this.cm = cm;
initOther();
x = 646;
new RunStar().start();
new Draw().start();
}
//初始化视图类的一些参数
private void initOther() {
try {
star = new Image[3];
MediaTracker mt = new MediaTracker(this);
for (int i = 0; i < 3; i++) {
star = Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("pic/game/star/" + (i + 1) + ".png"));
mt.addImage(star, i);
}
bg2 = Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("pic/game/bg2.png"));
ratio = Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("pic/game/ratio.png"));
mt.addImage(bg2, 4);
mt.addImage(ratio, 5);
/*
* ClientModel.java
*
* Created on 2007年10月2日, 下午2:02
* 此类封装了一些公共的数据,为视图类提供模型
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.hadeslee.apple.client;
/** Creates a new instance of ClientModel */
public ClientModel() {
v=new Vector<PP>();
ra=RatioA.A_20;
rb=RatioB.B_10;
}
public Image getPKBG(){
return pk;
}
public
void setPKBG(Image bg){
pk=bg;
}
public Image getBg() {
return bg;
}
public
int getRatioA(){
return ratioA;
}
public
int getRatioB(){
return ratioB;
}
public
void setRatioA(int a){
ratioA=a;
}
public
void setRatioB(int b){
ratioB=b;
}
public
void setBg(Image bg) {
this.bg = bg;
}
public Image getTable() {
return table;
}
public
void setTable(Image table) {
this.table = table;
}
/* public Image getRunA() {
return runA;
}
public void setRunA(Image run) {
this.runA = run;
}
public Image getRunB(){
return runB;
}
public void setRunB(Image run){
runB=run;
}
*/
public Vector<PP> getP() {
Vector<PP> vec=new Vector<PP>();
for(PP p: v){
vec.add(p);
}
return vec;
}
public
void addP(PP p){
v.add(p);
}
public
void setP(PP p){
if(v.size()>0){
v.remove(v.size()-1);
}
v.add(p);
}
public
void clearP(){
v.removeAllElements();
}
public Bet getBet() {
return bet;
}
public
void setBet(Bet bet) {
this.bet = bet;
}
public INFO getInfo() {
return info;
}
public
void setInfo(INFO info) {
this.info = info;
}
public
int getAllMoney() {
return allMoney;
}
public
void setAllMoney(int allMoney) {
this.allMoney = allMoney;
}
public
int getWinMoney() {
return winMoney;
}
public
void setWinMoney(int winMoney) {
this.winMoney = winMoney;
}
public
int getId() {
return id;
}
public
void setId(int id) {
this.id = id;
}
public Image[] getNumber() {
return number;
}
public
void setNumber(Image[] number) {
this.number = number;
}
public Image getNumber(int i){
return number;
} public
void setWinNumber(Image[] winNumber){
this.winNumber=winNumber;
}
public Image getWinNumber(int i){
return winNumber;
} public Image[] getWinNumber(){
return winNumber;
}
public
boolean isRunning() {
return Running;
}
public
void setRunning(boolean Running) {
this.Running = Running;
}
public
boolean isBetting() {
return Betting;
}
public
void setBetting(boolean Betting) {
this.Betting = Betting;
}
public
void setHasRunB(boolean b){
hasRunB=b;
}
public
boolean getHasRunB(){
return hasRunB;
}
public Image getPkA() {
return pkA;
}
public
void setPkA(Image pkA) {
this.pkA = pkA;
}
public Image getPkB() {
return pkB;
}
public
void setPkB(Image pkB) {
this.pkB = pkB;
}
public RatioA getRa() {
return ra;
}
public
void setRa(RatioA ra) {
this.ra = ra;
}
public RatioB getRb() {
return rb;
}
public
void setRb(RatioB rb) {
this.rb = rb;
}
//得到A国是否中奖
/*
* ClientControl.java
*
* Created on 2007年10月2日, 下午2:02
* 此类是客户端中的关键类,此类专门负责整个程序的
* 流程和表现,负责对外通讯
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.hadeslee.apple.client;
private
void handleResult(int a, int b) {
int current =
0;
//当A为布时
if (a ==
0) {
if (b ==
0) {
current = AB_TIE;
} else
if (b ==
1) {
current = A_WIN;
} else
if (b ==
2) {
current = B_WIN;
}
} else
if (a ==
1) {
if (b ==
0) {
current = B_WIN;
} else
if (b ==
1) {
current = AB_TIE;
} else
if (b ==
2) {
current = A_WIN;
}
} else
if (a ==
2) {
if (b ==
0) {
current = A_WIN;
} else
if (b ==
1) {
current = B_WIN;
} else
if (b ==
2) {
current = AB_TIE;
}
}
//当两者相等,则说明猜中了
private
void doOnceMore() throws Exception {
//doBlinkRatio();
// blinkRatio=false;
p.audio.play();
PP startP = p;
RatioA ra = RatioA.A_20;
RatioB rb = RatioB.B_10;
int total = (int) (Math.random()*3) +
3; //随机产生火车的节数
final AudioClip wu = Applet.newAudioClip(this.getClass().getResource("sound/oncemore/wu.au"));
final AudioClip run = Applet.newAudioClip(this.getClass().getResource("sound/oncemore/run.au"));
final AudioClip stop = Applet.newAudioClip(this.getClass().getResource("sound/oncemore/stop.au"));
//先处理一个一个的出来
for (int i =
0; i < total; i++) {
Thread.sleep(1000);
stop.play();
cm.addP(startP = startP.next());
cv.repaint(178, 50, 465, 413);
}
es.execute(new Runnable() {
private
int getWinMin() {
Bet bet = cm.getBet();
int index =
0;
int win = Integer.MAX_VALUE;
int[] wins =
new
int[10];
for (int i =
1; i <
10; i++) {
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<= win) {
if (wins== win) {
if (Math.random() *
10
>
5) {
win = wins;
index = i;
}
} else {
win = wins;
index = i;
}
}
}
return getTotal(p, index);
}
//实现赢得最多方案
private
int getWinMax() {
Bet bet = cm.getBet();
int index =
0;
int win = Integer.MIN_VALUE;
int[] wins =
new
int[10];
for (int i =
1; i <
10; i++) {
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> win) {
win = wins;
index = i;
}
}
return getTotal(p, index);
}
//实现开最大的那个方案(50或100倍)
private
int getOpenMax() {
return getTotal(p, 5);
}
//实现开最小的那个方案(2或5倍)
private
int getOpenMin() {
int rad = (int) (Math.random()*10);
if (rad >
5) {
return getTotal(p, 1);
} else {
return getTotal(p, 9);
}
}
//实现只要让玩家输即可,不一定让玩得输最多
public
int getOpenLose() {
int betAll = cm.getBet().getTotal();
//用一个哈希表来存储输的下标和输的金额
EnumMap<PP, Integer> lose =
new EnumMap<PP, Integer>(PP.class);
for (int i =
1; i <
10; i++) {
int wins =
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 < betAll) {
//如果赢的小于下注的,则输的个数加1
lose.put(p, betAll - wins);
}
}
PP last = PP.p1; //最后要得到的位置
Set<PP> keys = lose.keySet();
System.out.println("会输的PP的个数是:"
+ keys.size());
//分三种方式去取想要的位置
int MyRad = (int) (Math.random()*100) +
1;
if (MyRad >
85) {
//第一种方式用权重去取到底开什么
System.out.println("用权重去取开什么牌了");
double value =
0;
Iterator<PP> it = keys.iterator();
while (it.hasNext()) {
PP now = it.next();
double myValue = lose.get(now) *
1.0
/ now.random;
if (myValue >= value) {
value = myValue;
last = now;
}
}
} else
if (MyRad >
30) {
//第二种是概率去取到底开什么
System.out.println("用概率去取什么牌了");
int rand = (int) (Math.random()*100) +
1; //产生一个1-100的随机数
System.out.println("产生的随机数是:"
+ rand);
//然后把要开的位置按概率从小到大排,尽量满足概率小的开
PP[] ps = keys.toArray(new PP[keys.size()]);
for (int i =
0; i < keys.size(); i++) {
for (int j = i +
1; j < keys.size(); j++) {
if (ps.random > ps[j].random) {
PP temp = ps;
ps= ps[j];
ps[j] = temp;
}
}
}
System.out.println("排列前的顺序是:"
+ keys);
System.out.println("排列后的顺序是:");
for (PP now : ps) {
System.out.print(now +
",");
}
System.out.println();
//双重循环,已经把概率小的排在前面了
boolean isFind =
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("没有匹配到,随机找一个");
int index = (int) (Math.random()*keys.size());
last = ps[index];
}
} else {
//第三种是直接以概率来取
System.out.println("直接取概率");
PP[] ps = keys.toArray(new PP[keys.size()]);
int index = (int) (Math.random()*keys.size());
last = ps[index];
}
System.out.println("===============================================");
return getTotal(p, last.index);
}
//表示开大彩金的实现
private
int getOpenBigBonus() {
return getTotal(p, BIG_BONUS);
}
//表示开小彩金的实现
private
int getOpenSmallBonus() {
return getTotal(p, SMALL_BONUS);
}
//表示开出送灯的实现
private
int getOpenSongDeng() {
return getTotal(p, SONG_DENG);
}
//表示开出跑火车的实现
private
int getOpenOnceMore() {
return getTotal(p, ONCE_MORE);
}
//实现到传入的位置,应该全速跑的数量total
private
int getTotal(PP from, int index) {
int sum = (int) (Math.random()+26) +
30;
while (fromIndex(from, sum).index != index) {
sum++;
}
return sum;
}
//实现智能自动控
private
int getAutoControl() {
System.out.println("总下注是:"
+ allBet);
System.out.println("总赢 是:"
+ allWin);
int rad = (int) (Math.random()*100);
System.out.println("随机数是:"
+ rad);
//输了九成了,这个时候送个大的