Java实现一个基于CLI的抽扑克牌游戏
2018-04-03 23:48
711 查看
题目来源:大工慕课 链接
作者:Caleb Sung
游戏设计要求:
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
作者: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!"); } } }
相关文章推荐
- java 实现一个基于控制台的 扫雷 游戏
- JAVA基于UDP的多点广播数据报技术的一个实现例子
- java基于swing实现的五子棋游戏代码
- 一个基于AbstractQueuedSynchronizer类的Java的Mutex类的实现
- 解析扑克牌游戏发牌算法——java实现
- 扑克牌24点游戏JAVA实现
- JAVA基于UDP的多点广播数据报技术的一个实现例子
- 一个由java实现的打地鼠游戏
- 基于Java代码实现游戏服务器生成全局唯一ID的方法汇总
- [Java] 实现一个基于命令行的用户管理
- 用java实现一个单线程基于控制台和以文件为数据存储并加上一点MVC思想的ATM
- 一个基于Winform+Java WebService的实现
- [置顶] 基于Java的“多功能五子棋”游戏的设计和实现
- 基于Java web服务器简单实现一个Servlet容器
- Java.NET --一个基于Java的Microsoft.NET框架的实现
- 用java设计实现一个简单的动作冒险类游戏
- Java实现一个简单的RPC框架(五) 基于Socket的传输层实现
- Java学习3:基于Intellij IDEA实现一个简单Servlet
- 猜数字游戏。编写一个Java应用程序,实现功能:
- java java local cache本地缓存的两种实现,一个基于list轮询一个基于timer定时