1.变量作用域没考虑好,导致有一个方法写得有点过长
2.洗牌一开始直接用random换牌,比较粗暴但麻烦。后来发现可以用collection.shuffle方法
3.弄拙成巧,加上了自定义哪个玩家先拿牌的代码
package pokergame;
public class Errorinput extends Exception {
public Errorinput(){
}
public Errorinput(String message){
super(message);
}
}
package pokergame;
public class Poker {
public String color;
public String number;
public Poker(String color, String number){
this.color=color;
this.number=number;
}}
package pokergame;
import java.util.Comparator;
public class Compare implements Comparator<Poker> {
public int compare(Poker arg0, Poker arg1) {
String color="黑桃 红桃 梅花 方块";
String numbers="2 3 4 5 6 7 8 9 10 J Q K A";
int result=numbers.indexOf(arg0.number)-numbers.indexOf(arg1.number);
if(result<0){
return -1;
}else if(result>0){
return 1;
}else {
int result2=color.indexOf(arg0.color)-color.indexOf(arg0.color);
if(result2>0){
return 1;
}else if(result<0){
return -1;
}else{
return 0;
}
}
}
}
package pokergame;
public class User {
public String name;
public String[] pai;
public User(String name, String[] pai){
this.name=name;
this.pai=pai;
}
}
package pokergame;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
public class Pokergamesystem {
public List<Poker> poker;
public Map<Integer, User> users;
public Pokergamesystem(){
this.poker=new ArrayList<Poker>();
this.users=new HashMap<Integer,User>();
}
/**
* 生成一副扑克牌
*/
public void pokergenerator(){
System.out.println("-------开始生成扑克牌");
String[] hua={"黑桃","红桃","梅花","方块"};
String[] shu={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
for(int i=0;i<4;i++){
String hua1=hua[i];
for(int j=0;j<13;j++){
String shu1=shu[j];
Poker pk=new Poker(hua1,shu1);
poker.add(pk);
}
}
System.out.println("-------纸牌初始化完毕,为");
for (Object obj : poker) {
Poker pk=(Poker)obj;
System.out.print(pk.color+pk.number+" ");
}
System.out.println(" ");
}
/**
* id输入部分,要求输入的数据类型必须为integer类型,否则会抛出异常
* @throws Errorinput
*/
public int userid(int i) throws Errorinput{
System.out.println("请输入第"+i+"个玩家的id号");
Scanner sc=new Scanner(System.in);
String input=sc.next().trim();
int number;
try{
number=Integer.parseInt(input);
return number;
}catch(Exception e){
throw new Errorinput("请输入数字形式的id");
}
}
/**
* 生成两名玩家
*/
public void usergenerator(){
int i=1;
while(i<3){
try{
int id=userid(i);
// 判断该ID是否被占用(一开始没有在构造器中初始化users,导致报错)
User us= users.get(id);
if(us==null){
System.out.println("请输入玩家昵称");
Scanner sc=new Scanner(System.in);
String name=sc.next();
User newuser= new User(name,null);
users.put(id, newuser);
System.out.println("成功添加玩家:"+users.get(id).name);
i++;
}
else{
System.out.println("该玩家id已被占用,请重新输入!");
}
}catch(Errorinput e){
System.out.println(e.getMessage());
}}
}
/**
* 洗牌,基本思想为交换list中任意两个对象的位置,重复1000次
*/
public void neworder(){
// for(int s=0;s<1000;s++){
// int number1=new Random().nextInt(52);
// int number2=new Random().nextInt(52);
// Poker pk1=(Poker)poker.get(number1);
// Poker pk2=(Poker)poker.get(number2);
// String temp1 = pk2.color;
// String temp2 = pk2.number;
// pk2.color=pk1.color;
// pk2.number=pk1.number;
// pk1.color=temp1;
// pk1.number=temp2;//直接将poker的值对换是不行的,
// //可能是因为自带了索引位置信息,所以即使对换还是在原来的位置?
// }
Collections.shuffle(poker);
System.out.println("");
System.out.println("--------洗牌结束");
for (Object obj : poker) {
Poker pk=(Poker)obj;
System.out.print(pk.color+pk.number+" ");
System.out.print(" ");
}
}
public void giveandcompare(){
//give
System.out.println("\n"+"请输入先得牌玩家的id号");
Scanner sc1=new Scanner(System.in);
String input1=sc1.next();
Integer id1=Integer.parseInt(input1);
System.out.println("请输入后得牌玩家的id号");
Scanner sc2=new Scanner(System.in);
String input2=sc2.next();
Integer id2=Integer.parseInt(input2);
List<Poker> user1=new ArrayList<Poker>();
List<Poker> user2=new ArrayList<Poker>();
for(int i=0;i<2;i++){
System.out.println(users.get(id1).name+"得牌");
user1.add(poker.get(2*i));
System.out.println(users.get(id2).name+"得牌");
user2.add(poker.get(2*i+1));
}
System.out.println(users.get(id1).name+"你得到了:");
for (Poker poker : user1) {
System.out.println(poker.color+poker.number);
}
Collections.sort(user1,new Compare());
System.out.println(" the biger card is :"+user1.get(1).color+" "+user1.get(1).number);
System.out.println(users.get(id2).name+"你得到了:");
for (Poker poker : user2) {
System.out.println(poker.color+poker.number);
}
Collections.sort(user2,new Compare());
System.out.println(" the biger card is :"+user2.get(1).color+" "+user2.get(1).number);
//compare
List<Poker> lastcards=new ArrayList<Poker>();
lastcards.add(user1.get(1));
lastcards.add(user2.get(1));
Collections.sort(lastcards,new Compare());
if(lastcards.get(1).color.equals(user1.get(1).color)lastcards.get(1).number.equals(user1.get(1).number)){
System.out.println("\n"+users.get(id1).name+" is the winner!");
}else{
System.out.println("\n"+users.get(id2).name+" is the winner!");
}
}
public static void main(String[] args) {
Pokergamesystem pks=new Pokergamesystem();
pks.pokergenerator();
pks.usergenerator();
pks.neworder();
pks.giveandcompare();
}
}
点击查看更多内容
16人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦