package main;
import java.util.Scanner;
public class test {
public static String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Ace", "Jack", "Queen", "King"};
public static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
String[] array = new String[13];
for(int i = 0; i<array.length; i ) {
array[i] = ranks[i] ;
}
for (int i = 0; i<array.length; i ) {
System.out.println(array[i]);
}
cutDeck(array);
for (int i = 0; i<array.length; i ) {
System.out.println(array[i]);
}
}
public static String[] cutDeck(String[] deck) {
System.out.println("Cut please. 'Choose between 1-51'");
int cutPoint = scanner.nextInt();
String[] topDeck= new String[52];
String[] bottomDeck = new String[52];
String[] newDeck = new String[deck.length];
for (int i = 1; i<=cutPoint ; i ) { // Topdeck
topDeck[i-1] = deck[deck.length-1*i];
}
for (int i = 0; i < cutPoint / 2; i ) // Reverse topdeck
{
String temp = topDeck[i];
topDeck[i] = topDeck[topDeck.length - i - 1];
topDeck[topDeck.length - i - 1] = temp;
}
for (int i = 0; i<deck.length - cutPoint; i ) { //Bottom cut point
bottomDeck[i] = deck[i];
}
for (int i = 0; i<deck.length; i ) {
if (cutPoint > i) {
newDeck[i] = topDeck[i];
} else {
newDeck[i] = bottomDeck[i];
}
}
return newDeck;
}
}
I am trying to cut the deck while asking to the user.
This function does not cut the deck.
Where am I doing wrong ?
I tried everything but I lost my brain can you guys help me please?
I am open to another ideas so you can give improve my code.
Thanks in advance.
CodePudding user response:
I can see three obvious issues with this code:
The first issue is that the cutDeck method is not actually modifying the original deck array. Instead, it is creating a new array and returning it. In order to actually shuffle the original deck, you would need to modify the cutDeck method to modify the original array, rather than returning a new one.
Another issue is that the cutDeck method is currently assuming that the deck has 52 cards. However, the code in the main method is only creating an array with 13 cards. This means that the cutDeck method will not work properly with the deck created in main.
Finally, the code is currently hard-coding the number of cards in the deck as 52, and it is also assuming that the user will always enter a valid cut point when prompted. It would be better to make the code more flexible and robust by using the length of the input array to determine the number of cards in the deck, and also by checking that the user's input is within the valid range for the deck size.
An alternative way to do this would be:
public static void cutDeck(String[] deck) {
System.out.println("Cut please. 'Choose between 1-" (deck.length - 1) "'");
int cutPoint = scanner.nextInt();
// Check that the cut point is within the valid range for the deck size
if (cutPoint < 1 || cutPoint >= deck.length) {
System.out.println("Invalid cut point. Please try again.");
return;
}
// Create the top and bottom halves of the deck
String[] topDeck = new String[cutPoint];
String[] bottomDeck = new String[deck.length - cutPoint];
for (int i = 0; i < cutPoint; i ) {
topDeck[i] = deck[i];
}
for (int i = cutPoint; i < deck.length; i ) {
bottomDeck[i - cutPoint] = deck[i];
}
// Reverse the top half of the deck
for (int i = 0; i < cutPoint / 2; i ) {
String temp = topDeck[i];
topDeck[i] = topDeck[topDeck.length - i - 1];
topDeck[topDeck.length - i - 1] = temp;
}
// Combine the top and bottom halves to create the shuffled deck
for (int i = 0; i < deck.length; i ) {
if (i < cutPoint) {
deck[i] = topDeck[i];
} else {
deck[i] = bottomDeck[i - cutPoint];
}
}
}
CodePudding user response:
Another way is to use existing methods in the Arrays
API and the List
API
public static String[] cutDeck (String [] deck, int n) {
List<String> cutDeck = new ArrayList<> (deck.length);
List<String> bottom = Arrays.asList (Arrays.copyOfRange (deck,n, deck.length));
cutDeck.addAll (bottom);
List<String> top = Arrays.asList (Arrays.copyOfRange (deck, 0, n));
cutDeck.addAll (top);
return cutDeck.toArray(deck);
}
This assumes your deck
is 1D array of String
. Your test
or other method can call it like this:
array = cutDeck (array, cutPoint);
Note that this requires the value for cutPoint
be set before calling the cut
method. Moving the code to interact with the user to a different method makes the cutDeck
method more cohesive.
Note that this does not reverse part of the deck. In real life, cutting a deck of cards does not reverse the order of part of the deck, unless part of the deck is flipped over. This is normally not done, since playing cards in a deck should all have the same back. If you want to reverse part of the deck, you may add your own reverse
method.
As noted in the previous answer, cutDeck(array);
in your code causes the shuffled deck result to be ignored. If you want to keep your original code, change that line to array = cutDeck(array);
The other points in the first answer are also valid:
- You want to ensure the
cutPoint
is valid. - The
cutDeck
method is more flexible if it gets its length from the array parameter
By the way, the Arrays
API has several toString
methods. Using it can shorten your code for printing arrays, but you have less control over formatting the output:
System.out.println (Arrays.toString(array));