我该如何归还我的卡片?

时间:2015-04-30 13:11:36

标签: java arrays arraylist

我创建了一个纸牌游戏,我不知道如何返回我的数组,因为我过去使用过不同的方法使用instanceof但从来没有像这样的整个数组。我把我的代码放在下面:

卡类 -

/**
 *
 * Constructor for a single card
 *
 */

public class Card {
  public String number;
  public String suit;

  public Card(String n, String s) {
    number = n;
    suit = s;
  }

  @
  Override
  public String toString() {
    return number + suit;
  }
}

甲板课 -

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class Deck {
  private ArrayList < Card > cards;

  public Deck() {
    cards = new ArrayList < Card > ();
  }

  public void buildDeck() throws IOException {

    List < String > cardLines = Files.readAllLines(Paths.get("cards.txt"));



    for (int i = 0; i < cardLines.size(); i += 2) { //if lines read are smaller than file then read for another card
      cards.add(new Card(cardLines.get(i), cardLines.get(i + 1))); //add new card to cards array
    }

  }

  public List < Card > getDeck() {
    return cards;

    /*public void shuffle() {
        // fill in
    }*/

  }
}

GUI类 -

import java.util.List;
import java.util.Scanner;


public class Game {

  private Scanner scan;
  private Deck deck;

  private void runMenu() {
    String response;
    do {
      printMenu();
      System.out.println("What would you like to do:");
      scan = new Scanner(System.in);
      response = scan.nextLine().toUpperCase();
      switch (response) {
        case "1":
          PrintDeck();
          break;
        case "2":
          ShuffleCards();
          break;
        case "3":
          DealCard();
          break;
        case "4":
          MoveToPrevious();
          break;
        case "5":
          Move2PilesBack();
          break;
        case "6":
          AmalgamateInMiddle();
          break;
        case "7":
          PlayforMe();
          break;
        case "8":
          ShowLowScores();
        case "Q":
          break;
        default:
          System.out.println("Try again");
      }
    } while (!(response.equals("Q")));
  }

  private void ShowLowScores() {
    // TODO Auto-generated method stub

  }

  private void PlayforMe() {
    // TODO Auto-generated method stub

  }

  private void AmalgamateInMiddle() {
    // TODO Auto-generated method stub

  }

  private void Move2PilesBack() {
    // TODO Auto-generated method stub

  }

  private void MoveToPrevious() {
    // TODO Auto-generated method stub

  }

  private void DealCard() {
    // TODO Auto-generated method stub

  }

  private void ShuffleCards() {
    // TODO Auto-generated method stub

  }

  private void PrintDeck() {
    List < Card > buildadeck = deck.getDeck();
    System.out.println(buildadeck);


  }

  private void printMenu() {

    System.out.println("1 -  Print the pack ");
    System.out.println("2 -  Shuffle");
    System.out.println("3 -  Deal a card");
    System.out.println("4 -  Move last pile onto previous one");
    System.out.println("5 -  Move last pile back over two piles");
    System.out.println("6 -  Amalgamate piles in the middle");
    System.out.println("7 -  Play for me");
    System.out.println("8 -  Show low scores");
    System.out.println("q - Quit");

  }

  public static void main(String args[]) {
    System.out.println("****Welcome to patience is virtue****");
    Game cardsgame = new Game();
    cardsgame.runMenu();
    System.out.println("****Thanks for playing****");
  }
}

我刚刚更新了代码,没有语法错误但是当我运行程序时,我在PrintDeck中遇到异常问题,任何想法为什么?

2 个答案:

答案 0 :(得分:1)

我希望我明白你在问什么。 基本上你想要下面的方法,返回卡片列表,这样你就可以在GUI中打印列表了吗?

使用buildDeck方法制作套牌,并将卡片添加到卡片列表中。你不需要返回任何东西。相反,您可以为卡组创建一个getter方法,如下所示。

public void buildDeck() throws IOException {

    List<String> cardLines = Files.readAllLines(Paths.get("cards.txt"));

    for (int i = 0; i < cardLines.size(); i += 2) { //if lines read are smaller than file then read for another card
        cards.add(new Card(cardLines.get(i), cardLines.get(i + 1))); //add new card to cards array
    }
}

public List<Card> getDeck() {
     return cards;
}

Game课程中你应该这样做:

public class Game {
    private Deck deck;

public void runMenu() {
    deck = new Deck();
    deck.buildDeck();

然后在printDeck中,您可以按如下方式使用此实例:

public void printDeck() {
    for(Card card : deck.getDeck()) {
        // print card.
    }
}

完全不同的是,您的方法应该以小写字母开头。 printDeck而非PrintDeck

答案 1 :(得分:0)

我会将printDeckMethod改为此。

<select ng-init="Request.CategoryID=Request.CategoryDescription " ng-model="Request.CategoryID"  ng-options="category.CategoryID as category.CategoryDescription for category in Module.RequestReasons" >
                    <option  default value="">Reasons</option></select>

通过这种方式,印刷版不会担心卡片的创建,只需打印它。您构建代码的代码中的其他地方。

相关问题