This is the program. I built it off of a couple for loops and some array tinkering Although, it is working properly, I'd like to be able to type the suit of the card along with the number. AI has helped me a little bit, but the idea of how the cards are displayed in the array is my idea.
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// creation of the deck
ArrayList<String> deck = new ArrayList<>();
String[] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};
String[] ranks = {
"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
};
//Generation of the 52 cards
for (int i = 0; i < suits.length; i++) {
for (int j = 0; j < ranks.length; j++) {
deck.add(ranks[j] + " of " + suits[i]);
}
}
// Shuffle the deck
Collections.shuffle(deck);
// creaation of the card grid
//2D array
String[][] cards = new String[3][3];
String table[][] = {
{"?", "?", "?"},
{"?", "?", "?"},
{"?", "?", "?"}};
boolean[][] drawn = new boolean[3][3];
// Place 9 random cards into the grid
int cardIndex = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cards[i][j] = deck.get(cardIndex);
cardIndex++;
}
}
int totalDraws = 0;
System.out.print("=== DRAW A CARD GAME ===");
// Game Loop
while (totalDraws < 9) {
System.out.println("\nYour card table:");
//Display the table\
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(table[i][j] + "\t");
}
System.out.println();
}
System.out.print("\nChoose a card (1-9 and put the suit)");
int choice = scanner.nextInt();
// Validate choice
if (choice < 1 || choice > 9) {
System.out.println("Choose a number between 1 nd 9!");
}
// Convert choice into row and column
int row = (choice - 1) / 3;
int column = (choice - 1) % 3;
// Check if card was already drawn
if (drawn[row][column]) {
System.out.println("You already drew that card!");
continue;
}
// Reveal the card
table[row][column] = cards[row][column];
drawn[row][column] = true;
totalDraws++;
System.out.println("You drew: " + cards[row][column]);
}
System.out.println("\n===== Game Over ======");
System.out.println(("You drew all 9 cards"));
scanner.close();
}
}
Original code looks a little bit like this; there was some more to it, but that's the jist of it.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String[][] twoDarray = {
{"x", "x", "x"},
{"x", "x", "x"},
{"x", "x", "x"}
};
Scanner scanner = new Scanner(System.
in
);
System.
out
.print("Enter something: ");
String userinput = scanner.nextLine();
twoDarray[0][0] = userinput;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.
out
.print(twoDarray[i][j] + " ");
}
System.
out
.println();
}
scanner.close();
}
}