JAVA 单张牌出牌逻辑 实现斗地主
- Card类
public class Card {
Integer degree;
String color;
public Card(Integer degree, String color) {
this.degree = degree;
this.color = color;
}
public Card() {
}
}
- Player类
import java.util.ArrayList;
public class Player {
String name;
ArrayList<Card> cards;
}
- Landlord类 (main)
import java.util.*;
public class Landlord {
ArrayList<Player> Players = new ArrayList<Player>();
HashMap<Integer, ArrayList<Card>> cards = new HashMap<Integer, ArrayList<Card>>();
String[] colors = "♥-♠-♦-♣".split("-");
String[] nums = "3-4-5-6-7-8-9-10-J-Q-K-A-2".split("-");
public Landlord() {
Player player1 = new Player();
player1.name = "农民1";
player1.cards = new ArrayList<>();
Players.add(player1);
Player player2 = new Player();
player2.name = "农民2";
player2.cards = new ArrayList<>();
Players.add(player2);
Player player3 = new Player();
player3.name = "农民3";
player3.cards = new ArrayList<>();
Players.add(player3);
Player surplus = new Player();
surplus.name = "底牌";
surplus.cards = new ArrayList<>();
Players.add(surplus);
}
public void setCards() {
Integer index = 1;
for (String num : nums) {
ArrayList<Card> colours = new ArrayList<Card>();
for (String color : colors) {
colours.add(new Card(index, num + color));
}
cards.put(index++, colours);
}
ArrayList<Card> colours = new ArrayList<Card>();
colours.add(new Card(index, "小王"));
cards.put(index++, colours);
colours = new ArrayList<Card>();
colours.add(new Card(index, "大王"));
cards.put(index, colours);
int i = 1;
}
public void sortCards() {
for (int i = 0; i < 4; i++) {
ArrayList<Card> cards = new ArrayList<>();
for (Card c : Players.get(i).cards) {
insertSort(cards, c);
}
Players.get(i).cards = cards;
}
}
public void insertSort(ArrayList<Card> cards, Card card) {
if (cards.isEmpty()) {
cards.add(card);
return;
}
for (int i = 0; i < cards.size(); i++) {
if (card.degree < cards.get(i).degree) {
cards.add(i, card);
return;
}
}
cards.add(card);
return;
}
public void distributeCards() {
Random random = new Random();
HashMap<Integer, ArrayList<Card>> cardsClone = (HashMap<Integer, ArrayList<Card>>) cards.clone();
int p;
for (p = 0; p < 3; p++) {
for (int i = 1; i <= 17; i++) {
List<Integer> keysAsArray = new ArrayList<Integer>(cardsClone.keySet());
int i1 = keysAsArray.get(random.nextInt(keysAsArray.size()));
ArrayList<Card> colorClone = cardsClone.get(i1);
int i2 = random.nextInt(colorClone.size());
Players.get(p).cards.add(colorClone.get(i2));
colorClone.remove(i2);
if (colorClone.isEmpty()) {
cardsClone.remove(i1);
}
}
}
for (int i = 1; i <= 3; i++) {
List<Integer> keysAsArray = new ArrayList<Integer>(cardsClone.keySet());
int i1 = keysAsArray.get(random.nextInt(keysAsArray.size()));
ArrayList<Card> colorClone = cardsClone.get(i1);
int i2 = random.nextInt(colorClone.size());
Players.get(p).cards.add(colorClone.get(i2));
colorClone.remove(i2);
if (colorClone.isEmpty()) {
cardsClone.remove(i1);
}
}
int test = 0;
}
public void showCards() {
System.out.println("\n**********手牌展示**********");
int i;
for (i = 0; i < 3; i++) {
System.out.print("玩家" + (i + 1) + ":");
for (Card c : Players.get(i).cards) {
System.out.print(c.color + " ");
}
System.out.println();
}
System.out.print("底牌" + ":");
for (Card c : Players.get(i).cards) {
System.out.print(c.color + " ");
}
System.out.println("\n*************************");
}
public void dealOneCard() {
Random random = new Random();
Player lord = Players.get(0);
Player player2 = Players.get(1);
Player player3 = Players.get(2);
for (int i = 0; i < 3; i++) {
lord.cards.add(Players.get(3).cards.get(i));
}
Players.get(3).cards.clear();
showCards();
int flag = 0;
Card tempCard = new Card();
while (true) {
if (flag == 0) {
int k = random.nextInt(lord.cards.size());
tempCard = lord.cards.get(k);
System.out.println("地主:一个" + tempCard.color);
lord.cards.remove(k);
} else {
for (int j = 0; j < lord.cards.size(); j++) {
if (lord.cards.get(j).degree > tempCard.degree) {
System.out.println("地主:看招,一个" + lord.cards.get(j).color);
tempCard = lord.cards.get(j);
lord.cards.remove(j);
flag = 0;
break;
}
}
}
if (flag != 0) {
System.out.println("地主:要不起");
switch (flag) {
case 2:
int t = random.nextInt(player3.cards.size());
tempCard = player3.cards.get(t);
System.out.println(player3.name + ":一个" + tempCard.color);
player3.cards.remove(t);
break;
case 3:
int s = random.nextInt(player2.cards.size());
tempCard = player2.cards.get(s);
System.out.println(player2.name + ":一个" + tempCard.color);
player2.cards.remove(s);
break;
}
} else {
for (int j = 0; j < player2.cards.size(); j++) {
if (player2.cards.get(j).degree > tempCard.degree) {
System.out.println(player2.name + ":管上! " + player2.cards.get(j).color);
tempCard = player2.cards.get(j);
player2.cards.remove(j);
flag = 2;
break;
}
}
for (int j = 0; j < player3.cards.size() && flag == 0; j++) {
if (player3.cards.get(j).degree > tempCard.degree) {
System.out.println(player3.name + ":大你! " + player3.cards.get(j).color);
tempCard = player3.cards.get(j);
player3.cards.remove(j);
flag = 3;
break;
}
}
}
if (flag == 0) {
System.out.println("农民们:pass...");
}
if (lord.cards.isEmpty()) {
System.out.println("地主获胜!");
showCards();
return;
}
if (player2.cards.isEmpty() || player3.cards.isEmpty()) {
System.out.println("农民获胜!");
showCards();
return;
}
}
}
public static void main(String[] args) {
Landlord landlord = new Landlord();
landlord.setCards();
landlord.distributeCards();
landlord.sortCards();
landlord.showCards();
landlord.dealOneCard();
}
}
应付下作业还是可以的,【狗头】