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
05 | Card(String suit, int no){ |
08 | this .point=no== 1 ? 14 :no; |
11 | return this .suit+ this .no; |
HighLowApp.java
001 | import java.util.Random; |
002 | import java.util.Scanner; |
003 | public class HighLowApp { |
004 | static Card[] deck= new Card[ 52 ]; |
006 | static int highScore= 1000 ; |
007 | static int gameScore; |
009 | static Scanner scan= new Scanner(System.in); |
010 | static Random rand= new Random(); |
015 | public static void main(String[] args) { |
019 | System.out.println( "****High And Low****" ); |
025 | if (!isTurnContinue()) { |
027 | System.out.println( "今回の記録は" +highScore+ "でした。" ); |
028 | System.out.println( "アプリケーションを終了します。" ); |
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 ); |
051 | private static void gameStart() { |
055 | System.out.printf( "HighScore:%d%n" ,highScore); |
056 | System.out.printf( "Gold:%d%n" ,gold); |
060 | * ゲームを行い。負けた場合にfalseを返す |
063 | private static boolean isTurnContinue() { |
064 | boolean isContinue= true ; |
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; |
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" ); |
082 | (select.equals( "h" ) && twoCards[ 0 ].point < twoCards[ 1 ].point) |
083 | ||(select.equals( "l" ) && twoCards[ 0 ].point > twoCards[ 1 ].point) |
085 | System.out.println( "Win" ); |
089 | System.out.println( "Lose" ); |
093 | System.out.println( "Gold:" +gold); |
099 | * @return ゲームをやめるか? trueでやめる |
101 | private static boolean isGameEnd() { |
102 | System.out.println( "GameOver" ); |
103 | System.out.printf( "%dまで行きました。%n" ,gameScore); |
104 | if (gameScore > highScore) { |
107 | System.out.print( "もう一回やりますかYes...y NO...n>" ); |
108 | String select=scan.next(); |
109 | return select.equals( "n" ); |
[記録]
さっきやったらこんな記録がでました!
2018/07/23 turn20 65536000
****turn20****
S7
High…h Low…l>h
D3
Lose
Gold:0
GameOver
65536000まで行きました。
もう一回やりますかYes…y NO…n>n
コメント