携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第三天,点击查看活动详情
上面两章描述了洗牌发牌,玩家出牌,判断出牌是否正确等操作,接下来就是人机按照上家的出牌来出牌,需要按照以下规则来操作:
- 上家不出牌,则人机需要出的牌则需要大于上上家的牌
- 如果上家出的是单张,则人机出的牌需要大于该单张,或者出炸弹
- 如果上家出的牌是单张对子顺子,则人机也必须出对子,且大于该对子,或者出炸弹
- 如果上家出的是炸弹,则人机需要判断是否有大于该炸弹的炸弹
- 人机出牌后没有玩家出牌,则人机继续出牌
public static List playHandPlayer1(String pok, Map<String, List> pokMap, Map<String, Integer> valueMap, List<String> allpok, List boards, String local, boolean self) {
List pokList = pokMap.get(pok);
//初始化牌
Map<Integer, List<String>> initMap = initBoard(pokList, valueMap);
//判断该出牌是否为人机出牌,如果是人机出牌则需要判断出牌规则是否正确
if ((!local.equals(pok) && !self) || self) {
boards = playHand(initMap, valueMap, boards);
}
if (boards != null && boards.size() > 0) {
System.out.println("【" + pok + "】出的牌为:" + boards.toString());
System.out.println("");
//出牌完成则在总牌数List中去掉所出的牌
allpok.removeAll(boards);
pokList.removeAll(boards);
pokMap.put(pok, pokList);
//如果出的牌为最后的牌则当前玩家赢了
if (pokList.size() <= 0) {
boards.clear();
boards.add("end");
System.out.println("【" + pok + "】:哈哈,我赢啦!!!!!!!!!!!");
} else {
//判断是否显示除当前玩家外其他玩家的牌
// if (local.equals(pok)) {
// System.out.println("【" + pok + "】剩下的牌为:" + pokList);
// System.out.println("");
// }
}
} else {
System.out.println("【" + pok + "】表示他要不起(╥╯^╰╥)");
// System.out.println("【" + pok + "】剩下的牌为:" + pokList);
System.out.println("");
}
return boards;
}
按照出牌规则进行出牌,判断自己是否第一个出牌,判断对方出牌的规则,根据对方出牌规则将自己出的牌放入List,且在Map中去掉已出的牌,避免重复出牌
/**
* 根据上家出的牌来出牌
*
* @param initMap 初始化的牌
* @param valueMap
* @param boards 上家的牌
*/
public static List playHand(Map<Integer, List<String>> initMap, Map<String, Integer> valueMap, List<String> boards) {
List<String> oneList = initMap.get(1);
List<String> twoList = initMap.get(2);
List<String> threeList = initMap.get(3);
List<String> fourList = initMap.get(4);
//对子及三张也可作为单张出牌
oneList.addAll(twoList);
oneList.addAll(threeList);
//三张可作为对子出牌
twoList.addAll(threeList);
//按照顺利理牌
listSort(oneList, valueMap);
listSort(twoList, valueMap);
listSort(threeList, valueMap);
// System.out.println("oneList" + oneList);
// System.out.println("twoList" + twoList);
// System.out.println("threeList" + threeList);
//判断是否有顺子
Map<String, Integer> ontMap = clocks(oneList, valueMap, 5);
//判断是否有连对
Map<String, Integer> twoMap = clocks(twoList, valueMap, 3);
//判断是否有飞机
Map<String, Integer> threeMap = clocks(threeList, valueMap, 2);
// System.out.println("map" + ontMap);
// System.out.println("map1" + twoMap);
// System.out.println("map2" + threeMap);
List retBoard = new ArrayList();
//如果没有上家出牌,则当前玩家为第一个出牌或者继续出牌
if (boards == null || boards.size() == 0) {
if (ontMap.size() > 0) {
retBoard = getBoard(ontMap, valueMap, 1, oneList);
} else if (twoMap.size() > 0) {
retBoard = getBoard(twoMap, valueMap, 2, twoList);
} else if (threeMap.size() > 0) {
retBoard = getBoard(threeMap, valueMap, 3, threeList);
} else {
// retBoard.add(oneList.get(0));
if (threeList != null && threeList.size() > 0) {
retBoard = checkOtherPok(threeList, threeMap, valueMap, "", 3);
} else if (twoList != null && twoList.size() > 0) {
retBoard = checkOtherPok(twoList, twoMap, valueMap, "", 2);
} else if (oneList != null && oneList.size() > 0) {
retBoard = checkOtherPok(oneList, ontMap, valueMap, "", 1);
} else if (fourList != null && fourList.size() > 0) {
retBoard = checkOtherPok(fourList, null, valueMap, "", 4);
}
}
} else {
//判断对方出的牌的规则
Map<Integer, List<String>> initMap1 = initBoard(boards, valueMap);
List<String> oneList1 = initMap1.get(1);
List<String> twoList1 = initMap1.get(2);
List<String> threeList1 = initMap1.get(3);
List<String> fourList1 = initMap1.get(4);
listSort(oneList1, valueMap);
listSort(twoList1, valueMap);
listSort(threeList1, valueMap);
if (oneList1.size() > 0 && threeList1.size() == 0 && fourList1.size() == 0) {
if (oneList1.size() > 4) {
//判断是否有顺子
Map<String, Integer> ontMap1 = clocks(oneList1, valueMap, 5);
if (ontMap1.size() > 0) {
retBoard = checkPock(ontMap1, ontMap, valueMap, 1, oneList);
}
} else {
//单张
if (oneList1.size() == 1) {
retBoard = checkOtherPok(oneList, ontMap, valueMap, oneList1.get(0), 1);
}
}
} else if (twoList1.size() > 0 && threeList1.size() == 0 && fourList1.size() == 0) {
if (twoList1.size() > 2) {
//判断是否有连对
Map<String, Integer> twoMap1 = clocks(twoList1, valueMap, 3);
if (twoMap1.size() > 0) {
//连对
retBoard = checkPock(twoMap1, twoMap, valueMap, 2, twoList);
}
} else {
//对子
retBoard = checkOtherPok(twoList, twoMap, valueMap, twoList1.get(0), 2);
}
} else if (threeList1.size() > 0) {
if (threeList1.size() > 3) {
//判断是否有飞机
Map<String, Integer> threeMap1 = clocks(threeList1, valueMap, 2);
if (threeMap1.size() > 0) {
//飞机
retBoard = checkPock(threeMap1, threeMap, valueMap, 3, threeList);
}
} else {
//三带一
retBoard = checkOtherPok(threeList, threeMap, valueMap, threeList1.get(0), 3);
}
} else if (fourList1.size() > 0) {
if (fourList1.size() == 2) {
//王炸
// retBoard = null;
} else {
//炸弹
retBoard = checkOtherPok(fourList, null, valueMap, fourList1.get(0), 4);
}
}
}
if (retBoard == null || retBoard.size() == 0) {
return retBoard;
}
retBoard = listSort(retBoard, valueMap);
//出完牌后在map中去掉牌
ontMap.remove(retBoard);
twoMap.remove(retBoard);
threeMap.remove(retBoard);
return retBoard;
}
至此人机斗地主游戏已完成,玩家可在代码中通过main方法运行,体验游戏的乐趣,在写代码中找到快乐,over~