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;
}
}
}
}