亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

交作業啦!

/*
Poker類
*/

public class Poker implements Comparable<Poker>{

List<Poker> stringList = new ArrayList<Poker>();//有順序的撲克集合

public static List<Poker> stringList1 = new ArrayList<Poker>();// 打亂順序的撲克集合

Random random = new Random();? //隨機數

public static List<Poker> pokerList = new ArrayList<Poker>();? //存儲玩家一的手牌

public static List<Poker> pokerList1 = new ArrayList<Poker>(); //存儲玩家二的手牌

Person person ;

private String color;

private String num;


public Poker() {

}

public Poker(String color,String num) {

this.color = color;

this.num = num;

// this.stringList = new ArrayList<Poker>();

}

public String toString() {

return color+num;

}

public? List<Poker> add() {

String [] colors= {"黑桃","紅桃","梅花","方塊"};

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

for(String color : colors) {

for(String nums : num) {

stringList.add(new Poker(color,nums));

}

}

return stringList;

}

public void showPoker() {

for (Poker string : stringList) {

System.out.print(string+",");

}

}

/*

* 打亂撲克順序,并傳到新的集合中

* */

public void ShuffleTheCards() {

stringList1 = stringList;

Collections.shuffle(stringList1);

for (Poker string : stringList1) {

System.out.print(string+",");

}

}


/*

*發牌

**/

public void Licensing() {

Poker a ;

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

a = stringList1.get(i);

if(i%2==0) {

pokerList.add(a);

System.out.println("-----玩家:"+Person.personList.get(0).getName()+"-拿牌");

}else {

pokerList1.add(a);

System.out.println("-----玩家:"+Person.personList.get(1).getName()+"-拿牌");

}

}

for(Poker string : pokerList1) {

Person.personList.get(0).handCards.add(string);

}

for(Poker string : pokerList) {

Person.personList.get(1).handCards.add(string);

}

}

/*

* 判斷牌面大小

* */

public void Compare() {

Poker p1 = Person.personList.get(0).handCards.get(0);

Poker p2 = Person.personList.get(0).handCards.get(1);

Poker p3 = Person.personList.get(1).handCards.get(0);

Poker p4 = Person.personList.get(1).handCards.get(1);

if(p1.compareTo(p2)>0) {

System.out.println("玩家:"+Person.personList.get(0).getName()+"最大的手牌為:"+p1);

}else{

System.out.println("玩家:"+Person.personList.get(0).getName()+"最大的手牌為:"+p2);

}

if(p3.compareTo(p4)>0) {

System.out.println("玩家:"+Person.personList.get(1).getName()+"最大的手牌為:"+p3);

}else{

System.out.println("玩家:"+Person.personList.get(1).getName()+"最大的手牌為:"+p4);

}

if(p1.compareTo(p2)>0) {

if(p3.compareTo(p4)>0) {

if(p1.compareTo(p3)>0) {

System.out.println("------------"+Person.personList.get(0).getName()+"獲勝!---------------");

}else{

System.out.println("------------"+Person.personList.get(1).getName()+"獲勝!---------------");

}

}else {

if(p1.compareTo(p4)>0) {

System.out.println("------------"+Person.personList.get(0).getName()+"獲勝!---------------");

}else {

System.out.println("------------"+Person.personList.get(1).getName()+"獲勝!---------------");

}

}

}else {

if(p3.compareTo(p4)>0) {

if(p2.compareTo(p3)>0) {

System.out.println("------------"+Person.personList.get(0).getName()+"獲勝!---------------");

}else{

System.out.println("------------"+Person.personList.get(1).getName()+"獲勝!---------------");

}

}else {

if(p2.compareTo(p4)>0) {

System.out.println("------------"+Person.personList.get(0).getName()+"獲勝!---------------");

}else {

System.out.println("------------"+Person.personList.get(1).getName()+"獲勝!---------------");

}

}

}

}

public String getColor() {

return color;

}


public void setColor(String color) {

this.color = color;

}


public String getNum() {

return num;

}


public void setNum(String num) {

this.num = num;

}


/*

* 利用字符串的IndexOf方法獲取某個字符的位置,比較兩個字符位置下標的大小。

* 下標數字大的則牌面大

* */

@Override

public int compareTo(Poker o) {

String colorOrder = "黑桃,紅桃,梅花,方塊";

String numOrder = "3,4,5,6,7,8,9,10,J,Q,K,A,2";

int x = numOrder.indexOf(this.getNum());

int y = numOrder.indexOf(o.getNum());

if(x<y) {

return -1;

}else if(x>y) {

return 1;

}else {

x = colorOrder.indexOf(this.getColor());

y = colorOrder.indexOf(o.getColor());

if(x<y) {

return 1;

}else if(x>y){

return -1;

}

}

return 0;

}

}


/*

Person類

*/

public class Person {

public static List<Person> personList = new ArrayList<Person>();

private Integer id;

private String name;

public List<Poker> handCards;

public Person() {

}

public Person(Integer id,String name) {

this.id = id;

this.name = name;

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

}

public Integer getId() {

return id;

}


public void setId(Integer id) {

this.id = id;

}


public String getName() {

return name;

}


public void setName(String name) {

this.name = name;

}


@SuppressWarnings("resource")

public void creatPlayer(){

int id=0;

String name=null;

while(true) {

try {

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

Scanner sc = new Scanner(System.in);

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

System.out.println("請輸入ID:");

id = sc.nextInt();

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

name = sc.next();

personList.add(new Person(id,name));

}

for(int i=0;i<personList.size();i++) {

System.out.println("------歡迎玩家:"+personList.get(i).getName());

}

break;

}catch(Exception e) {

System.out.println("請輸入整型的數字ID:");

continue;

}

}

}

}

/*

測試類

*/

public class Play {

Random random = new Random();

Scanner sc = new Scanner(System.in);

public static void main(String[] args) throws ClassNotFoundException{

System.out.println("------------創建撲克牌---------------");

Poker pk = new Poker();

pk.add();

System.out.println("------------創建撲克牌成功---------------");

System.out.print("撲克牌有:");

pk.showPoker();

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

pk.ShuffleTheCards();

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

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

Person person = new Person();

try {

person.creatPlayer();

} catch (Exception e) {

e.printStackTrace();

}

System.out.println("------------開始發牌---------------");

pk.Licensing();

System.out.println("------------發牌結束---------------");

System.out.println("------------游戲開始---------------");

pk.Compare();

System.out.println(Person.personList.get(0).getName()+"的手牌為:"+Person.personList.get(0).handCards);

System.out.println(Person.personList.get(1).getName()+"的手牌為:"+Person.personList.get(1).handCards);

????}

}

正在回答

舉報

0/150
提交
取消
Java入門第三季
  • 參與學習       409767    人
  • 解答問題       4543    個

Java中你必須懂得常用技能,不容錯過的精彩,快來加入吧

進入課程

交作業啦!

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號