Remember the concentration game that you might have 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 and two 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 thatshe would like to select through a coordinate system.
For example, in the following layout:
1 2 3 4
1 A S $ $
2 $ $ $ $
3 $ A $ $
4 $ $ $ $
Java
package ClassObjectThree;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class TurnOver {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[]arr = {"A","A","Q","Q","K","K","J","J","2","2","5","5","6","6","9","9"};
int status = 8;//全部朝上;
String[] newArr = (String[]) shufCard(arr).toArray();//得到打乱的数组
//System.out.println(newArr[0]);
Card[][] cards = new Card[4][4];
generateCards(cards,newArr);//生成初始序列
showCards(cards);
showInitCards(cards);
int x1;//坐标
int y1;
int x2;
int y2;
while(status > 0) {
System.out.println("Input coordinates:x1 y1 x2 y2");
x1 = in.nextInt();
y1 = in.nextInt();
x2 = in.nextInt();
y2 = in.nextInt();
if(cards[x1-1][y1-1].status == 0 && cards[x2-1][y2-1].status == 0) {//全部朝下
if(cards[x1-1][y1-1].content == cards[x2-1][y2-1].content) {//匹配成功
cards[x1-1][y1-1].status=1;
cards[x2-1][y2-1].status=1;
status-=1;
}
}
else {
System.out.println("已经成功翻牌了!");
}
showCards(cards);
}
System.out.println("Game win!");
}
public static List shufCard(String[] card) {//打乱数组
List list = Arrays.asList(card);
Collections.shuffle(list);
return list;
}
public static void generateCards(Card[][] cards,String[]Arr) {
int temp = 0;
for(int j = 0 ; j < 4; ++j) {
for(int k = 0 ; k < 4; ++k) {
cards[j][k] = new Card(Arr[temp],j,k);
temp+=1;
}
}
//System.out.println("temp :" + temp);
}
public static void showCards(Card[][] cards) {
for(int i = 0 ; i < 4 ; ++i) {
for(int j = 0 ; j < 4; ++j) {
if(j == 0) {
System.out.print(cards[i][j].Output(cards[i][j].status));
}else {
System.out.print(" "+ cards[i][j].Output(cards[i][j].status));
}
}
System.out.println();
}
}
public static void showInitCards(Card[][] cards) {
for(int i = 0 ; i < 4 ; ++i) {
for(int j = 0 ; j < 4; ++j) {
if(j == 0) {
System.out.print(cards[i][j].Output(1));
}else {
System.out.print(" "+ cards[i][j].Output(1));
}
}
System.out.println();
}
}
}
class Card{
String content;//牌的内容
int status=0;//牌的状态朝上(1)还是朝下(0)。
int x;//横坐标
int y;//纵坐标
public String Output(int status) {
if(status == 1)
return this.content;
else
return "$";
}
public Card(String content, int x, int y) {
this.content = content;
this.x = x;
this.y = y;
}
}