利用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