【翻牌游戏】源码

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 zxiao@stu.ouc.edu.cn
 * 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 zxiao@stu.ouc.edu.cn
 * 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 zxiao@stu.ouc.edu.cn
 * 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();
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值