java实现多人多牌数比较游戏
程序员文章站
2024-03-01 19:28:52
本文实例为大家分享了java实现多人多牌数比较游戏的具体代码,供大家参考,具体内容如下
具体实现步骤如下:
实现扑克card类,用于储存扑克牌,1-13代表点数...
本文实例为大家分享了java实现多人多牌数比较游戏的具体代码,供大家参考,具体内容如下
具体实现步骤如下:
- 实现扑克card类,用于储存扑克牌,1-13代表点数,4-1代表花色(黑桃,红桃,梅花,分块)
- 实现扑克列表cardlist类,用于实现生成一副牌,洗牌,发牌的功能
- 实现玩家player类,用于储存玩家信息
- 实现玩家列表playerlist类,用于储存所有玩家信息,并且实现玩家洗牌、发牌、比较牌大小的方法
- 主方法index05类,用于启动扑克游戏
card.java
/** * */ package dk.card; /** * @author administrator * 扑克类 */ public class card implements comparable<card>{ private int number;//牌的点数--1到13 private int type;//牌的花色--4种花色:♠(黑桃):4 ♥(红桃):3 ♣(梅花):2 方块:1 public card() { super(); } public card(int number, int type){ if(number==1) number=14; this.number = number; this.type = type; } public int getnumber() { return number; } public void setnumber(int number) { this.number = number; } public int gettype() { return type; } public void settype(int type) { this.type = type; } @override public int hashcode() { final int prime = 31; int result = 1; result = prime * result + number; result = prime * result + type; return result; } @override public boolean equals(object obj) { if (this == obj) return true; if (obj == null) return false; if (getclass() != obj.getclass()) return false; card other = (card) obj; if (number != other.number) return false; if (type != other.type) return false; return true; } @override public int compareto(card o) { if(this.number>o.number) return -1; else if(this.number<o.number) return 1; else if(this.type>o.type) return -1; else if(this.type<o.type) return 1; return 0; } public string tostring(){ string str =""; switch(this.type){ case 1: str = "方块";break; case 2: str = "梅花";break; case 3: str = "红桃";break; case 4: str = "黑桃";break; } string n=""; switch(this.number){ case 14:n="a";break; case 11:n="j";break; case 12:n="q";break; case 13:n="k";break; default:n=this.number+""; } return str+n; } }
cardlist.java
package dk.card; import java.util.arraylist; import java.util.list; import java.util.random; /** * @author administrator * 牌表操作类 */ public class cardlist { public static list<card> cardlist = new arraylist<card>(52);//储存扑克牌 public static list<arraylist<card>> poker;//储存发牌结果 //生成一副牌 public static void generatecard() { for(int i=1;i<=13;i++){ for(int j=1;j<=4;j++){ card c = new card(i,j); cardlist.add(c); } } } //打印一副牌 public static void printcardlist(){ system.out.print("["); for(card c:cardlist){ system.out.print(c.tostring()+" "); } system.out.println("]"); } //洗牌 public static void shufflecard(){ list<card> scard = new arraylist<card>(52); card c; random random = new random(); for(int i=1;i<=52;i++){ do{ c = cardlist.get(random.nextint(52)); }while(scard.contains(c)); scard.add(c); } cardlist.clear(); cardlist = scard; printcardlist(); } /** * 发牌 * @param i number of player * @param j number of card * @return */ public static list<arraylist<card>> dealcard(int i,int j){ try{ if(i<=1){ throw new exception("发牌规则有误:玩家至少为2人,请重新定义规则!"); } if(i*j>52){ throw new exception("发牌规则有误:牌数超出扑克张数,请重新定义规则!"); } poker = new arraylist<arraylist<card>>(i);//储存玩家发到的牌 //生成玩家手中的牌列表 for(int h=0;h<i;h++){ arraylist<card> player_card_list = new arraylist<card>(j); poker.add(player_card_list); } //给每个玩家发牌 for(int k=0;k<j;k++){ for(int d=0;d<i;d++){ arraylist<card> player_card_list = poker.get(d); card card = getcard(); player_card_list.add(card); } } }catch(exception e){ e.printstacktrace(); } return poker; } //抓取一张牌 public static card getcard(){ return cardlist.remove(0); } //获取一位玩家的牌 public static arraylist<card> getpoker(){ return poker.remove(0); } public static void clear(){ cardlist.clear(); poker.clear(); printcardlist(); } }
player.java
/** * */ package dk.card; import java.util.arraylist; import java.util.collections; import java.util.list; /** * @author administrator * 玩家类 */ public class player implements comparable<player>{ private string id;//玩家id private string name;//玩家姓名 private list<card> poker;//玩家手牌 private card max;//玩家最大的牌 public player(string id ,string name){ this.id = id; this.name = name; this.setpoker(new arraylist<card>()); } public string getid() { return id; } public void setid(string id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public list<card> getpoker() { return poker; } public card getmax() { return max; } public void setmax(card max) { this.max = max; } public void setpoker(list<card> poker) { this.poker = poker; collections.sort(this.poker); for(card c :this.poker){ this.setmax(c); break; } } public string tostring(){ return "玩家id:"+this.id+"\t玩家姓名:"+this.name; } //玩家摊牌 public string showcard(){ string msg=""; for(card c:this.poker){ msg+=c.tostring()+" "; } return this.tostring()+"\t\n"+"玩家牌信息:\t\n"+msg; } @override public int compareto(player o) { return this.max.compareto(o.getmax()); } }
playerlist.java
package dk.card; import java.util.arraylist; import java.util.collections; import java.util.list; /** * @author administrator * 储存玩家列表 */ public class playerlist { public static list<player> playerlist = new arraylist<player>();//储存扑玩家列表 public static int sum; public static list<string> rules = new arraylist<string>(); public playerlist(){ } public static void setrule(string rule){ rules.add(rule); } public static string getrules(){ string str=""; for(string s:rules){ str+=s+"\t\n"; } return str; } public static void printrules(){ show(getrules()); } //添加玩家 public static boolean addplayer(player player){ show("添加玩家:"+player.tostring()); show("----------------------------------------"); sum++; return playerlist.add(player); } //给所有玩家发牌 public static void shufflecardtoplayer(){ for(player player:playerlist){ player.setpoker(cardlist.getpoker()); } } //显示发牌结果 public static void showcard(){ for(player player:playerlist){ show(player.showcard()); show("----------------------------------------"); } } //比较牌点大小并且显示赢家信息 public static void showwinner(){ collections.sort(playerlist); player p = playerlist.get(0); show(p.showcard()); show("本局赢家是:"+p.getname()); } //玩家人数 public static int size(){ return sum; } public static void show(object obj){ system.out.println(obj.tostring()); } public static void clear(){ playerlist.clear(); sum = 0 ; } }
index05.java
/** * */ package dk.main; import java.util.scanner; import dk.card.cardlist; import dk.card.player; import dk.card.playerlist; /** * @author administrator * */ public class index05 extends show { public static scanner scanner = new scanner(system.in); /** * 调用的主方法 * @param args */ public static void main(string[] args) { show("是否开始游戏?0/1"); int isstart = scanner.nextint(); if(isstart==1){ startgame(); } else{ show("退出游戏!"); } } //启动扑克游戏 public static void startgame(){ show("游戏规则如下:"); playerlist.setrule("1.至少需要两名玩家"); playerlist.setrule("2.所有玩家的牌数加起来不超过52张牌"); playerlist.setrule("3.比较所有玩家手中最大的牌点,最大的牌点数的玩家获胜"); playerlist.setrule("4.总的牌数只有52张,没有大小王"); playerlist.setrule("5.黑桃牌a最大,方块3最小,点数相同时,比较花色,黑桃较大。"); playerlist.printrules(); show("请输入玩家人数:"); int players = 2;//默认玩家数目 int cardofplay = 2;//默认每个玩家的手牌数目 try { players = scanner.nextint(); while(players<2){ show("玩家人数至少是两人"); show("请重新输入人数:"); players = scanner.nextint(); } show("请输入每个玩家持有的牌数:"); cardofplay = scanner.nextint(); while(cardofplay*players>52){ show("玩家*牌数超出范围!请重新输入牌数"); cardofplay = scanner.nextint(); } show("开始输入玩家信息:"); for(int i=0;i<players;i++){ show("请输入第"+(i+1)+"个玩家姓名:"); string name = scanner.next(); player p =new player("00"+i,name); playerlist.addplayer(p); } show("一副没有洗的牌:"); cardlist.generatecard();//生成一副扑克 cardlist.printcardlist();//打印扑克 show("========================================"); show("开始洗牌:"); cardlist.shufflecard();//洗牌 show("========================================"); show("开始发牌:........发牌中......"); cardlist.dealcard(players,cardofplay);//发牌 playerlist.shufflecardtoplayer();//给玩家发牌 show("发牌结果:"); playerlist.showcard();//显示所有玩家牌信息 show("========================================"); show("比较结果:比较牌点大小并且显示赢家信息"); playerlist.showwinner();//显示本轮赢家 show("========================================"); } catch (exception e) { e.printstacktrace(); } } //清空游戏 public static void cleargame(){ cardlist.clear(); playerlist.clear(); } //测试扑克游戏 public static void testgame(){ try { show("一副没有洗的牌:"); cardlist.generatecard(); cardlist.printcardlist(); show("========================================"); show("开始添加玩家:"); player p =new player("001","dekingchen"); player p1 =new player("002","chenzhenquan"); player p2 =new player("003","czq"); playerlist.addplayer(p); playerlist.addplayer(p1); playerlist.addplayer(p2); show("开始洗牌:"); cardlist.shufflecard(); show("========================================"); show("开始发牌:........发牌中......"); int players = playerlist.size();//玩家数目 int cardofsum = 3;//每个玩家的手牌数目 cardlist.dealcard(players,cardofsum); playerlist.shufflecardtoplayer(); show("发牌结果:"); playerlist.showcard(); show("========================================"); show("比较结果:比较牌点大小并且显示赢家信息"); playerlist.showwinner(); show("========================================"); } catch (exception e) { e.printstacktrace(); } } }
show.java(辅助显示类)
package dk.main; public abstract class show { public static void show(object obj){ system.out.println(obj.tostring()); } }
实验结果:
是否开始游戏?0/1 1 游戏规则如下: 1.至少需要两名玩家 2.所有玩家的牌数加起来不超过52张牌 3.比较所有玩家手中最大的牌点,最大的牌点数的玩家获胜 4.总的牌数只有52张,没有大小王 5.黑桃牌a最大,方块3最小,点数相同时,比较花色,黑桃较大。 请输入玩家人数: 5 请输入每个玩家持有的牌数: 5(<a href="http://www.dztcsd.com/">资质代办</a>) 开始输入玩家信息: 请输入第1个玩家姓名: dekingchen 添加玩家:玩家id:000 玩家姓名:dekingchen ---------------------------------------- 请输入第2个玩家姓名: czq 添加玩家:玩家id:001 玩家姓名:czq ---------------------------------------- 请输入第3个玩家姓名: chenzhenquan 添加玩家:玩家id:002 玩家姓名:chenzhenquan ---------------------------------------- 请输入第4个玩家姓名:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。