Java(HighAndLow)

Java

Q
High And Lowゲームを作成する。
[流れ]
1.52枚のカードの中から1枚カードが表示される。
2.2枚目のカードが1枚目のカードより大きいか小さいかを予想する。この際大きい小さいは基本カードの数字に依存するが1は例外で最大とする。
3.当たったら掛け金が倍になる。
4.外れるまで行い、最大どこまで到達したかを記録する。
なお、ターンごとにカード全体がシャッフルされそこから2枚を選ぶこととする。
2枚のカードの数字が同じ場合はDrawとし、Goldはそのままで継続する。
最初は1000goldから始める。
詳しくは実行例を参考のこと

[実行例]
****High And Low****
HighScore:1000
Gold:1000
****turn1****
H4
High…h Low…l>h
S13
Win
Gold:2000
****turn2****
H10
High…h Low…l>l
C5
Win
Gold:4000
****turn3****
S5
High…h Low…l>h
D4
Lose
Gold:0
GameOver
4000まで行きました。
もう一回やりますかYes…y NO…n>y
HighScore:4000
Gold:1000
****turn1****
H6
High…h Low…l>h
H2
Lose
Gold:0
GameOver
1000まで行きました。
もう一回やりますかYes…y NO…n>y
HighScore:4000
Gold:1000
****turn1****
H9
High…h Low…l>l
D9
Draw
Gold:1000
****turn2****
H13
High…h Low…l>l
H1
Lose
Gold:0
GameOver
1000まで行きました。
もう一回やりますかYes…y NO…n>n
今回の記録は4000でした。
アプリケーションを終了します。

[解答例]
Card.java

01public class Card {
02  String suit;
03  int no;
04  int point;
05  Card(String suit,int no){
06    this.suit=suit;
07    this.no=no;
08    this.point=no==1? 14:no;
09  }
10  String show() {
11    return this.suit+this.no;
12  }
13}

HighLowApp.java

001import java.util.Random;
002import java.util.Scanner;
003public class HighLowApp {
004  static Card[] deck=new Card[52];
005  static int turn=0;
006  static int highScore=1000;
007  static int gameScore;
008  static int gold;
009  static Scanner scan=new Scanner(System.in);
010  static Random rand=new Random();
011  /**
012   * メインメソッド
013   * @param args
014   */
015  public static void main(String[] args) {
016    //52枚のカード生成
017    createCard();
018    //オープニング
019    System.out.println("****High And Low****");
020    //ゲーム全体の繰り返し
021    while(true) {
022      gameStart();
023      //ターンごとの繰り返し
024      while(true) {
025        if(!isTurnContinue()) {
026          if(isGameEnd()) {
027            System.out.println("今回の記録は"+highScore+"でした。");
028            System.out.println("アプリケーションを終了します。");
029            scan.close();
030            return;
031          }
032          break;
033        }
034      }
035    }
036  }
037  /**
038   * 52枚のカードを生成する
039   */
040  private static void createCard() {
041    String[] suits= {"S","D","H","C"};
042    for(int i=0;i<suits.length;i++) {
043      for(int j=0;j<13;j++) {
044        deck[i*13+j]=new Card(suits[i],j+1);
045      }
046    }
047  }
048  /**
049   * ゲームの最初に表示する
050   */
051  private static void gameStart() {
052    turn=0;
053    gold=1000;
054    gameScore=gold;
055    System.out.printf("HighScore:%d%n",highScore);
056    System.out.printf("Gold:%d%n",gold);
057  }
058  /**
059   * ゲームのメインロジック。
060   * ゲームを行い。負けた場合にfalseを返す
061   * @return 勝ったか?
062   */
063  private static boolean isTurnContinue() {
064    boolean isContinue=true;
065    turn++;
066    System.out.printf("****turn%d****%n", turn);
067    Card[] twoCards=new Card[2];
068    for(int i=0;i<2;i++) {
069      int index=rand.nextInt(deck.length-i);
070      twoCards[i]=deck[index];
071      Card temp=deck[index];
072      deck[index]=deck[deck.length-1-i];
073      deck[deck.length-1-i]=temp;
074    }
075    System.out.println(twoCards[0].show());
076    System.out.print("High...h Low...l>");
077    String select=scan.next();
078    System.out.println(twoCards[1].show());
079    if(twoCards[0].point==twoCards[1].point) {
080      System.out.println("Draw");
081    }else if(
082        (select.equals("h") && twoCards[0].point < twoCards[1].point)
083        ||(select.equals("l") && twoCards[0].point > twoCards[1].point)
084        ){
085      System.out.println("Win");
086      gold *=2;
087      gameScore=gold;
088    }else {
089      System.out.println("Lose");
090      gold=0;
091      isContinue=false;
092    }
093    System.out.println("Gold:"+gold);
094    return isContinue;
095  }
096  /**
097   * 外したときに遷移する。
098   * 記録を表示し、継続するかを尋ねる
099   * @return ゲームをやめるか? trueでやめる
100   */
101  private static boolean isGameEnd() {
102    System.out.println("GameOver");
103    System.out.printf("%dまで行きました。%n",gameScore);
104    if(gameScore > highScore) {
105      highScore=gameScore;
106    }
107    System.out.print("もう一回やりますかYes...y NO...n>");
108    String select=scan.next();
109    return select.equals("n");
110  }
111}

[記録]
さっきやったらこんな記録がでました!
2018/07/23 turn20 65536000
****turn20****
S7
High…h Low…l>h
D3
Lose
Gold:0
GameOver
65536000まで行きました。
もう一回やりますかYes…y NO…n>n

コメント

タイトルとURLをコピーしました