欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Android斗地主游戏的牌桌实现,android斗地主牌桌,发一个Android斗地

来源: javaer 分享于  点击 21538 次 点评:64

Android斗地主游戏的牌桌实现,android斗地主牌桌,发一个Android斗地


发一个Android斗地主游戏的牌桌实现。

为了节约内存资源,每张扑克牌都是剪切形成的,当然这也是当前编程的主流方法。

1、主ActivityjavaNone3、相关实体类

扑克牌类:```javapackage com.bison.utils;

import java.util.Random;

/ * 生成一副洗好的牌,并且 设计为单例模式
* @author Bison

*/
public class Cards {
// 声明一副扑克牌
public int[] pukes = new int[54];

private static Cards cardsInstance = null;private Cards() {      setPuke();      shuffle();  }public static Cards getInstance() {      if (cardsInstance == null) {          cardsInstance = new Cards();      }      return cardsInstance;  }/** 给54张扑克牌赋值 :1~54 */  private void setPuke() {      for (int i = 0; i < 54; i++) {          pukes[i] = i + 1;      }  }/** 洗牌 */  private void shuffle() {      Random rdm = new Random();      for (int i = 0; i < 54; i++) {          // random.nextInt();是个前闭后开的方法:0~53           int rdmNo = rdm.nextInt(54);          int temp = pukes[i];          pukes[i] = pukes[rdmNo];          pukes[rdmNo] = temp;      }  }

}

玩家类:

package com.bison.utils;

import android.graphics.Rect;

/ * 这个是玩家的实体类
* @author Bison

*/
public class Person {
private final Cards mCards = Cards.getInstance();

public int[] person1 = new int[17];  public int[] person2 = new int[17];  public int[] person3 = new int[17];// 余下三张属于地主的   public int[] threePukes = new int[3];public Person() {      personHold(mCards.pukes);  }/** 分牌 */  private void personHold(int[] pukes) {      int k = 0;      for (int i = 0; i < 3; i++) {          if (i == 0) {              for (int j = 0; j < 17; j++) {                  person1[j] = pukes[k++];              }              // 将其排序               sort(person1);          }          if (i == 1) {              for (int j = 0; j < 17; j++) {                  person2[j] = pukes[k++];              }              // 将其排序               sort(person2);          }          if (i == 2) {              for (int j = 0; j < 17; j++) {                  person3[j] = pukes[k++];              }              // 将其排序               sort(person3);          }      }    threePukes[0] = pukes[51];      threePukes[1] = pukes[52];      threePukes[2] = pukes[53];  }/** 对每个玩家手里的牌排序:使用冒泡排序 */  private void sort(int[] ary) {      for (int i = 0; i < ary.length; i++) {          for (int j = 0; j < ary.length - i - 1; j++) {              if (ary[j] > ary[j + 1]) {                  int temp = ary[j];                  ary[j] = ary[j + 1];                  ary[j + 1] = temp;              }          }      }  }/**  * 对应扑克所在图片上的位置   * 1 5 9 ………… 53   * 2 6 10 ………… 54   * 3 7 11   * 4 8 12  */  public Rect cardRect(int cardValue, int width, int height) {      int x = 0, y = 0;      if (cardValue % 4 == 0) {          x = cardValue / 4 - 1;          y = 4;      } else {          x = cardValue / 4;          y = cardValue % 4;      }    int left = x * width;      int top = (y - 1) * height;      int right = (x + 1) * width;      int bottom = (y) * height;      return new Rect(left, top, right, bottom);  }

}

```斗地主还是可以做成很复杂的。相关图片

相关栏目:

用户点评