天天看点

Java:实现一个基于CLI的抽扑克牌游戏(第七周)

题目来源:大工慕课 链接

作者:Caleb Sung

题目要求

完成CardDeck类,以便您可以通过比较您和计算机抽取的卡片与计算机进行PK。

游戏设计要求:

  • In this game, the value of a card is set from small to big: Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen and King.
  • And the suit of a card is set from small to big: Clubs, Diamonds, Hearts and Spades

解答示范

Card类

这个类定义扑克牌的基本属性:花色和数字,并分别设置一个返回函数

class Card {
    private int suit;
    private int value;

    public Card (int s, int v) {
        suit = s;
        value = v;
    }

    public int getSuit() {
        return suit;
    }

    public int getValue() {
        return value;
    }
}
           

CardDeck类

这个类负责初始化54张扑克牌,洗牌和随机抽牌,最后再与电脑抽取的扑克牌进行比较,将抽出的牌以单词的形式返回出来,并告知孰输孰赢

class CardDeck {
    // fields
    private Card[] cards;

    //constructors
    public CardDeck(){
        cards = new Card[];
        int k = ;
        for(int i = ; i < ; ++i)
            for(int j = ; j <= ; ++j)
                cards[k++] = new Card(j, i);
    }
    //methods
    void shuffle() {
        for(int i = ; i < ; ++i) {
            int replacement = (int)(Math.random() * );
            swap(replacement, i);
        }
    }

    public Card drawRand() {
        int num = (int)(Math.random() * );
        return cards[num];
    }

    public Card drawManually(int i) {
        return cards[i];
    }

    private void swap(int i, int j) {
        Card tmp = cards[i];
        cards[i] = cards[j];
        cards[j] = tmp;
    }

    public String value_to_str(int value) {
        String str1 = null;
        switch(value){  
        case :  
            str1="Ace";  
            break;  
        case :  
            str1="2";  
            break;  
        case :  
            str1="3";  
            break;  
        case :  
            str1="4";  
            break;  
        case :  
            str1="5";  
            break;  
        case :  
            str1="6";  
            break;  
        case :  
            str1="7";  
            break;  
        case :  
            str1="8";  
            break;  
        case :  
            str1="9";  
            break;  
        case :  
            str1="10";  
            break;  
        case :  
            str1="Jack";  
            break;  
        case :  
            str1="Queen";  
            break;  
        case :  
            str1="King";  
            break;  
        }  
        return str1;
    }

    public String suit_to_str(int suit) {
        String str2 = null;
        switch(suit){  
        case :  
            str2="Clubs";  
            break;  
        case :  
            str2="Diamonds";  
            break;  
        case :  
            str2="Hearts";  
            break;  
        case :  
            str2="Spades";  
            break;  
        }  
        return str2;
    }

    int cmp_value(int Random_value, int Manually_value) {
        if (Random_value < Manually_value)
            return ;
        else if (Random_value > Manually_value)
            return -;
        else
            return ;
    }

    int cmp_suit(int Random_suit, int Manually_suit) {
        if (Random_suit < Manually_suit)
            return ;
        else if (Random_suit > Manually_suit)
            return -;
        else
            return ;
    }
}
           

主函数

import java.util.Scanner;

public class CardDeckTest {

    private static Scanner in;

    public static void main(String[] args) {
        CardDeck cd = new CardDeck();

        cd.shuffle();

        System.out.println("This is a poker game that judge who is the winner.");
        System.out.println("Powered by Caleb Sung, on 2018/4/3");
        System.out.println("-----------------------------------------");
        System.out.println("\nIn this game, the value of a card is set from small to big:");
        System.out.println("Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen and King.\n");
        System.out.println("And the suit of a card is set from small to big:");
        System.out.println("Clubs, Diamonds, Hearts and Spades.\n");
        System.out.println("-----------------------------------------");
        System.out.println("\nTime for you to choose a card!");
        System.out.println("Please input a number between 1 and 52:\n");
        in = new Scanner(System.in);
        int i = in.nextInt() - ;
        if (i> ) {
            System.out.println("Illegal value!");
            System.exit();
        }

        int Random_value = cd.drawRand().getValue();
        int Manually_value = cd.drawManually(i).getValue();
        int Random_suit = cd.drawRand().getSuit();
        int Manually_suit = cd.drawManually(i).getSuit();

        String str_of_value_random = cd.value_to_str(Random_value);
        String str_of_suit_random = cd.suit_to_str(Random_suit);
        String str_of_value_manually = cd.value_to_str(Manually_value);
        String str_of_suit_manually = cd.suit_to_str(Manually_suit);

        System.out.println("-----------------------------------------");
        System.out.println("The card you choose is "+str_of_value_manually+" of "+str_of_suit_manually);
        System.out.println("The card computer choose is "+str_of_value_random+" of "+str_of_suit_random);
        System.out.println("-----------------------------------------");

        int cmp = cd.cmp_value(Random_value, Manually_value);
        if (cmp == ) 
            System.out.println("Congratulations! You win!");
        else if (cmp == -) 
            System.out.println("Sorry, you lose!");
        else if (cmp == ) {
            cmp = cd.cmp_suit(Random_suit, Manually_suit);
            if (cmp == ) 
                System.out.println("Congratulations! You win!");
            else if (cmp == -) 
                System.out.println("Sorry, you lose!");
            else
                System.out.println("Unimaginable! Draw!");
        }
    }
}