------------------------------EasyPokerGame类-----------------------------------------
public class EasyPokerGame {
public static List<PokerCard> poker=new ArrayList<PokerCard>();
public static Scanner input=new Scanner(System.in);
public static String[] card_number={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
public static String[] flower_colour={"黑桃","红桃","梅花","方块"};
public static void main(String[] args) {
System.out.println("**简易扑克牌游戏****");
System.out.println("-------创建扑克牌-------");
newPoker();
System.out.println("--------扑克牌创建成功-----------");
System.out.print("扑克牌为:");
printPoker();
System.out.println("请输入第一位玩家的id和姓名:");
Player one=setPlayer();
System.out.println("请输入第二位玩家的id和姓名:");
Player two=setPlayer();
System.out.println("------欢迎玩家:"+one.name);
System.out.println("------欢迎玩家:"+two.name);
sendCard(one,two);
System.out.println("---------发牌结束-----------");
System.out.println("---------开始游戏-----------");
System.out.println("玩家:"+one.name+"最大的手牌为"+one.findMyMaxCard().flower_colour+one.findMyMaxCard().card_number);
System.out.println("玩家:"+two.name+"最大的手牌为"+two.findMyMaxCard().flower_colour+two.findMyMaxCard().card_number);
if(compareCard(one, two)==1){
System.out.println("---------玩家:"+one.name+"获胜-----------");
}
else if(compareCard(one, two)==-1){
System.out.println("---------玩家:"+two.name+"获胜-----------");
}
else{
System.out.println("---------平局-----------");
}
System.out.println("玩家各自的手牌为:");
System.out.print(one.name+"的手牌为:");
one.printmycard();
System.out.print(two.name+"的手牌为:");
two.printmycard();
}
public static void newPoker(){
for(int i=0;i<flower_colour.length;i++){
for(int j=0;j<card_number.length;j++){
poker.add(new PokerCard(flower_colour[i],card_number[j],i,j));
}
}
}
public static void printPoker(){
for (PokerCard pc : poker) {
System.out.print(pc.flower_colour+pc.card_number+" ");
}
System.out.println();
}
public static Player setPlayer(){
int id;
String name;
/**
- 进入死循环-》捕获了输入异常后continue,没有捕获时return跳出,实现了重新输入
- scanner有滞纳效应如果不每次更新就会陷入死循环
-
循环次数多了不断new scanner出来会增加内存损耗?如何解决?
*/
while (true) {
try {
System.out.println("请输入id:");
id = input.nextInt();
System.out.println("请输入姓名:");
name = input.next();
} catch (Exception e) {
System.out.println("请输入整数类型的id!");
// input=new Scanner(System.in);
input.next();//吃掉错误的输入--.
continue;
}
return new Player(id,name);
}
}
public static void sendCard(Player one,Player two){
System.out.println("---------开始洗牌-----------");
//Collections.shuffle(poker);
List<Integer> list=RandomCards();
System.out.println("---------开始发牌-----------");for (int i = 0; i < 4; i=i+2) { System.out.println("----玩家" + one.name + "拿牌"); one.mycard.add(poker.get(list.get(i))); System.out.println("----玩家" + two.name + "拿牌"); two.mycard.add(poker.get(list.get(i+1))); }
}
public static int compareCard(Player one,Player two){
return one.findMyMaxCard().compareTo(two.findMyMaxCard());
}
public static List<Integer> RandomCards() {
Random random=new Random();
System.out.println("------------------------------2.洗牌操作------------------------------");
int k=0;List<Integer> list = new ArrayList<Integer>(); while (list.size() < 52) { Integer i = new Integer(random.nextInt(52)); if (!list.contains(i)) { list.add(i); //System.out.print(poker.get(list.get(k)).flower_colour+poker.get(list.get(k)).card_number+" ");
// k++;
//
// if (k % 13 ==0) {
// System.out.println(";");
// }
}
}
System.out.println("------------------------------洗牌完成------------------------------");
System.out.println();
return list;
}
}
---------------------------PokerCard类---------------------------------------------------------------
public class PokerCard implements Comparable<PokerCard>{
public String flower_colour;
public String card_number;
public Integer flowerVaule;
public Integer numberVaule;
public PokerCard(String flower_colour, String card_number, int flowerVaule, int numberVaule) {
super();
this.flower_colour = flower_colour;
this.card_number = card_number;
this.flowerVaule = flowerVaule;
this.numberVaule = numberVaule;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
@Override
public int compareTo(PokerCard o) {
if(this.numberVaule.compareTo(o.numberVaule)!=0){
return numberVaule.compareTo(o.numberVaule);
}
else{
return -(flowerVaule.compareTo(o.flowerVaule));
}
}
}
-----------------------Player类-------------------------------------------------
public class Player {
public int id;
public String name;
public List<PokerCard> mycard=new ArrayList<PokerCard>();
public Player(int id, String name) {
super();
this.id = id;
this.name = name;
}
public PokerCard findMyMaxCard(){
Collections.sort(mycard);
return mycard.get(1);
}
public void printmycard(){
for (PokerCard pc : mycard) {
System.out.print(pc.flower_colour+pc.card_number+" ");
}
System.out.println();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章