为了账号安全,请及时绑定邮箱和手机立即绑定

交作业!!!!!

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

import java.util.List;

import java.util.Scanner;

public class PokerGameTest {

public static List<Poker> pokerList;

public Scanner input;

public PokerGameTest() {

this.pokerList=new ArrayList<Poker>();

Scanner input=new Scanner(System.in);

}

public void PokerAdd() {

//创建牌类

Poker[] poker=new Poker[52];

String[]color= {"黑桃","红桃","梅花","方片"};

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

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

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

poker[(i*13+j)]=new Poker(color[i],number[j]);

}

}

pokerList.addAll(Arrays.asList(poker));

}

public void Collections() {

//乱序

Collections.shuffle(pokerList);

}

public void ForEach() {

//遍历乱序后的pokerList

for (Object obj : pokerList) {

Poker p=(Poker)obj;

System.out.print(p.getColor()+p.getNumber()+",");

}

}

public void create() {

//创建2名玩家

Player p1=new Player();

Player p2=new Player();

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

try {

Scanner input=new Scanner(System.in);

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

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

int id=input.nextInt();

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

  p1.setName(input.next());

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

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

int id1=input.nextInt();

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

  p2.setName(input.next());

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

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

}catch(Exception e) {

//用try catch捕捉 输入类型不同的异常

System.out.println("请输入整数类型的ID!");

create();

}

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

for(int i=0;i<2;i++) {

//创建第一位玩家手上的手牌交替发牌

Poker po1=pokerList.get(2*i);

p1.handpoker.add(po1);

}

for(int i=0;i<2;i++) {

//创建第二位玩家手上的手牌交替发牌

Poker po2=pokerList.get(2*i+1);

p2.handpoker.add(po2);

}

for(int i=0;i<2;i++) {

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

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

}

Collections.sort(p1.handpoker);//给第一位玩家手上的牌 排序

Collections.sort(p2.handpoker);//给第二位玩家手上的牌 排序

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

System.out.println("-----开始游戏---------------");

System.out.println("玩家:"+p1.getName()+"最大的牌是:"+Collections.max(p1.handpoker).getColor()+

Collections.max(p1.handpoker).getNumber());

System.out.println("玩家:"+p2.getName()+"最大的牌是:"+Collections.max(p2.handpoker).getColor()+

Collections.max(p2.handpoker).getNumber());

//用 compareTo来比较 玩家1和玩家2手上牌的大小

int r=Collections.max(p1.handpoker).compareTo(Collections.max(p2.handpoker));

switch(r) {

case 1:

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

break;

case -1:

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

break;

}

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

System.out.print(p1.getName()+":");

for (Poker p : p1.handpoker) {

System.out.print(p.getColor()+p.getNumber()+",");

}

System.out.println();

System.out.print(p2.getName()+":");

for (Poker p : p2.handpoker) {

System.out.print(p.getColor()+p.getNumber()+",");

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

PokerGameTest pgt=new PokerGameTest();

System.out.println("----------创建扑克牌--黑桃>红桃>梅花>方片------------");

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

pgt.PokerAdd();

pgt.ForEach();

System.out.println();

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

pgt.Collections();

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

pgt.create();

}


}


import java.util.Comparator;


public class Poker implements Comparable<Poker> {

private String number;

private String color;

private int colorvalue;

private int numbervalue;


public int getColorvalue() {

return colorvalue;

}

public void setColorvalue(int colorvalue) {

this.colorvalue = colorvalue;

}

public int getNumbervalue() {

return numbervalue;

}

public void setNumbervalue(int numbervalue) {

this.numbervalue = numbervalue;

}

public String getNumber() {

return number;

}

public void setNumber(String number) {

this.number = number;

}

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

public Poker() {

}

public Poker(String color,String number) {

this.color=color;

this.number=number;

if(color=="黑桃") {

colorvalue=4;

}else if(color=="红桃") {

colorvalue=3;

}else if(color=="梅花") {

colorvalue=2;

}else if(color=="方片") {

colorvalue=1;

}

  if(number=="J") {

numbervalue=11;

}

else if(number=="Q") {

numbervalue=12;

}

else if(number=="K") {

numbervalue=13;

}

else if(number=="A") {

numbervalue=14;

}

else {

this.numbervalue=Integer.valueOf(number);

// 把String转换成int可以做比较大小

}

}

@Override

public int compareTo(Poker o) {

int value=0;

if(this.numbervalue>o.numbervalue) {

value=1;

}else if(this.numbervalue<o.numbervalue) {

value=-1;

}else if(this.numbervalue==o.numbervalue) {

if(this.colorvalue>o.colorvalue) {

value=1;

}

else if(this.colorvalue<o.colorvalue) {

value=-1;

}

else {

value=0;

}

return value;

}


}


import java.util.ArrayList;

import java.util.HashSet;

import java.util.List;

import java.util.Set;


public class Player {

private int id;

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;

}

private String name;

  public List<Poker>  handpoker;

  public Player() {

  this.handpoker=new ArrayList<Poker>();

  }

  public Player(int id,String name) {

  this.id=id;

  this.name=name;

  this.handpoker=new ArrayList<Poker>();

  }

}



Poker类中借鉴了别的大神的变量和方法

正在回答

举报

0/150
提交
取消
Java入门第三季
  • 参与学习       409792    人
  • 解答问题       4340    个

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

进入课程

交作业!!!!!

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信