作业提交!请大佬指出优化之处

来源:7-1 简易扑克牌游戏

qq_慕桂英0406520

2018-12-09 17:54


public class Test {

private List<PuKe> pukes   = new ArrayList();

private List<Player> plays = new ArrayList();

private Random     random  = new Random();

private Scanner    scanner = new Scanner(System.in);

public static void main(String[] args) {

// TODO Auto-generated method stub

Test test = new Test();

test.createPuKe();

test.shuffleCards();

test.createPlay();

test.Licensing();

}

//创建扑克

private void createPuKe() {

System.out.println("-----------创建扑克。。。。 -----------");

String[] key = {"♦","♣","♥","♠"};

String[] value = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

for (int i = 0; i < key.length; i++) {

for (int j = 0; j < value.length; j++) {

pukes.add(new PuKe(key[i],value[j]));

}

}

System.out.println("------------创建扑克成功------------");

System.out.println("为"+Arrays.toString(pukes.toArray(new PuKe[pukes.size()])));

}

//洗牌 

private void shuffleCards() {

System.out.println("-----------开始洗牌。。。。-----------");

for (int i = 0; i < 5; i++) {//洗牌五次

for (int j = 0; j < 52; j++) {

int endIndex = random.nextInt(52 - j) + j;//随机交换位置的角标

PuKe start   = pukes.get(j);

PuKe end     = pukes.get(endIndex);

//两者交换位置

pukes.set(j,end);

pukes.set(endIndex,start);

}

}

System.out.println("--------------洗牌结束-------------");

System.out.println("-------------洗牌后顺序------------");

System.out.println("为"+Arrays.toString(pukes.toArray(new PuKe[pukes.size()])));

}

//创建玩家

private void createPlay() {

System.out.println("-------------创建玩家 -------------");

for (int i = 0; i < 2; i++) {//循环创建两位玩家

Player player = new Player();

System.out.println("请输入第"+(i+1)+"位玩家的ID和姓名:");

System.out.println("输入ID:");

player.setId(scanner.next());

System.out.println("输入姓名:");

player.setName(scanner.next());

plays.add(player);

}

for (Player player : plays) {

System.out.println("-----欢迎玩家:"+player.getName());

}

}

//发牌并比较大小

private void Licensing() {

System.out.println("-----------开始发牌。。。。-----------");

int pukeSize = pukes.size();

for (int i = 0; i < 2; i++) {//发牌为两轮

for (Player player : plays) {//每轮每家一张牌

System.out.println("-----玩家:"+player.getName()+"--拿牌");

PuKe puke = pukes.get(random.nextInt(pukeSize));

player.setPuKe(puke);

pukes.remove(puke);

pukeSize--;//发完一张牌就少一张

}

}

System.out.println("--------------发牌结束-------------");

System.out.println("玩家各自的手牌为:");

ArrayList<PuKe> maxPuKe = new ArrayList();//创建两玩家最大牌的集合

for (Player player : plays) {

System.out.println(player.getName()+":"+Arrays.toString(player.getPlayPuKeList().toArray(new PuKe[player.getPlayPuKeList().size()])));

//排序

Collections.sort(player.getPlayPuKeList());

//排序之后打印

// System.out.println(player.getName()+":"+Arrays.toString(player.getPlayPuKeList().toArray(new PuKe[player.getPlayPuKeList().size()])));

//将最大的牌存储

maxPuKe.add(player.getPlayPuKeList().get(1));

}

//两玩家的大牌间的比较

Collections.sort(maxPuKe);

for (Player player : plays) {

if(player.getPlayPuKeList().contains(maxPuKe.get(1))) {

System.out.println("玩家"+player.getName()+"获胜");

}

}

}

}



public class Player {

private String name;

private String id;

private List<PuKe> playPuKe = new ArrayList<>();

public void setPuKe(PuKe pk) {

playPuKe.add(pk);

}

public List<PuKe> getPlayPuKeList(){

return playPuKe;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

}


public class PuKe implements Comparable<PuKe>{

private String key;

private String value;

String[] KEY = {"♦","♣","♥","♠"};

String[] VALUE = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

public PuKe() {

super();

}


public PuKe(String key, String value) {

super();

this.key = key;

this.value = value;

}

public String getKey() {

return key;

}

public void setKey(String key) {

this.key = key;

}

public String getValue() {

return value;

}

public void setValue(String value) {

this.value = value;

}


@Override

public String toString() {

return key+value;

}


@Override

public int compareTo(PuKe o) {

List<String> keys = Arrays.asList(KEY);

List<String> values = Arrays.asList(VALUE);

if(values.indexOf(this.value) == values.indexOf(o.getValue())) {//value相等就需要判断key

if(keys.indexOf(this.key) == keys.indexOf(o.getKey())) {

return 0;

}else {

return keys.indexOf(this.key) - keys.indexOf(o.getKey());

}

}else {

return values.indexOf(this.value) - values.indexOf(o.getValue());

}

}

}


写回答 关注

1回答

  • 慕码人804219
    2018-12-12 15:18:36

    ArrayList后面你好像忘了写泛型了......

Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409792 学习 · 4340 问题

查看课程

相似问题