Jump to content

Poker Game - can't access array.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
agnl666

agnl666

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
I'm posting yet again about the poker game I am working on. It is for my computer programming 12 class and is due Tuesday. I don't expect it to be done though I plan to finish it this summer. I need a little help though.

In the game I create an array called cards containing 9 cards(2 for user, 2 for computer, and five on the table. its texas hold'em). This array is created if the button deal is clicked. The problem I'm having though is I can not use it in later if statements.

Bellow is the code I am talking about.


public void actionPerformed(ActionEvent event) {


    	String eventName = event.getActionCommand();


    	if (eventName == "Deal" & Pot == 0) {

 			

			String[] cards;

    		cards = new String[9];


			String[] out;

			out = new String[52];


			out[0] = "2C.jpg";

			out[1] = "2D.jpg";

			out[2] = "2H.jpg";

			out[3] = "2S.jpg";

			out[4] = "3C.jpg";

			out[5] = "3D.jpg";

			out[6] = "3H.jpg";

			out[7] = "3S.jpg";

			out[8] = "4C.jpg";

			out[9] = "4D.jpg";

			out[10] = "4H.jpg";

			out[11] = "4S.jpg";

			out[12] = "5C.jpg";

			out[13] = "5D.jpg";

			out[14] = "5H.jpg";

			out[15] = "5S.jpg";

			out[16] = "6C.jpg";

			out[17] = "6D.jpg";

			out[18] = "6H.jpg";

			out[19] = "6S.jpg";

			out[20] = "7C.jpg";

			out[21] = "7D.jpg";

			out[22] = "7H.jpg";

			out[23] = "7S.jpg";

			out[24] = "8C.jpg";

			out[25] = "8D.jpg";

			out[26] = "8H.jpg";

			out[27] = "8S.jpg";

			out[28] = "9C.jpg";

			out[29] = "9D.jpg";

			out[30] = "9H.jpg";

			out[31] = "9S.jpg";

			out[32] = "10C.jpg";

			out[33] = "10D.jpg";

			out[34] = "10H.jpg";

			out[35] = "10S.jpg";

			out[36] = "jackC.jpg";

			out[37] = "jackD.jpg";

			out[38] = "jackH.jpg";

			out[39] = "jackS.jpg";

			out[40] = "queenC.jpg";

			out[41] = "queenD.jpg";

			out[42] = "queenH.jpg";

			out[43] = "queenS.jpg";

			out[44] = "kingC.jpg";

			out[45] = "kingD.jpg";

			out[46] = "kingH.jpg";

			out[47] = "kingS.jpg";

			out[48] = "aceC.jpg";

			out[49] = "aceD.jpg";

			out[50] = "aceH.jpg";

			out[51] = "aceS.jpg";


			int j = 52;

			int x = 0;

			int i;

			int q;


			do {


				Random generator = new Random();

    			int rnd = generator.nextInt(j);

				q = rnd;


				cards[x] = out[q];


				x++;

				i = 1;


				do {

					out[q] = out[q + i];

					i++;

				} while (q + i < j);

				j--;

			}while(x < 9);

			

			//Sets the cards images

			flpPane.remove(lblCards0);

    		lblCards0 = new JLabel(cards[0] + " ");

    		flpPane.add(lblCards0);

    		

    		flpPane.remove(lblCards1);

    		lblCards1 = new JLabel(cards[1] + " ");

    		flpPane.add(lblCards1);

    		

    		flpPane.remove(lblCards2);

    		lblCards2 = new JLabel(cards[2] + " ");

    		flpPane.add(lblCards2);

    		

    		flpPane.remove(lblCards3);

    		lblCards3 = new JLabel(cards[3] + " ");

    		flpPane.add(lblCards3);

    		

    		flpPane.remove(lblCards4);

    		lblCards4 = new JLabel(cards[4] + " ");

    		flpPane.add(lblCards4);


    		contentPane.add(tblPane, BorderLayout.NORTH);

    		contentPane.add(flpPane, BorderLayout.SOUTH);

    		contentPane.add(btnPane, BorderLayout.SOUTH);

    		tblFrame.setContentPane(contentPane);

    		tblFrame.pack();


    	} if (eventName == "Match") {


    		tblPane.remove(lblPot);

    		Pot = Pot + 5;

    		lblPot = new JLabel("Pot : " + Pot);

    		tblPane.add(lblPot);


    		contentPane.add(tblPane, BorderLayout.NORTH);

    		contentPane.add(flpPane, BorderLayout.SOUTH);

    		contentPane.add(btnPane, BorderLayout.SOUTH);

    		tblFrame.setContentPane(contentPane);

    		tblFrame.pack();


    	} if (eventName == "Raise") {


    		tblPane.remove(lblPot);

    		Pot = Pot + 10;

    		lblPot = new JLabel("Pot : " + Pot);

    		tblPane.add(lblPot);


    		contentPane.add(tblPane, BorderLayout.NORTH);

    		contentPane.add(flpPane, BorderLayout.SOUTH);

    		contentPane.add(btnPane, BorderLayout.SOUTH);

    		tblFrame.setContentPane(contentPane);

    		tblFrame.pack();


    	} if (eventName == "Fold") {


    		tblPane.remove(lblPot);

    		Pot = 0;

    		lblPot = new JLabel("Pot : " + Pot);

    		tblPane.add(lblPot);

    		

    		//sets card images blank

    		flpPane.remove(lblCards0);

    		lblCards0 = new JLabel(new ImageIcon("img\\cards\\Lay.jpg"));

    		flpPane.add(lblCards0);

    		

    		flpPane.remove(lblCards1);

    		lblCards1 = new JLabel(new ImageIcon("img\\cards\\Lay.jpg"));

    		flpPane.add(lblCards1);

    		

    		flpPane.remove(lblCards2);

    		lblCards2 = new JLabel(new ImageIcon("img\\cards\\Lay.jpg"));

    		flpPane.add(lblCards2);

    		

    		flpPane.remove(lblCards3);

    		lblCards3 = new JLabel(new ImageIcon("img\\cards\\Lay.jpg"));

    		flpPane.add(lblCards3);

    		

    		flpPane.remove(lblCards4);

    		lblCards4 = new JLabel(new ImageIcon("img\\cards\\Lay.jpg"));

    		flpPane.add(lblCards4);


    		contentPane.add(tblPane, BorderLayout.NORTH);

    		contentPane.add(flpPane, BorderLayout.SOUTH);

    		contentPane.add(btnPane, BorderLayout.SOUTH);

    		tblFrame.setContentPane(contentPane);

    		tblFrame.pack();

    	}

    }


I am using the variable turn to count how many betting rounds have gone by. This is used to flip the cards. Bet one then flip three, then bet two flip one, bet three flip one, bet and then a winner is chosen. At the moment I do not have the computer betting though I want to get this working first. I cannot tell the JLabel to change to the string held in the array though.

I would have the JLabel changing to the picture that is labeled by the string in the cards array though I currently do not have the pictures.

If you guys could help me out with being able to access the array I would really appreciate it. Thanks !

#2
tate

tate

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
You can't use the cards array in later if statements because it is local to the if statement "if(eventName == "Deal" & Pot == 0){" and as soon as that if statement is through the cards array is gone forever. You need to initialize the array outside of the method call so you can use it outside of that if statement for example:
public class Test2{
    
    public String[] cards = new String[9];
    
    public void setCards(){
        for(int i=0; i<cards.length; i++){
            cards[i] = i+"";
        }
    }
    
    public void accessAfterSettingCards(){
        for(int i=0; i<cards.length; i++){
            System.out.println(cards[i]);
        }
    }
    
    public static void main(String args[]){
        Test2 t = new Test2();
        t.setCards(); // give cards their values
        t.accessAfterSettingCards(); // access and print their values
    }
}

twas brillig

#3
GMVResources

GMVResources

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
Agnl i really think you should only have just one main thread for the PokerGame:lol:

#4
agnl666

agnl666

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
kk. thank you and i wont make any additional threads than these. I'll add to this one if i have any additional questions. Thanks you guys though, i really appreciate the help :)

#5
GMVResources

GMVResources

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
sure ^^

#6
agnl666

agnl666

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
I'm having yet anoher issue that I can't figure out. I se a new variable called turn. It keeps track of which stage of the betting your at. Thus, you be once and i flips 3 cards, bet again and i flips another card on the flop, bet and then flip the final card and then bet again. I'm using a if statement and and saying if turn == 0 flip 3, if turn == 1 flip 1, if turn == 2 flip 1, if turn == 3, end & turn == 0. The trouble i'm having though is that i'm putting these if statments in both the raise and match. It is removing the cards and not putting them back in though :( how would I get it to place them back in properly?