###前提・実現したいこと
私はプログラミングの勉強を始めて1週間に満たないものです。
現在Javaトランプゲームを作成中です。今実現させたいことは以下の通りです
13 x 4 枚のカードを作成しダイヤ1〜13、スペード1〜13、ハート1〜13、クラブ1〜13 の順に並べて山にする。
つまり、山の一番上(先頭)がダイヤのエースになっている。
そして、作成した山をテストで、main()を持つクラスで実行させたいのですが正しい実行のさせ方がわかりません。
そもそも山を作成するコードが間違っているのでしょうか。
今後シャッフルしたり、山を半分に分けたりして、最終的に大小ゲームをする予定です。
未熟者ですがよろしくお願いします
カードを作成するために以下のCardsクラスを作成しました。
Java
1public class Cards { 2 private int number; 3 private String mark; 4 public String anotherName; 5 private boolean open; 6 7 public Cards(String mark, int number) { 8 this.number = number; 9 this.mark = mark; 10 this.open = true; 11 if (this.number == 11) { 12 this.anotherName = this.mark + ":" + "Jack"; 13 } else if (this.number == 12) { 14 this.anotherName = this.mark + ":" + "Queen"; 15 } else if (this.number == 13) { 16 this.anotherName = this.mark + ":" + "King"; 17 } else if (this.number == 1) { 18 this.anotherName = this.mark + ":" + "Ace"; 19 } 20 } 21 22 public int getNumber() throws NotCardsOpenException { /*札の数を取得*/ 23 if (!this.open) { 24 throw new NotCardsOpenException("Cards Closed"); 25 } 26 return this.number; 27 } 28 29 public String getMark() throws NotCardsOpenException { /*札のマークを取得*/ 30 if (!this.open) { 31 throw new NotCardsOpenException("Cards Closed"); 32 } 33 return this.mark; 34 } 35以下長いので割愛させていただきます。
以下が山を作るために作成したCardsMountクラスです
Java
1import java.util.ArrayList; 2import java.util.List; 3 4public class CardsMount { 5 6 private ArrayList<Cards> cards; 7 8 public List<Cards> makeMountCards() { 9 cards = new ArrayList<Cards>(); 10 for (int i = 1; i <= 13; i++) { 11 cards.add(new Cards("クラブ", i)); 12 } 13 for (int i = 1; i <= 13; i++) { 14 cards.add(new Cards("ハート", i)); 15 } 16 for (int i = 1; i <= 13; i++) { 17 cards.add(new Cards("スペード", i)); 18 } 19 for (int i = 1; i <= 13; i++) { 20 cards.add(new Cards("ダイヤ", i)); 21 } 22 return cards; 23 } 24} 25
###該当のソースコード
訂正 以下のようなコードに書き直しましたが上手く表示されません。
Java
1public class TestRun { 2 3 public static void main(String[] args) throws NotCardsOpenException{ 4 CardsMount cardsmount = new CardsMount(); 5 for(int i = 0; i < cardsmount.makeMountCards().size(); i++){ 6 System.out.println(cardsmount.makeMountCards().get(i)); 7 } 8 } 9} 10