J2ME的遊戲堆積二(入門篇---鬥地主)

目前類的結構如下(部分在前面已經描述過):

                                                   

其中的方法在(一)中已經描述,主要fightlord包

-------------------------

Squeezer.java

牌的描述類

/*
 * Created on 2006-3-20
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.leqi.fightlord;

/**
 * * @author Administrator
 *
 * create  2006-3-20
 *
 * msn: [email protected]  
 *
 * qq: 26826880
 *
 * mail: [email protected]
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Squeezer {

 private byte type = 1;
 private byte value = 2;
 private byte sign = 0;
 /**
  *
  */
 public Squeezer() {
  super();
  // TODO Auto-generated constructor stub
 }
 

 /**
  * @return Returns the type.
  */
 public byte getType() {
  return type;
 }
 /**
  * @param type The type to set.
  */
 public void setType(byte type) {
  this.type = type;
 }
 /**
  * @return Returns the value.
  */
 public byte getValue() {
  return value;
 }
 /**
  * @param value The value to set.
  */
 public void setValue(byte value) {
  this.value = value;
 }
 
 /**
  * @return Returns the sign.
  */
 public byte getSign() {
  return sign;
 }
 /**
  * @param sign The sign to set.
  */
 public void setSign(byte sign) {
  this.sign = sign;
 }
}

------------------------

InitSqueezer.java

對54張牌進行初始化工作

/*
 * Created on 2006-3-20
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.leqi.fightlord;

/**
 * * @author Administrator
 *
 * create  2006-3-20
 *
 * msn: [email protected]  
 *
 * qq: 26826880
 *
 * mail: [email protected]
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class InitSqueezer {

 
 public static Squeezer[] init(){
  Squeezer squeezer;
  Squeezer[] squeezers = null;
  squeezers = new Squeezer[54];
  int i = 0;
  byte type = 8;
  int value = 1;
  for(type = 8; type >= 1;type >>>=1){
   for(value = 1; value < 14; i++, value++){
    squeezer = new Squeezer();
    squeezer.setType(type);
    squeezer.setValue((byte)value);
    squeezer.setSign((byte)(0));
    squeezers[i] = squeezer;
    }
  }
  squeezer = new Squeezer();
  squeezer.setType(type);
  squeezer.setValue((byte)0);
  squeezer.setSign((byte)(0));
  squeezers[i] = squeezer;
  
  i++;
  squeezer = new Squeezer();
  squeezer.setType(type);
  squeezer.setValue((byte)1);
  squeezer.setSign((byte)(0));
  squeezers[i] = squeezer;
  
  return squeezers;
  
 }
}

-------------------------------

Deal.java

這是遊戲初始的一個關鍵類,根據鬥地主遊戲的規則,對InitSqueezer類初始好的牌進行分發:隨機保留下三張牌,做爲底牌,將剩下的51張牌隨機分配給三個用戶。

/*
 * Created on 2006-3-20
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.leqi.fightlord;

import java.util.Random;

/**
 * * @author Administrator
 *
 * create  2006-3-20
 *
 * msn: [email protected]  
 *
 * qq: 26826880
 *
 * mail: [email protected]
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Deal {

 private static Squeezer[] squeezers;
 /**
  *
  */
 public Deal() {
  super();
  // TODO Auto-generated constructor stub
 }
 public static Squeezer[] deal(Squeezer[] squs) {
  squeezers = squs;
  
  Random random = new Random();
  //隨機找出三張做底牌
  for(int i=0; i<3;){
   int n = 0;
   n = random.nextInt();
   n>>>=26;
   byte sign = (byte)(n%54);
   if(squeezers[sign].getSign() == (byte)0){
    squeezers[sign].setSign((byte)4);
    i++;
   }
  }
  //將剩下的51張牌隨機分爲三份
  //設置第1份
  for(int i=0; i<17;){
   int n = 0;
   n = random.nextInt();
   n>>>=26;
   byte sign = (byte)(n%54);
   if(squeezers[sign].getSign() == (byte)0){
    squeezers[sign].setSign((byte)1);
    i++;
   }
  }
//  設置第2份
  for(int i=0; i<17;){
   int n = 0;
   n = random.nextInt();
   n>>>=26;
   byte sign = (byte)(n%54);
   if(squeezers[sign].getSign() == (byte)0){
    squeezers[sign].setSign((byte)2);
    i++;
   }
  }
//  設置第3份
  for(int i=0; i<54; i++){
   if(squeezers[i].getSign() == (byte)0){
    squeezers[i].setSign((byte)3);
   }
  }
  //打印分牌的信息
//  for(int i=0; i<54; i++){
//   System.out.println(squeezers[i].getSign()+"hava:"
//     + squeezers[i].getType()+"values:" +
//     squeezers[i].getValue());
//  }
  
  
  return squeezers;
 }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章