public class PokerGame {
List<Poker> pokers = new ArrayList<>();
public PokerGame() {
int count = 13;
for(int i = 14; i > 1 ; i--,count--){
String pString = i+"";
if (i == 11){
pString = "J";
}
if (i == 12){
pString = "Q";
}
if (i == 13){
pString = "K";
}
if (i == 14){
pString = "A";
}
Poker[] spade = new Poker[count];
//《错误日志》上面申请的是count长度的数组,但是数组是从零开始的,所以最大的数组下标应该是count减一
spade[count-1] = new Poker();
spade[count-1].setPokerFaces(pString);
spade[count-1].setFlowerColor("黑桃");
pokers.add(spade[count-1]);
}
for(Poker p:pokers){
System.out.println(p.getFlowerColor()+p.getPokerFaces());
}
}
public void playGame(){
Player p1 = new Player();
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.println("请输入第一位玩家的姓名:");
p1.setName(input.next());
Player p2 = new Player();
@SuppressWarnings("resource")
Scanner input2 = new Scanner(System.in);
System.out.println("请输入第二位玩家的姓名:");
p2.setName(input2.next());
Random random = new Random();
int r = (random.nextInt(12));
String p1hand = pokers.get(r).getFlowerColor() + pokers.get(r).getPokerFaces();
p1.setHandPoker(p1hand);
pokers.remove(r);
Random random2 = new Random();
int r2 = (random2.nextInt(12));
String p2hand = pokers.get(r2).getFlowerColor() + pokers.get(r2).getPokerFaces();
p2.setHandPoker(p2hand);
pokers.remove(r2);
if(r < r2){
System.out.println("玩家" + p1.getName() + "获得胜利!");
}else {
System.out.println("玩家" + p2.getName() + "获得胜利!");
}
System.out.println("玩家" + p1.getName() + "的手牌是:" + p1.getHandPoker());
System.out.println("玩家" + p2.getName() + "的手牌是:" + p2.getHandPoker());
}
public static void main(String[] args) {
PokerGame pokerGame = new PokerGame();
pokerGame.playGame();
}
}
相关分类