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

Java入门第三季—————简易扑克牌游戏

//扑克类
package com.imooc;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.Comparator;import java.util.List;import java.util.Scanner;public class Puke implements Comparator{public String color;public String num;public List<Puke> player1cards;//存放手牌public List<Puke> player2cards;//存放手牌public List<Puke>pukes;public List<Player>players; //玩家列表public Puke(String color,String num){	this.color=color;	this.num=num;	this.pukes=new ArrayList<Puke>();	this.players=new ArrayList<Player>();	this.player1cards=new ArrayList<Puke>();	this.player2cards=new ArrayList<Puke>();}public Puke(){	this.pukes=new ArrayList<Puke>();	this.players=new ArrayList<Player>();}/* *创建扑克牌 */public void creatPuke(){	String color[]={"黑桃","红桃","梅花","方片"};	String num[]={"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++){			Puke puke=new Puke(color[i],num[j]);			pukes.add(puke);		}	}}/* * 创建玩家 */public void creatPlayer(){	Scanner input=new Scanner(System.in);	for(int i=1;i<=2;i++){		System.out.println("请玩家输入ID:");		String id=input.next();		System.out.println("请玩家输入姓名:");		String name=input.next();		Player player=new Player(id,name);		players.add(player);	}	System.out.println("******一共有"+players.size()+"名玩家******");	for(Player player:players){		System.out.println("*********玩家:"+player.name+"**********");	}}/* * 洗牌 */public void rushPuke(){	System.out.println("******洗**牌******");	Collections.shuffle(pukes);}/* * 发牌 */public void sendPuke(){	System.out.println("******洗牌*结束******");	System.out.println("********发**牌********");	Puke a;	for(int i=0;i<4;i++){		a=pukes.get(i);		if(i%2==0){			players.get(0).player1cards.add(a);			System.out.println("*****玩家"+players.get(0).name+"拿牌*****");		}else{			players.get(1).player2cards.add(a);			System.out.println("*****玩家"+players.get(1).name+"拿牌*****");		}		}}/* * 比较大小 */public void comparable(){Puke p1=players.get(0).player1cards.get(0);Puke p2=players.get(0).player1cards.get(1);Puke p3=players.get(1).player2cards.get(0);Puke p4=players.get(1).player2cards.get(1);String max1;String max2;if(p1.num.compareTo(p2.num)>0){	System.out.println("玩家"+players.get(0).name+"最大手牌是:"+p1.num);	max1=p1.num;}else{	System.out.println("玩家"+players.get(0).name+"最大手牌是:"+p2.num);	max1=p2.num;}if(p3.num.compareTo(p4.num)>0){	System.out.println("玩家"+players.get(1).name+"最大手牌是:"+p3.num);	max2=p3.num;}else{	System.out.println("玩家"+players.get(1).name+"最大手牌是:"+p4.num);	max2=p4.num;}if((p1.num.compareTo(p3.num)>0)){	System.out.println("游戏获胜者是玩家:"+players.get(0).name);}if((p2.num.compareTo(p3.num)>0)){	System.out.println("游戏获胜者是玩家:"+players.get(0).name);}}/* * 遍历扑克 */public void forPuke(){	System.out.println("52张扑克如下");	for(Puke puke:pukes){		System.out.println(puke.color+":"+puke.num);	}}public static void main(String[] args) {	Puke pk=new Puke();	pk.creatPuke();	pk.forPuke();	pk.creatPlayer();	pk.rushPuke();	pk.sendPuke();	pk.comparable();}}
//玩家类:
package com.imooc;import java.util.ArrayList;import java.util.List;public class Player {public String id;public String name;public List<Puke> player1cards;//存放手牌public List<Puke> player2cards;//存放手牌public Player(String id,String name){	this.id=id;	this.name=name;	this.player1cards=new ArrayList<Puke>();	this.player2cards=new ArrayList<Puke>();}}
//comparable接口及方法
package com.imooc;import java.util.Comparator;public class PukeCompartor implements Comparator<Puke> {	@Override	public int compare(Puke o1, Puke o2) {		// TODO Auto-generated method stub		return o1.num.compareTo(o2.num);	}


点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消