利用Card类和循环语句编写一个扑克牌游戏
编写“21点”的扑克游戏
a) 计算机随机地向用户发5张牌,如果牌的总点数小於或等於21点,则用户赢;超过21点则计算机赢。
注意类的没有参数的构造器随机产生一张牌,并且不会重复发牌。类模仿真实牌局的行为,可以用restart
方法从一副牌中重复抽取牌。
你的程序应该:
抽取一张牌;
向用户显示这张牌;
显示到目前为止已发牌的总点数
b) 为使程序更象真实的牌局,用户应该能与计算机互动玩牌。修改刚才的程序,计算机仍然抽取5张牌,不超过21点的是赢家,如果双方都不超过21点,则双方都不算赢,你的程序应该能准确地报告结果。
程序应该:
为用户和计算机抽牌;
向用户显示牌;
分别显示用户和计算机的当前总点数;
为使游戏更加有趣,每抽一张新牌时都要显示牌和总点数
c) 修改程序使用户可以选择是否抽牌,而规定计算机必须抽5张牌或直到牌的总点数大於等於21
d)修改程序使得用户可以一直玩下去,并分别累计用户和计算机赢的牌局数。
package java.lancs ;
/**
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
问一下计算点数方面是按照标准的游戏规则进行计算还是说只需要在其对应的值上+1即可?如(A为1点 K为13点 )百度百科上对于21点的点数计算规则与此不同
可以参考这篇文章中的代码
http://t.csdn.cn/qCmDd
这card类这么复杂吗
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("1、start");
if ("1".equals(sc.next())){
int pSum = 0;//玩家总点数
int cSum = 0;//电脑总点数
Card player = null;//玩家牌
Card computer = null;//电脑牌
for (int i = 0; i < 5; i++) {
if (player != null){player.restart();}
if (computer != null){computer.restart();}
player = new Card();
computer = new Card();
System.out.println("您的牌为:"+player.getSuitString()+" "+player.getValue());
System.out.println("电脑的牌为:"+computer.getSuitString()+" "+computer.getValue());
pSum += player.getValue();
cSum += computer.getValue();
if (i != 4){
System.out.println("您目前的总点数为:"+pSum);
System.out.println("电脑目前的总点数为:"+cSum);
System.out.print("输入1继续,或其他字符退出游戏 ");
if (!"1".equals(sc.next())){
System.out.println("Game Over!");
break;
}
}
}
System.out.println("您的总点数为:"+pSum);
System.out.println("电脑的总点数为:"+cSum);
System.out.println(pSum<=cSum?(pSum<=21&&cSum>21?"the player win!":"draw game!"):(cSum<=21&&pSum>21?"the computer win!":"draw game!"));
}
}