Note that there are some explanatory texts on larger screens.

plurals
  1. POCard Game - Problems with selection sort of players hand
    text
    copied!<p>We are making a simple 52 card game. We are having a lot of trouble dealing, shuffling and storing cards. 5 cards should be dealt to each player. The game is similar to Hearts but each player only gets 5 cards. This is our code so far:</p> <pre><code> int[] deck = new int[52]; String[] suits = { "Spades", "Hearts", "Diamonds", "Clubs" }; String[] ranks = { "A ", "2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10", "J ", "Q ", "K " }; // Initialize the cards for (int i = 0; i &lt; deck.length; i++) deck[i] = i; // Shuffle the cards for (int i = 0; i &lt; deck.length; i++) { // Generate an index randomly int index = (int) (Math.random() * deck.length); int temp = deck[i]; deck[i] = deck[index]; deck[index] = temp; } // Display the first five cards System.out.println("player 1 has:"); for (int i = 0; i &lt; 5; i++) { String suit = suits[deck[i] / 13]; String rank = ranks[deck[i] % 13]; selectionSort(deck); System.out.println("Card number " + deck[i] + " : " + rank + " of " + suit); } int player1[] = deck; // Display the first five cards System.out.println("\n" + "player 2 has:"); for (int i = 5; i &lt; 10; i++) { String suit = suits[deck[i] / 13]; String rank = ranks[deck[i] % 13]; System.out.println("Card number " + deck[i] + " : " + rank + " of " + suit); } int player2[] = deck; // Display the first five cards System.out.println("\n" + "player 3 has:"); for (int i = 10; i &lt; 15; i++) { String suit = suits[deck[i] / 13]; String rank = ranks[deck[i] % 13]; System.out.println("Card number " + deck[i] + " : " + rank + " of " + suit ); } int player3[] = deck; } public static void selectionSort(int[] deck) { for (int i = 52; i &lt; deck.length - 1; i--){ int currentMax = deck[i]; int currentMaxIndex = i; for (int j = i + 1; j &lt; deck.length; j++) { if (currentMax &gt; deck[j]) { currentMax = deck[j]; currentMaxIndex = j; }} if (currentMaxIndex != i) { deck[currentMaxIndex] = deck[i]; deck[i] = currentMax; } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload