21点游戏

2点游戏有BUG

 

public abstract class Player {

 public int score = 0;

 public abstract boolean needCard();

 public void count() {
  boolean need = needCard();
  if (need == true) {
   setScore(PlayCard.playN());
  }

 }

 public int getScore() {
  return score;
 }

 public void setScore(int rscore) {
  this.score = this.score + rscore;
 }

 public String toString() {
  return getClass().getSimpleName() + " " + getScore();
 }

}

 

import java.util.Scanner;

public class HumanPlayer extends Player {
 
 public boolean needCard = true;

 @Override
 public boolean needCard() {
  Scanner in = new Scanner(System.in);
  String str = in.nextLine();
  if ("y".equalsIgnoreCase(str)) {
   return true;
  } else {
   this.needCard = false;
   return false;
  }
 }

 public boolean isNeedCard() {
  return needCard;
  
 }
}

public class ConPlayer extends Player {

 @Override
 public boolean needCard() {
  if (score < 12) {
   return true;
  } else {
   return false;
  }
 }

}

 

public class RadicalPlayer extends Player {

 @Override
 public boolean needCard() {
  if (score < 21) {
   return true;
  } else {
   return false;
  }
 }

}

 

public class WisdomPlayer extends Player {

 @Override
 public boolean needCard() {
  if (score < 16) {
   return true;
  } else {
   return false;
  }
 }

}

 

public class PlayCard {

 public static int playN() {
  int cardNum = (int) (Math.random() * 10+1);
  return cardNum;
  
 }

}

 

import java.util.Scanner;

public class PlayGame {
 public static void main(String[] args) {

  System.out.println("21game is start!");
  System.out.println("The game has four player:HumanPlayer,HumanPlayer,ConPlayer,WisdomPlayer;");
  System.out.println("the HumanPlayer need card should input y or Y");
  while (true) {
   HumanPlayer humanPlayer = new HumanPlayer();
   RadicalPlayer radicalPlayer = new RadicalPlayer();
   ConPlayer conPlayer = new ConPlayer();
   WisdomPlayer wisdomPlayer = new WisdomPlayer();
   while (true) {
    humanPlayer.count();
    radicalPlayer.count();
    conPlayer.count();
    wisdomPlayer.count();
    System.out.println("Now HumanPlayer's score is:"+humanPlayer.getScore());
    boolean gameOver = humanPlayer.isNeedCard()
      || radicalPlayer.needCard()
      || conPlayer.needCard()
      || wisdomPlayer.needCard();
    if (!gameOver) {
     break;
    }
   }

   Player[] players = { humanPlayer, radicalPlayer, conPlayer,
     wisdomPlayer };

   /**
    * sort the players as the score
    */
   for (int i = 0; i < players.length; i++) {
    for (int j = i + 1; j < players.length; j++) {
     if (players[i].getScore() < players[j].getScore()) {
      Player tempPlayer = players[j];
      players[j] = players[i];
      players[i] = tempPlayer;
     }
    }
    
   }

   for (int i = 0; i < players.length; i++) {
    if (players[i].getScore() <= 21) {
     StringBuilder sb = new StringBuilder();
     sb.append("The winner is:");
     sb.append(players[i].getClass().getSimpleName());
     sb.append(",score is:");
     sb.append(players[i].getScore());
     if(i+1<players.length&&players[i+1].getScore()==players[i].getScore()){
      sb.append(" and ");
      sb.append(players[i+1].getClass().getSimpleName());
      sb.append(",score is:");
      sb.append(players[i+1].getScore());
     }
     if(i+2<players.length&&players[i+2].getScore()==players[i].getScore()){
      sb.append(" and ");
      sb.append(players[i+2].getClass().getSimpleName());
      sb.append(",score is:");
      sb.append(players[i+2].getScore());
     }
     if(i+3<players.length&&players[i+3].getScore()==players[i].getScore()){
      sb.append(" and ");
      sb.append(players[i+3].getClass().getSimpleName());
      sb.append(",score is:");
      sb.append(players[i+3].getScore());
     }
     System.out.println(sb.toString());
     break;
    }
   }
   System.out.println();
   System.out.println("The player and score list");
   System.out.println("----"+"Player"+"----"+"Score"+"----");
   for(Player p: players){
    System.out.println(p.toString());
   }

   System.out
     .println("game is over ,check any key continue,if you want exit please input exit");
   Scanner scanner = new Scanner(System.in);
   if ("exit".equalsIgnoreCase(scanner.nextLine())) {
    break;
   }

  }

 }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值