Search code examples
javabluej

solitaire shuffle and upper string an asterisk for back cards


I'm creating a solitaire game. *Where there are 28 value in the elements of array for play area. The play area is in the solitaire format. * And there are 24 value in the elements of array for bank. I loop it but it doesn't works

How can i turn to upside down right triangle (solitaire format) the elements on the array? With shuffling it. *And also how to turn the upper strings an asterisk that stands for the back of the cards.

public static void main(String[] args) {
  int row, space, column;
 //declaring 52 cards for solitaire
String[] cards = { "A@", "A#", "A$", "A&",  "2@", "3@", "4@", "5@", "6@",
                   "7@","8@", "9@", "10@", "K@", "Q@", "J@", "2#", "3#", "4#",
                   "5#", "6#", "7#", "8#","9#", "10#", "K#", "J#",  "2$" , 
                   "3$", "4$" , "5$", "6$" , "7$", "8$" , "9$", "10$" , "K$", "Q$", "J$",
                    "2&" , "3&", "4&" , "5&", "6&" , "7&", "8&" , "9&", "10&" , "K&", "Q&",
                   "J&", "Q#" };

List<String> list = Arrays.asList(cards);

Collections.shuffle(list);

//looping for solitaire format
for(row=1;row<=28;row++)
{
     for(String alpha : list){
    for(space=28;space>=row;space--)
    {
        System.out.print(" ");
    }

    for(column=1;column<=row;column++)
    {

        System.out.print("  " +alpha);

    }

    System.out.print("\n");

Solution

  • Assuming each "card" occupies one row, then your problem is you want 7 rows with an decreasing number of columns in each row (7 + 6 + 5 + 4 + 3 + 2+ 1 = 28 cards) - not 28 rows.

    public static void main(String... args) {
        String[] cards =
            { "A@", "A#", "A$", "A&", "2@", "3@", "4@", "5@", "6@", "7@", "8@", "9@",
                "10@", "K@", "Q@", "J@", "2#", "3#", "4#", "5#", "6#", "7#", "8#",
                "9#", "10#", "K#", "J#", "2$", "3$", "4$", "5$", "6$", "7$", "8$",
                "9$", "10$", "K$", "Q$", "J$", "2&", "3&", "4&", "5&", "6&", "7&",
                "8&", "9&", "10&", "K&", "Q&", "J&", "Q#" };
    
        List<String> list = Arrays.asList(cards);
    
        Collections.shuffle(list);
        final int columns = 7;
        final int rows = 7;
        int card = 0;
    
        // loop over rows
        for (int i=0; i<rows; i++) {
            // Fill empty columns in this row
            for (int j=0; j<i; j++) {
                System.out.print("\t");
            }
            // Add #columns - row# cards to this row
            for (int j=i; j<columns; j++) {
                System.out.print(list.get(card++) + "\t");
            }
            // advance to next row
            System.out.println();
        }
    }
    

    If this doesn't solve your problem, you may want to look over a glossary of solitaire terms to better describe what you're trying to do.