JAVA programming (11) ----- face the object elementary design Mahjong creates Mahjong cards and then shuffling the cards to deal with them ~ Well, that's it.
ZzzzZZZZ
1. At the beginning, I still wanted to map objects ...... This is a mahjong
Package com. lovo; import java. awt. graphics; import java. awt. image;/*** class: Mahjong card * @ author Abe attribute: Color points Image */public class Mahjong {private Suite suite; private int face; private image Image; /*** constructor ** @ param suits * @ param face * @ param image */public Mahjong (Suite suits, int face/*, Image image */) {this. suite = suits; this. face = face; // this. image = image;}/*** draw a card */public void draw (Graphics g, int x, int y) {g. drawImage (image, x, y, 50,100, null);}/*** number of points and color of a card */public String toString () {String str = ""; switch (suite) {case CIRCLE: str + = face + "tube"; break; case BAMBOO: str + = face + "bar"; break; case CHARACTER: str + = face + "Ten Thousand"; break;} return str;}/*** get the color and points * @ return */public Suite getSuits () {return suite ;} public int getFace () {return face ;}}
2. A pair of Mahjong
Package com. lovo; import java. awt. image; import javax. swing. imageIcon;/*** class: a mahjong card (108 images) ** @ author Abe attribute: number of cards x/public class Mahjongs {// private static Image [] images = new Image [36]; private Mahjong [] mah = new Mahjong [108]; private int sheet = 0; // static {// static loader // 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 (); //}/*** constructor initialization */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] */);}/*** behavior: shuffling */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 ;}} /*** action: licensed */public Mahjong deal () {return sheet <mah. length? Mah [sheet ++]: null; // This sentence is still a bit fuzzy. Do You Need To ++ it after the sheet is output ?} Public Mahjong [] getAll () {return mah ;}}
3. Print
Package com. lovo;/*** Mahjong * @ 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 ("");}}}}
Appropriate ~