改编版,玩法不一样。
账号密码admin
User类
package poker;
public class User {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
Card类
package poker;
import java.util.ArrayList;
import java.util.List;
public class Card {
public String suit; //花色
public String cardNum; //点数
public String[] suits = new String[]{"红桃", "梅花", "方块", "红心"}; //花色集合
public String[] cardNums = new String[]{"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; //点数集合
@Override
public String toString() {
return suit + cardNum;
}
public Card() {
}
public Card(String suit, String cardNum) {
this.suit = suit;
this.cardNum = cardNum;
}
public static List<Card> sendCard() {
List<Card> allCards = new ArrayList<>();
Card card = new Card();
for (int i = 0; i < card.suits.length; i++) {
for (int j = 0; j < card.cardNums.length; j++) {
allCards.add(new Card(card.suits[i], card.cardNums[j]));
}
}
return allCards;
}
public static Boolean compare(List<Card> yourcardList, List<Card> competitorCardList) {
int youCount = 0;
int competitorCount = 0;
for (Card card : yourcardList) {
String cardNum = card.cardNum;
switch (cardNum) {
case "J":
cardNum = "10";
break;
case "Q":
cardNum = "11";
break;
case "K":
cardNum = "12";
break;
case "A":
cardNum = "13";
break;
}
youCount += Integer.parseInt(cardNum);
}
for (Card card : competitorCardList) {
String cardNum = card.cardNum;
switch (cardNum) {
case "J":
cardNum = "10";
break;
case "Q":
cardNum = "11";
break;
case "K":
cardNum = "12";
break;
case "A":
cardNum = "13";
break;
}
competitorCount += Integer.parseInt(cardNum);
}
System.out.println("您的点数之和是" + youCount);
System.out.println("对手的点数之和是" + competitorCount);
if (youCount > competitorCount) {
return true;
} else {
return false;
}
}
}
开始游戏类
package poker;
import java.util.*;
public class GameStart {
public static void main(String[] args) {
int loginResult = login();
while (loginResult == 0) {
loginResult = login();
if (loginResult == 1) {
break;
}
}
User yourself = new User(); //实例化一个自己的对象。
User competitor = new User(); //实例化一个竞争者对象。
System.out.println("===================================请输入您的姓名===================================");
Scanner sc = new Scanner(System.in);
yourself.setUsername(sc.nextLine()); //设定自己的名字
System.out.println("欢迎您,玩家:" + yourself.getUsername() + "!");
System.out.println("===================================请输入竞争者的姓名===================================");
competitor.setUsername(sc.nextLine()); //设定竞争者的名字
System.out.println("您的竞争对手设定为:" + competitor.getUsername() + "!");
System.out.println("===================================开始发牌:===================================");
List<Card> allCards = Card.sendCard();
System.out.print("牌已发完:");
System.out.println(allCards);
Collections.shuffle(allCards); //打乱顺序
System.out.println("===================================洗牌完毕===================================");
Boolean tag = true;
List<Card> cardList = new ArrayList<>();
while (true) {
try {
System.out.println("请抽第一张卡,输入1-52中的任意数字:");
Card firstCard = allCards.get(Integer.parseInt(sc.nextLine()) - 1); //这样如果输入字符串 抛出的是NumberFormatException 不会无限循环。
cardList.add(firstCard);
System.out.println("您抽的第一张卡是:" + firstCard);
System.out.println("请抽第二张卡,输入1-52中的任意数字:");
Card secondCard = allCards.get(Integer.parseInt(sc.nextLine()) - 1);
cardList.add(secondCard);
System.out.println("您抽的第二张卡是:" + secondCard);
System.out.println("请抽第三张卡,输入1-52中的任意数字:");
Card thirdCard = allCards.get(Integer.parseInt(sc.nextLine()) - 1);
cardList.add(thirdCard);
System.out.println("您抽的第三张卡是:" + thirdCard);
System.out.println("您抽取的三张卡分别是" + cardList);
break;
} catch (Exception e) {
System.out.println("请重新输入");
cardList.clear();
continue;
}
}
//竞争对手抽牌
List<Card> competitorCardList = new ArrayList<>();
competitorCardList.add(allCards.get(new Random().nextInt(51) + 1));
competitorCardList.add(allCards.get(new Random().nextInt(51) + 1));
competitorCardList.add(allCards.get(new Random().nextInt(51) + 1));
System.out.println(competitor.getUsername() + "的牌分别是" + competitorCardList);
if (Card.compare(cardList, competitorCardList)) {
System.out.println("您获胜了");
} else {
System.out.println("您输了,再接再厉!");
}
;
}
public static int login() {
System.out.println("===================================欢迎来到勇者斗恶龙赌场!!===================================");
Scanner sc = new Scanner(System.in);
System.out.println("请输入账号");
String username = sc.nextLine();
while ((username.trim()).equals("")) {
System.out.println("账号不能为空,请输入账号");
username = sc.nextLine();
}
System.out.println("请输入密码");
String password = sc.nextLine();
while ((password.trim()).equals("")) {
System.out.println("密码不能为空,请输入账号");
password = sc.nextLine();
}
if (username.equals("admin") && password.equals("admin")) {
System.out.println("登录成功");
return 1;
} else {
System.out.println("登录失败");
return 0;
}
}
}
热门评论
感觉在第二三次抽牌之前也加上一句Collections.shuffle(allCards); 重新洗牌比较好,不然如果三次都输入同一个数字的话,“我”三次抽到的都是同一张牌。仅个人建议哈~