Java翻牌游戏

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;
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
记忆翻牌游戏是一种常见的益智游戏,玩家需要在有限时间内翻开所有的卡片并找到相同的卡片配对。以下是一个简单的记忆翻牌游戏Java实现: ``` import java.util.*; public class MemoryGame { public static void main(String[] args) { Scanner input = new Scanner(System.in); int size, numPairs, guess1, guess2, count = 0; boolean gameOver = false; System.out.print("Enter the size of the board (even number): "); size = input.nextInt(); numPairs = size*size/2; int[] board = new int[size*size]; for (int i = 0; i < numPairs; i++) { board[2*i] = i+1; board[2*i+1] = i+1; } shuffle(board); while (!gameOver) { drawBoard(board); System.out.print("Enter the first card to guess: "); guess1 = input.nextInt()-1; System.out.print("Enter the second card to guess: "); guess2 = input.nextInt()-1; if (board[guess1] == board[guess2]) { board[guess1] = 0; board[guess2] = 0; count += 2; } else { System.out.println("Not a match!"); } if (count == size*size) { gameOver = true; System.out.println("Congratulations, you won!"); } } } public static void shuffle(int[] arr) { Random rand = new Random(); for (int i = 0; i < arr.length; i++) { int j = rand.nextInt(arr.length); int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } public static void drawBoard(int[] arr) { for (int i = 0; i < arr.length; i++) { if (arr[i] == 0) { System.out.print("[ ]"); } else { System.out.print("["+arr[i]+"]"); } if ((i+1) % Math.sqrt(arr.length) == 0) { System.out.println(); } } } } ``` 该实现使用了一个一维数组来表示卡片,其中每个元素都是卡片上的数字,相同的数字表示一对卡片。`shuffle`方法用于随机打乱数组顺序,`drawBoard`方法用于绘制当前卡片状态的矩阵。在主函数中,程序通过不断读入用户猜测的卡片位置并与之前猜测的位置进行比较,如果两个位置的卡片数字相同则表示猜对了一对,并将这两张卡片从矩阵中删除。当所有卡片都被删除后,游戏结束。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值