完成一个变形版的纸牌21点游戏。该游戏来源于21点游戏,实现人机对战。游戏说明如下:
(1)该游戏需要两副牌,没有Joker,共104张。每张“纸牌”应具有花色与数字两个属性。
(2)游戏在机器与人类玩家之间进行。游戏一开始应先洗牌(将104张牌打乱)。(4)选择适当的java集合类来实现“发牌牌堆”和“手牌”(不允许都使用数组)。
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
enum colo {
HEARTS, DIAMOND, SPADE, CLUB //红桃,方块,黑桃,梅花
}
class Card {
public colo c;
public int num;
public Card() {
}
public Card(int i, int num) {
if (i == 0) {
c = colo.HEARTS;
} else if (i == 1) {
c = colo.DIAMOND;
} else if (i == 2) {
c = colo.SPADE;
} else {
c = colo.CLUB;
}
this.num = num;
}
public void set(colo co, int nu) {
c = co;
num = nu;
}
public void setcolo(colo c) {
this.c = c;
}
public void setnum(int num) {
this.num = num;
}
@Override
public boolean equals(Object o) {
if (o instanceof Card) {
Card s = (Card) o;
return c == s.c && num == s.num;
}
return false;
}
public String toString() {
String s = null;
String nu = " ";
if (c == colo.HEARTS) {
s = "红桃";
} else if (c == colo.DIAMOND) {
s = "方块";
} else if (c == colo.CLUB) {
s = "梅花";
} else {
s = "黑桃";
}
if (num == 1) {
nu = "A";
} else if (num == 11) {
nu = "J";
} else if (num == 12) {
nu = "Q";
} else if (num == 13) {
nu = "K";
} else {
nu = num + " ";
}
return "card [ " + s + nu + " ] ";
}
}
public class CardGame {
//List<Card> heapcard=new ArrayList<Card>();
private Card[] heapcard;
int sumcomputer;
int sumgamer;
int flag = 0;
int finish;
Card sameC = new Card();
Card sameG = new Card();
private List<Card> gamer = new ArrayList<Card>();
private List<Card> computer = new ArrayList<Card>();
public CardGame() {
heapcard = new Card[104];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 13; j++) {
heapcard[i * 13 + j] = new Card(i % 4, j + 1);
}
}
}
public void shuffle() {
System.out.println("*****************正在洗牌 **************");
sumcomputer = 0;
sumgamer = 0;
gamer.clear();
computer.clear();
sameC = null;
sameG = null;
int k = (int) (Math.random() * 100 + 30);
Card temp = new Card();
while (k > 0) {
int i = (int) (Math.random() * 104);
int j = (int) (Math.random() * 104);
if (i != j) { //temp.c=(heapcard[i].c);temp.num=heapcard[i].num;
temp = heapcard[i];
heapcard[i] = heapcard[j];
heapcard[j] = temp;
k--;
}
gamer.clear();
computer.clear();
}
}
public int sum(int s, Card a) {
if (a.num > 9) {
return s + 10;
} else if (a.num == 1) {
if (s + 11 > 21) {
return s + 1;
} else {
return s + 11;
}
} else {
return s + a.num;
}
}
public void deal_computer() {
int i;
while (sumcomputer < 17) {
i = (int) (Math.random() * 104);
Card temp = new Card();
if (heapcard[i] != null) {
sumcomputer = sum(sumcomputer, heapcard[i]);
temp = heapcard[i];
heapcard[i] = null;
if (sumcomputer > 21) {
// System.out.println("庄家爆牌!");
computer.add(temp);
break;
}
if (computer.contains(temp)) {
sameC = temp;
//System.out.println("庄家有相同的牌"+sameC);
computer.add(heapcard[i]);
break;
}
computer.add(temp);
}
}
System.out.println("庄家(电脑)正在摸牌***********************");
System.out.println("电脑要了" + computer.size() + "张");
}
public void show(List l) {
// System.out.println("拍数为:" + l.size());
for (int i = 0; i < l.size(); i++) {
// System.out.println(((Card)(l.get(i))).c+" "+((Card)(l.get(i))).num);
System.out.println(((Card) (l.get(i))).toString());
}
}
public void deal_gamer() {
int i;
Scanner in = new Scanner(System.in);
String s = "y";
System.out.println("玩家开始摸牌***************");
Card temp = new Card();
while (sumgamer < 21 && s.equalsIgnoreCase("Y")) {
//输入n/y
/* if (!(gamer.isEmpty())) {
System.out.println("玩家现在的牌是:");
show(gamer);
s = null;
// System.out.println("输入Y/N继续摸牌或者放弃!");
}*/
// System.out.println("输入Y/N继续摸牌或者放弃!");
//s = in.nextLine();
i = (int) (Math.random() * 104);
if (heapcard[i] != null) {
sumgamer = sum(sumgamer, heapcard[i]);
temp = heapcard[i];
heapcard[i] = null;
if (sumgamer > 21) {
// System.out.println("玩家爆牌!");
gamer.add(temp);
break;
}
if (gamer.contains(temp)) {
gamer.add(temp);
sameG = temp;
System.out.println("玩家有相同的牌" + sameG);
break;
}
gamer.add(temp);
}
if (!(gamer.isEmpty())) {
System.out.println("玩家现在的牌是:");
show(gamer);
s = null;
// System.out.println("输入Y/N继续摸牌或者放弃!");
}
System.out.println("输入Y/N继续摸牌或者放弃!");
s = in.nextLine();
}
}
public void win() {
System.out.println();
System.out.println();
System.out.println("庄家的总点数是" + sumcomputer + "共有" + computer.size() + "张牌 "
+ " 牌是:");
// System.out.println();
show(computer);
System.out.println();
System.out.println("玩家的总点数是" + sumgamer + "玩家摸了" + gamer.size() + "张牌"
+ " 牌是 :");
show(gamer);
if (sumcomputer > 21) {
System.out.println("庄家爆牌!");
}
if(sumgamer>21) {System.out.println("玩家爆牌!"); }
if (sameC != null) {
System.out.println("庄家有相同的牌" + sameC);
}
//else if(sameG!=null) { System.out.println("玩家有相同的牌"+sameG); finish=1;
if (sameG == null && ((sumcomputer > sumgamer && sumcomputer <= 21) || (sumgamer > 21)))
{
System.out.println("庄家取胜!");
} else
if (sameC == null && (sumcomputer < sumgamer && sumgamer <= 21 || (sumcomputer > 21))) {
System.out.println("玩家取胜!");
} else {
System.out.println("啊。平手!!");
}
}
public static void main(String args[]) // throws IOException
{
CardGame c = new CardGame();
c.shuffle();
c.deal_computer();;
c.deal_gamer();
//Runtime.getRuntime().exec("cmd /c start cls ");
c.win();
}
}