编写个程序
发布网友
发布时间:2022-06-16 23:37
我来回答
共4个回答
热心网友
时间:2023-11-09 11:57
import java.util.Random;
import java.util.Scanner;
/**---------------------------------------没有 Bug 可以重复游戏 -------------------------------------------------
*
* @作者: lili
* @日期: Nov 6, 2009
*/
// 游戏规则:游戏开始时玩家(player)手上有$10的赌本,然后分别随机发一张牌给computer和玩家。
// 玩家有4个选项的押注:
// 1.赌$5:玩家的牌大于电脑的牌。
// 2.赌$10:玩家的牌大于电脑的牌。
// 3.赌$5:玩家的牌小于电脑的牌。
// 4.赌$10:玩家的牌小于电脑的牌。
// 当玩家下完注后,显示结果:扑克大小比较结果和玩家剩余的赌本。
// (注意:当玩家的牌和电脑的牌一样大时,算电脑赢!)
// ------------------------------------------------
// 游戏是循环进行的,只有当以下条件中任意一个发生时游戏结束:
// ---玩家的赌金变为0或者小于0.
// ---玩家赢了$100
// ---52张牌都发完了,还没有赢家。
// 游戏结束时,输出游戏结束信息(game over)。
// ---------------------------------------------------
// 希望能在代码上添加注释,请将代码发至邮箱:plushtunes@163.com
// 先谢谢了!
// 我写的代码只做参考,但是程序一定要有class CardBet,class Card,class
// Deck这3个类。(需要实现上面的游戏规则,其它方法请自行添加)
public class CardGame52 {
/** 玩家 */
private Player player = new Player("玩家", 60);
/** 电脑 */
private Player computer = new Player("电脑", 0);
/** 一副牌 */
private Deck deck = new Deck();
/** 游戏结束 */
private boolean ISOVER = false;
/** 游戏结束 */
private String prompts;
/** 游戏规则说明 */
private String playRule;
private static int MAX_MONEY = 100;
public CardGame52() {
}
private void init() {
this.player = new Player("玩家", 60);
this.computer = new Player("电脑", 0);
this.deck = new Deck();
this.ISOVER = false;
}
/**
* -----------------开始发牌-------------------------
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
StringBuffer sb = new StringBuffer();
sb.append("下注规则>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
sb.append("\n");
sb.append("1.赌$5:玩家的牌大于电脑的牌。\n2.赌$10:玩家的牌大于电脑的牌。");
sb.append("\n");
sb.append("3.赌$5:玩家的牌小于电脑的牌。\n4.赌$10:玩家的牌小于电脑的牌。");
sb.append("\n");
sb.append("下注规则<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
String playRole = sb.toString();
String prompts = "请选择你要下的注:";
CardGame52 bet = new CardGame52();
bet.startPlay(1, 4, prompts, playRole);
}
/**
* 4个选项的押注 读取器
*
* @param min
* 最小
* @param max
* 最大
* @param prompts
* 提示语
* @return
*/
private int getChooseNumFromInput(int min, int max, String prompts) {
System.out.println(prompts);
if (min > max) {
int temp = max;
max = min;
min = temp;
}
int chooseNum = 0;
Scanner san = new Scanner(System.in);
String str = san.next();
try {
chooseNum = Integer.valueOf(str);
} catch (Exception e) {
chooseNum = 0;
}
boolean flag = chooseNum <= max && chooseNum >= min ? true : false;
while (!flag) {
return getChooseNumFromInput(min, max, prompts);
}
return chooseNum;
}
/**
* 开始游戏------------------
*
* @param min
* 玩家可选下注的最小号
* @param max
* 玩家可选下注的最大号
* @param prompts
* 玩家下注提示语
* @param playRole
* 游戏规则说明
* @throws Exception
*/
public void startPlay(int min, int max, String prompts, String playRole)
throws Exception {
System.out.println(playRole);
System.out.println("你的初始资本为:$"+this.player.getMoney());
while (!this.isOver()) {
// 玩家下注号
int choose = this.getChooseNumFromInput(min, max, prompts);
System.out.println(" 玩家下注号: " + choose);
// 玩家的牌
Card cardOfplayer = deck.dealCard();
System.out.println("玩家的牌: " + cardOfplayer);
// 电脑的牌
Card cardOfcomputer = deck.dealCard();
System.out.println("电脑的牌: " + cardOfcomputer);
// 比较 两张牌 大小,按规则给玩家 加钱 或 减钱
this.CompareByComparisonRule(cardOfplayer, cardOfcomputer, choose);
System.out.println("当前玩家赌资为:$" + this.player.getMoney());
}
if (this.player.getMoney() >= MAX_MONEY ) {
System.out.println("你已赢了 $"+this.player.getMoney()+" !要继续玩吗?");
String input = this.isGoOn();
if ("y".equalsIgnoreCase(input)) {
this.init();
this.startPlay(min, max, prompts, playRole);
} else if ("n".equalsIgnoreCase(input)) {
System.out.println("-----------Game is over !---------------");
}
} else if (this.player.getMoney() <= 0) {
System.out.println("你的Money 只有 $"+this.player.getMoney()+" ,要重新来过吗?");
String input = this.isGoOn();
if ("y".equalsIgnoreCase(input)) {
this.init();
this.startPlay(min, max, prompts, playRole);
} else if ("n".equalsIgnoreCase(input)) {
System.out.println("-----------Game is over !---------------");
}
}else{
System.out.println("牌已发完 !要重新来过吗?");
String input = this.isGoOn();
if ("y".equalsIgnoreCase(input)) {
this.init();
this.startPlay(min, max, prompts, playRole);
} else if ("n".equalsIgnoreCase(input)) {
System.out.println("-----------Game is over !---------------");
}
}
}
private String isGoOn() {
System.out.println("请输入y(继续) 或 n(退出): ");
Scanner san = new Scanner(System.in);
String input = san.next();
while (!"yYnN".contains(input)) {
input = this.isGoOn();
}
return input;
}
/**
* 比较 两张牌 大小,按规则给玩家 加钱 或 减钱
* <li>1.赌$5:如果 玩家的牌大于电脑的牌 ,返回 玩家 $5</li>
* <li>2.赌$10:如果 玩家的牌大于电脑的牌 ,返回 玩家 $10</li>
* <li>3.赌$5:如果 玩家的牌小于电脑的牌 ,返回 玩家 $5</li>
* <li>4.赌$10:如果 玩家的牌小于电脑的牌 ,返回 玩家 $10</li>
*
* @param cardOfplayer
* 玩家
* @param cardOfcomputer
* 电脑
* @param choose
* 玩家的押注
* @return
* @throws Exception
*/
public void CompareByComparisonRule(Card cardOfplayer, Card cardOfcomputer,
int choose) throws Exception {
if (cardOfcomputer == null || cardOfplayer == null) {
throw new Exception("牌 异常!");
}
// 比较后的结果
int result = cardOfplayer.compareTo(cardOfcomputer);
// System.out.println("比较后的结果:"+result);
if (result > 0) {
switch (choose) {
case 1:
this.player.setMoney(this.player.getMoney() + 5);
break;
case 2:
player.setMoney(player.getMoney() + 10);
break;
case 3:
player.setMoney(player.getMoney() - 5);
break;
case 4:
player.setMoney(player.getMoney() - 10);
default:
break;
}
} else if (result == 0) {
switch (choose) {
case 1:
this.player.setMoney(this.player.getMoney() - 5);
break;
case 2:
player.setMoney(player.getMoney() - 10);
break;
case 3:
player.setMoney(player.getMoney() - 5);
break;
case 4:
player.setMoney(player.getMoney() - 10);
default:
break;
}
} else if (result < 0) {
switch (choose) {
case 1:
this.player.setMoney(this.player.getMoney() - 5);
break;
case 2:
player.setMoney(player.getMoney() - 10);
break;
case 3:
player.setMoney(player.getMoney() + 5);
break;
case 4:
player.setMoney(player.getMoney() + 10);
default:
break;
}
}
return;
}
private boolean isOver() {
ISOVER = player.getMoney() <= 0
|| deck.getDeal_cards_sum() >= Deck.CARDS_NUM - 1
|| player.getMoney() >= MAX_MONEY;
return ISOVER;
}
public Player getComputer() {
return computer;
}
public Deck getDeck() {
return deck;
}
public Player getPlayer() {
return player;
}
public String getPrompts() {
return prompts;
}
public void setPrompts(String prompts) {
this.prompts = prompts;
}
public String getPlayRule() {
return playRule;
}
public void setPlayRule(String playRule) {
this.playRule = playRule;
}
}
/**
* 一副牌--------------------实体类---------------------------
*
* @作者: lili
* @日期: Nov 6, 2009
*/
class Deck {
/** 牌的总张数 */
public final static int CARDS_NUM = 52;
/** 牌 */
private Card[] cards = new Card[Deck.CARDS_NUM];
/** 已发出的牌的张数 */
private int deal_cards_sum = 0;
public Deck() {
this.init();
}
/**
* 顺序初始化 一副牌
*
*/
public void init() {
int index = 0;
for (Suit suit : Suit.values()) {
for (Face face : Face.values()) {
cards[index++] = new Card(suit, face);
}
}
}
/**
* 随机发牌
*
* @param deck
* 一副牌
* @return
*/
public Card dealCard() {
Card card = null;
Random r = new Random();
int index = r.nextInt(Deck.CARDS_NUM);
while (this.cards[index].isIS_VISIT()) {
index = r.nextInt(Deck.CARDS_NUM);
}
this.cards[index].setIS_VISIT(true);
card = this.cards[index];
this.deal_cards_sum++;
return card;
}
public int getDeal_cards_sum() {
return deal_cards_sum;
}
}
/**
* 玩家--------------------实体类---------------------------
*
* @作者: lili
* @日期: Nov 6, 2009
*/
class Player {
private String playerName;
private int money;
public Player(String name) {
this.playerName = name;
}
public Player(String name, int money) {
this.playerName = name;
this.money = money;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public String getPlayerName() {
return playerName;
}
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
}
/**
* 一张牌------------------实体类-------------------------
*
* @作者: lili
* @日期: Nov 6, 2009
*/
class Card implements java.lang.Comparable<Card> {
/** 是否已被访问 */
private boolean IS_VISIT = false;
public boolean isIS_VISIT() {
return IS_VISIT;
}
public void setIS_VISIT(boolean is_visit) {
IS_VISIT = is_visit;
}
public Card() {
}
private Suit suit;
private Face face;
public Card(Suit suit, Face face) {
super();
this.suit = suit;
this.face = face;
}
public Suit getSuit() {
return suit;
}
public void setSuit(Suit suit) {
this.suit = suit;
}
public Face getFace() {
return face;
}
public void setFace(Face face) {
this.face = face;
}
public String toString() {
return "{" + this.getSuit() + " " + this.getFace() + "}";
}
/**
* 比较 两张牌的大小(只比牌面值)
* <li>如果 this > other ,返回 1</li>
* <li>如果 this = other ,返回 0</li>
* <li>如果 this < other ,返回 -1</li>
*
*/
public int compareTo(Card other) {
int res = 0;
int self = this.getValue();
int oelf = other.getValue();
res = (self == oelf) ? 0 : ((self > oelf) ? 1 : -1);
return res;
}
/**
* 返回 牌面 的值(1-13)
*
* @return
*/
public int getValue() {
String seq = this.getFace().toString();
if (seq.matches("\\d+")) {
return Integer.parseInt(this.getFace().toString());
} else {
char c = seq.charAt(0);
switch (c) {
case 'A':
return 1;
case 'J':
return 11;
case 'Q':
return 12;
case 'K':
return 13;
default:
return 0;
}
}
}
}
/**
* 花色--------------------枚举-----------------------
*
* @作者: lili
* @日期: Nov 6, 2009
*/
enum Suit {
// 黑桃
SPADE {
public String toString() {
return "黑桃";
}
},
// 红桃
HEART {
public String toString() {
return "红桃";
}
},
// 梅花
CLUB {
public String toString() {
return "梅花";
}
},
// 方块
DIAMOND {
public String toString() {
return "方块";
}
}
}
/**
* 牌面--------------------枚举-----------------------
*
* @作者: lili
* @日期: Nov 6, 2009
*/
enum Face {
ONE {
public String toString() {
return "A";
}
},
TWO {
public String toString() {
return "2";
}
},
THREE {
public String toString() {
return "3";
}
},
FOUR {
public String toString() {
return "4";
}
},
FIVE {
public String toString() {
return "5";
}
},
SIX {
public String toString() {
return "6";
}
},
SEVEN {
public String toString() {
return "7";
}
},
EIGHT {
public String toString() {
return "8";
}
},
NINE {
public String toString() {
return "9";
}
},
TEN {
public String toString() {
return "10";
}
},
JACK {
public String toString() {
return "J";
}
},
QUEEN {
public String toString() {
return "Q";
}
},
KING {
public String toString() {
return "K";
}
}
}
热心网友
时间:2023-11-09 11:58
import java.util.Random;
import java.util.Scanner;
public class CardBet {
private Player pl = new Player(10);
private Deck de = new Deck();
private boolean finish;
private static Scanner san = new Scanner(System.in);
public void play() {
de.shuffle();
while (!this.isFinish()) {
Card pls = de.deal();
Card pcs = de.deal();
System.out.println("你得到的牌是: " + pls);
System.out.println("1.赌$5:玩家的牌大于电脑的牌。\n2.赌$10:玩家的牌大于电脑的牌。"
+ "\n3.赌$5:玩家的牌小于电脑的牌。\n4.赌$10:玩家的牌小于电脑的牌。");
System.out.print("请输入数字押注: ");
int choc = san.nextInt();
int res = pls.compareTo(pcs);
System.out.println("电脑的牌是: " + pcs);
if (res > 0) {
switch (choc) {
case 1:
pl.setMoney(pl.getMoney() + 5);
break;
case 2:
pl.setMoney(pl.getMoney() + 10);
break;
case 3:
pl.setMoney(pl.getMoney() - 5);
break;
case 4:
pl.setMoney(pl.getMoney() - 10);
default:
break;
}
System.out.println("当前赌注:" + pl.getMoney());
} else {
switch (choc) {
case 1:
pl.setMoney(pl.getMoney() - 5);
break;
case 2:
pl.setMoney(pl.getMoney() - 10);
break;
case 3:
pl.setMoney(pl.getMoney() + 5);
break;
case 4:
pl.setMoney(pl.getMoney() + 10);
default:
break;
}
System.out.println("当前赌注:" + pl.getMoney());
}
}
System.out.println("Game over.");
}
public CardBet() {
}
public static void main(String[] args) {
CardBet bet = new CardBet();
bet.play();
// bet.de.show();
}
public boolean isFinish() {
finish = pl.getMoney() <= 0
|| de.getCurrentCardIndex() >= Deck.CARDS_NUM - 1
|| pl.getMoney() >= 110;
return finish;
}
public void setFinish(boolean finish) {
this.finish = finish;
}
}
class Player {
private int money = 10;
public Player() {
}
public Player(int money) {
this.money = money;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}
class Deck {
public static int CARDS_NUM = 52;
private Card[] cards = new Card[Deck.CARDS_NUM];
private int currentCardIndex = 0;
public Deck() {
int index = 0;
for (Suit suit : Suit.values()) {
for (Face face : Face.values()) {
cards[index++] = new Card(suit, face);
}
}
}
public void shuffle() {
Random random = new Random();
for (int i = 0; i < cards.length; ++i) {
ArrayUtilities.swap(cards, i, random.nextInt(cards.length));
}
}
public Card deal() {
if (currentCardIndex > cards.length)
return null;
Card card = cards[currentCardIndex];
cards[currentCardIndex++] = null;
return card;
}
public void show() {
for (Card c : cards) {
System.out.println(c);
}
}
public int getCurrentCardIndex() {
return currentCardIndex;
}
}
class ArrayUtilities {
public static void swap(Object[] dist, int from, int to) {
Object obj = dist[from];
dist[from] = dist[to];
dist[to] = obj;
}
}
class Card implements java.lang.Comparable<Card> {
public Card() {
}
private Suit suit;
private Face face;
public Card(Suit suit, Face face) {
super();
this.suit = suit;
this.face = face;
}
public Suit getSuit() {
return suit;
}
public void setSuit(Suit suit) {
this.suit = suit;
}
public Face getFace() {
return face;
}
public void setFace(Face face) {
this.face = face;
}
public String toString() {
return this.getSuit() + " " + this.getFace();
}
public int compareTo(Card o) {
int res = 0;
int self = this.getValue();
int oelf = o.getValue();
res = (self == oelf) ? 0 : ((self > oelf) ? 1 : -1);
return res;
}
public int getValue() {
String seq = this.getFace().toString();
if (seq.matches("\\d+")) {
return Integer.parseInt(this.getFace().toString());
} else {
char c = seq.charAt(0);
switch (c) {
case 'A':
return 1;
case 'J':
return 11;
case 'Q':
return 12;
case 'K':
return 13;
default:
return 0;
}
}
}
}
enum Suit {
SPADE {
public String toString() {
return "Spade";
}
},
HEART {
public String toString() {
return "Heart";
}
},
CLUB {
public String toString() {
return "Club";
}
},
DIAMOND {
public String toString() {
return "Diamond";
}
}
}
enum Face {
ACE {
public String toString() {
return "A";
}
},
DEUCE {
public String toString() {
return "2";
}
},
THREE {
public String toString() {
return "3";
}
},
FOUR {
public String toString() {
return "4";
}
},
FIVE {
public String toString() {
return "5";
}
},
SIX {
public String toString() {
return "6";
}
},
SEVEN {
public String toString() {
return "7";
}
},
EIGHT {
public String toString() {
return "8";
}
},
NINE {
public String toString() {
return "9";
}
},
TEN {
public String toString() {
return "10";
}
},
JACK {
public String toString() {
return "J";
}
},
QUEEN {
public String toString() {
return "Q";
}
},
KING {
public String toString() {
return "K";
}
}
}
热心网友
时间:2023-11-09 11:58
楼上正解,真快
玩了一把
110,我赢了
哈哈
热心网友
时间:2023-11-09 11:59
import java.util.Random;