【翻牌遊戲】源碼

Remember the concentration game that you mighthave played as a kid with some cards. The idea of the game is to find identical pairs among a shuffled pack of cards laidout. For example, let us assume that you are given 10 cards in the deck, with two Aces, two Queens, two 5’s, two Jacks andtwo labeled 9. The cards are shuffled and placed face down on the table.
A player then selects two cards that are face down, turns them face up, and if the cards match they are left face up. If the two cards do not match they are returned to their original face down position. The game continues until all cards are face up.
Write a program that plays this game of concentration. Use 16 cards that are shuffled and laid out in a 4 by 4 square. These
cards should be labeled with pairs of card numbers (A, Q, K,J, 2, 5, 6, 9).
Your program should allow the player to specify the cards that she would like to select through a coordinate system.
For example, in the following layout:
                                        

All of the cards are face down indicated by $. The pairs of Awhich are face up and at coordinates (1,1) and (2,3). To hide
the cards that have been temporarily placed face up, output a large number of newlines to force the old board off the screen
(or find something better in the JAVA API).


MyCard.java

package com.zx.bean;
/**
 * 
 * @author SenseChuang
 * @email [email protected]
 * MyCard.java
 */
public class MyCard {
	private String tag;
	private int x;
	private int y;
	private boolean status;
	
	public MyCard(String tag, int x, int y, boolean status) {
		super();
		this.tag = tag;
		this.x = x;
		this.y = y;
		this.status = status;
	}
	
	public MyCard() {
		super();
	}
	
	@Override
	public String toString() {
		return "MyCard [tag=" + tag + ", x=" + x + ", y=" + y + ", status=" + status + "]";
	}

	public String getTag() {
		return tag;
	}
	public void setTag(String tag) {
		this.tag = tag;
	}
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
	public boolean getStatus() {
		return status;
	}
	public void setStatus(boolean status) {
		this.status = status;
	}
	
}

InitialArray.java

package com.zx.bean;

import java.util.Random;

/**
 * @author SenseChuang
 * @email [email protected]
 * InitialArray.java
 */
public class InitialArray {
	public static String[][] initialArray() {
		 final  String [] arr = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
		 String [] eightNumOfGame =new String[16]; //存放隨機獲得8個放在遊戲中的數
		 String [] eightNumOfGameCache =new String[16]; 
		 int [] eightNumOfRandom =new int[8]; //存放隨機獲得8個不可重複數
		 int [] sixteenNumOfRandom =new int[16]; //存放隨機獲得8個不可重複數
		 int [] cache =new int[16]; //存放複製後隨機後的16個數字
		 String [][] loc = new String [4][4];//定義一個二維數組表示
			Random randomNum = new Random();
			for(int i = 0;i < 8;i++) {//獲取隨機不重複數
				int rn = randomNum.nextInt(14);
				int j = 0;
				//以下注釋寫法錯誤,雖然j = 0,但是該次循環已經執行完成,因此j需要加一,容易導致其他位存在一個與第一位重複
				/*for(int j = 0;j <= i;j++) {//去重
					if(rn == eightNumOfRandom[j]) {//如果rn等於數組中的數,則重新生成
						rn = randomNum.nextInt(14);
						j = 0;//從頭重新開始遍歷
					}
				}*/
				while(j <= i) {
					if(rn == eightNumOfRandom[j]) {
						rn = randomNum.nextInt(14);
						j = 0;
					}
					else {
						j++;
					}
				}
				eightNumOfRandom[i] = rn;
				eightNumOfGame[i] = arr[eightNumOfRandom[i] - 1];
				eightNumOfGame[8+i] = eightNumOfGame[i];//將剩餘8個填滿
			};
			
			//打亂
			for(int i = 0;i < 16;i++) {
				int rn = randomNum.nextInt(16);
				int j = 0;
				while(j <= i && j!=15) {//注意設置j!=15 ,否則會死循環
					if(rn == cache[j]) {
						rn = randomNum.nextInt(16);
						j = 0;
					}
					else {
						j++;
					}
				}
				cache[i] = rn;
				eightNumOfGameCache[i] = eightNumOfGame[i];//複製數組
			}
			for(int i = 0;i < 16;i++) {
				eightNumOfGame[i] = eightNumOfGameCache[cache[i]];//是的,就是那個意思,把隨機數組中元素賦值給真·數組
				//System.out.println(eightNumOfGame[i]);
			}
			
			//將打亂後的數賦值給二維數組
			for(int i = 0;i < 4;i++) {
				for(int j = 0;j < 4;j++) {
					loc[i][j] = eightNumOfGame[i * 4 + j];//將一維數組轉換爲二維數組
				}
			}
			/*for (String[] strings : loc) {
				for (String string : strings) {
					System.out.print(string+"\t");
				}
				System.out.println();
			}*/
		return loc;
	}
}

Main.java

package com.zx.bean;

import java.util.Scanner;

/**
 * 
 * @author SenseChuang
 * @email [email protected]
 * Main.java
 */
@SuppressWarnings("all")
public class Main {
	
	public static void main(String[] args) {
		long start = System.currentTimeMillis();   
		MyCard[][] mc = new MyCard[4][4];
		String[][] loc = new String[4][4];
		for(int i= 0;i < 4;i++) {
			for(int j= 0;j < 4;j++) {
				mc[i][j] = new MyCard();
			}
		}
		loc = InitialArray.initialArray();
		for(int i= 0;i < 4;i++) {
			for(int j= 0;j < 4;j++) {
				String str = loc[i][j];
				mc[i][j].setTag(str);
			}
		}
		while(!weatherAllUncover(mc)) {
			System.out.println("\t"+"1\t"+"2\t"+"3\t"+"4\t");
			for(int i = 0;i < 4;i++) {
				System.out.print(i+1+"\t");
				for(int j = 0;j < 4;j++) {
					if(mc[i][j].getTag().equals(" ")) {
						System.out.print(" "+"\t");
					}else {
						System.out.print("$\t");

					}
				}
				System.out.println();
			}
			System.out.println("plz input two coordinate,such as\"(1,3),(3,4)\":");
			Scanner sc = new Scanner(System.in);
			String nextLine = sc.nextLine();
			if(nextLine.equals("000")) {
				showAllTag(mc);
			}else {
				int x1 = nextLine.charAt(1) -'0';
				int y1 = nextLine.charAt(3) -'0';
				int x2 = nextLine.charAt(7) -'0';
				int y2 = nextLine.charAt(9) -'0';
				System.out.println("x1= " + x1 + ",y1= "+ y1 + ",x2= " + x2 + ",y2=" + y2);
				System.out.println("\t"+"1\t"+"2\t"+"3\t"+"4\t");
				for(int i = 0;i < 4;i++) {
					System.out.print(i+1+"\t");
					for(int j = 0;j < 4;j++) {
						if((i==(x1 - 1)&&j==(y1 - 1)) ||(i==(x2 - 1)&&j==(y2 - 1))) {
							System.out.print(mc[i][j].getTag()+"\t");
						}
						else {
							if(mc[i][j].getTag().equals(" ")) {
								System.out.print(" "+"\t");
							}else {
								System.out.print("$\t");
							}
						}
					}
					System.out.println();
				}
				if(mc[x1-1][y1-1].getTag().equals(mc[x2-1][y2-1].getTag())) {
					mc[x1-1][y1-1].setStatus(true);
					mc[x1-1][y1-1].setTag(" ");
					mc[x2-1][y2-1].setStatus(true);
					mc[x2-1][y2-1].setTag(" ");
				}
				try {
					System.out.println();
					Thread.sleep(3000);
					for(int i = 0;i < 100;i++) {
						System.out.println();
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		 long end = System.currentTimeMillis();   
		 long total = (end - start)%1000;
		 System.out.println("Congratulation!You did it!!!AllTime is" + total + "ms");  
	}
	//判斷是否全部掀起
	static boolean weatherAllUncover(MyCard[][] mc) {
		for(int i = 0;i < 4;i++) {
			for(int j = 0;j < 4;j++) {
				if(mc[i][j].getStatus()== false) {
					return false;
				}
			}
		}
		return true;
	}
	static void showAllTag(MyCard[][] mc) {
		System.out.println("\t"+"1\t"+"2\t"+"3\t"+"4\t");
		for(int i = 0;i < 4;i++) {
			System.out.print(i+1+"\t");
			for(int j = 0;j < 4;j++) {
				System.out.print(mc[i][j].getTag()+"\t");
			}
			System.out.println();
		}
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章