package fapaigaijin;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ChuangRen {//创建人
public List players;
public int num=1;
public ChuangRen(){
this.players=new ArrayList<Player>();
}
public void chuangjianren(){
System.out.println("创建玩家");
while(true){
try{
System.out.println("请输入玩家个数:");
Scanner input1=new Scanner(System.in);
int nums=input1.nextInt();
while(num<=nums){
System.out.println("请输入第"+num+"位玩家的id:");
Scanner input2=new Scanner(System.in);
Integer id=input2.nextInt();
System.out.println("请输入第"+num+"位玩家的姓名:");
Scanner input3=new Scanner(System.in);
String name=input3.next();
players.add(new Player(id,name));
num=num+1;
}
for (Object object : players) {
Player player=(Player)object;
System.out.println("欢迎玩家"+player.id+":"+player.name);
}
break;
}catch(Exception e){
System.out.println("请输入准确的数字命令!!");
continue;
}
}
}
}
package fapaigaijin;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ChuangPai {//创建扑克牌
public List pkps;
public ChuangPai(){
this.pkps=new ArrayList<PuKePai>();
}
public void chuangjianpai(){
String[]fcolours={"方片","梅花","红桃","黑桃"};
Integer[]fcolzhis={1,2,3,4};
String[]dianshus={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
Integer[]zhis={2,3,4,5,6,7,8,9,10,11,12,13,14};
for(int i=0;i<4;i++){
String fcolour=fcolours[i];
Integer fcolzhi=fcolzhis[i];
for(int j=0;j<13;j++){
pkps.add(new PuKePai(fcolour,fcolzhi,dianshus[j],zhis[j]));
}
}
System.out.println("扑克牌创建成功!");
System.out.print("为:");
for (Object i:pkps) {
PuKePai pai=(PuKePai)i;
System.out.print(pai.fcolour+pai.dianshu+";");
}
System.out.println("");
}
}
package fapaigaijin;
import java.util.ArrayList;
import java.util.List;
public class Player {//玩家类
public Integer id;
public String name;
public List<PuKePai> shoupais;
public Player(Integer id,String name){
this.id=id;
this.name=name;
this.shoupais=new ArrayList<PuKePai>();
}
}
package fapaigaijin;
public class ZuHe implements Comparable<ZuHe>{//组合类,用来存放每个玩家的信息及他的最大手牌
public PuKePai pai;
public Player play;
public ZuHe(PuKePai pai,Player play){
this.pai=pai;
this.play=play;
}
@Override
public int compareTo(ZuHe o) {
// TODO 自动生成的方法存根
if(this.pai.zhi==o.pai.zhi){
return this.pai.fcolzhi.compareTo(o.pai.fcolzhi);
}else{
return this.pai.zhi.compareTo(o.pai.zhi);
}
}
}
package fapaigaijin;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
//主程序
public class Test {
public static void main(String[] args) {
ChuangPai cp=new ChuangPai();
cp.chuangjianpai();
System.out.println("开始洗牌");
Collections.shuffle(cp.pkps);
System.out.println("洗牌结束");
ChuangRen cr=new ChuangRen();
cr.chuangjianren();
System.out.println("开始发牌");
System.out.println("请输入每个人发几张牌");
Scanner input=new Scanner(System.in);
int paishu=input.nextInt();
int renshu=cr.num-1;
for(int i=1;i<=paishu*renshu;i++){
if((i%renshu)==0){
((Player)(cr.players.get(renshu-1))).shoupais.add((PuKePai)(cp.pkps.get(i-1)));
System.out.println("玩家"+((Player)(cr.players.get(renshu-1))).name+"拿牌");
}else{
((Player)(cr.players.get((i%renshu)-1))).shoupais.add((PuKePai)(cp.pkps.get(i-1)));
System.out.println("玩家"+((Player)(cr.players.get((i%renshu)-1))).name+"拿牌");
}
}
System.out.println("发牌结束");
System.out.println("开始游戏");
List<ZuHe>maxPaiheren=new ArrayList<ZuHe>();
for(int a=1;a<=renshu;a++){
Collections.sort(((Player)(cr.players.get(a-1))).shoupais);
ZuHe zh=new ZuHe(((PuKePai)((Player)(cr.players.get(a-1))).shoupais.get(paishu-1)),((Player)(cr.players.get(a-1))));
maxPaiheren.add(zh);
}
Collections.sort(maxPaiheren);
System.out.println(maxPaiheren.get(renshu-1).play.name+"获胜");
System.out.println("玩家手里的牌为:");
for(int a=1;a<=renshu;a++){
System.out.print(((Player)(cr.players.get(a-1))).name+":[");
for (Object obj :((Player)(cr.players.get(a-1))).shoupais ) {
PuKePai p=(PuKePai)obj;
System.out.print(p.fcolour+p.dianshu);
System.out.print(",");
}
System.out.print("]");
System.out.println("");
}
}
}
package fapaigaijin;
public class PuKePai implements Comparable<PuKePai>{//牌类
public String fcolour;
public Integer fcolzhi;
public String dianshu;
public Integer zhi;
public PuKePai(){
}
public PuKePai(String fcolour,Integer fcolzhi,String dianshu,Integer zhi){
this.fcolour=fcolour;
this.fcolzhi=fcolzhi;
this.dianshu=dianshu;
this.zhi=zhi;
}
@Override
public int compareTo(PuKePai o) {
if(this.zhi==o.zhi){
return this.fcolzhi.compareTo(o.fcolzhi);
}else{
return this.zhi.compareTo(o.zhi);
}
}
}
这是小虾米学习完之后自己试试,请大侠看看有什么不合适地方,有更好的方法!欢迎虾米一起讨论学习JAVA!
点击查看更多内容
3人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦