需求:https://www.imooc.com/video/5475
扑克类
package com.cuihongming;
/**
*
* @author cui
*
*/
public class Poker implements Comparable<Poker>{
public static final String[] des = {"方片","梅花","黑桃","红桃"};
public static final String[] num = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
private String design;
private String number;
public Poker(String design, String number) {
super();
this.number = number;
this.design = design;
}
public String getNumber() {
return number;
}
public String getDesign() {
return design;
}
/**(非 Javadoc)
* @see java.lang.Comparable#compareTo(java.lang.Object)
* 扑克牌比较比较大小,若该扑克大于扑克o返回1,小于返回-1,一样大小返回
*
*/
@Override
public int compareTo(Poker o) {
// TODO 自动生成的方法存根
MyArrayList<String> ListDesigns = new MyArrayList<String>(num);
int Index1 = ListDesigns.indexOf(number);
int Index2 = ListDesigns.indexOf(o.number);
if(Index1 == Index2) {
MyArrayList<String> ListNumber = new MyArrayList<String>(des);
Index1 = ListNumber.indexOf(design);
Index2 = ListNumber.indexOf(o.design);
if(Index1 == Index2) {
return 0;
}else if(Index1 > Index2) {
return 1;
}else if(Index1 < Index2) {
return -1;
}
}else if(Index1 > Index2) {
return 1;
}
return -1;
}
}
牌堆类
package com.cuihongming;
import java.util.ArrayList;
public class Pokers {
public ArrayList<Poker> pokers;
public Pokers() {
pokers = new ArrayList<Poker>();
for(String d:Poker.des) {
for(String n:Poker.num) {
pokers.add(new Poker(d,n));
}
}
}
public void print() {
String str = "[";
for(int i = 0;i < pokers.size();i++) {
if(i == 0) {
str += (pokers.get(i).getDesign()+pokers.get(i).getNumber());
continue;
}
str += (","+pokers.get(i).getDesign()+pokers.get(i).getNumber());
}
str += "]";
System.out.println(str);
}
public void shuffle() {
int random;
ArrayList<Poker> tmpPokers = new ArrayList<Poker>();
do {
random = (int)(Math.random()*(pokers.size()));
tmpPokers.add(pokers.remove(random));
}while(!pokers.isEmpty());
for(Poker p:tmpPokers) {
pokers.add(p);
}
}
public void sendCards(Player player) {
player.addPoker(pokers.remove(0));
}
public void sendCards(int num,Player player1,Player player2) {
for(int i = 0;i < num;i++) {
sendCards(player1);
System.out.println("玩家:"+player1.getName()+"-拿牌");
sendCards(player2);
System.out.println("玩家:"+player2.getName()+"-拿牌");
}
}
public Player starGame(Player player1,Player player2) {
Poker MaxPoker_1 = player1.theMaxPoker();
System.out.println("玩家:"+player1.getName()+"最大的手牌为:"
+MaxPoker_1.getDesign()+MaxPoker_1.getNumber());
Poker MaxPoker_2 = player2.theMaxPoker();
System.out.println("玩家:"+player2.getName()+"最大的手牌为:"
+MaxPoker_2.getDesign()+MaxPoker_2.getNumber());
if(MaxPoker_1.compareTo(MaxPoker_2) == 1)
return player1;
return player2;
}
}
玩家类
package com.cuihongming;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class Player {
private int id;
private String name;
private ArrayList<Poker> pokers;
private static int PlayersNum = 0;
private static ArrayList<Integer> IDs = new ArrayList<Integer>();
public Player() {
super();
pokers = new ArrayList<Poker>();
setPlayersNum(getPlayersNum() + 1);
}
public int getId() {
return id;
}
public void setId(int id) throws ErrorInputException {
this.id = id;
addID(id);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void addPoker(Poker poker) {
pokers.add(poker);
}
@SuppressWarnings("resource")
public static Player makePlayer() {
Player player = new Player();
System.out.println("请输入第"+Player.getPlayersNum()+"位玩家的ID和姓名:");
Scanner in = new Scanner(System.in);
while(true) {
System.out.println("请输入ID:");
try {
player.setId(in.nextInt());
}catch(NoSuchElementException e) {
System.out.println("请输入整数类型的ID!");
in.next();
continue;
}catch(ErrorInputException e) {
System.out.println("该ID被占用请重新输入!");
continue;
}
break;
}
System.out.println("输入姓名:");
player.setName(in.next());
return player;
}
public static int getPlayersNum() {
return PlayersNum;
}
private static void setPlayersNum(int playersNum) {
PlayersNum = playersNum;
}
public Poker theMaxPoker() {
Poker biggest = pokers.get(0);
for(Poker p:pokers) {
if(biggest.compareTo(p) == -1) {
biggest = p;
}
}
return biggest;
}
public void PokersPrint() {
String str = getName()+":[";
for(int i = 0;i < pokers.size();i++) {
if(i == 0) {
str += (pokers.get(i).getDesign()+pokers.get(i).getNumber());
continue;
}
str += (","+pokers.get(i).getDesign()+pokers.get(i).getNumber());
}
str += "]";
System.out.println(str);
}
public static void addID(int id) throws ErrorInputException{
if(IDs.contains(id)) {
throw new ErrorInputException("玩家ID被占用");
}
IDs.add(id);
}
}
扩展的数组列表工具类
构造时可用数组初始化
package com.cuihongming;
import java.util.ArrayList;
public class MyArrayList<E> extends ArrayList<E> {
private static final long serialVersionUID = 125204210876761836L;
public MyArrayList(E[] e1){
super();
for(E e : e1) {
this.add(e);
}
}
}
自定义的错误||非法输入异常类
package com.cuihongming;
import java.util.ArrayList;
public class MyArrayList<E> extends ArrayList<E> {
/**
*
*/
private static final long serialVersionUID = 125204210876761836L;
public MyArrayList(E[] e1){
super();
for(E e:e1) {
this.add(e);
}
}
}
游戏类
package com.cuihongming;
import java.util.ArrayList;
public class Game {
public static void main(String[] args) {
// TODO 自动生成的方法存根
System.out.println("----------创建扑克牌----------");
Pokers pokers = new Pokers();
System.out.println("----------牌创建成功----------");
pokers.print();
System.out.println("----------开始洗牌-----------");
pokers.shuffle();
System.out.println("----------洗牌结束----------");
pokers.print();
System.out.println("----------创建玩家----------");
Player player1 = Player.makePlayer();
Player player2 = Player.makePlayer();
System.out.println("----------欢迎玩家:"+player1.getName());
System.out.println("----------欢迎玩家:"+player2.getName());
System.out.println("----------开始发牌----------");
pokers.sendCards(8, player1, player2);
System.out.println("----------发牌结束----------");
System.out.println("----------开始游戏----------");
Player winner = pokers.starGame(player1, player2);
System.out.println("----------玩家:"+winner.getName()+"获胜!----------");;
System.out.println("玩家各自的手牌为:");
player1.PokersPrint();
player2.PokersPrint();
}
}
点击查看更多内容
1人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦