您的位置:首页 > 编程语言 > Java开发

Java实现一个基于CLI的抽扑克牌游戏

2018-04-03 23:48 711 查看
题目来源:大工慕课 链接

作者: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[52];
int k = 0;
for(int i = 0; i < 13; ++i)
for(int j = 1; j <= 4; ++j)
cards[k++] = new Card(j, i);
}
//methods
void shuffle() {
for(int i = 0; i < 52; ++i) {
int replacement = (int)(Math.random() * 52);
swap(replacement, i);
}
}

public Card drawRand() {
int num = (int)(Math.random() * 52);
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 0:
str1="Ace";
break;
case 1:
str1="2";
break;
case 2:
str1="3";
break;
case 3:
str1="4";
break;
case 4:
str1="5";
break;
case 5:
str1="6";
break;
case 6:
str1="7";
break;
case 7:
str1="8";
break;
case 8:
str1="9";
break;
case 9:
str1="10";
break;
case 10:
str1="Jack";
break;
case 11:
str1="Queen";
break;
case 12:
str1="King";
break;
}
return str1;
}

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

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

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


主函数

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() - 1;
if (i> 52) {
System.out.println("Illegal value!");
System.exit(0);
}

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 == 1)
System.out.println("Congratulations! You win!");
else if (cmp == -1)
System.out.println("Sorry, you lose!");
else if (cmp == 0) {
cmp = cd.cmp_suit(Random_suit, Manually_suit);
if (cmp == 1)
System.out.println("Congratulations! You win!");
else if (cmp == -1)
System.out.println("Sorry, you lose!");
else
System.out.println("Unimaginable! Draw!");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: