继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Java入门第三季综合实战——简易扑克牌游戏

不告诉你哈
关注TA
已关注
手记 4
粉丝 1
获赞 4

Player类
import java.util.ArrayList;

import com.poker.Poker;

public class Player {
private int id;
private String name;
private ArrayList<Poker> al;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public ArrayList<Poker> getAl() {
    return al;
}

public void setAl(ArrayList<Poker> al) {
    this.al = al;
}

public Player() {

}

public Player(int id, String name) {
    this.id = id;
    this.name = name;
    this.al = new ArrayList<Poker>();
}

}

Poker类
public class Poker implements Comparable<Poker> {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(Poker o) {
return this.id-o.id;
}
public Poker(){

}
public Poker(int id,String name){
    this.id=id;
    this.name=name;
}

}

实现类
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;

import com.player.Player;
import com.poker.Poker;

public class Go {
/**

  • 创建扑克牌
  • @param hSet
  • 52张扑克牌列表
    */
    public void createPuker(HashSet<Poker> hSet) {
    // ArrayList<Puker> arrayList=new ArrayList<Puker>();
    String lin = "";
    int num = 100;
    Poker puker;
    String[] lin2 = new String[] { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
    System.out.println("------------------创建扑克牌--------------------------");
    System.out.println("------------------扑克牌创建成功!!--------------------------");
    System.out.println("扑克牌为:");
    for (int i = 0; i < 52; i++) {
    if (i < 13) {
    lin = "方片";
    puker = new Poker(num, lin + lin2[i]);
    hSet.add(puker);
    num += 10;
    if (i == 0) {
    System.out.print("[" + puker.getName());
    } else {
    System.out.print("," + puker.getName());
    }
    }
    if (i >= 13 && i < 26) {
    if (i == 13) {
    num = 102;
    }
    lin = "梅花";
    puker = new Poker(num, lin + lin2[i - 13]);
    hSet.add(puker);
    num += 10;
    System.out.print("," + puker.getName());
    }
    if (i >= 26 && i < 39) {
    if (i == 26) {
    num = 104;
    }
    lin = "红心";
    puker = new Poker(num, lin + lin2[i - 26]);
    hSet.add(puker);
    num += 10;
    System.out.print("," + puker.getName());
    }
    if (i >= 39 && i < 52) {
    if (i == 39) {
    num = 104;
    }
    lin = "黑桃";
    puker = new Poker(num, lin + lin2[i - 39]);
    hSet.add(puker);
    num += 10;
    if (i == 51) {
    System.out.println("," + puker.getName() + "]");
    } else {
    System.out.print("," + puker.getName());
    }
    }
    }
    System.out.println("------------------开始洗牌...--------------------------");
    System.out.println("------------------洗牌结束!--------------------------");
    // System.out.println("扑克牌有:"+hSet.size()+"张");
    // for(Puker pk:hSet){
    // System.out.println("扑克牌有:"+pk.getName());
    // }
    }

    /**

  • 创建玩家
  • @param players
  • 玩家列表
    */
    public void createPlayer(ArrayList<Player> players) {
    System.out.println("------------------创建玩家...--------------------------");
    int playNum = 0;// 玩家人数
    Scanner scanner = new Scanner(System.in);
    while (playNum < 2) {
    System.out.println("请输入第" + (playNum + 1) + "位玩家的ID和姓名:");
    System.out.println("输入ID:");
    int id = 0;
    try {
    id = scanner.nextInt();
    } catch (Exception e) {
    System.out.println("请输入整数类型ID!");
    scanner.nextLine();
    continue;
    }
    System.out.println("输入姓名:");
    String name = scanner.next();
    players.add(new Player(id, name));
    playNum++;
    }
    // 遍历玩家名字
    for (Player player : players) {
    System.out.println("----欢迎玩家:" + player.getName());
    }
    }

    /**

  • 发牌给玩家
  • @param pokers
  • 52张扑克牌
  • @param players
  • 玩家列表
    */
    public void givePoker(HashSet<Poker> pokers, ArrayList<Player> players) {
    System.out.println("------------------开始发牌...--------------------------");
    int num = 2;// 派发次数
    for (Poker poker : pokers) {
    if (num % 2 == 0) {
    players.get(0).getAl().add(poker);

        } else {
            players.get(1).getAl().add(poker);
        }
        num++;
        if (num == 6) {
            break;
        }
    }
    System.out.println("------------------发牌结束!--------------------------");

    }

    /**

  • 开始游戏
  • @param players
  • 玩家列表
    */
    public void playGame(ArrayList<Player> players) {
    System.out.println("------------------开始游戏...--------------------------");
    for (Player player : players) {
    ArrayList<Poker> aList = player.getAl();
    Collections.sort(aList);
    System.out.println(
    "玩家:" + player.getName() + "最大的手牌为:" + player.getAl().get(player.getAl().size() - 1).getName());
    }
    if (players.get(0).getAl().get(players.get(0).getAl().size() - 1).getId() > players.get(1).getAl()
    .get(players.get(1).getAl().size() - 1).getId()) {
    System.out.println("------------------玩家:" + players.get(0).getName() + "获胜!----------------------");
    } else {
    System.out.println("------------------玩家:" + players.get(1).getName() + "获胜!----------------------");
    }
    }

    /**

  • 展示手牌
  • @param players
  • 玩家列表
    */
    public void showPoker(ArrayList<Player> players) {
    System.out.println("玩家各自的手牌为:");

    for (Player player : players) {
        System.out.print(player.getName() + ":[");
        int s=0;
        for (Poker poker : player.getAl()) {
            System.out.print(poker.getName());
            if(s==0){
                System.out.print(" , ");
                s++;
            }
    
        }
        System.out.print("]" + "\n");
    }

    }

    public static void main(String[] args) {
    Go go = new Go();
    ArrayList<Player> players = new ArrayList<Player>();
    HashSet<Poker> pokers = new HashSet<Poker>();
    go.createPuker(pokers);
    go.createPlayer(players);
    go.givePoker(pokers, players);
    go.playGame(players);
    go.showPoker(players);
    }
    }

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP