import java.util.ArrayList;
import java.util.List;
public class player {
public int id;
public String name;
public List<poker> myPo = new ArrayList<poker>();
public player(int id,String name)
{
this.id = id;
this.name = name;
}
}
public class poker implements Comparable<poker> {
public String flower;
public String num;
public Integer k;//定义点数大小
public Integer f;//定义花色大小
public poker(String flower,String num){
this.flower = flower;
this.num = num;
if(num.equals("J"))
{
this.k = 11;
}
else if(num.equals("Q"))
{
this.k = 12;
}
else if(num.equals("K"))
{
this.k = 13;
}
else if(num.equals("A"))
{
this.k = 14;
}
else
{
this.k = Integer.parseInt(num);
}
if(flower.equals("方块"))
{
this.f = 1;
}
else if(flower.equals("梅花"))
{
this.f = 2;
}
else if(flower.equals("红心"))
{
this.f = 3;
}
else
{
this.f = 4;
}
}
public int compareTo(poker arg0){
if(!(this.k==arg0.k))
{
return this.k.compareTo(arg0.k);
}
else
{
return this.f.compareTo(arg0.f);
}
}
public String toString(){
return (this.flower+this.num);
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class PlayGame {
List<poker> allPokers = new ArrayList<poker>();
List<player> players = new ArrayList<player>();
public void createPokers(){
System.out.println("----------创建扑克牌----------");
String[] flowers = {"方块","梅花","红心","黑桃"};
String[] nums = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
for(int i=0;i<4;i++)
{
for(int j=0;j<13;j++)
{
allPokers.add(new poker(flowers[i],nums[j]));
}
}
System.out.println("----------扑克牌创建成功!----------");
System.out.print("为:"+allPokers.toString());
}
public void shuf(){
System.out.println("----------开始洗牌...----------");
Collections.shuffle(allPokers);
System.out.println("----------洗牌结束!----------");
}
public void createPlayers(int num){
System.out.println("----------创建玩家...----------");
Scanner console = new Scanner(System.in);
int id = 0;
boolean isError = true;
for(int i=0;i<num;i++)
{
System.out.println("请输入第"+(i+1)+"位玩家id和姓名:");
while(isError)
{
System.out.println("输入id:");
try{
console = new Scanner(System.in); //所输入的非整型一直在扫描器,因此会陷入死循环
id = console.nextInt();
break;
}catch(Exception e){
System.out.println("请输入整数类型的id!");
// String s = console.next();
}
}
System.out.println("输入姓名:");
String name = console.next();
players.add(new player(id,name));
}
for(player p:players)
{
System.out.println("欢迎玩家:"+p.name);
}
}
public void deal(int numOfPokes){
System.out.println("----------开始发牌--------------");
int j = 0;
for(int i =0;i<numOfPokes;i++)
{
for(player p:players)
{
System.out.println("玩家:"+p.name+"-拿牌");
p.myPo.add(allPokers.get(j++));
}
}
System.out.println("----------发牌结束!----------");
}
public void startGame(){
System.out.println("----------开始游戏----------");
for(player p:players)
{
// Collections.sort(p.myPo);
System.out.println("玩家:"+p.name+
"最大的手牌为:"+Collections.max(p.myPo).toString());
}
}
public void winOrLose(){
player winner = players.get(0);
for(int i=1; i<players.size()-1;i++)
{
poker p = Collections.max(players.get(i).myPo);
if(p.compareTo(Collections.max(winner.myPo))>1)
{
winner = players.get(i);
}
}
System.out.println("----------玩家:"+winner.name+"获胜!----------");
}
public void pokersOfPlayer(){
System.out.println("玩家各自的手牌为:");
for(player p:players)
{
System.out.println(p.name+":"+p.myPo.toString());
}
}
public static void main(String[] args) {
PlayGame pg = new PlayGame();
pg.createPokers();
pg.shuf();
pg.createPlayers(3);
pg.deal(3);
pg.startGame();
pg.winOrLose();
pg.pokersOfPlayer();
}
}