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

阅读: 评论:0

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

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 {// fieldsprivate Card[] cards;//constructorspublic 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);}//methodsvoid 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;elsereturn 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;elsereturn 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!");it(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!");elseSystem.out.println("Unimaginable! Draw!");}}
}

本文发布于:2024-02-04 08:26:26,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170703075953949.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:扑克牌   游戏   Java   CLI
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23