21点游戏java实现
21点的基本知识
21点是世界上比较流行的扑克游戏项目
除掉大小王的一副扑克牌,共计52张牌
21点不区分花色,其中A----10均代表扑克牌本身的点数J Q K代表10点
区分庄家和闲家,其中闲家可以优先要牌 而庄家在与闲家点数一样大的情况下,庄家赢,当有一方点数超过21点则另一方自动获胜,庄家的点数如果正好是21点,那么庄家直接获胜,其他情况以点数大的获胜
流程设计:
人机对战模式!
1由用户输入选择作庄还是作闲,电脑自动成为另一方
2随机为双方发两张牌
3询问闲家是否要牌,一直到闲家不再要或者发生暴点为止
4询问庄家要牌,一直到庄家不再要或者暴点或者正好21点为止
5比较游戏输赢
6询问是否继续
7统计战果
技术问题:
1. 发牌的时候随机发取
2. 发牌出来还要判断是否本张牌已经发过了!如果已经发过了,那么就要重新随机发另外一张牌,要保证牌的唯一性
3. 当给电脑派牌的时候 要考虑AI的问题!让电脑的逻辑稍微模拟人的思维
主类:
import java.util.Scanner; public class CardTest { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int win = 0; int lose = 0; while (true) { System.out.println("请问做庄家1/闲家2?"); int num = sc.nextInt(); if (num == 1) { System.out.println("你选择做庄家"); Ruler r = new Ruler(); int n=r.play(); if(n==0) { //庄家赢 win++; } else lose++; } else if (num == 2) { System.out.println("你选择做闲家"); Player p = new Player(); int n= p.play(); if(n==1) { //闲家赢 win++; } else lose++; } else { System.out.println("输入错误,重新输入"); } System.out.println("是否继续Y/N"); if (sc.next().charAt(0) == 'Y') continue; else { System.out.println("游戏结束"); System.out.println("赢:"+win); System.out.println("输:"+lose); break; } } } }
import java.util.Random; public class Card { int index = 0; // 发牌索引下标 String[] card = { "红桃A", "红桃2", "红桃3", "红桃4", "红桃5", "红桃6", "红桃7", "红桃8", "红桃9", "红桃10", "红桃J", "红桃Q", "红桃K", "黑桃A", "黑桃2", "黑桃3", "黑桃4", "黑桃5", "黑桃6", "黑桃7", "黑桃8", "黑桃9", "黑桃10", "黑桃J", "黑桃Q", "黑桃K", "梅花A", "梅花2", "梅花3", "梅花4", "梅花5", "梅花6", "梅花7", "梅花8", "梅花9", "梅花10", "梅花J", "梅花Q", "梅花K", "方片A", "方片2", "方片3", "方片4", "方片5", "方片6", "方片7", "方片8", "方片9", "方片10", "方片J", "方片Q", "方片K", }; public void show() { for (int i = 0; i < 52; i++) { System.out.println(card[i]); } } // 洗牌,打乱牌的顺序 public void cutcards() { int index=0; Random rand = new Random(); for (int i = 0; i < 52; i++) { int n = rand.nextInt(52); String temp; temp = card[n]; card[n] = card[51 - n]; card[51 - n] = temp; } } // 发牌,按顺序发牌,从下标0开始 public String deal() { String c = card[index]; index++; return c; } }
public class JudgeNum { public int judgeString(String num) { String word= num.substring(2); if(word.equals("A")) { return 1; } else if(num.substring(2).equals("J")|| num.substring(2).equals("Q")|| num.substring(2).equals("K")|| num.substring(2).equals("10")) { return 10; } else { int n=(int)num.substring(2).charAt(0)-48; return n; } } }
import java.util.Random; import java.util.Scanner; public class Player { /* * 选择做闲家 先得牌 您选择: 闲家
发牌时间: 闲家:红桃K 方片3
庄家:黑桃6 梅花4 * 闲家要牌时间:
闲家是否要牌(Y/N)?
Y
发牌:黑桃A * 闲家是否要牌(Y/N)?
Y 发牌:梅花5 闲家是否要牌(Y/N)?
N 闲家不再要牌:
闲家点数:19点
庄家要牌时间:
庄家要牌:梅花5 * 庄家要牌:黑桃4
庄家不再要牌:
庄家点数19点 庄赢 * * * */ Scanner sc= new Scanner(System.in); public int play() { int playernum=0; int rulernum=0; String player1; String player2; String ruler1; String ruler2; JudgeNum judge = new JudgeNum(); Card c = new Card(); c.cutcards(); player1 = c.deal(); player2 = c.deal(); System.out.println("发牌时间:"); System.out.println("闲家:" + player1 + " " + player2); playernum=judge.judgeString(player1)+judge.judgeString(player2); ruler1 = c.deal(); ruler2 = c.deal(); System.out.println("庄家:" + ruler1 + " " + ruler2); rulernum=judge.judgeString(ruler1)+judge.judgeString(ruler2); System.out.println("闲家点数:"+playernum); System.out.println("庄家点数:"+rulernum); System.out.println("闲家要牌时间: " ); //循环要牌阶段 while(true) { System.out.println("闲家是否要牌(Y/N)? " ); char w = sc.next().charAt(0); if(w=='Y') { System.out.println("闲家继续要牌"); String player3=c.deal(); System.out.println("闲家: "+player3 ); playernum=playernum+judge.judgeString(player3); System.out.println("闲家点数:"+playernum); if(playernum>21) { System.out.println("闲家爆掉,庄家赢"); return 0; } } else if(w=='N') { System.out.println("闲家不再要牌,闲家点数:"+playernum); break; } } System.out.println("庄家要牌时间"); while(true) { if(rulernum>=playernum) { System.out.println("庄家不再要牌,庄家赢"); return 0; } else { System.out.println("庄家要牌时间:"); String ruler3=c.deal(); System.out.println("庄家:"+ruler3); rulernum=rulernum+judge.judgeString(ruler3); if(rulernum>21) { System.out.println("庄家点数"+rulernum); System.out.println("庄家爆掉,闲家赢"); return 1; } else if(rulernum>=playernum) { System.out.println("庄家点数:"+rulernum+" 闲家点数:"+playernum); System.out.println("庄家赢"); return 0; } } } } }
import java.util.Scanner; public class Ruler { // 选择做庄家 /* * 闲家先要牌 * */ Scanner sc = new Scanner(System.in); public int play() { int playernum = 0; int rulernum = 0; String player1; String player2; String ruler1; String ruler2; JudgeNum judge = new JudgeNum(); Card c = new Card(); c.cutcards(); player1 = c.deal(); player2 = c.deal(); System.out.println("发牌时间:"); System.out.println("闲家:" + player1 + " " + player2); playernum = judge.judgeString(player1) + judge.judgeString(player2); ruler1 = c.deal(); ruler2 = c.deal(); System.out.println("庄家:" + ruler1 + " " + ruler2); rulernum = judge.judgeString(ruler1) + judge.judgeString(ruler2); System.out.println("闲家点数:" + playernum); System.out.println("庄家点数:" + rulernum); System.out.println("闲家要牌时间:"); while (true) { String player3; if (playernum <rulernum) { System.out.println("闲家要牌"); player3 = c.deal(); System.out.println("闲家:" + player3); playernum = playernum + judge.judgeString(player3); } else if (playernum > 21) { System.out.println("闲家点数:" + playernum); System.out.println("闲家爆掉,庄家赢"); return 0; } else if (playernum >= 15) { System.out.println("闲家不再要牌"); System.out.println("闲家点数" + playernum); break; }else { System.out.println("闲家要牌"); String player4 = c.deal(); System.out.println("闲家:" + player4); playernum=playernum+judge.judgeString(player4); if (playernum > 21) { System.out.println("闲家点数:" + playernum); System.out.println("闲家爆掉,庄家赢"); return 0; } } } // 庄家要牌时间 System.out.println("庄家要牌时间:"); while (true) { System.out.println("庄家是否要牌(Y/N)"); char w = sc.next().charAt(0); if (w == 'Y') { System.out.println("庄家继续要牌"); String ruler3 = c.deal(); System.out.println("庄家: " + ruler3); rulernum = rulernum + judge.judgeString(ruler3); System.out.println("庄家点数:" + rulernum); if (rulernum > 21) { System.out.println("庄家爆掉,闲家赢"); return 1; } } else if (w == 'N') { System.out.println("庄家不再要牌,庄家点数:" + rulernum); break; } } if (rulernum >= playernum) { System.out.println("庄家赢"); return 0; } else { System.out.println("闲家赢"); return 1; } } }