May 7, 201115 yr (Warning: You are now approaching a wall of text. For a TL;DR detour scroll to the bottom of my post! :]) Hello all. I wasn't exactly sure where to make a topic for the problem that I'm having. I figured I'd put it in this category since what I'm doing is not web-related, however, its a language I'm sure a lot of users on here are familiar with. Okay so I'm to make a blackjack program for my java dev class. Its rather simple (as i'm still learning) and can pretty much be summed up in the following steps: Assign two cards to the dealer. One will be shown, the other will not. Player will pick two cards (randomly chosen, of course) The player has the option of adding an additional four cards to his hand (player can have max 6 cards in hand) Total up dealer's/player's hands and determine a winner/loser I plan on setting up a handful of arrays with the purpose of holding dynamic values such as: Dealer's score Player's score Dealer's hand Player's hand In addition to these arrays, there will be an array that will work as a "deck", in other words, it will hold all the cards values and the path to their image location. Unfortunately, this is where I am stuck. My Problem I have setup a class called cards.java that is used to populate the array. Another class called hits.java will randomly generate a number that I will then use to point to an index position on the "deck" array. My main.java class wraps it all up and takes the value from hits.java and adds it to the frame. At least, that is how it was supposed to go according to plan. I've pretty much pinpointed the problem; which to me is somewhere in my cards.java class: public class cards { // Create a 2D Array, 51x51. public String[][] deck = new String[51][51]; // Create instance of main class. private main mi = new main(); // Run Method. public void runCards() { // Populate the array. for(int i = 0; i < deck.length; i++) { // Aces. for(int a = 0; a == 3; a++) { deck[a][0] = "1"; deck[a][1] = mi.home + "/My Documents/NetBeansProjects/CCP Blackjack/src/deck/a" + a + ".png"; } // King, Queen, Jack, Ten. for(int b = 4; b == 19; b++) { deck[b][0] = "10"; deck[b][1] = mi.home + "/My Documents/NetBeansProjects/CCP Blackjack/src/deck/b" + b + ".png"; } // Nine. for(int c = 20; c == 23; c++) { deck[c][0] = "9"; deck[c][1] = mi.home + "/My Documents/NetBeansProjects/CCP Blackjack/src/deck/c" + c + ".png"; } // Eight. for(int d = 24; d == 27; d++) { deck[d][0] = "8"; deck[d][1] = mi.home + "/My Documents/NetBeansProjects/CCP Blackjack/src/deck/d" + d + ".png"; } // And so on........... } } } I'll also post my hits.java class, whose purpose is to randomly generate a number and point to that index location in my array: // Import necessary classes. import java.util.Random; public class hits { // Create random object. Random rand = new Random(); // Declare variables. int card; String cardVal, cardPic; // Instantiate the needed classes. main mi = new main(); cards ci = new cards(); // Constructor for the class. public hits() { // Randomly generate a number (0 - 51). card = rand.nextInt(10); // Populate the array. ci.runCards(); // Assign the cards according to the num. generated. cardVal = ci.deck[card][0]; cardPic = ci.deck[card][1]; } } Take note of the bottom lines: cardVal = ci.deck[card][0]; I've changed that to: cardVal = ci.deck[0][0]; In order to see if my array was even populated and nothing displayed. When I changed that to a regular string: cardVal = "hello"; Just to see if "cardVal" was even displaying in my JLabel........ it worked. TL;DR: Take a look at my for-loop. For some reason my array is not populating...... what am I doing wrong!! Thanks in advance for any help given. :] Edited May 7, 201115 yr by larsson719
May 8, 201115 yr At a glance, your nested for loops are incorrect: // Aces. for(int a = 0; a == 3; a++) { deck[a][0] = "1"; deck[a][1] = mi.home + "/My Documents/NetBeansProjects/CCP Blackjack/src/deck/a" + a + ".png"; } // King, Queen, Jack, Ten. for(int b = 4; b == 19; b++) { deck[b][0] = "10"; deck[b][1] = mi.home + "/My Documents/NetBeansProjects/CCP Blackjack/src/deck/b" + b + ".png"; } // Nine. for(int c = 20; c == 23; c++) { deck[c][0] = "9"; deck[c][1] = mi.home + "/My Documents/NetBeansProjects/CCP Blackjack/src/deck/c" + c + ".png"; } // Eight. for(int d = 24; d == 27; d++) { deck[d][0] = "8"; deck[d][1] = mi.home + "/My Documents/NetBeansProjects/CCP Blackjack/src/deck/d" + d + ".png"; } Let's take one of those out, the top one: for(int a = 0; a == 3; a++) { deck[a][0] = "1"; deck[a][1] = mi.home + "/My Documents/NetBeansProjects/CCP Blackjack/src/deck/a" + a + ".png"; } The 'a == 3' should be a <= 3 as it is essentially creating a while loop. For loops are not strictly necessary in programming languages. What you're saying is: a = 0; while (a == 3){ //Do this, but a doesn't equal 3 so it will never get here. //This loop will never run, so your deck array is not being populated. a++; } What you need to say is: a = 0; while (a <= 3){ //Do this, and this time it will run at least once since 0 is < 3 //If you increment a by one each loop, this will run a total of 4 times a++; } Edited May 8, 201115 yr by andyl
May 8, 201115 yr Author At a glance....... haha i like how u said that. So yea, simple logic mistake, figures. So much to learn its like learning another language with so many reserved words, classes, and functions i've yet to fully understand.... Thanks a lot man. I'll update you when i test out. Got caught up in some other business and i haven't been able to get to it yet, but im pretty positive it will populate now :] Edited May 8, 201115 yr by larsson719
Create an account or sign in to comment