package test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Card implements Comparable<Card>{
String color;
String size;
static List<Card> cards=new ArrayList<Card>();
public Card(String color,String size){
this.color=color;
this.size=size;
}
/**
* 定义纸牌比较规则
*/
public int compareTo(Card o){
String a="方块梅花黑桃红桃";
String b="345678910JQKA2";
int c=b.indexOf(this.size)-b.indexOf(o.size);
int d=a.indexOf(this.color)-a.indexOf(o.color);
if(c==0){
if(d==0){
return 0;
}else if(d>0){
return 1;
}else {
return -1;
}
}else if(c>0){
return 1;
}else {
return -1;
}
}
/**
* 创建扑克牌对象,加入到cards序列中
*/
public static void createCard(){
String colors[]={"红桃","黑桃","梅花","方块"};
String sizes[]={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
System.out.println("******创建扑克牌******");
for(int i=0;i<colors.length;i++){
for(int j=0;j<sizes.length;j++){
Card a=new Card(colors[i],sizes[j]);
cards.add(a);
}
}
System.out.println("******创建扑克牌成功******");
// System.out.print("为:[");
// for(int i=0;i<cards.size()-1;i++){
// System.out.print(cards.get(i).color+cards.get(i).size+",");
// }
// System.out.println(cards.get(cards.size()-1).color+cards.get(cards.size()-1).size+"]");
String cards1[]=new String[cards.size()];
for(int i=0;i<cards.size();i++){
cards1[i]=cards.get(i).color+cards.get(i).size;
}
System.out.print("为:");
System.out.println(Arrays.toString(cards1));
}
}
package test;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
public class Player {
int ID;
String name;
List<Card> cards;
static List<Player> players=new ArrayList<Player>();
public Player(int ID,String name){
this.ID=ID;
this.name=name;
cards=new ArrayList<Card>();
}
/**
* 创建num个玩家,加入到players序列中
* @param num
*/
public static void createPlayer(int num){
for(int i=1;i<=num;i++){
System.out.println("请输入第"+i+"个玩家的ID和姓名");
int b;
while(true){
try{
Scanner a=new Scanner(System.in);
System.out.println("输入ID:");
b=a.nextInt();
break;
}catch(InputMismatchException c){
System.out.println("请输入整数类型的ID!");
}
}
System.out.println("输入姓名");
Scanner d=new Scanner(System.in);
String c=d.next();
Player p=new Player(b,c);
players.add(p);
}
}
}
package test;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Game {
/**
* 传入Card型的序列a并打乱顺序
* @param a
*/
public void shuffle(List<Card> a){
System.out.println("******开始洗牌******");
Collections.shuffle(a);
System.out.println("******洗牌结束******");
}
/**
* 给序列Player.players中的玩家发牌,每人num张牌
* @param num
*/
public void prepare(int num){
for(Player a:Player.players){
System.out.println("****欢迎玩家"+a.name);
}
System.out.println("******开始发牌******");
int size=Player.players.size();
int i=0;int j=0;
for(int a=0;a<num;a++){
for(;i<size;i++,j++){
Player b=Player.players.get(i);
System.out.println("******"+"玩家:"+b.name+"-拿牌");
b.cards.add(Card.cards.get(j));
}
i=0;
}
System.out.println("******发牌结束!******");
}
/**
* 显示序列Player.players中玩家的各自手牌
* 调用此方法在begin()前后显示不同,begin()方法中有对玩家手牌排序操作的代码
*/
public void playerCardMessage(){
System.out.println("玩家各自的手牌为:");
for(int i=0;i<Player.players.size();i++){
System.out.print(Player.players.get(i).name+":");
int size=Player.players.get(i).cards.size();
String a[]=new String[size];
for(int j=0;j<size;j++){
a[j]=Player.players.get(i).cards.get(j).color+Player.players.get(i).cards.get(j).size;
}
System.out.println(Arrays.toString(a));
}
}
/**
* 开始游戏,对序列Player.players中玩家的手牌排序,输出各自的最大手牌,并按顺序将各自最大手牌加入到cards1序列中
* 将cards1序列中的Card对象和下标添加到cards2映射中
* 对cards1排序,得到序列最后一个card对象,即最大的纸牌
* 根据最大纸牌对象查找cards2中对应的此对象排序前下标,即序列Player.players中玩家的下标
* 输出胜者名字
*/
public void begin(){
System.out.println("******开始游戏******");
List<Card> cards1=new ArrayList<Card>();
Map<Card,Integer> cards2=new HashMap<Card,Integer>();
for(int i=0;i<Player.players.size();i++){
Player a=Player.players.get(i);
Collections.sort(a.cards);
System.out.println("玩家:"+a.name+"最大的手牌为:"+a.cards.get(a.cards.size()-1).color+a.cards.get(a.cards.size()-1).size);
cards1.add(a.cards.get(a.cards.size()-1));
}
for(int j=0;j<cards1.size();j++){
cards2.put(cards1.get(j), j);
}
Collections.sort(cards1);
int winNum=cards2.get(cards1.get(cards1.size()-1));
System.out.println("******玩家:"+Player.players.get(winNum).name+"获胜!******");
}
public static void main(String[]args){
Game one=new Game();
Card.createCard();
one.shuffle(Card.cards);
Player.createPlayer(3);
one.prepare(8);
one.playerCardMessage();
one.begin();
}
}
打开App,阅读手记
热门评论
看不懂比较的方法,楼主可以帮忙解释一下吗