zoukankan      html  css  js  c++  java
  • 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 + "	");
    			if(i % 9 == 8){
    				System.out.println("");
    			}
    		}
    	}
    }
    
    妥妥的~

  • 相关阅读:
    直击微软第九频道著名主持Robert Green 对话一站式示例代码库大老板梁梅女士
    微软发布中文版一站式示例代码浏览器
    每日一例,练就编程高手
    微软发布Visual Studio 2012 示例代码浏览器
    微软发布Sample Browser for Windows 8版:5000示例代码,"触手可及"
    arthas使用总结
    前端如何生成二维码
    golang的helloworld以及nonmain package的troubleshooting
    监控文件的网页工具
    postfix + courierimap + squirrelMail 邮件服务器
  • 原文地址:https://www.cnblogs.com/mthoutai/p/6941447.html
Copyright © 2011-2022 走看看