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

请大家多指教

Player类

import java.util.ArrayList;

import java.util.List;


public class Player {

public int ID;

public String name;

public List<Card> playerCard;

public Player(int ID,String name) {

this.ID=ID;

this.name=name;

this.playerCard=new ArrayList<Card>();

}

}


Card类


public class Card implements Comparable<Card>{

public String paihao;

public String huase;

public Integer paihaonum;

public Integer huasenum;

public Card(String paihao,String huase,int huasenum,int paihaonum) {

this.paihao=paihao;

this.huase=huase;

this.huasenum=huasenum;

this.paihaonum=paihaonum;

}

@Override

public String toString() {

return huase+paihao;

}

@Override

public int compareTo(Card o) {  //compareTo可以比较包装类的整形,不能比较基本类型

if (!this.paihao.equals(o.paihao))

return this.paihaonum.compareTo(o.paihaonum);

else 

return this.huasenum.compareTo(o.huasenum);

}

}


Main函数


package CardGame;


import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

import java.util.List;

import java.util.Random;

import java.util.Scanner;




public class main {

public static void main(String[] args) {

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

List<Card>cardList=new ArrayList<Card>();

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

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

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

for(Integer j=0;j<13;j++) {

cardList.add(new Card(paihao[j],huase[i],i,j));

}

}

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

System.out.println("为:"+cardList.toString());

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

Collections.shuffle(cardList);

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

//创建玩家

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

Scanner scan=new Scanner(System.in);

Player player[]=new Player[2];

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

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

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

int id=scan.nextInt();

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

String name=scan.next();

player[i]=new Player(id,name);

}

System.out.println("---欢迎玩家:"+player[0].name);

System.out.println("---欢迎玩家:"+player[1].name);

//给玩家发牌

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

Random random=new Random();

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

player[0].playerCard.add(cardList.get(i));

System.out.println("---玩家"+player[0].name+"拿牌");

player[1].playerCard.add(cardList.get(i+1));

System.out.println("---玩家"+player[1].name+"拿牌");

}

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

//开始比大小

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

Collections.sort(player[0].playerCard);

Collections.sort(player[1].playerCard);

System.out.println("玩家:"+player[0].name+"最大的手牌为:"+player[0].playerCard.get(1).toString());

System.out.println("玩家:"+player[1].name+"最大的手牌为:"+player[1].playerCard.get(1).toString());

Card bigcard[]={player[0].playerCard.get(1),player[1].playerCard.get(1)};

List<Card>bigCard=new ArrayList<Card>();

bigCard.addAll(Arrays.asList(bigcard));

Collections.sort(bigCard);

if (bigCard.get(1).equals(player[0].playerCard.get(1))) {

System.out.println("---------------玩家:"+player[0].name+"获胜-------------------------");

}else {

System.out.println("---------------玩家:"+player[1].name+"获胜-------------------------");

}

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

System.out.println(player[0].name+":"+player[0].playerCard.toString());

System.out.println(player[1].name+":"+player[1].playerCard.toString());

}

}


正在回答

举报

0/150
提交
取消

请大家多指教

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