Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to learn how to make a poker game, so i searched around websites for poker projects.
This is the deck class i found.
This is the part of the code that make no sense to me.
int index_1, index_2;
for (int i = 0; i < 100; i++) {
index_1 = generator.nextInt(cards.size() - 1);
index_2 = generator.nextInt(cards.size() - 1);
temp = cards.get(index_2);
cards.set(index_2, cards.get(index_1));
cards.set(index_1, temp);
}
}
Looks like a random shuffle of the deck, picks two card positions at random. The nextInt(cards.size() - 1) will generate a random index in [0,cards.size() - 1) range:
index_1 = generator.nextInt(cards.size() - 1);
index_2 = generator.nextInt(cards.size() - 1);
swap the two cards, since you have to replace one of the cards first and you don't want to lose that object, the code uses temp to hold the first card to be replaced, cards.get(index_2) will obtain the first card object.
temp = cards.get(index_2);
The first set will replace the object in index_2 will the object from index_1 and the second set will replace the object in index_1 with the temporary object we stored earlier:
cards.set(index_2, cards.get(index_1));
cards.set(index_1, temp);
It does this procedure 100 times:
for (int i = 0; i < 100; i++)
If we assume cards.size() - 1 is the index of the last card in the deck, the nextInt call will skip the last card though, hard to know if that was intentional without more context.
What this code is doing is it's swapping two cards in the deck. It does this 100 times arbitrarily. A better way to do this is to randomly assign a card to a position in a deck. The way it is right now may never touch certain groups of cards and you may end up with a clump of cards in the same order as before in the same position. Plus it would only involve 52 moves instead of 300 (card2 -> temp, card1 -> card2, temp -> card1 ).
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have a 1D matrix with data and another with scores. I'm trying to loop across all of the elements in the data and find the element in the same position in the score matrix, and keep adding these values together during each loop. For some reason, my script keeps starting from sum = zero instead of retaining and adding to the sum from the previous loop. For the below example, I expect sum = 1 in the first loop, 3 after the second loop (since 1+2=3) and 6 after the third loop (3+3=6). Instead, sum just yields the last value retrieved from scores. What am I doing wrong here?
public static int calc_score( )
{
String [] dat = {"A", "B","C"};
int [][] scores = new int [1][3];
scores[0][0] = 1;
scores[0][1] = 2;
scores[0][2] = 3;
int sum = 0;
for (int i = 0; i < dat[0].length(); i++)
{
if (dat[i].equals("A")) {
sum = sum + scores[i][0];
// scores[i][0] returns the expected value of 1 in the first loop
}
else if (dat[i].equals("B")) {
sum = sum + scores[i][1];
}
else if (dat[i].equals("C")) {
sum = sum + scores[i][2];
}
}
System.out.println(sum);
return sum;
}
I tried modifying sum = sum + scores[i][1]; to sum+=scores[i][1] but that doesn't fix this. I have to be missing something simple.
Learn to debug. Add println statements, or use a debugger, and track, on paper if you prefer, what you think the program should do. Where the computer does something different from what you thought: Voila. You found a bug; there may be more.
If you can't figure out why something is happening, go back and re-check assumptions.
Had you done that here, for example, you might have eventually noticed: Huh, that for loop really is only running exactly once, that's bizarre. Eventually you'd check what dat[0].length() returns and then realized it returns, mysteriously, 1, and perhaps then you'd have one of those slap yourself on the forehead moments: dat[0] refers to the first entry in the dat array, so, the string "A". Then you ask that string about length, which dutifully returns 1.
I assume you wanted dat.length instead.
Note that scores[1][0] is 0 too, you have more than one problem here.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 years ago.
Improve this question
i need to sort an array of the type intgere in a basic way (nothing too complex) for a school computer science project. It would be nice if sb could give me not only a one sentence answer waht to consider but also some code i can work with.
thanks a lot.
You could've either googled it but since you didn't, use this bubblesort method:
boolean swapped; // to notice swaps during a pass
do {
swapped = false;
for (int i=1; i<a.length; i++)
if (a[i-1] > a[i]) {
// Swap!
int swap = a[i];
a[i] = a[i-1];
a[i-1] = swap;
swapped = true;
}
} while (swapped); // another pass if swaps happened
It swaps the ints next to each other until you have them sorted from the smallest to the greatest number. If you want it the other way around simply swap the ">" with a "<".
I hope that will help you.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Having a bit of issue here:
Question(input): complete the function draftPick(). function takes two arguments: an array of integers teams representing the order of teams and how many spots they have to fill, and an integer i that determines which team in the order (zero-indexed) we want to know the last pick for.
Output: function should return an integer representing which player the ith team in the draft order (zero-indexed) will select with their last pick
Expected runtime: O(n)
sample input: lineArray = [5, 1, 2]
position = 2
sample output: 5
Explanation: call the tree teams A, B, C. The draft has team A picking first and trying to fill 5 roster spots, team B picking second and filling 1 roster spot, and team C picking last and filling 2 roster spots:
turn 1: A picks 1st best player
turn 2: B picks 2nd best player
turn 3: team C picks 3rd best player
turn 4: team A picks again and get 4th best player
turn 5: since team b has already filled all their roster spots, team C picks 5th best player (THUS THE ANSWER 5)
Attempt: trying to develop an algorithm - i see that the ith team for example in [5,1,2] this team has 2 spots to fill. we know that the order of play he picks depends on the number of players in the other elements. i tried traversing the array subtracting (-1) from each element from i=0 to i
Here's an algorithm that I made that suffices according to my testing:
int draftPick(int[] teams, int target) {
int targetTeam = teams[target];
int finalPick = 0;
for(int i = 0; i <= target; i++) {
int team = teams[i];
if(team > targetTeam) team = targetTeam;
finalPick += team;
}
for(int i = (target + 1); i < teams.length; i++) {
int team = teams[i];
if(team > targetTeam - 1) team = targetTeam - 1;
finalPick += team;
}
return finalPick;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
The following code shuffles playing cards in a game. I have to explain what the code does in a presentation. I am a newbie in Java coding, so can anyone please explain to me the following code line by line:
// Deck shuffling method
public void shuffleDeck() {
//Seed the Random instance with nanoTime
Random random = new Random(System.nanoTime());
for(int i = 0; i < 52; i++) {
int swapIndex = random.nextInt(52);
if (swapIndex != i) {
PlayingCard temp = cardDeckArray[i];
cardDeckArray[i] = cardDeckArray[swapIndex];
cardDeckArray[swapIndex] = temp;
}
}
cardIndex = 0; //Next card to be pulled off the deck
}
This is a shuffle of a deck, at each iteration you swap two cards at random, and it will result in some random permutation of the deck.
However, this algorithm is flawed and biased, there are some permutations that are more likely to be generated than others, to use an unbiased shuffle, you should use fisher-yates shuffle (which is basically the same idea, but generate a random number between i to 52, instead between 0 to 52)
The reason and results of this bias is discussed thoroughly in the thread: What distribution do you get from this broken random shuffle?
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What would this code do?
for(int i = 0; i < enemysno; i++){
g.drawString("\nArray size: " + i, 200, 200);
}
enemysno is a random number between 0 and 10, and works fine. Problem is, it loops once, but then stops adding new lines after the first iteration.
As Psuedo code, I though the i starts as 0. Then compares the condition, if its false, does the code, then makes the ++ iterations, then repeats the loop?
Ultimatly, I want to add n objects to an array, but I can quite get this to work simple array to work!
A simple test proves the loop indeed works as intended:
public static void main(String[] args)
{
int enemysno = 5;
for (int i = 0; i < enemysno; i++)
{
System.out.println("lalala " + i);
}
}
this works fine producing
lalala 0
lalala 1
lalala 2
lalala 3
lalala 4
It was kind of obvious, but via debugging or such a test you could determine that the loop itself is entered the desired numer of times. The problem must be in your string display: most probably your drawString method overwrites the printed string each time.
It should be obvious if you checked the numbers on your output.
The solution?
use a string builder to concatenate the partial strings and then draw the final string using your drawString method