一、游戏背景
参赛人数:2人
准备用具:一副扑克
规则:
扑克牌A,2,3,4,5,6,7,8,9,10,J,Q,K分别代表数字1,2,3,4,5,6,7,8,9,10,11,12,13.
每人轮流取扑克牌,目的是凑成“和”为21点,谁先凑成21谁就胜利。如果没有人凑到21点,就规定最接近21点且小于21点的一方为胜者,每人均可要数次牌,需要注意的是,如果点数和超出21点,就是爆掉了(输了)
二、功能实现
本次游戏的实现一共分为三各类。 分别为:Play(玩家类) 、Poker:(扑克类) 以及RunGame(主游戏界面类)。
Play(玩家类) :
import java.util.Arrays; public class Play { private String name; private Poker[] pokers; public Play() {} public Play(String name) { super(); this.name=name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Poker[] getPokers() { return pokers; } public void setPoker(Poker[] pokers) { this.pokers=pokers; } @Override public String toString() { String info="[暗牌,"; for (int i = 0; i < pokers.length; i++) { info+=pokers[i].toString(); if(i!=pokers.length-1) { info+=","; } } return this.name+"\n"+ info+"]"; } public String getInfo() { return this.name+":\n"+ Arrays.toString(this.pokers); } /** * 计算点数 */ public int getPointers() { int pointers=0; int countA=0; for(Poker poker:pokers) { //判断是否为A if(poker.getNumber()==1) { pointers+=11; countA++; }else if(poker.getNumber()>10) { pointers+=10; }else { pointers+=poker.getNumber(); } } //判断总点数是否大于21,如果是则减去含有A个数的10 if(pointers>21) { pointers-=countA*10; } return pointers; } /** * 收牌 */ void getPoker(Poker poker) { if(this.pokers==null) { this.pokers=new Poker[1]; }else { //新建数组 Poker[] newArray=new Poker[pokers.length+1]; //将原有牌赋予新数组 for (int i = 0; i < pokers.length; i++) { newArray[i]=pokers[i]; } //让扑克数组,指向新数组 pokers=newArray; } //将新牌赋予最后一个数组位置 pokers[pokers.length-1]=poker; } }
Poker:(扑克类)
public class Poker implements Comparable { //花色四种:0-3 private int type; //点数:1-13 private int number; public Poker() {} public Poker(int type, int number) { this.type = 0; this.number = 1; if (type < 0 || type > 3 || this.number < 1 || this.number > 13) { return; } this.type = type; this.number = number; } public int getType() { return type; } public void setType(int type) { this.type = type; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } @Override public String toString() { String[] types = {"♠", "♥", "♣", "♦"}; String[] pointers = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; return types[this.type] + pointers[this.number-1]; } @Override public int compareTo(Object o) { if(o==null) { return -1; } if(!(o instanceof Poker)) { return -1; } if(Math.random()>=0.5) { return 1; } return -1; } }
RunGame(主游戏界面类)
import java.util.Arrays; import javax.swing.JOptionPane; public class RunGame { //玩家 private static Play mine; //电脑 private static Play computer; //扑克 private static Poker[] pokers; // 当前可以发牌的位置 private static int pokersIndex = 0; // 计算机要牌的上限 private static int LIMIT_POINTER = 10; public static void main(String[] args) { // 初始化游戏 initGame(); // 显示界面 viewGame(); } /** * 显示界面 */ private static void viewGame() { boolean flage=true; while(flage) { String info="---------------21点游戏-----------------\n"+ computer+"\n"+mine.getInfo()+"\n"+"1\t要牌\t2.停牌"; String input=JOptionPane.showInputDialog(info); if(input==null||input.trim().isEmpty()) { JOptionPane.showMessageDialog(null, "请正确输入"); continue; } switch(input.trim().charAt(0)){ case '1': //自己要牌 mine.getPoker(pokers[pokersIndex++]); //电脑要牌 if(computer.getPointers()<LIMIT_POINTER) { computer.getPoker(pokers[pokersIndex++]); } break; case '2': //电脑判断是否需要牌 while(computer.getPointers()<LIMIT_POINTER) { computer.getPoker(pokers[pokersIndex++]); } //判断输赢 boolean isWin=true; if(computer.getPointers()==21) { //判断谁是 black jack 谁赢,如果都是 black jack 则电脑赢。 isWin=false; }else if(mine.getPointers()>21) { isWin=false; }else if(computer.getPointers() < 21) { isWin=mine.getPointers()>computer.getPointers(); } //比较点数,提示结果 info="---------你"+(isWin?"赢了":"输了")+"--------------------\n"+ computer.getInfo()+" :"+computer.getPointers()+"\n"+ mine.getInfo()+" :"+mine.getPointers()+"点"; JOptionPane.showMessageDialog(null, info); //结束游戏 flage=false; break; default: JOptionPane.showMessageDialog(null, "请正确输入"); continue; } } } /** * 初始化游戏 */ private static void initGame() { // 构建一副扑克并且洗牌 initPokers(); // 输入玩家姓名 String playerName = JOptionPane.showInputDialog("请输入你的名字"); if (playerName == null || playerName.trim().isEmpty()) { playerName = "匿名"; } mine = new Play(playerName); // 建立电脑玩家 computer = new Play("高进"); // 初始化,每人发两张牌 computer.getPoker(pokers[pokersIndex++]); mine.getPoker(pokers[pokersIndex++]); computer.getPoker(pokers[pokersIndex++]); mine.getPoker(pokers[pokersIndex++]); } /** * 构建一副扑克并且洗牌 */ private static void initPokers() { pokers = new Poker[52]; // 循环花色 for (int i = 0; i < 4; i++) { // 循环点数 for (int j = 1; j < 14; j++) { int index = i * 13 + j - 1; pokers[index] = new Poker(i, j); } } // 洗牌 /*for (int i = 0; i < 36; i++) { int posi1 = (int) (Math.random() * 52); int posi2 = (int) (Math.random() * 52); swap(pokers, posi1, posi2); }*/ //利用Comparable接口实现洗牌 Arrays.sort(pokers); } private static void swap(Poker[] pokers, int posi1, int posi2) { Poker temp = pokers[posi1]; pokers[posi1] = pokers[posi2]; pokers[posi2] = temp; } }
三、效果展示
1.首先需要输入玩家的名字,如果不输入就会以“匿名”的身份进行游戏。
2.玩家选择是否继续要牌,如果要则输入1,如果停牌则输入2。这里如果玩家选择停牌的话,电脑会进行判断是否继续要牌
四、总结
本次实现的21点小游戏,主要使用Swing组件以及面向对象的思想,游戏本身涉及的技术点不是很多,但是通过这一点一滴小的练习,可以提高自己对以后更深层次学习的兴趣,也是对动手能力的锻炼,继续加油,然后秃见成效!!