纸牌小游戏
package cn.com.group;
public class PuKe {
public String huase;
public String value;
public PuKe() {
}
public PuKe(String huase,String value) {
this.huase= huase;
this.value = value;
}
public String getHuase() {
return this.huase;
}
public PuKe myCompare(PuKe p) {
if (this.value.compareTo(p.value) > 0) {
return this;
}
if (this.value.compareTo(p.value) < 0) {
return p;
}
if (this.value.compareTo(p.value) == 0) {
if (this.huase.equals("黑桃")) {
return this;
} else if (p.huase.equals("黑桃")) {
return p;
} else if (this.huase.equals("红桃")) {
return this;
} else if (p.huase.equals("红桃")) {
return p;
} else if (this.huase.equals("梅花")) {
return this;
}
else {
return p;
}
}
return new PuKe(); // 这行代码必须要有,否则报错
}
}
package cn.com.group;
import java.util.List;
public class Player {
public String name;
public int id;
public List<PuKe> list;
public Player() {
}
public Player(String name,int id) {
this.name=name;
this.id=id;
}
}
package cn.com.group;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Game {
public List<PuKe> puke;
public Player[] player;
public void testGame() {
int tempid=0;
String tempName="";
System.out.println("欢迎来到游戏");
System.out.println("---洗牌--");
puke = new ArrayList();
for(int i=0;i<13;i++) {
if(i==0) {
puke.add(new PuKe("方片","A"));
puke.add(new PuKe("梅花","A"));
puke.add(new PuKe("红桃","A"));
puke.add(new PuKe("黑桃","A"));
}
else if(i>1&&i<10) {
puke.add(new PuKe("方片",String.valueOf(i)));
puke.add(new PuKe("梅花",String.valueOf(i)));
puke.add(new PuKe("红桃",String.valueOf(i)));
puke.add(new PuKe("黑桃",String.valueOf(i)));
}
else if(i==10){
puke.add(new PuKe("方片","J"));
puke.add(new PuKe("梅花","J"));
puke.add(new PuKe("红桃","J"));
puke.add(new PuKe("黑桃","J"));
}
else if(i==11) {
puke.add(new PuKe("方片","Q"));
puke.add(new PuKe("梅花","Q"));
puke.add(new PuKe("红桃","Q"));
puke.add(new PuKe("黑桃","Q"));
}
else if(i==12) {
puke.add(new PuKe("方片","K"));
puke.add(new PuKe("梅花","K"));
puke.add(new PuKe("红桃","K"));
puke.add(new PuKe("黑桃","K"));
}
}
System.out.println("创建成功");
for(PuKe puke1:puke) {
System.out.println(puke1.huase+puke1.value);
}
System.out.println("开始洗牌");
Collections.shuffle(puke);
System.out.println("洗牌完成");
System.out.println("选择玩家");
player = new Player[2];
Scanner console = new Scanner(System.in);
for(int i=0;i<2;i++) {
System.out.println("请输入第"+(i+1)+"个玩家的游戏名称");
try {
tempName = console.next();
// tempid=i;
}catch(Exception e) {
e.printStackTrace();
System.out.println("请输入String类型的字符串");
}
player[i]=new Player(tempName,tempid);
}
System.out.println("欢迎玩家:" + player[0].name);
System.out.println("欢迎玩家:" + player[1].name);
// 接下来每人发两张牌
player[0].list = new ArrayList<PuKe>();
player[1].list = new ArrayList<PuKe>();
System.out.println("--------开始发牌------------------");
System.out.println("玩家:" + player[0].name + "拿牌");
player[0].list.add(puke.get(0));
System.out.println("玩家:" + player[1].name + "拿牌");
player[1].list.add(puke.get(1));
System.out.println("玩家:" + player[0].name + "拿牌");
player[0].list.add(puke.get(2));
System.out.println("玩家:" + player[1].name + "拿牌");
player[1].list.add(puke.get(3));
System.out.println("---------发牌结束----------------");
// 开始pk
PuKe p1max = player[0].list.get(0).myCompare(player[0].list.get(1));
PuKe p2max = player[1].list.get(0).myCompare(player[1].list.get(1));
if (p1max.myCompare(p2max).equals(p1max)) {
System.out.println("恭喜玩家:" + player[0].name + "获胜");
} else {
System.out.println("恭喜玩家:" + player[1].name + "获胜");
}
System.out.println("玩家:" + player[0].name + "的牌是:"+
player[0].list.get(0).huase+player[0].list.get(0).value+","+player[0].list.get(1).huase+player[0].list.get(1).value);
System.out.println("玩家:" + player[1].name + "的牌是:"+
player[1].list.get(0).huase+player[1].list.get(0).value+","+player[1].list.get(1).huase+player[1].list.get(1).value);
// for (PuKe i : puke) {
// System.out.println(i.huase + i.value);
// }
}
public static void main(String[] args) {
Game game = new Game();
game.testGame();
}
}