只是把大体的功能实现出来,没有封装,没有抛异常及处理异常 所以仅供参考 而且我这小新手的很多规范是糊涂的,所以只是提供个思路
一共三个类,接下来是贴代码时间
class Player:
package com.imooc.ruige;
import java.util.ArrayList;
import java.util.List;
public class Player {
int id;
String name;
List<Pocker> PockerList= new ArrayList<>();
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;
}
}
class Pocker:
package com.imooc.ruige;
import java.util.Arrays;
public class Pocker implements Comparable<Pocker> {
static String [] suits = {"spade","heart","diamond","club"};
static String [] values ={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
String value;
String suit;
public Pocker(String suit,String value){
this.suit= suit;
this.value= value;
}
@Override
public String toString() {
return "Pocker [value=" + value + ", suit=" + suit + "]";
}
@Override
public int compareTo(Pocker pocker) {
// TODO Auto-generated method stub
//result1 正数大 result2负数大
int result2= Arrays.asList(suits).indexOf(this.suit)-Arrays.asList(suits).indexOf(pocker.suit);
int result1= Arrays.asList(values).indexOf(this.value)-Arrays.asList(values).indexOf(pocker.value);
if(result1<0){
return -1;
}else if(result1>0){
return 1;
}else {
if(result2>0){
return -1;
}else if(result2<0){
return 1;
}else{
return 0;
}
}
}
}
class PockerMain:
package com.imooc.ruige;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class PockerMain {
static int J=11,Q=12,K=13,A=1;
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Pocker> TotalPocker =new ArrayList<>();
Scanner in = new Scanner(System.in);
for (String suit:Pocker.suits)//创建一副扑克牌
{
for(String value:Pocker.values){
Pocker pocker = new Pocker(suit, value);
TotalPocker.add(pocker);
}
}
// System.out.println("---------随机前------------------------");
// for(Pocker pocker:TotalPocker){
// System.out.println(pocker);
// }
// System.out.println(TotalPocker.size());
//接下来把扑克牌打乱 定义一个索引,此处使用ID:winxp0621这位同学找到的打乱方法
Random random = new Random();
int index=TotalPocker.size()-1;//51
for(int i =0;i<=index-1;i++){//0~51 52个元素要交换51次
int count =index-i;
// System.out.println(count);
int randomIndex = random.nextInt(count);
Collections.swap(TotalPocker, randomIndex, count);
}
// System.out.println(TotalPocker.get(TotalPocker.size()-1).suit);
// System.out.println("---------随机后------------------------");
// for(Pocker pocker:TotalPocker){
// System.out.println(pocker);
// }
// 创建俩玩家
Player playerA = new Player();
Player playerB = new Player();
Pocker[] pockerA ={TotalPocker.get(0),TotalPocker.get(2)};
playerA.PockerList.addAll(Arrays.asList(pockerA));
Pocker[] pockerB ={TotalPocker.get(1),TotalPocker.get(3)};
playerB.PockerList.addAll(Arrays.asList(pockerB));
System.out.println("输入玩家A的id:");
playerA.id=in.nextInt();
System.out.println("输入玩家A的姓名:");
playerA.name=in.next();
System.out.println("---------------A的信息输入成功啦------------");
System.out.println("输入玩家B的id:");
playerB.id=in.nextInt();
System.out.println("输入玩家B的姓名:");
playerB.name=in.next();
System.out.println("-----------B的信息输入成功啦------------");
Collections.sort(playerA.PockerList);//自然排序当然是1 2 3 ....
Collections.sort(playerB.PockerList);
System.out.println(playerA.name+" 的 手牌");
for(Pocker pocker:playerA.PockerList){
System.out.println(pocker);
}
System.out.println(playerB.name+"B 的 手牌");
for(Pocker pocker:playerB.PockerList){
System.out.println(pocker);
}
//
List<Pocker> endP= new ArrayList<Pocker>();
endP.add(playerA.PockerList.get(1));
endP.add(playerB.PockerList.get(1));
Collections.sort(endP);
System.out.println("最大牌是:"+endP.get(1));
if(endP.get(0).compareTo(endP.get(1))==0)
System.out.println("凑巧,两者同牌");
else if(endP.get(1).value.equals(playerA.PockerList.get(1).value)){
System.out.println("赢家是"+playerA.name);
}else{
System.out.println("赢家是"+playerB.name);
}
}
}
嗯,贴代码结束。写了好久。。。。运行结果
热门评论
厉害 谢谢大神!!!