一、引入
昨天突然想玩斗地主,然后想去自己写代码实现,但是心有余而力不足。
就去搜一搜博客。
然后,就找到这样一篇博客
这是它是双人玩的(一个人和一个电脑)
就想着把它改成三人的
二、三人出牌逻辑
- 出牌分为主动出牌和被动出牌;
- 主动出牌:自己拿到牌权;经过一轮或几轮,其他两家都不要;
轮次可以指上一次自己出牌不分主被),又一次该自己出牌(不分主被)
也可以指上一次有人主动出牌,又一次有人主动出牌,期间出的牌进入废牌堆。(两人不一定是同一人)
被动出牌:自己要压出下的牌 - 首先介绍三人的出牌逻辑:
t表示出牌,f表示不要; l=上家;m=自己;r=下家; 默认出牌顺序:l-->m-->r-->l-->.... lz表示l主动出牌;mz表示m主动出牌;rz表示r主动出牌 lc表示l出牌;mc表示m主动出牌;rc表示r出牌 首先除lz是t之外,mz,rz,l,mc,rz都是f;即默认lz主动出牌;
每次某人出牌,
会把自己的主动变为t,其他两家的主动变为f;
自己变成false;自己的下家变为true;
例如:
l出牌,经过一轮,l再次拿到牌权
lz-->t; m-->f; r-->f; ========= lz-->t; ...
l出牌,经过一轮,m拿到牌权
lz-->t; m-->t; r-->f; l-->f; ======= mz-->t; ...
l出牌,经过一轮,r拿到牌权
lz-->t; m-->t; r-->t; l-->f; m-->f ======= rz-->t; ...
三、三人出牌模拟测试代码
test
import java.util.Scanner; public class test { /** * 三人出牌测试 * <p> * 输入t,出,f,不出 */ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); boolean l = false;//上家 boolean lz = true;//上家z boolean m = false;//我 boolean mz = false;//我z boolean r = false;//下家 boolean rz = false;//下家z for (int i = 0; i <10; i++) { //主要为了避免同一人同时出两次 if (lz){ // if (l){ // l=!l; // } l=false; }if (mz){ // if (m){ // m=!m; // } m=false; } if (rz){ // if (r){ // r=!r; // } r=false; } if (lz){ System.out.println("========================="); System.out.println("上家z输入"); String s = scanner.nextLine(); if (s.equals("t")) { //为了避免同一人同时出两次 l=false; System.out.println("上家z出牌了"); //下家出牌 m=!m; } } if (l) { System.out.println("上家输入"); String s = scanner.nextLine(); if (s.equals("t")) { System.out.println("上家出牌了"); //其他两家的主动变为f; if(mz){ mz=!mz; } if (rz){ rz=!rz; } //自己的主动变为t lz=true; //自己变成false;自己的下家变为true; l = !l; m = !m; } else if (s.equals("f")) { System.out.println("上家不要"); //自己变成false;自己的下家变为true; l = !l; m = !m; } } if (mz){ System.out.println("========================="); System.out.println("我z输入"); String s = scanner.nextLine(); if (s.equals("t")) { m=false; System.out.println("我z出牌了"); r = !r; } } if (m) { System.out.println("我输入"); String s = scanner.nextLine(); if (s.equals("t")) { System.out.println("我出牌了"); if (lz){ lz=!lz; } if (rz){ rz=!rz; } mz=true; m = !m; r = !r; } else if (s.equals("f")) { System.out.println("我不要"); m = !m; r = !r; } } if (rz){ System.out.println("========================="); System.out.println("下家z输入"); String s = scanner.nextLine(); if (s.equals("t")) { r=false; System.out.println("下家z出牌了"); l = !l; } } if (r) { System.out.println("下家输入"); String s = scanner.nextLine(); if (s.equals("t")) { System.out.println("下家出牌了"); if (lz){ lz=!lz; } if(mz){ mz=!mz; } rz=true; r = !r; l = !l; } else if (s.equals("f")) { System.out.println("下家不要了"); r = !r; l = !l; } } } } }
四、模拟测试结果
测试一
========================= 上家z输入 t 上家z出牌了 我输入 f 我不要 下家输入 f 下家不要了 ========================= 上家z输入
测试二
========================= 上家z输入 t 上家z出牌了 我输入 t 我出牌了 下家输入 f 下家不要了 上家输入 f 上家不要 ========================= 我z输入
测试三
========================= 上家z输入 t 上家z出牌了 我输入 t 我出牌了 下家输入 t 下家出牌了 上家输入 f 上家不要 我输入 f 我不要 ========================= 下家z输入
测试四
========================= 上家z输入 t 上家z出牌了 我输入 f 我不要 下家输入 f 下家不要了 ========================= 上家z输入 t 上家z出牌了 我输入 t 我出牌了 下家输入 f 下家不要了 上家输入 f 上家不要 ========================= 我z输入 t 我z出牌了 下家输入 t 下家出牌了 上家输入 f 上家不要 我输入 f 我不要 ========================= 下家z输入 t 下家z出牌了 上家输入 f 上家不要 我输入 f 我不要 ========================= 下家z输入 t 下家z出牌了 Process finished with exit code 0
五、 三人出牌模拟代码优化(只在代码角度优化,算法一样)
代码优化一
test1
import java.util.Scanner; public class test1 { /** * 三人出牌测试 * <p> * 输入t,出,f,不出 */ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); boolean l = false;//上家 boolean lz = true;//上家z boolean m = false;//我 boolean mz = false;//我z boolean r = false;//下家 boolean rz = false;//下家z for (int i = 0; i < 10; i++) { if (lz){ l = false; } if (mz) { m = false; } if (rz) { r = false; } if (lz) { System.out.println("========================="); System.out.println("上家z输入"); String s = scanner.nextLine(); if (s.equals("t")) { l = false; System.out.println("上家z出牌了"); m = !m; } } if (l) { System.out.println("上家输入"); String s = scanner.nextLine(); if (s.equals("t")) { System.out.println("上家出牌了"); mz = false; rz = false; lz = true; } else if (s.equals("f")) { System.out.println("上家不要"); } l = !l; m = !m; } if (mz) { System.out.println("========================="); System.out.println("我z输入"); String s = scanner.nextLine(); if (s.equals("t")) { m = false; System.out.println("我z出牌了"); r = !r; } } if (m) { System.out.println("我输入"); String s = scanner.nextLine(); if (s.equals("t")) { System.out.println("我出牌了"); lz = false; rz = false; mz = true; } else if (s.equals("f")) { System.out.println("我不要"); } m = !m; r = !r; } if (rz) { System.out.println("========================="); System.out.println("下家z输入"); String s = scanner.nextLine(); if (s.equals("t")) { r = false; System.out.println("下家z出牌了"); l = !l; } } if (r) { System.out.println("下家输入"); String s = scanner.nextLine(); if (s.equals("t")) { System.out.println("下家出牌了"); lz = false; mz=false; rz = true; } else if (s.equals("f")) { System.out.println("下家不要了"); } r = !r; l = !l; } } } }
代码优化二
test2
import java.util.Scanner; public class test2 { /** * 三人出牌测试 * <p> * 输入t,出,f,不出 * l=上家;m=自己;r=下家; * 默认出牌顺序:l-->m-->r-->l-->.... * lz表示l主动出牌;mz表示m主动出牌;rz表示r主动出牌,主动出牌只能输入t * lc表示l出牌;mc表示m主动出牌;rc表示r出牌,可输入t/f * * 首先除lz是t之外,mz,rz,l,mc,rz都是f;即默认lz主动出牌; */ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); boolean l = false;//上家 boolean lz = true;//上家z boolean m = false;//我 boolean mz = false;//我z boolean r = false;//下家 boolean rz = false;//下家z for (int i = 0; i < 10; i++) { if (lz) { l = false; } if (mz) { m = false; } if (rz) { r = false; } if (lz) { System.out.println("========================="); System.out.println("上家z输入"); String s = scanner.nextLine(); if (s.equals("t")) { System.out.println("上家z出牌了"); l = false; m = true; } } if (l) { System.out.println("上家输入"); String s = scanner.nextLine(); if (s.equals("t")) { System.out.println("上家出牌了"); mz = false; rz = false; lz = true; } else if (s.equals("f")) { System.out.println("上家不要"); } l = false; m = true; } if (mz) { System.out.println("========================="); System.out.println("我z输入"); String s = scanner.nextLine(); if (s.equals("t")) { System.out.println("我z出牌了"); m = false; r = true; } } if (m) { System.out.println("我输入"); String s = scanner.nextLine(); if (s.equals("t")) { System.out.println("我出牌了"); lz = false; rz = false; mz = true; } else if (s.equals("f")) { System.out.println("我不要"); } m = false; r = true; } if (rz) { System.out.println("========================="); System.out.println("下家z输入"); String s = scanner.nextLine(); if (s.equals("t")) { System.out.println("下家z出牌了"); r = false; l = true; } } if (r) { System.out.println("下家输入"); String s = scanner.nextLine(); if (s.equals("t")) { System.out.println("下家出牌了"); lz = false; mz = false; rz = true; } else if (s.equals("f")) { System.out.println("下家不要了"); } r = false; l = true; } } } }
代码优化三
import java.util.Scanner; public class test2 { /** * 三人出牌测试 * <p> * 输入t,出,f,不出 * l=上家;m=自己;r=下家; * 默认出牌顺序:l-->m-->r-->l-->.... * lz表示l主动出牌;mz表示m主动出牌;rz表示r主动出牌,主动出牌只能输入t * lc表示l出牌;mc表示m主动出牌;rc表示r出牌,可输入t/f * * 首先除lz是t之外,mz,rz,l,mc,rz都是f;即默认lz主动出牌; */ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); boolean l = false;//上家 boolean lz = true;//上家z boolean m = false;//我 boolean mz = false;//我z boolean r = false;//下家 boolean rz = false;//下家z for (int i = 0; i < 10; i++) { if (lz) { System.out.println("========================="); System.out.println("上家z输入"); String s = scanner.nextLine(); if (s.equals("t")) { System.out.println("上家z出牌了"); l = false; m = true; } } if (l) { System.out.println("上家输入"); String s = scanner.nextLine(); if (s.equals("t")) { System.out.println("上家出牌了"); mz = false; rz = false; lz = true; } else if (s.equals("f")) { System.out.println("上家不要"); } l = false; m = true; } if (mz) { System.out.println("========================="); System.out.println("我z输入"); String s = scanner.nextLine(); if (s.equals("t")) { System.out.println("我z出牌了"); m = false; r = true; } } if (m) { System.out.println("我输入"); String s = scanner.nextLine(); if (s.equals("t")) { System.out.println("我出牌了"); lz = false; rz = false; mz = true; } else if (s.equals("f")) { System.out.println("我不要"); } m = false; r = true; } if (rz) { System.out.println("========================="); System.out.println("下家z输入"); String s = scanner.nextLine(); if (s.equals("t")) { System.out.println("下家z出牌了"); r = false; l = true; } } if (r) { System.out.println("下家输入"); String s = scanner.nextLine(); if (s.equals("t")) { System.out.println("下家出牌了"); lz = false; mz = false; rz = true; } else if (s.equals("f")) { System.out.println("下家不要了"); } r = false; l = true; } } } }
六、实现斗地主三人出牌
只需把Play1.java复制在那篇博客包下,运行Play1就可实现。
基本可以玩耍,有可能会有不知道的bug,欢迎评论留言指出
Play1
package com.ddz; import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * * Cc代表大小王,0代表10 * 大小写jqk都可以代表JQK * Aa1都代表A * * 多少副牌sum、是否包含345contains345可以在main方法中更改 * * 四炸<双王炸<五炸<六炸<三王炸<七炸<八炸<四王炸 * 并且[大王, 大王]是可以打赢[大王, 小王]和[小王, 小王]的 * * */ public class Play1 { private static List<Card> player;//玩家 private static List<Card> left;//上家 private static List<Card> right;//下家 private static CardProduct last = CardProduct.createNullType(); //Cc代表大小王,0代表10 /** * 发牌 * * @param sum 多少副牌 * @param contains345 是否包含345 */ public static void licensing(int sum, boolean contains345) { player = new ArrayList<>(); left = new ArrayList<>(); right = new ArrayList<>(); ArrayList<Integer> list = new ArrayList<>();//牌堆 for (int i = 0; i < sum; i++) { for (int j = contains345 ? GameConstants.ID_3_1 : GameConstants.ID_6_1; j < 54; j++) { list.add(j); } } //发牌 while (list.size() > 0) { player.add(new Card(list.remove((int) (Math.random() * list.size())))); left.add(new Card(list.remove((int) (Math.random() * list.size())))); right.add(new Card(list.remove((int) (Math.random() * list.size())))); } player.sort((o1, o2) -> o2.id - o1.id); left.sort((o1, o2) -> o2.id - o1.id); right.sort((o1, o2) -> o2.id - o1.id); } public static List<Integer> searchIndexByNum(List<Card> list, List<Card> card) { if (card.size() > list.size()) return null; int[] cardNum = new int[list.size()]; for (int i = 0; i < list.size(); i++) { cardNum[i] = list.get(i).num; } List<Integer> indexes = new ArrayList<>(); for (int i = 0; i < card.size(); i++) { if (card.get(i).num > GameConstants.NUM_2) { final int id = card.get(i).id; for (int j = 0; j < list.size(); j++) { if (card.get(i).id == list.get(j).id && !indexes.contains(j)) { indexes.add(j); break; } } } else { for (int j = 0; j < cardNum.length; j++) { if (card.get(i).num == cardNum[j] && !indexes.contains(j)) { indexes.add(j); break; } } } } if (indexes.size() != card.size()) return null; return indexes; } public static void main(String[] args) { int sum=1;//多少副牌 boolean contains345=true;//是否包含345 licensing(sum, contains345); System.out.println("您的扑克牌:" + player); System.out.println("上家扑克牌:" + left); System.out.println("下家扑克牌:" + right); boolean playing = true; boolean toLeft=false;//出牌 boolean toMe = false;//Math.random() < 0.5;//我出 boolean toRight=false; boolean l=true;//主动出牌 boolean m=false; boolean r=false; boolean isContinue=false; boolean isMe=false; Scanner scanner = new Scanner(System.in); while (playing) { if (isMe){//输入?(提示),或输入(查看),或出牌不符合规则--》还该自己出牌 isMe=false; toLeft=false; toMe=true; toRight=false; l=false; m=false; r=false; } if (l){ System.out.println("=====================================上家主动出牌=================================="); System.out.println("上家当前扑克牌:" + left); last = CardProduct.createNullType(); Prompt prompt = new Prompt(left, last); if (prompt.prompt.size() > 0) { CardProducts cardProducts = CardsFactory.builder(prompt.prompt.get(0)); int index = cardProducts.isGreaterThan(last); if (index >= 0) { last = cardProducts.list.get(index); left.removeAll(last.list); System.out.println("上家主动出牌:" + last.list); if (left.size() == 0) { System.out.println("======================================================================="); System.out.println("上家赢了!"); System.out.println("你输啦!"); break; } toLeft=false; toMe=true; } } } if(toLeft){ System.out.println("=============上家================="); System.out.println("上家当前扑克牌:" + left); Prompt prompt = new Prompt(left, last); if (prompt.prompt.size() > 0) { CardProducts cardProducts = CardsFactory.builder(prompt.prompt.get(0)); int index = cardProducts.isGreaterThan(last); if (index >= 0) { last = cardProducts.list.get(index); left.removeAll(last.list); System.out.println("上家出牌:" + last.list); if (left.size() == 0) { System.out.println("======================================================================="); System.out.println("上家赢了!"); System.out.println("你输啦!"); break; } m=false; r=false; l=true; toLeft=false; toMe = true; } } else { System.out.println("上家不要"); toLeft=false; toMe = true; } } if (m) { System.out.println("================================玩家主动出牌======================================"); last = CardProduct.createNullType(); System.out.println("上一次出牌:" + last); System.out.println("您当前的扑克牌:" + player); System.out.println("请您出牌:(输入?提示 输入,偷看电脑的牌)"); String line = scanner.nextLine(); if (line == null || line.length() == 0) { isMe=true; continue; } else if ("?".equals(line)) { System.out.println("提示:" + new Prompt(player, last).prompt); isMe=true; continue; } else if (",".equals(line)) { System.out.println("上家当前扑克牌:" + left); System.out.println("下家当前扑克牌:" + right); isMe=true; continue; } List<Integer> indexes = searchIndexByNum(player, CardUtils.CreateCards(line)); if (indexes == null) { System.out.println("您输入的扑克牌无效请重新输入"); isMe=true; continue; } CardProducts cardProducts = CardsFactory.builder(CardUtils.getCardsByIndexes(player, indexes)); int index = cardProducts.isGreaterThan(last); if (index >= 0) { CardProduct newCardProduct = cardProducts.list.get(index); last = newCardProduct; player.removeAll(last.list); System.out.println("出牌成功:" + last); if (player.size() == 0) { System.out.println("======================================================================="); System.out.println("你赢啦!"); break; } toMe = false; toRight=!toRight; } else { System.out.println("不符合游戏规则:" + cardProducts); isMe=true; continue; } } if (toMe) { System.out.println("=============玩家================="); System.out.println("上一次出牌:" + last); System.out.println("您当前的扑克牌:" + player); System.out.println("请您出牌:(输入.不出 输入?提示 输入,偷看电脑的牌)"); String line = scanner.nextLine(); if (line == null || line.length() == 0) { isMe=true; continue; } else if (".".equals(line)) { toMe=false; toRight=true; } else if ("?".equals(line)) { System.out.println("提示:" + new Prompt(player, last).prompt); isMe=true; continue; } else if (",".equals(line)) { System.out.println("上家当前扑克牌:" + left); System.out.println("下家当前扑克牌:" + right); isMe=true; continue; }else { List<Integer> indexes = searchIndexByNum(player, CardUtils.CreateCards(line)); if (indexes == null) { System.out.println("您输入的扑克牌无效请重新输入"); isMe = true; continue; } CardProducts cardProducts = CardsFactory.builder(CardUtils.getCardsByIndexes(player, indexes)); int index = cardProducts.isGreaterThan(last); if (index >= 0) { CardProduct newCardProduct = cardProducts.list.get(index); last = newCardProduct; player.removeAll(last.list); System.out.println("出牌成功:" + last); if (player.size() == 0) { System.out.println("======================================================================="); System.out.println("你赢啦!"); break; } l = false; r = false; m = true; toMe = false; toRight = true; } else { System.out.println("不符合游戏规则:" + cardProducts); isMe = true; continue; } } } if (r) { System.out.println("================================下家主动出牌======================================="); System.out.println("下家当前扑克牌:" + right); last = CardProduct.createNullType(); Prompt prompt = new Prompt(right, last); if (prompt.prompt.size() > 0) { CardProducts cardProducts = CardsFactory.builder(prompt.prompt.get(0)); int index = cardProducts.isGreaterThan(last); if (index >= 0) { last = cardProducts.list.get(index); right.removeAll(last.list); System.out.println("下家主动出牌:" + last.list); if (right.size() == 0) { System.out.println("======================================================================="); System.out.println("下家赢了!"); System.out.println("你输啦!"); break; } toRight= false; toLeft = true; } } } if(toRight){ System.out.println("=============下家================="); System.out.println("下家当前扑克牌:" + right); Prompt prompt = new Prompt(right, last); if (prompt.prompt.size() > 0) { CardProducts cardProducts = CardsFactory.builder(prompt.prompt.get(0)); int index = cardProducts.isGreaterThan(last); if (index >= 0) { last = cardProducts.list.get(index); right.removeAll(last.list); System.out.println("下家出牌:" + last.list); if (right.size() == 0) { System.out.println("======================================================================="); System.out.println("下家赢了!"); System.out.println("你输啦!"); break; } l = false; m = false; r = true; toRight=false; toLeft = true; } } else { System.out.println("下家不要"); toRight=false; toLeft = true; } } } } }
七、运行结果
玩了几盘,终于赢了。
您的扑克牌:[大王, 2, K, K, J, J, 9, 9, 9, 8, 8, 7, 7, 6, 6, 4, 4, 4] 上家扑克牌:[2, A, A, A, K, K, Q, Q, J, J, 10, 10, 10, 7, 5, 5, 4, 3] 下家扑克牌:[小王, 2, 2, A, Q, Q, 10, 9, 8, 8, 7, 6, 6, 5, 5, 3, 3, 3] =====================================上家主动出牌================================== 上家当前扑克牌:[2, A, A, A, K, K, Q, Q, J, J, 10, 10, 10, 7, 5, 5, 4, 3] 上家主动出牌:[A, A, K, K, Q, Q, J, J, 10, 10] =============玩家================= 上一次出牌:CardProduct{list=[A, A, K, K, Q, Q, J, J, 10, 10], type=连对, maxCard=A, len=10, group=[(num=11, sum=2), (num=10, sum=2), (num=9, sum=2), (num=8, sum=2), (num=7, sum=2)]} 您当前的扑克牌:[大王, 2, K, K, J, J, 9, 9, 9, 8, 8, 7, 7, 6, 6, 4, 4, 4] 请您出牌:(输入.不出 输入?提示 输入,偷看电脑的牌) . =============下家================= 下家当前扑克牌:[小王, 2, 2, A, Q, Q, 10, 9, 8, 8, 7, 6, 6, 5, 5, 3, 3, 3] 下家不要 =====================================上家主动出牌================================== 上家当前扑克牌:[2, A, 10, 7, 5, 5, 4, 3] 上家主动出牌:[3] =============玩家================= 上一次出牌:CardProduct{list=[3], type=单牌, maxCard=3, len=1, group=[(num=0, sum=1)]} 您当前的扑克牌:[大王, 2, K, K, J, J, 9, 9, 9, 8, 8, 7, 7, 6, 6, 4, 4, 4] 请您出牌:(输入.不出 输入?提示 输入,偷看电脑的牌) 2 出牌成功:CardProduct{list=[2], type=单牌, maxCard=2, len=1, group=[(num=12, sum=1)]} =============下家================= 下家当前扑克牌:[小王, 2, 2, A, Q, Q, 10, 9, 8, 8, 7, 6, 6, 5, 5, 3, 3, 3] 下家出牌:[小王] =============上家================= 上家当前扑克牌:[2, A, 10, 7, 5, 5, 4] 上家不要 =============玩家================= 上一次出牌:CardProduct{list=[], type=无, maxCard=null, len=0, group=null} 您当前的扑克牌:[大王, K, K, J, J, 9, 9, 9, 8, 8, 7, 7, 6, 6, 4, 4, 4] 请您出牌:(输入.不出 输入?提示 输入,偷看电脑的牌) C 出牌成功:CardProduct{list=[大王], type=单牌, maxCard=大王, len=1, group=[(num=13, sum=1)]} =============下家================= 下家当前扑克牌:[2, 2, A, Q, Q, 10, 9, 8, 8, 7, 6, 6, 5, 5, 3, 3, 3] 下家不要 =============上家================= 上家当前扑克牌:[2, A, 10, 7, 5, 5, 4] 上家不要 ================================玩家主动出牌====================================== 上一次出牌:CardProduct{list=[], type=无, maxCard=null, len=0, group=null} 您当前的扑克牌:[K, K, J, J, 9, 9, 9, 8, 8, 7, 7, 6, 6, 4, 4, 4] 请您出牌:(输入?提示 输入,偷看电脑的牌) 667788 出牌成功:CardProduct{list=[8, 8, 7, 7, 6, 6], type=连对, maxCard=8, len=6, group=[(num=5, sum=2), (num=4, sum=2), (num=3, sum=2)]} =============下家================= 下家当前扑克牌:[2, 2, A, Q, Q, 10, 9, 8, 8, 7, 6, 6, 5, 5, 3, 3, 3] 下家不要 =============上家================= 上家当前扑克牌:[2, A, 10, 7, 5, 5, 4] 上家不要 ================================玩家主动出牌====================================== 上一次出牌:CardProduct{list=[], type=无, maxCard=null, len=0, group=null} 您当前的扑克牌:[K, K, J, J, 9, 9, 9, 4, 4, 4] 请您出牌:(输入?提示 输入,偷看电脑的牌) 444jj 出牌成功:CardProduct{list=[4, 4, 4, J, J], type=三带一对, maxCard=4, len=5, group=[(num=1, sum=3), (num=8, sum=2)]} =============下家================= 下家当前扑克牌:[2, 2, A, Q, Q, 10, 9, 8, 8, 7, 6, 6, 5, 5, 3, 3, 3] 下家不要 =============上家================= 上家当前扑克牌:[2, A, 10, 7, 5, 5, 4] 上家不要 ================================玩家主动出牌====================================== 上一次出牌:CardProduct{list=[], type=无, maxCard=null, len=0, group=null} 您当前的扑克牌:[K, K, 9, 9, 9] 请您出牌:(输入?提示 输入,偷看电脑的牌) 999kk 出牌成功:CardProduct{list=[9, 9, 9, K, K], type=三带一对, maxCard=9, len=5, group=[(num=6, sum=3), (num=10, sum=2)]} ======================================================================= 你赢啦! Process finished with exit code 0
八 日后升级
0 图像界面,联机玩
1 单击可设置几人游玩,(几个真人几个电脑)
增加Player类(Type属性设置人/机)
2 可以加入底牌
增加地主牌
开始随机选取一张牌,作为地主牌,哪个玩家有这个牌,就开始叫地主
给Player对象增加积分属性
叫分制(1分、2分、3分)
抢地主制
增加底牌的倍数
3开发更多模式
炸弹场
不洗牌模式
癞子(天地)玩法
九、感谢
感谢这位博主的博客【狗头】
十、修复bug
bug1 上家出牌,我点提示,然后不出,如果下家也不出 就会导致上家不要,该我出牌,本应该是上家拿到牌权出牌
此bug是continue语句所导致
if (isMe){//输入?(提示),或输入(查看),或出牌不符合规则--》还该自己出牌 isMe=false; toLeft=false; toMe=true; toRight=false; l=false; m=false; r=false; ifMe=true; }
使得进入if(isMe)语句,导致l=false,使得上家l丧失了牌权。
加上下面的判断使得上家l重新获得了牌权
//ifMe表示是否进入if(isMe) //MeGuo表示玩家是否不要 //RGuo表示下家是否不要 if (ifMe&&MeGuo&&RGuo){ l=true; }
同理,还有相似情况
当下家得到牌权,上家不要,我点提示,然后不要时,会导致下家出牌,本应该是主动出牌。
加上下面的判断使得下家重新获得了牌权
//Luo表示上家是否不要 if (ifMe&&MeGuo&&LGuo){ r=true; }
针对bug1修改代码如下
package com.ddz; import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * * Cc代表大小王,0代表10 * 大小写jqk都可以代表JQK * Aa1都代表A * * 多少副牌sum、是否包含345contains345可以在main方法中更改 * * 四炸<双王炸<五炸<六炸<三王炸<七炸<八炸<四王炸 * 并且[大王, 大王]是可以打赢[大王, 小王]和[小王, 小王]的 * * */ public class Play1 { private static List<Card> player;//玩家 private static List<Card> left;//上家 private static List<Card> right;//下家 private static CardProduct last = CardProduct.createNullType(); //Cc代表大小王,0代表10 /** * 发牌 * * @param sum 多少副牌 * @param contains345 是否包含345 */ public static void licensing(int sum, boolean contains345) { player = new ArrayList<>(); left = new ArrayList<>(); right = new ArrayList<>(); ArrayList<Integer> list = new ArrayList<>();//牌堆 for (int i = 0; i < sum; i++) { for (int j = contains345 ? GameConstants.ID_3_1 : GameConstants.ID_6_1; j < 54; j++) { list.add(j); } } //发牌 while (list.size() > 0) { player.add(new Card(list.remove((int) (Math.random() * list.size())))); left.add(new Card(list.remove((int) (Math.random() * list.size())))); right.add(new Card(list.remove((int) (Math.random() * list.size())))); } player.sort((o1, o2) -> o2.id - o1.id); left.sort((o1, o2) -> o2.id - o1.id); right.sort((o1, o2) -> o2.id - o1.id); } public static List<Integer> searchIndexByNum(List<Card> list, List<Card> card) { if (card.size() > list.size()) return null; int[] cardNum = new int[list.size()]; for (int i = 0; i < list.size(); i++) { cardNum[i] = list.get(i).num; } List<Integer> indexes = new ArrayList<>(); for (int i = 0; i < card.size(); i++) { if (card.get(i).num > GameConstants.NUM_2) { final int id = card.get(i).id; for (int j = 0; j < list.size(); j++) { if (card.get(i).id == list.get(j).id && !indexes.contains(j)) { indexes.add(j); break; } } } else { for (int j = 0; j < cardNum.length; j++) { if (card.get(i).num == cardNum[j] && !indexes.contains(j)) { indexes.add(j); break; } } } } if (indexes.size() != card.size()) return null; return indexes; } public static void main(String[] args) { int sum=1;//多少副牌 boolean contains345=true;//是否包含345 licensing(sum, contains345); System.out.println("您的扑克牌:" + player); System.out.println("上家扑克牌:" + left); System.out.println("下家扑克牌:" + right); boolean playing = true; boolean toLeft=false;//出牌 boolean toMe = false;//Math.random() < 0.5;//我出 boolean toRight=false; boolean l=true;//主动出牌 boolean m=false; boolean r=false; boolean isMe=false; boolean ifMe=false; boolean LGuo=false; boolean MeGuo=false; boolean RGuo=false; Scanner scanner = new Scanner(System.in); while (playing) { if (isMe){//输入?(提示),或输入(查看),或出牌不符合规则--》还该自己出牌 isMe=false; toLeft=false; toMe=true; toRight=false; l=false; m=false; r=false; ifMe=true; } if (ifMe&&MeGuo&&RGuo){ l=true; } if (ifMe&&MeGuo&&LGuo){ r=true; } if (l){ System.out.println("=====================================上家主动出牌=================================="); System.out.println("上家当前扑克牌:" + left); last = CardProduct.createNullType(); Prompt prompt = new Prompt(left, last); if (prompt.prompt.size() > 0) { CardProducts cardProducts = CardsFactory.builder(prompt.prompt.get(0)); int index = cardProducts.isGreaterThan(last); if (index >= 0) { last = cardProducts.list.get(index); left.removeAll(last.list); System.out.println("上家主动出牌:" + last.list); if (left.size() == 0) { System.out.println("======================================================================="); System.out.println("上家赢了!"); System.out.println("你输啦!"); break; } toLeft=false; toMe=true; LGuo=false; } } } if(toLeft){ System.out.println("=============上家================="); System.out.println("上家当前扑克牌:" + left); Prompt prompt = new Prompt(left, last); if (prompt.prompt.size() > 0) { CardProducts cardProducts = CardsFactory.builder(prompt.prompt.get(0)); int index = cardProducts.isGreaterThan(last); if (index >= 0) { last = cardProducts.list.get(index); left.removeAll(last.list); System.out.println("上家出牌:" + last.list); if (left.size() == 0) { System.out.println("======================================================================="); System.out.println("上家赢了!"); System.out.println("你输啦!"); break; } m=false; r=false; l=true; toLeft=false; toMe = true; RGuo=false; } } else { System.out.println("上家不要"); toLeft=false; toMe = true; LGuo=true; } } if (m) { System.out.println("================================玩家主动出牌======================================"); last = CardProduct.createNullType(); System.out.println("上一次出牌:" + last); System.out.println("您当前的扑克牌:" + player); System.out.println("请您出牌:(输入?提示 输入,偷看电脑的牌)"); String line = scanner.nextLine(); if (line == null || line.length() == 0) { isMe=true; continue; } else if ("?".equals(line)) { System.out.println("提示:" + new Prompt(player, last).prompt); isMe=true; continue; } else if (",".equals(line)) { System.out.println("上家当前扑克牌:" + left); System.out.println("下家当前扑克牌:" + right); isMe=true; continue; } List<Integer> indexes = searchIndexByNum(player, CardUtils.CreateCards(line)); if (indexes == null) { System.out.println("您输入的扑克牌无效请重新输入"); isMe=true; continue; } CardProducts cardProducts = CardsFactory.builder(CardUtils.getCardsByIndexes(player, indexes)); int index = cardProducts.isGreaterThan(last); if (index >= 0) { CardProduct newCardProduct = cardProducts.list.get(index); last = newCardProduct; player.removeAll(last.list); System.out.println("出牌成功:" + last); if (player.size() == 0) { System.out.println("======================================================================="); System.out.println("你赢啦!"); break; } toMe = false; toRight=!toRight; MeGuo=false;// } else { System.out.println("不符合游戏规则:" + cardProducts); isMe=true; continue; } } if (toMe) { System.out.println("=============玩家================="); System.out.println("上一次出牌:" + last); System.out.println("您当前的扑克牌:" + player); System.out.println("请您出牌:(输入.不出 输入?提示 输入,偷看电脑的牌)"); String line = scanner.nextLine(); if (line == null || line.length() == 0) { isMe=true; continue; } else if (".".equals(line)) { toMe=false; toRight=true; MeGuo=true; } else if ("?".equals(line)) { System.out.println("提示:" + new Prompt(player, last).prompt); isMe=true; continue; } else if (",".equals(line)) { System.out.println("上家当前扑克牌:" + left); System.out.println("下家当前扑克牌:" + right); isMe=true; continue; }else { List<Integer> indexes = searchIndexByNum(player, CardUtils.CreateCards(line)); if (indexes == null) { System.out.println("您输入的扑克牌无效请重新输入"); isMe = true; continue; } CardProducts cardProducts = CardsFactory.builder(CardUtils.getCardsByIndexes(player, indexes)); int index = cardProducts.isGreaterThan(last); if (index >= 0) { CardProduct newCardProduct = cardProducts.list.get(index); last = newCardProduct; player.removeAll(last.list); System.out.println("出牌成功:" + last); if (player.size() == 0) { System.out.println("======================================================================="); System.out.println("你赢啦!"); break; } l = false; r = false; m = true; toMe = false; toRight = true; MeGuo=false;// } else { System.out.println("不符合游戏规则:" + cardProducts); isMe = true; continue; } } } if (r) { System.out.println("================================下家主动出牌======================================="); System.out.println("下家当前扑克牌:" + right); last = CardProduct.createNullType(); Prompt prompt = new Prompt(right, last); if (prompt.prompt.size() > 0) { CardProducts cardProducts = CardsFactory.builder(prompt.prompt.get(0)); int index = cardProducts.isGreaterThan(last); if (index >= 0) { last = cardProducts.list.get(index); right.removeAll(last.list); System.out.println("下家主动出牌:" + last.list); if (right.size() == 0) { System.out.println("======================================================================="); System.out.println("下家赢了!"); System.out.println("你输啦!"); break; } toRight= false; toLeft = true; RGuo=false; } } } if(toRight){ System.out.println("=============下家================="); System.out.println("下家当前扑克牌:" + right); Prompt prompt = new Prompt(right, last); if (prompt.prompt.size() > 0) { CardProducts cardProducts = CardsFactory.builder(prompt.prompt.get(0)); int index = cardProducts.isGreaterThan(last); if (index >= 0) { last = cardProducts.list.get(index); right.removeAll(last.list); System.out.println("下家出牌:" + last.list); if (right.size() == 0) { System.out.println("======================================================================="); System.out.println("下家赢了!"); System.out.println("你输啦!"); break; } l = false; m = false; r = true; toRight=false; toLeft = true; RGuo=false; } } else { System.out.println("下家不要"); RGuo=true;// toRight=false; toLeft = true; } } } } }
bug2,对bug1的修改还存在bug当玩家和下家同时不要时,上家出牌,玩家出错牌,会直接导致上家获得主动牌权
bug情况如下:
您的扑克牌:[大王, 2, A, K, K, Q, Q, Q, J, J, 10, 10, 10, 9, 9, 7, 4, 3] 上家扑克牌:[2, K, Q, J, 9, 8, 8, 8, 7, 6, 6, 6, 5, 5, 5, 4, 3, 3] 下家扑克牌:[小王, 2, 2, A, A, A, K, J, 10, 9, 8, 7, 7, 6, 5, 4, 4, 3] =====================================上家主动出牌================================== 上家当前扑克牌:[2, K, Q, J, 9, 8, 8, 8, 7, 6, 6, 6, 5, 5, 5, 4, 3, 3] 上家主动出牌:[6, 6, 6, 5, 5, 5, 8, 8, 3, 3] =============玩家================= 上一次出牌:CardProduct{list=[6, 6, 6, 5, 5, 5, 8, 8, 3, 3], type=飞机带对子, maxCard=6, len=10, group=[(num=3, sum=3), (num=2, sum=3), (num=5, sum=2), (num=0, sum=2)]} 您当前的扑克牌:[大王, 2, A, K, K, Q, Q, Q, J, J, 10, 10, 10, 9, 9, 7, 4, 3] 请您出牌:(输入.不出 输入?提示 输入,偷看电脑的牌) . =============下家================= 下家当前扑克牌:[小王, 2, 2, A, A, A, K, J, 10, 9, 8, 7, 7, 6, 5, 4, 4, 3] 下家不要 =====================================上家主动出牌================================== 上家当前扑克牌:[2, K, Q, J, 9, 8, 7, 4] 上家主动出牌:[4] =============玩家================= 上一次出牌:CardProduct{list=[4], type=单牌, maxCard=4, len=1, group=[(num=1, sum=1)]} 您当前的扑克牌:[大王, 2, A, K, K, Q, Q, Q, J, J, 10, 10, 10, 9, 9, 7, 4, 3] 请您出牌:(输入.不出 输入?提示 输入,偷看电脑的牌) 3 不符合游戏规则:1 [CardProduct{list=[3], type=单牌, maxCard=3, len=1, group=[(num=0, sum=1)]}] =====================================上家主动出牌================================== 上家当前扑克牌:[2, K, Q, J, 9, 8, 7] 上家主动出牌:[7] =============玩家================= 上一次出牌:CardProduct{list=[7], type=单牌, maxCard=7, len=1, group=[(num=4, sum=1)]} 您当前的扑克牌:[大王, 2, A, K, K, Q, Q, Q, J, J, 10, 10, 10, 9, 9, 7, 4, 3] 请您出牌:(输入.不出 输入?提示 输入,偷看电脑的牌)
修复
直接去掉continue对外层循环的影响,让其内层循环下起作用,并且有break来跳出内层循环,当玩家赢了之后,加入isMe来辅助结束游戏,不会是下家结束后仍然可以出牌
并且去除了while(m)中的语句“判断是否出牌成功”
(即打不打得出过的牌,(但因玩家主动出牌,出过的牌赋值null,所以判断无用)
Play2
package com.ddz; import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * Cc代表大小王,0代表10 * 大小写jqk都可以代表JQK * Aa1都代表A * <p> * 多少副牌sum、是否包含345contains345可以在main方法中更改 * <p> * 四炸<双王炸<五炸<六炸<三王炸<七炸<八炸<四王炸 * 并且[大王, 大王]是可以打赢[大王, 小王]和[小王, 小王]的 */ public class Play2 { private static List<Card> player;//玩家 private static List<Card> left;//上家 private static List<Card> right;//下家 private static CardProduct last = CardProduct.createNullType(); //Cc代表大小王,0代表10 /** * 发牌 * * @param sum 多少副牌 * @param contains345 是否包含345 */ public static void licensing(int sum, boolean contains345) { player = new ArrayList<>(); left = new ArrayList<>(); right = new ArrayList<>(); ArrayList<Integer> list = new ArrayList<>();//牌堆 for (int i = 0; i < sum; i++) { for (int j = contains345 ? GameConstants.ID_3_1 : GameConstants.ID_6_1; j < 54; j++) { list.add(j); } } //发牌 while (list.size() > 0) { player.add(new Card(list.remove((int) (Math.random() * list.size())))); left.add(new Card(list.remove((int) (Math.random() * list.size())))); right.add(new Card(list.remove((int) (Math.random() * list.size())))); } player.sort((o1, o2) -> o2.id - o1.id); left.sort((o1, o2) -> o2.id - o1.id); right.sort((o1, o2) -> o2.id - o1.id); } public static List<Integer> searchIndexByNum(List<Card> list, List<Card> card) { if (card.size() > list.size()) return null; int[] cardNum = new int[list.size()]; for (int i = 0; i < list.size(); i++) { cardNum[i] = list.get(i).num; } List<Integer> indexes = new ArrayList<>(); for (int i = 0; i < card.size(); i++) { if (card.get(i).num > GameConstants.NUM_2) { final int id = card.get(i).id; for (int j = 0; j < list.size(); j++) { if (card.get(i).id == list.get(j).id && !indexes.contains(j)) { indexes.add(j); break; } } } else { for (int j = 0; j < cardNum.length; j++) { if (card.get(i).num == cardNum[j] && !indexes.contains(j)) { indexes.add(j); break; } } } } if (indexes.size() != card.size()) return null; return indexes; } public static void main(String[] args) { int sum = 1;//多少副牌 boolean contains345 = true;//是否包含345 licensing(sum, contains345); System.out.println("您的扑克牌:" + player); System.out.println("上家扑克牌:" + left); System.out.println("下家扑克牌:" + right); boolean playing = true; boolean toLeft = false;//出牌 boolean toMe = false;//Math.random() < 0.5;//我出 boolean toRight = false; boolean l = true;//主动出牌 boolean m = false; boolean r = false; boolean isMe = false;//me获胜 Scanner scanner = new Scanner(System.in); while (playing) { if (l) { System.out.println("=====================================上家主动出牌=================================="); System.out.println("上家当前扑克牌:" + left); last = CardProduct.createNullType(); Prompt prompt = new Prompt(left, last); if (prompt.prompt.size() > 0) { CardProducts cardProducts = CardsFactory.builder(prompt.prompt.get(0)); int index = cardProducts.isGreaterThan(last); if (index >= 0) { last = cardProducts.list.get(index); left.removeAll(last.list); System.out.println("上家主动出牌:" + last.list); if (left.size() == 0) { System.out.println("======================================================================="); System.out.println("上家赢了!"); System.out.println("你输啦!"); break; } toLeft = false; toMe = true; } } } if (toLeft) { System.out.println("=============上家================="); System.out.println("上家当前扑克牌:" + left); Prompt prompt = new Prompt(left, last); if (prompt.prompt.size() > 0) { CardProducts cardProducts = CardsFactory.builder(prompt.prompt.get(0)); int index = cardProducts.isGreaterThan(last); if (index >= 0) { last = cardProducts.list.get(index); left.removeAll(last.list); System.out.println("上家出牌:" + last.list); if (left.size() == 0) { System.out.println("======================================================================="); System.out.println("上家赢了!"); System.out.println("你输啦!"); break; } m = false; r = false; l = true; toLeft = false; toMe = true; } } else { System.out.println("上家不要"); toLeft = false; toMe = true; } } while (m) { System.out.println("================================玩家主动出牌======================================"); last = CardProduct.createNullType(); System.out.println("上一次出牌:" + last); System.out.println("您当前的扑克牌:" + player); System.out.println("请您出牌:(输入?提示 输入,偷看电脑的牌)"); String line = scanner.nextLine(); if (line == null || line.length() == 0) { continue; } else if ("?".equals(line)) { System.out.println("提示:" + new Prompt(player, last).prompt); continue; } else if (",".equals(line)) { System.out.println("上家当前扑克牌:" + left); System.out.println("下家当前扑克牌:" + right); continue; } List<Integer> indexes = searchIndexByNum(player, CardUtils.CreateCards(line)); if (indexes == null) { System.out.println("您输入的扑克牌无效请重新输入"); continue; } CardProducts cardProducts = CardsFactory.builder(CardUtils.getCardsByIndexes(player, indexes)); int index = cardProducts.isGreaterThan(last); // if (index >= 0) { CardProduct newCardProduct = cardProducts.list.get(index); last = newCardProduct; player.removeAll(last.list); System.out.println("出牌成功:" + last); if (player.size() == 0) { System.out.println("======================================================================="); System.out.println("你赢啦!"); isMe=true; break; } toMe = false; toRight = !toRight; break; // } else { // System.out.println("不符合游戏规则:" + cardProducts); // continue; // } } if (isMe) { break; } while (toMe) { System.out.println("=============玩家================="); System.out.println("上一次出牌:" + last); System.out.println("您当前的扑克牌:" + player); System.out.println("请您出牌:(输入.不出 输入?提示 输入,偷看电脑的牌)"); String line = scanner.nextLine(); if (line == null || line.length() == 0) { continue; } else if (".".equals(line)) { toMe = false; toRight = true; break; } else if ("?".equals(line)) { System.out.println("提示:" + new Prompt(player, last).prompt); continue; } else if (",".equals(line)) { System.out.println("上家当前扑克牌:" + left); System.out.println("下家当前扑克牌:" + right); continue; } else { List<Integer> indexes = searchIndexByNum(player, CardUtils.CreateCards(line)); if (indexes == null) { System.out.println("您输入的扑克牌无效请重新输入"); continue; } CardProducts cardProducts = CardsFactory.builder(CardUtils.getCardsByIndexes(player, indexes)); int index = cardProducts.isGreaterThan(last); if (index >= 0) { CardProduct newCardProduct = cardProducts.list.get(index); last = newCardProduct; player.removeAll(last.list); System.out.println("出牌成功:" + last); if (player.size() == 0) { System.out.println("======================================================================="); System.out.println("你赢啦!"); isMe=true; break; } l = false; r = false; m = true; toMe = false; toRight = true; } else { System.out.println("不符合游戏规则:" + cardProducts); continue; } } } if (isMe){ break; } if (r) { System.out.println("================================下家主动出牌======================================="); System.out.println("下家当前扑克牌:" + right); last = CardProduct.createNullType(); Prompt prompt = new Prompt(right, last); if (prompt.prompt.size() > 0) { CardProducts cardProducts = CardsFactory.builder(prompt.prompt.get(0)); int index = cardProducts.isGreaterThan(last); if (index >= 0) { last = cardProducts.list.get(index); right.removeAll(last.list); System.out.println("下家主动出牌:" + last.list); if (right.size() == 0) { System.out.println("======================================================================="); System.out.println("下家赢了!"); System.out.println("你输啦!"); break; } toRight = false; toLeft = true; } } } if (toRight) { System.out.println("=============下家================="); System.out.println("下家当前扑克牌:" + right); Prompt prompt = new Prompt(right, last); if (prompt.prompt.size() > 0) { CardProducts cardProducts = CardsFactory.builder(prompt.prompt.get(0)); int index = cardProducts.isGreaterThan(last); if (index >= 0) { last = cardProducts.list.get(index); right.removeAll(last.list); System.out.println("下家出牌:" + last.list); if (right.size() == 0) { System.out.println("======================================================================="); System.out.println("下家赢了!"); System.out.println("你输啦!"); break; } l = false; m = false; r = true; toRight = false; toLeft = true; } } else { System.out.println("下家不要"); toRight = false; toLeft = true; } } } } }