✅作者简介:热爱国学的Java后端开发者,修心和技术同步精进。
个人主页:乐趣国学的博客
个人信条:不迁怒,不贰过。小知识,大智慧。
当前专栏:Java案例分享专栏
✨特色专栏:国学周更-心性养成之路
本文内容:Java——“21点”扑克游戏系统(变量+循环)
更多内容点击
Java——对象和类案例代码详解
目录
⛳️ 一、项目需求
⛳️ 二、代码实现
⛳️ 三、效果展示
编写“21点”的扑克游戏(每个需求用一个测试类来完成测试)
需求一: 计算机随机地向用户发5张牌,如果牌的总点数小於或等於21点,则用户赢;超过21点则计算机赢。
注意类的没有参数的构造器随机产生一张牌,并且不会重复发牌。类模仿真实牌局的行为,可以用getValue方法从一副牌中重复抽取牌。
你的程序应该:
抽取一张牌;
向用户显示这张牌;
显示到目前为止已发牌的总点数
需求二: 为使程序更象真实的牌局,用户应该能与计算机互动玩牌。修改刚才的程序,计算机仍然抽取5张牌,不超过21点的是赢家,如果双方都不超过21点,则双方都不算赢,你的程序应该能准确地报告结果。
程序应该:
为用户和计算机抽牌;
向用户显示牌;
分别显示用户和计算机的当前总点数;
为使游戏更加有趣,每抽一张新牌时都要显示牌和总点数
需求三: 修改程序使用户可以选择是否抽牌,而规定计算机必须抽5张牌或直到牌的总点数大於等於21
需求四: 修改程序使得用户可以一直玩下去,并分别累计用户和计算机赢的牌局数。
1、提供默认引用类Card类,代码如下:
package cn.bdqn.demo04 ;
/**
* Represents a playing card
* @author Roger Garside/John Mariani
* @version Last Rewritten: 16th Sept 1997
*/
import java.util.* ;
public class Card
{
// Card Class Variables
private static boolean initialised = false ;
private static Random rand = new Random() ;
private static boolean[][] dealt = new boolean[4][13] ;
private static int noDealt ;
// Card Instance Variables
/*
* the suit of the card (0 to 3)
*/
private int suit ;
/*
* the value of the card (0 to 12)
*/
private int value ;
// Card Class Constants
/**
* Constant - Spades
*/
public static final int SPADES = 0 ;
/**
* Constant - Hearts
*/
public static final int HEARTS = 1 ;
/**
* Constant - Clubs
*/
public static final int CLUBS = 2 ;
/**
* Constant - Diamonds
*/
public static final int DIAMONDS = 3 ;
// Card Constructor Methods
/**
* Creates an instance of the Card class with random values
* (if all have been dealt, then starts again)
*/
public Card()
{
if (noDealt == 52)
{
System.err.println("all 52 cards dealt") ;
initialised = false ;
}
if (!initialised)
{
for (int i = 0 ; i < 4 ; i++)
for (int j = 0 ; j < 13 ; j++)
dealt[i][j] = false ;
initialised = true ;
noDealt = 0 ;
}
int s, v ;
do
{
s = (int) (Math.abs(rand.nextInt()) % 4) ;
v = (int) (Math.abs(rand.nextInt()) % 13) ;
}
while (dealt[s][v]) ;
dealt[s][v] = true ;
suit = s ;
value = v ;
noDealt++ ;
} // end of constructor method
/**
* Creates an instance of the Card class with specified values
* (if all have been dealt, then starts again)
* @param s suit of the card
* @param v value of the card
*/
public Card(int s, int v)
{
if (noDealt == 52)
{
System.err.println("all 52 cards dealt") ;
initialised = false ;
}
if (!initialised)
{
for (int i = 0 ; i < 4 ; i++)
for (int j = 0 ; j < 13 ; j++)
dealt[i][j] = false ;
initialised = true ;
noDealt = 0 ;
}
if ((s < 0) || (s > 3))
{
System.out.println("invalid suit") ;
System.exit(1) ;
}
if ((v < 0) || (v > 12))
{
System.out.println("invalid value") ;
System.exit(1) ;
}
if (dealt[s][v]) ;
{
System.out.println("card already used") ;
System.exit(1) ;
}
dealt[s][v] = true ;
suit = s ;
value = v ;
noDealt++ ;
} // end of constructor method
// Card Instance Methods - Selectors
/**
* returns the suit attribute of the card
* @return the suit attribute of the card
*/
public int getSuit()
{
return suit ;
} // end of method getSuit
/**
* returns the suit attribute of the card as a string
* @return the suit attribute of the card as a string
*/
public String getSuitString()
{
switch (suit)
{
case SPADES :
return "Spades" ;
case HEARTS :
return "Hearts" ;
case CLUBS :
return "Clubs" ;
case DIAMONDS :
return "Diamonds" ;
default :
return "Unknown" ;
}
} //end of method getSuitString
/**
* returns the value attribute of the card
* @return the value attribute of the card
*/
public int getValue()
{
return value ;
} // end of class getValue
/**
* returns the value attribute of the card as a string
* @return the value attribute of the card as a string
*/
public String getValueString()
{
switch (value)
{
case 0 :
return "Ace" ;
case 1 :
return "two" ;
case 2 :
return "three" ;
case 3 :
return "four" ;
case 4 :
return "five" ;
case 5 :
return "six" ;
case 6 :
return "seven" ;
case 7 :
return "eight" ;
case 8 :
return "nine" ;
case 9 :
return "ten" ;
case 10 :
return "Jack" ;
case 11 :
return "Queen" ;
case 12 :
return "King" ;
default :
return "Unknown" ;
}
} //end of method getValueString
// Other Card Methods
/**
* Resets the deck of cards to all undealt
*/
public void restart()
{
for (int i = 0 ; i < 4 ; i++)
for (int j = 0 ; j < 13 ; j++)
dealt[i][j] = false ;
noDealt = 0 ;
} // end of method restart
/**
* return a string representing the card
* @return details of the card ('value of suit')
*/
public String toString()
{
return getValueString() + " of " + getSuitString() ;
} // end of method toString
/* public void setSuit(int s)
{
// s should be in the range 0 to 3
if ((s < 0) || (s > 3)) suit = 0;
else suit = s;
} // end of method setSuit
public void setValue(int v)
{
// value should be in the range 0 to 12
if ((v < 0) || (v > 12)) value = 0;
else value = v;
} // end of method setValue */
} // end of class card
2、需求一代码实现如下:
package cn.bdqn.demo04;
public class CardTest1 {
public static void main(String[] args) {
System.out.println("欢迎来到“21点”的扑克游戏!");
int num=0;
int sum=0;
for (int i = 1; i <= 5; i++) {
Card card=new Card();
num=card.getValue()+1;
System.out.println("第"+i+"张牌为:"+num);
sum+=num;
}
System.out.println("现在的总点数为:"+sum);
if(sum>21) {System.out.println("很遗憾,计算机赢,再接再厉!");}
else System.out.println("恭喜你,玩家赢!");
}
}
3、需求二代码实现如下:
package cn.bdqn.demo04;
public class CardTest2 {
public static void main(String[] args) {
System.out.println("欢迎来到“21点”的扑克游戏!");
int num = 0;
int sum1 = 0;
int sum2 = 0;
// 为计算机抽牌
System.out.println("计算机抽牌:");
for (int i = 1; i <= 5; i++) {
Card card = new Card();
num = card.getValue() + 1;
System.out.println("第" + i + "张牌为:" + num);
sum1 += num;
}
System.out.println("计算机的总点数为:" + sum1);
// 为用户抽牌
System.out.println("用户抽牌:");
for (int i = 1; i <= 5; i++) {
Card card = new Card();
num = card.getValue() + 1;
System.out.println("第" + i + "张牌为:" + num);
sum2 += num;
}
System.out.println("用户的总点数为:" + sum2);
//判断
if(sum1<=21&&sum2>21){
System.out.println("很遗憾,计算机赢,再接再厉!");
}else if(sum1<=21&&sum2<=21){
System.out.println("很遗憾,双方都不算赢!");
}else if(sum1>21&&sum2<=21){
System.out.println("恭喜玩家赢!");
}else{
System.out.println("很遗憾,双方都超过21点!");
}
}
}
4、需求三代码实现如下:
package cn.bdqn.demo04;
import java.util.Scanner;
public class CardTest3 {
public static void main(String[] args) {
//键盘输入
Scanner sc=new Scanner(System.in);
System.out.println("欢迎来到“21点”的扑克游戏!");
int num = 0;
int sum1 = 0;
int sum2 = 0;
// 为计算机抽牌
System.out.println("计算机抽牌:");
for (int i = 1; i <= 5; i++) {
Card card = new Card();
num = card.getValue() + 1;
System.out.println("第" + i + "张牌为:" + num);
sum1 += num;
if(sum1>21){
break;
}
}
System.out.println("计算机的总点数为:" + sum1);
// 为用户抽牌
System.out.println("请您输入1(抽牌)/0(不抽牌)选择是否抽牌:");
int isNum=sc.nextInt();
if(isNum==0){
System.out.println("您选择了不抽牌,游戏结束!");
}else if(isNum==1){
System.out.println("用户抽牌:");
for (int i = 1; i <= 5; i++) {
Card card = new Card();
num = card.getValue() + 1;
System.out.println("第" + i + "张牌为:" + num);
sum2 += num;
}
System.out.println("用户的总点数为:" + sum2);
//判断
if(sum1<=21&&sum2>21){
System.out.println("很遗憾,计算机赢,再接再厉!");
}else if(sum1<=21&&sum2<=21){
System.out.println("很遗憾,双方都不算赢!");
}else if(sum1>21&&sum2<=21){
System.out.println("恭喜玩家赢!");
}else{
System.out.println("很遗憾,双方都超过21点!");
}
}else{
System.out.println("选择错误,游戏结束!");
}
}
}
5、需求四代码实现如下:
package cn.bdqn.demo04;
import java.util.Scanner;
public class CardTest4 {
public static void main(String[] args) {
// 键盘输入
Scanner sc = new Scanner(System.in);
System.out.println("欢迎来到“21点”的扑克游戏!");
// 默认初始值
boolean flag = true;
// 循环初始值
int zeroNum = -1;
// 用户赢牌总局数
int userNums = 0;
// 计算机赢牌总局数
int comNums = 0;
do {
int num = 0;
int sum1 = 0;
int sum2 = 0;
// 为计算机抽牌
System.out.println("计算机抽牌:");
for (int i = 1; i <= 5; i++) {
Card card = new Card();
num = card.getValue() + 1;
System.out.println("第" + i + "张牌为:" + num);
sum1 += num;
if (sum1 > 21) {
break;
}
}
System.out.println("计算机的总点数为:" + sum1);
// 为用户抽牌
boolean isChou = false;
System.out.println("请您输入1(抽牌)/0(不抽牌)选择是否抽牌:");
int isNum = sc.nextInt();
if (isNum == 0) {
System.out.println("您选择了不抽牌,游戏结束!");
break;
} else if (isNum == 1) {
System.out.println("用户抽牌:");
for (int i = 1; i <= 5; i++) {
Card card = new Card();
num = card.getValue() + 1;
System.out.println("第" + i + "张牌为:" + num);
sum2 += num;
}
System.out.println("用户的总点数为:" + sum2);
// 判断
if (sum1 <= 21 && sum2 > 21) {
userNums++;
System.out.println("计算机赢,总牌局数为:" + userNums);
} else if (sum1 <= 21 && sum2 <= 21) {
System.out.println("很遗憾,双方都不算赢!");
} else if (sum1 > 21 && sum2 <= 21) {
comNums++;
System.out.println("用户赢,总牌局数为:" + comNums);
} else {
System.out.println("很遗憾,双方都超过21点!");
}
} else {
System.out.println("选择错误,游戏结束!");
break;
}
if (flag) {
System.out.println("输入0返回游戏:");
zeroNum = sc.nextInt();
} else {
break;
}
} while (zeroNum == 0);
}
}
1、需求一效果展示
2、需求二效果展示
3、需求三效果展示
4、需求四效果展示
码文不易,本篇文章就介绍到这里,如果想要学习更多Java系列知识,请关注博主,博主带你零基础学习Java知识。与此同时,对于日常生活有困扰的朋友,欢迎阅读我的第四栏目:《国学周更—心性养成之路》,学习技术的同时,我们也注重了心性的养成。