继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

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

东方的小王
关注TA
已关注
手记 2
粉丝 1
获赞 3

package com.Puke2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.Map.Entry;
;

public class Pukes {

public static void main(String[]args){
System.out.println("-------------简易扑克牌游戏-------------");
Puke p=new Puke();
Scanner inpit = new Scanner(System.in);
p.name();
System.out.println("有以下扑克牌:");
String shu[]={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
String hua[]={"","","",""};
for(int i=0;i<shu.length;i++){
for(int j=0;j<hua.length;j++){
System.out.print(hua[j]+shu[i]+"\t");
}
System.out.println();
}
System.out.println("----------------------");

p.puke();

}
}

class Puke{
HashMap<Integer,Game> name=new HashMap<Integer,Game>();//接收玩家名字
ArrayList<Integer> set = new ArrayList<Integer>();//接收数字
// ArrayList<String> list = new ArrayList<String>();//接收扑克牌集合
HashMap<Integer,String> map=new HashMap<Integer,String>();
private String shu[]={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};//扑克牌的大小
private String hua[]={"","","",""}; //和花色
public void name(){
for(int i=0;i<2;i++){//测试两位玩家
Scanner input =new Scanner(System.in);

    System.out.print("请第"+(i+1)+"位玩家输入姓名:");
    String name1=input.next();
    Game gm=new Game();
    //代入玩家类的对象中
    name.put((i+1),new Game(name1));//然后将类带入HashMap中
    }
}

public void puke(){
int a = 0;
for(int i=0;i<shu.length;i++){
    for(int j=0;j<hua.length;j++){//接收所有扑克牌
    a++;
    //System.out.print(a+" ");
    StringBuffer sb=new StringBuffer("");
    StringBuffer sb2=sb.append(hua[j]+shu[i]+"   ");//将牌面和花色融为一体
    String str=sb2+"";
    //测试带入的是否正确
    //System.out.print(a+str);
    map.put(a,str);//将代号和牌面放入HashMap容器中
    set.add(a);//将代号放入ArrayList容器中,方便后期洗牌
  }
//System.out.println();
 }
System.out.println();
int b=0;
Collections.shuffle(set);//写在外面,防止随机生成相同的牌
for(int i=0;i<2;i++){
    for(int j=0;j<2;j++){
    System.out.print("玩家"+name.get(j+1)+"摸牌");
        b++;
        System.out.println(map.get(set.get(b)));//测试随机生成的牌
}
}
System.out.println();
    System.out.print("玩家"+name.get(1)+"最大的牌是:");
    if(set.get(1)-set.get(3)>0){
        System.out.print(map.get(set.get(1)));
    }else{
        System.out.print(map.get(set.get(3)));
    }
    System.out.println();
    System.out.print("玩家"+name.get(2)+"最大的牌是:");
    if(set.get(2)-set.get(4)>0){
        System.out.print(map.get(set.get(2)));
    }else{
        System.out.print(map.get(set.get(4)));
    }
    //测试计算是否真确
//System.out.println(set.get(1)+" "+set.get(2)+" "+set.get(3)+" "+set.get(4)+" ");
    System.out.println("获胜者是:");
    if(set.get(1)-set.get(3)>0&&set.get(2)-set.get(4)>0){//当都是摸得第一张牌大

        if(set.get(1)-set.get(2)>0){
            System.out.println(name.get(1));
        }else{
            System.out.println(name.get(2));    
        }

    }else if(set.get(1)-set.get(3)>0&&set.get(2)-set.get(4)<0){//当第一位玩家摸得第一张牌,第二位玩家摸得第二张牌大

        if(set.get(1)-set.get(4)>0){
            System.out.println(name.get(1));
        }else{
            System.out.println(name.get(2));    
        }

    }else if(set.get(1)-set.get(3)<0&&set.get(2)-set.get(4)>0){//与上面的相反

        if(set.get(3)-set.get(2)>0){
            System.out.println(name.get(1));
        }else{
            System.out.println(name.get(2));    
        }

  }else if(set.get(1)-set.get(3)<0&&set.get(2)-set.get(4)<0){//最后一种情况

      if(set.get(3)-set.get(4)>0){    
            System.out.println(name.get(1));
        }else{
            System.out.println(name.get(2));    
        }

}
}
}
class Game{//玩家类
// HashMap<Integer,String> name=new HashMap<Integer,String>();

private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Game() {

}
public Game(String name) {

this.name = name;

}
@Override
public String toString() {
return name;
}

}

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP

热门评论

http://img.mukewang.com/59263f180001db4e03400561.jpg

http://img.mukewang.com/59263f180001be5103570558.jpg这是运行结果 ,之前一直在复习


网站的显示器有问题,一些类怎么

查看全部评论