手记

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人推荐
随时随地看视频
慕课网APP