Reputation: 47
I creating a card game and I don't know how to return my array, as I have used different methods in the past using instanceof but never just a whole array like this one. I have put my code below:
Card class -
/**
*
* 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;
}
}
Deck class -
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 class -
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****");
}
}
I've just updated the code, no syntax errors but when I run the program I get a Exception problem in PrintDeck, any idea why?
Upvotes: 0
Views: 236
Reputation: 346
I hope I understand what you are asking. Basically you want the method below, to return the list of cards so you can print the list in your GUI?
Use the buildDeck
method to build your deck and add the cards to the cards list as you already do. You don't need to return anything. Instead you can create a getter method, for the deck as shown below.
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;
}
In Game
class you should do this:
public class Game {
private Deck deck;
public void runMenu() {
deck = new Deck();
deck.buildDeck();
Then in printDeck you can use this instance as follows:
public void printDeck() {
for(Card card : deck.getDeck()) {
// print card.
}
}
A whole different thing is that your methods should start with a lowercase letter. printDeck
and not PrintDeck
.
Upvotes: 1
Reputation: 3576
I would change the printDeckMethod to this.
private void printDeck(Deck deck) {
// Print the deck
}
This way the print deck is not worried about the creation of a deck, just printing it. Some where else in your code you build the deck.
Upvotes: 0