JAVA程序设计(11)-----面对对象0基础设计 麻将 创建麻将牌 然后洗牌 发牌~ 恩 就这样
zzzzZZZZ
1.開始还想贴图的 实在太懒了…… 这是一张麻将
package com.lovo;
import java.awt.Graphics;
import java.awt.Image;
/**
* 类 : 麻将牌
* @author Abe 属性:花色 点数 图片
*/
public class Mahjong {
private Suits suits;
private int face;
private Image image;
/**
* 构造器
*
* @param suits
* @param face
* @param image
*/
public Mahjong(Suits 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 (suits) {
case CIRCLE:
str += face + "筒";
break;
case BAMBOO:
str += face + "条";
break;
case CHARACTER:
str += face + "万";
break;
}
return str;
}
/**
* 获得花色 和 点数
* @return
*/
public Suits getSuits() {
return suits;
}
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() {
Suits[] suits = {Suits.CIRCLE , Suits.BAMBOO , Suits.CHARACTER};
int[] faces = {1,2,3,4,5,6,7,8,9};
for (int i = 0 ; i < mah.length; i++ ){
mah[i] = new Mahjong(suits[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("");
}
}
}
}
妥妥的~
posted on
2017-06-04 18:46 mthoutai 阅读(
...) 评论(
...) 编辑 收藏