遇到问题:
1. 输入流异常处理并重新输入,无限循环抛出异常;
2.打印List时,对打印格式的控制( toString()方法的重写 );
3.编译器运行程序时,Set是无序的,但每次运行结果都相同(Random生成的随机数也是);
运行结果:
Poker类:
package testPoker;
public class Poker {
String color;
String num;
public Poker(String color,String num) {
this.color = color;
this.num = num;
}
@Override
public String toString() {
return color + num;
}
}
其中有对toString方法的重写,打印时可以不使用循环遍历。
Gamer类:
package testPoker;
import java.util.*;
public class Gamer {
Integer ID;
String name;
List<Poker> handPoker;
public Gamer(Integer ID,String name) {
this.ID = ID;
this.name = name;
handPoker = new ArrayList<Poker>();
}
}
pokerComparator类:
package testPoker;
import java.util.Comparator;
public class pokerComparator implements Comparator<Poker> {
@Override
public int compare(Poker arg0, Poker arg1) {
// TODO Auto-generated method stub
if(arg0.num==arg1.num){
return arg0.color.compareTo(arg1.color);
}else
{
return arg0.num.compareTo(arg1.num);
}
}
}
对compare()方法的重写,先比较数字后比较花色。在用Collection.sort()方法时会调用此方法。
Game类:
package testPoker;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
public class Game {
public Set<Poker> pokerSet;
public List<Poker> pokerList;
public List<Gamer> gamerList;
private Scanner console = new Scanner(System.in);
public Game() {
gamerList = new ArrayList<Gamer>();
pokerList = new ArrayList<Poker>();
pokerSet = new HashSet<Poker>();
}
public void createPoker() {
String[] color = {"方片","梅花","红桃","黑桃"};
String[] num = {"2","3","4","5","6","7",
"8","9","10","J","Q","K","A"};
System.out.println("------------创建扑克牌----------------");
for(int i = 0;i<color.length;i++) {
for(int j = 0;j<num.length;j++ )
{
Poker poker = new Poker(color[i],num[j]);
pokerList.add(poker);
}
}
System.out.println("------------创建扑克牌成功------------------");
System.out.print("为:");
System.out.println(pokerList.toString());
}
public void cleanPoker() {
System.out.println("------------开始洗牌------------------");
for(Poker poker:pokerList) {
pokerSet.add(poker);
}
System.out.println("------------洗牌结束------------------");
}
public void createGamer{
Integer ID;
System.out.println("--------------创建玩家-------------------");
for(int i=1;i<=2;i++) {
try{
System.out.println("请输入第"+i+"位玩家的ID和姓名");
System.out.println("输入ID:");
ID = console.nextInt();
}catch(InputMismatchException e) {
System.out.println("请输入输入正确的ID(ID为整数)");
console.next();
i--;
continue;
}
System.out.println("输入姓名");
String name = console.next();
Gamer gamer = new Gamer(ID,name);
gamerList.add(gamer);
}
for(Gamer gamer:gamerList) {
System.out.println("----欢迎玩家:"+gamer.name);
}
}
public void sendPoker() {
int i = 0;
for(Poker poker:pokerSet)
{
if(i%2 == 0) {
System.out.println("---玩家:"+gamerList.get(0).name+"-拿牌");
gamerList.get(0).handPoker.add(poker);
}else {
System.out.println("---玩家:"+gamerList.get(1).name+"-拿牌");
gamerList.get(1).handPoker.add(poker);
}
i++;
if(i==4)
break;
}
}
public void gameBegin() {
System.out.println("------------开始游戏------------");
for(int i = 0 ;i<2;i++) {
Collections.sort(gamerList.get(i).handPoker,new pokerComparator());
System.out.println("玩家:"+gamerList.get(i).name+"最大的手牌为:"
+gamerList.get(i).handPoker.get(gamerList.get(i).handPoker.size()-1).color
+gamerList.get(i).handPoker.get(gamerList.get(i).handPoker.size()-1).num);
}
pokerComparator pC = new pokerComparator();
int result = pC.compare(gamerList.get(0).handPoker.get(gamerList.get(0).handPoker.size()-1)
,gamerList.get(1).handPoker.get(gamerList.get(1).handPoker.size()-1));
if(result>0)
System.out.println("-------玩家:"+gamerList.get(0).name+"获胜!----------");
else
System.out.println("-------玩家:"+gamerList.get(1).name+"获胜!----------");
System.out.println("玩家各自的手牌为:");
System.out.print(gamerList.get(0).name+":");
System.out.println(gamerList.get(0).handPoker.toString());
System.out.print(gamerList.get(1).name+":");
System.out.println(gamerList.get(1).handPoker.toString());
}
public static void main(String[] args) {
Game game = new Game();
game.createPoker();
game.createGamer();
game.cleanPoker();
game.sendPoker();
game.gameBegin();
}
}
其中定义了零参构造方法,createPoker(),cleanPoker(),createGamerAndHandpoker(),sendPoker(),gameBegin()及main方法。
createPoker()方法:类似与二维数组,创建poker对象,将poker存入pokerList中(有序)。并用重写的toString()方法对pokerList进行打印。
cleanPoker()方法:遍历pokerList集合,把pokerList(有序)中的poker对象存入pokerSet(无序)集合中,从而打乱poker对象,实现洗牌功能。
createGamer()方法:创建两个gamer对象并存入gamerList集合中。遇到的问题主要是ID为Integer,输入流异常处理并重新输入,无限循环抛出异常;解决方法 在catch块中,异常处理时,声明一个console.next();这一句话的作用是读取一次内存中的内容,这样等待键盘输入内存中的数据被清空,下一次可以输入,并进行i--,continue;的操作实现回滚。
相关的处理方法(百度到的): java 输入流异常处理并重新输入,无限循环抛异常问题处理
sendPoker()方法:将洗完牌后的pokerSet集合中的4个poker对象分别添加到每位玩家的手牌handPoker中,实现发牌功能.
gameBegin()方法:将每位玩家的手牌handPoker用Collection.sort()方法及pokerComparator()进行排序。玩家最大的手牌即为排序后最末位的poker。
在利用pokerComparator中重写的Compare方法对两位为玩家最大的手牌进行比较,输出比较结果。利用.toString()打印方法每个玩家的手牌。
共同学习,写下你的评论
评论加载中...
作者其他优质文章