您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 鄂尔多斯分类信息网,免费分类信息发布

Java如何实现斗地主和猜数字游戏?

2024/5/30 13:01:21发布22次查看
斗地主小游戏之洗牌发牌任务描述编写一个斗地主发牌洗牌的程序,要求按照斗地主的规则完成洗牌发牌的过程,牌面由花色色和数字(包括j,q,k,a字母)组成,花色有红桃,黑桃,方块,梅花组成。还设有大王和小王。将这54张牌的顺序打乱,有三位玩家参与游戏,每人轮流一次摸一张牌,剩余的三张作为底牌,程序结束,打印出每人手中的纸牌和底牌。
运行结果   
任务目标学会分析斗地主小游戏之洗牌发牌程序的实现思路。
能够根据思路独立完成斗地主小游戏之洗牌发牌程序的代码编写,编译及运行。
掌握arraylist和hashmap集合的特点以及增强for循环的使用。
实现思路①要实现纸牌程序,首先要在程序中添加54张牌,这些牌包括红桃,黑桃,梅花,方块各13张,加上大王和小王。
②将花色集合和数字集合这两个集合进行嵌套循环,将花色与数字组合,形成52张牌,并且赋予编号,将组合后的牌放入hashmap集合中,集合的key值是编号,value值是组装完成的纸牌,还有大王和小王,由于组装规则不一致,需要单独使用add()方法将这两张纸牌加入到hashmap集合中。
③创建一个数字集合,用这个数字集合来替代纸牌完成洗牌和发牌的操作,由于一共有54张牌,所以创建集合的范围为0-53。
④可以使用collections类的shuffle()方法完成打乱数字集合的操作,实现洗牌的效果。由于只有三个人,所以可以使用for循环,通过将数字与3取余的方法,将代表不同纸牌的数字分配给不同的人和底牌,实现发牌效果。
⑤洗牌发牌完成之后,可通过collections类的sort()方法完成排序,之后通过增强for循环hashmap集合,根据数字查找对应的纸牌字符串,并存入新创建的字符串集合中,最后展示字符串集合。
实现代码package swing; import java.util.arraylist; import java.util.collection; import java.util.collections; import java.util.hashmap; public class doudizhu { public static void main(string[]args) { //准备花色 arraylist<string>color=new arraylist<string >(); color.add("黑桃"); color.add("红桃"); color.add("方块"); color.add("梅花"); //准备数字,用列表将纸牌从大到小排列 arraylist<string >number=new arraylist<string >(); for(int i=3;i<=10;i++){ number.add(i+""); } number.add("j"); number.add("q"); number.add("k"); number.add("a"); number.add("2"); //定义一个map集合,用来将数字与每一张纸牌进行对应 hashmap<integer,string>map=new hashmap<integer,string>(); //纸牌编号 int index=0; //循环纸牌数字 for(string thisnumber:number){ //循环纸牌花色 for(string thiscolor:color){ //将花色与数字组合,形成52张牌,并赋予编号 map.put(index++,thiscolor+thisnumber); } } map.put(index++,"小王"); map.put(index++,"大王"); //创建0-53的数字集合代表54张牌 arraylist<integer>cards=new arraylist<integer>(); for(int i=0;i<=53;i++){ cards.add(i); } //洗牌,使用collections工具类的shuffle()方法 collections.shuffle(cards); //创建三个玩家和底牌 arraylist<integer>iplayer=new arraylist<integer>(); arraylist<integer>iplayer2=new arraylist<integer>(); arraylist<integer>iplayer3=new arraylist<integer>(); arraylist<integer>isecretcards=new arraylist<integer>(); //遍历这副洗好的牌,遍历的过程中,将牌发到三个玩家和底牌中 for(int i=0;i<cards.size();i++){ if(i>=51){ //留取三个底牌 isecretcards.add(cards.get(i)); }else{ if(i%3==0){//与3取余为0的发给玩家1 iplayer.add(cards.get(i)); }else if(i%3==1){//与3取余为1的发给玩家2 iplayer2.add(cards.get(i)); }else {//剩余的牌发给玩家3 iplayer3.add(cards.get(i)); } } } //对每个人手中的牌进行排序,使用的使collections工具类中的sort()方法 collections.sort(iplayer); collections.sort(iplayer2); collections.sort(iplayer3); arraylist<string>splayer=new arraylist<string>(); arraylist<string>splayer2=new arraylist<string>(); arraylist<string>splayer3=new arraylist<string>(); arraylist<string>ssectcards=new arraylist<string>(); //循环主键,从map中获取纸牌 for (integer key:iplayer){ splayer.add(map.get(key)); } for (integer key:iplayer2){ splayer2.add(map.get(key)); } for (integer key:iplayer3){ splayer3.add(map.get(key)); } for (integer key:isecretcards){ ssectcards.add(map.get(key)); } //将分发的牌显示出来 system.out.println("玩家1:"+splayer); system.out.println("玩家2:"+splayer2); system.out.println("玩家3:"+splayer3); system.out.println("底牌:"+ssectcards); } }
猜数字游戏任务描述编写一个猜数字游戏,这个游戏就是"你出个数字,我来猜",程序后台预先生成一个0-9的随机数,用户键盘录入一个所猜的数字,如果输入的数字与后台预先生成的数字相同,则表示猜对了,这时,程序会打印出"恭喜,答对了",如果不相同,则比较输入的数字和后台预先生成的数字的大小,如果大了,打印"sorry,你猜大了!";如果小了,会打印"sorry,你猜小了";如果一直猜错,则游戏一直进行下去,直到数字猜对为止。
运行结果
任务目标学会分析"猜数字游戏"程序的实现思路。
根据思路独立完成"猜数字游戏"的源代码编写,编译和运行。
掌握在程序中使用if选择结构和while循环结构语句进行运算操作。
实现思路①要实现这个功能,首先程序要在后台预先生成一个0-9的随机数,生成随机数可以使用random类中的nextint(int n)方法,其具体的定义如下:
public int nextint(int n)
②要使用键盘输入所猜的数字,可以使用scanner类,可以让用户从键盘中输入数字。
scanner sc=new scanner(system.in); int i=sc.nextint();
③输入数字后,需要比较键盘输入的数字和后台预先生成的数字,由于猜数字并非一定一次成功,很可能是多次进行,因此可以通过while循环使程序能够多次从键盘输入,每次输入都进行猜数字对错判断。如果猜对了,跳出循环,输出"恭喜,你答对了!",游戏结束。
④如果猜错,这时使用if....else语句判断,将错误分为猜大了和猜小了两种结果。如果猜大了,打印"sorry,你猜大了!"继续下一次循环;如果猜小了,打印"sorry,你猜小了!"继续下一次循环。根据结果,给出提示,接着继续猜数字,游戏继续。
实现代码 package math; import java.util.random; import java.util.scanner; public class caishuzi { public static void main(string[] args) { int randomnumber = new random().nextint(10); system.out.println("随机数已经生成"); system.out.println("请输入你所猜的数字"); scanner sc =new scanner(system.in); int enternumber =sc.nextint(); //通过while循环,进行猜数字对错判断 //猜对,跳出循环,游戏结束 while(enternumber!=randomnumber){ //猜错了,根据结果,给出提示,接着猜数字,游戏继续 if(enternumber>randomnumber){ //猜大了给出的提示 system.out.println("sorry,你猜大了"); }else{ //猜小了,给出的提示 system.out.println("sorry,你猜小了"); } //输入猜的数字 system.out.println("请输入你猜的数字"); enternumber = sc.nextint(); } system.out.println("恭喜你,答对了!"); } }
以上就是java如何实现斗地主和猜数字游戏?的详细内容。
鄂尔多斯分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录