zzzzZZZZ
1.开始还想贴图的 实在太懒了…… 这是一张麻将
package com.lovo; import java.awt.Graphics; import java.awt.Image; /** * 类 : 麻将牌 * @author Abe 属性:花色 点数 图片 */ public class Mahjong { private Suite suite; private int face; private Image image; /** * 构造器 * * @param suits * @param face * @param image */ public Mahjong(Suite suits, int face/*, Image image*/) { this.suite = suits; this.face = face; // this.image = image; } /** * 绘制一张牌 */ public void draw(Graphics g,int x ,int y){ g.drawImage(image, x, y, 50, 100, null); } /** * 输出一张牌的点数和花色 */ public String toString(){ String str = ""; switch (suite) { case CIRCLE: str += face + "筒"; break; case BAMBOO: str += face + "条"; break; case CHARACTER: str += face + "万"; break; } return str; } /** * 获得花色 和 点数 * @return */ public Suite getSuits() { return suite; } public int getFace() { return face; } }
2.一副麻将
package com.lovo; import java.awt.Image; import javax.swing.ImageIcon; /** * 类:一副麻将牌(108张) * * @author Abe 属性:牌面图片 一副牌 第多少张 */ public class Mahjongs { // private static Image[] images = new Image[36]; private Mahjong[] mah = new Mahjong[108]; private int sheet = 0; // static {// 静态加载器 // for (int i = 0; i < images.length; i++) { // ImageIcon icon = new ImageIcon("mahjong/" + (i + 1) + ".jpg"); // for(int j = 0 ; j < 4 ; j++){ // images[i * 4 + j] = icon.getImage(); // } // } // } /** * 构造器 初始化 */ public Mahjongs() { Suite[] suites = {Suite.CIRCLE , Suite.BAMBOO , Suite.CHARACTER}; int[] faces = {1,2,3,4,5,6,7,8,9}; for (int i = 0 ; i < mah.length; i++ ){ mah[i] = new Mahjong(suites[i / 36], faces[i % 9]/*, images[i]*/); } } /** * 行为 : 洗牌 */ public void stuffle(){ for (int n = 0; n < 500; n++) { int i = (int) (Math.random() * 108); int j = (int) (Math.random() * 108); Mahjong temp; temp = mah[i]; mah[i] = mah[j]; mah[j] = temp; } } /** * 行为: 发牌 */ public Mahjong deal(){ return sheet < mah.length ? mah[sheet++] : null; //这句还是有点模糊 要在输出sheet之后再++的么? } public Mahjong[] getAll(){ return mah; } }
3.打印
package com.lovo; /** * 麻将 * @author Abe * */ public class TestMah { public static void main(String[] args) { Mahjongs mahj = new Mahjongs(); Mahjong one = null; mahj.stuffle(); for(int i = 0 ; i < 108 ; i++){ one = mahj.deal(); System.out.print(one + "\t"); if(i % 9 == 8){ System.out.println(""); } } } }
妥妥的~
时间: 2024-11-19 22:36:34