你好,游客 登录 注册 搜索
背景:
阅读新闻

Android开发教程:斗地主 [牌桌实现源码]

[日期:2012-04-06] 来源:Linux社区  作者:Linux [字体:大 中 小]

玩家类:

  1. package com.bison.utils;  
  2.   
  3. import Android.graphics.Rect;  
  4.   
  5. /** 
  6.  * 这个是玩家的实体类 
  7.  *  
  8.  * @author Bison 
  9.  *  
  10.  */  
  11. public class Person {  
  12.     private final Cards mCards = Cards.getInstance();  
  13.   
  14.     public int[] person1 = new int[17];  
  15.     public int[] person2 = new int[17];  
  16.     public int[] person3 = new int[17];  
  17.   
  18.     // 余下三张属于地主的   
  19.     public int[] threePukes = new int[3];  
  20.   
  21.     public Person() {  
  22.         personHold(mCards.pukes);  
  23.     }  
  24.   
  25.     /** 分牌 */  
  26.     private void personHold(int[] pukes) {  
  27.         int k = 0;  
  28.         for (int i = 0; i < 3; i++) {  
  29.             if (i == 0) {  
  30.                 for (int j = 0; j < 17; j++) {  
  31.                     person1[j] = pukes[k++];  
  32.                 }  
  33.                 // 将其排序   
  34.                 sort(person1);  
  35.             }  
  36.             if (i == 1) {  
  37.                 for (int j = 0; j < 17; j++) {  
  38.                     person2[j] = pukes[k++];  
  39.                 }  
  40.                 // 将其排序   
  41.                 sort(person2);  
  42.             }  
  43.             if (i == 2) {  
  44.                 for (int j = 0; j < 17; j++) {  
  45.                     person3[j] = pukes[k++];  
  46.                 }  
  47.                 // 将其排序   
  48.                 sort(person3);  
  49.             }  
  50.         }  
  51.   
  52.         threePukes[0] = pukes[51];  
  53.         threePukes[1] = pukes[52];  
  54.         threePukes[2] = pukes[53];  
  55.     }  
  56.   
  57.     /** 对每个玩家手里的牌排序:使用冒泡排序 */  
  58.     private void sort(int[] ary) {  
  59.         for (int i = 0; i < ary.length; i++) {  
  60.             for (int j = 0; j < ary.length - i - 1; j++) {  
  61.                 if (ary[j] > ary[j + 1]) {  
  62.                     int temp = ary[j];  
  63.                     ary[j] = ary[j + 1];  
  64.                     ary[j + 1] = temp;  
  65.                 }  
  66.             }  
  67.         }  
  68.     }  
  69.   
  70.     /** 
  71.      * 对应扑克所在图片上的位置  
  72.      * 1 5 9 ………… 53  
  73.      * 2 6 10 ………… 54  
  74.      * 3 7 11  
  75.      * 4 8 12 
  76.      */  
  77.     public Rect cardRect(int cardValue, int width, int height) {  
  78.         int x = 0, y = 0;  
  79.         if (cardValue % 4 == 0) {  
  80.             x = cardValue / 4 - 1;  
  81.             y = 4;  
  82.         } else {  
  83.             x = cardValue / 4;  
  84.             y = cardValue % 4;  
  85.         }  
  86.   
  87.         int left = x * width;  
  88.         int top = (y - 1) * height;  
  89.         int right = (x + 1) * width;  
  90.         int bottom = (y) * height;  
  91.         return new Rect(left, top, right, bottom);  
  92.     }  
  93. }  

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


更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11

linux
相关资讯       Android开发基础教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款