//看来异常方面还是要好好复习一下!
package com.Puke2;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
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);
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("joker\tJOKER");
System.out.println("----------------------");
p.puke();
}
}
class Puke{
private HashMap<Integer,Game> name=new HashMap<Integer,Game>();//接收玩家名字
private 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[]={"","","",""}; //和花色,可能会出现乱码或者空白
/**
*玩家方法
*/
private void name(){
for(int i=0;i<2;i++){//测试两位玩家
Scanner input =new Scanner(System.in);
// System.out.print("请第"+(i+1)+"位玩家输入id:");
// int id=0;
// try{
// id=input.nextInt();
// }catch(InputMismatchException e){
// System.out.println("异常!请重新运行!");//异常处理方面还是不太扎实
// }
System.out.print("请第"+(i+1)+"位玩家输入昵称:");
String name1=input.next();
Game gm=new Game();
//代入玩家类的对象中
name.put((i+1),new Game(name1));//然后将类带入HashMap中
}
}
/**
*扑克牌方法
*/
public void puke(){
name();
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();
}
map.put(shu.length*hua.length+1,"joker");
set.add(shu.length*hua.length+1);
// System.out.println(shu.length*hua.length+1);
map.put(shu.length*hua.length+2,"JOKER");
set.add(shu.length*hua.length+2);
//System.out.println(set);
//测试输出
/**
Set<Entry<Integer, String>> entrys=map.entrySet();//如果要输出键值对,一般用这种方法
for (Entry<Integer, String> entry : entrys) {
System.out.println(entry.getKey()+"--"+entry.getValue());
}
*/
System.out.println();
int b=0;
System.out.println("-------------开始洗牌--------------");
Collections.shuffle(set);//写在外面,防止随机生成相同的牌
System.out.println("-------------洗牌结束--------------");
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.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();
//测试计算是否真确
//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;
}
}
运行结果:
热门评论
这是运行的结果,qwwq