Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Texas Holdem GUI Game

$
0
0
Hi All,

I'm working on a texas holdem game. My project creates a JFrame with JPanel and JLabel's added to the JPanel. I know my code is not great....(I'm still learning). Everything displays for me but the main problems i'm having is a) when i click on the buttons the dealer and player cards display fine, but the sequence of the community cards does not work as i want. It should only display the three community cards on the first deal click, then the turn card on the next and the river card on the final click. B)/> i have also tried to generate an ArrayList containing the images as it would be easier to remove cards once they are displayed, but have not been able to implement this properly...

Also, as i have set "dealPressed" to generate random numbers, sometimes the same card gets displayed twice.

P.S this project is not for a school assignment or work.

Any advice on where i am going wrong, or even general advice on my code would be great.

Thanks in advance.
Here is my code which creates the jpanel and adds the labels and buttons....

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CardImages extends JPanel implements ActionListener {

	public JPanel p = new JPanel();
	public JLabel community1 = new JLabel();
	public JLabel community2 = new JLabel();
	public JLabel community3 = new JLabel();
	public JLabel holeCard1 = new JLabel();
	public JLabel holeCard2 = new JLabel();
	public JLabel dealer1 = new JLabel();
	public JLabel dealer2 = new JLabel();
	public JLabel turn = new JLabel();
	public JLabel river = new JLabel();
	public JButton deal = new JButton("Deal");
	public JButton play = new JButton ("Play");
	public JButton playAgain = new JButton ("Play Again");
	public int dealPressed;
	
	public ImageIcon images[] = new ImageIcon[52];
	{
	
	images[0] = new ImageIcon("Ace_Clubs.jpg");				images[12] = new ImageIcon("Four_Clubs.jpg");
	images[1] = new ImageIcon("Ace_Diamonds.jpg");			images[13] = new ImageIcon("Four_Diamonds.jpg");
	images[2] = new ImageIcon("Ace_Hearts.jpg");			images[14] = new ImageIcon("Four_Hearts.jpg");
	images[3] = new ImageIcon("Ace_Spades.jpg");			images[15] = new ImageIcon("Four_Spades.jpg");
	images[4] = new ImageIcon("Eight_Clubs.jpg");			images[16] = new ImageIcon("Jack_Clubs.jpg");
	images[5] = new ImageIcon("Eight_Diamonds.jpg");		images[17] = new ImageIcon("Jack_Diamonds.jpg");	
	images[6] = new ImageIcon("Eight_Hearts.jpg");			images[18] = new ImageIcon("Jack_Hearts.jpg");
	images[7] = new ImageIcon("Eight_Spades.jpg");			images[19] = new ImageIcon("Jack_Spades.jpg");
	images[8] = new ImageIcon("Five_Clubs.jpg");			images[20] = new ImageIcon("King_Clubs.jpg");
	images[9] = new ImageIcon("Five_Diamonds.jpg");			images[21] = new ImageIcon("King_Diamonds.jpg");
	images[10] = new ImageIcon("Five_Hearts.jpg");			images[22] = new ImageIcon("King_Hearts.jpg");
	images[11] = new ImageIcon("Five_Spades.jpg");			images[23] = new ImageIcon("King_Spades.jpg");
	
	images[24] = new ImageIcon("Nine_Clubs.jpg");			images[40] = new ImageIcon("Ten_Clubs.jpg");
	images[25] = new ImageIcon("Nine_Diamonds.jpg");		images[41] = new ImageIcon("Ten_Diamonds.jpg");
	images[26] = new ImageIcon("Nine_Hearts.jpg");			images[42] = new ImageIcon("Ten_Hearts.jpg");
	images[27] = new ImageIcon("Nine_Spades.jpg");			images[43] = new ImageIcon("Ten_Spades.jpg");
	images[28] = new ImageIcon("Queen_Clubs.jpg");			images[44] = new ImageIcon("Three_Clubs.jpg");
	images[29] = new ImageIcon("Queen_Diamonds.jpg");		images[45] = new ImageIcon("Three_Diamonds.jpg");
	images[30] = new ImageIcon("Queen_Hearts.jpg");			images[46] = new ImageIcon("Three_Hearts.jpg");
	images[31] = new ImageIcon("Queen_Spades.jpg");			images[47] = new ImageIcon("Three_Spades.jpg");
	images[32] = new ImageIcon("Seven_Clubs.jpg");			images[48] = new ImageIcon("Two_Clubs.jpg");
	images[33] = new ImageIcon("Seven_Diamonds.jpg");		images[49] = new ImageIcon("Two_Diamonds.jpg");
	images[34] = new ImageIcon("Seven_Hearts.jpg");			images[50] = new ImageIcon("Two_Hearts.jpg");
	images[35] = new ImageIcon("Seven_Spades.jpg");			images[51] = new ImageIcon("Two_Spades.jpg");
	images[36] = new ImageIcon("Six_Clubs.jpg");
	images[37] = new ImageIcon("Six_Diamonds.jpg");
	images[38] = new ImageIcon("Six_Hearts.jpg");
	images[39] = new ImageIcon("Six_Spades.jpg");
	
	}
	
	
	public void paintComponent (Graphics g){
	
		p.setLocation(0,0);
		p.setBackground(Color.green);
		p.setSize(600, 500);
		p.setVisible(true);
		this.add(p);
		
		community1.setSize(72, 97);
		community1.setLocation(10, 160);
		community1.setVisible(true);
		p.add(community1);
		
		community2.setSize(72, 97);
		community2.setLocation(100, 160);
		community2.setVisible(true);
		p.add(community2);
		
		community3.setSize(72, 97);
		community3.setLocation(190, 160);
		community3.setVisible(true);
		p.add(community3);
		
		holeCard1.setSize(72, 97);
		holeCard1.setLocation(140, 280);
		holeCard1.setVisible(true);
		p.add(holeCard1);
		
		holeCard2.setSize(72, 97);
		holeCard2.setLocation(240, 280);
		holeCard2.setVisible(true);
		p.add(holeCard2);
		
		dealer1.setSize(72, 97);
		dealer1.setLocation(140, 40);
		dealer1.setVisible(true);
		p.add(dealer1);
		
		dealer2.setSize(72, 97);
		dealer2.setLocation(240, 40);
		dealer2.setVisible(true);
		p.add(dealer2);
		
		turn.setSize(72, 97);
		turn.setLocation(280, 160);
		turn.setVisible(true);
		p.add(turn);
		
		river.setSize(72, 97);
		river.setLocation(370, 160);
		river.setVisible(true);
		p.add(river);
		
		deal.setSize(100, 30);
		deal.setLocation(250, 430);
		deal.setVisible(true);
		deal.addActionListener(this);
		p.add(deal);
		
		play.setSize(100, 30);
		play.setLocation(150,430);
		play.setVisible(true);
		play.addActionListener(this);
		p.add(play);
		
		playAgain.setSize(100, 30);
		playAgain.setLocation(350,430);
		playAgain.setVisible(true);
		playAgain.addActionListener(this);
		p.add(playAgain);
		
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		Object target = e.getSource();
		
		if(target == play){
			dealPressed = 0;
			for (int i = (int)(Math.random()*52); i < 52; images[i].getImage()) {
					
			holeCard1.setIcon(images[i]);
			
			i = (int)(Math.random()*52);
			images[i].getImage();
			if (holeCard1.getIcon() == images[i]){
				i = (int)(Math.random()*52);
				images[i].getImage();
			}
			holeCard2.setIcon(images[i]);
			
			i = (int)(Math.random()*52);
			images[i].getImage();
			if (holeCard1.getIcon() == images[i] || holeCard2.getIcon() == images[i]){
				i = (int)(Math.random()*52);
				images[i].getImage();
			}
			dealer1.setIcon(images[i]);
			
			i = (int)(Math.random()*52);
			images[i].getImage();
			if (holeCard1.getIcon() == images[i] || holeCard2.getIcon() == images[i] 
			                     || dealer1.getIcon() == images[i]){
				i = (int)(Math.random()*52);
				images[i].getImage();
			}
			dealer2.setIcon(images[i]);
			play.setEnabled(false);
			playAgain.setEnabled(false);
			
			dealPressed = 0;
			break;
			}	
		}
		else if(target == deal  && dealPressed == 0){
			dealPressed++;
			for (int j = (int)(Math.random()*52); j < 52;images[j].getImage()) {
				
			if (holeCard1.getIcon() == images[j] || holeCard2.getIcon() == images[j]
			     || dealer1.getIcon() == images[j] || dealer2.getIcon() == images[j]){
				j = (int)(Math.random()*52);
				images[j].getImage();
			}
			
			community1.setIcon(images[j]);
			
			j = (int)(Math.random()*52);
			images[j].getImage();
			if(community1.getIcon() == images[j] || holeCard1.getIcon() == images[j]
			       || holeCard2.getIcon() == images[j]  || dealer1.getIcon() == images[j]
			                       || dealer2.getIcon() == images[j]){
				j = (int)(Math.random()*52);
				images[j].getImage();
			}
			community2.setIcon(images[j]);
			
			j = (int)(Math.random()*52);
			images[j].getImage();
			if(community1.getIcon() == images[j] || community2.getIcon() == images[j]
			   || dealer1.getIcon() == images[j] || dealer2.getIcon() == images[j] 
			   || holeCard1.getIcon() == images[j] || holeCard2.getIcon() == images[j]){
				j = (int)(Math.random()*52);
				images[j].getImage();
			}
			community3.setIcon(images[j]);
			
			break;
			}	
		}
		else if(target == deal && dealPressed == 1){
			dealPressed++;
			for (int k = (int)(Math.random()*52); k < 52; images[k].getImage()) {
			if(dealer1.getIcon() == images[k] || dealer2.getIcon() == images[k] || community1.getIcon() == images[k]
			                  || community2.getIcon() == images[k] || community3.getIcon() == images[k] 
			                  || holeCard1.getIcon() == images[k] || holeCard2.getIcon() == images[k]){
				k = (int)(Math.random()*52);
				images[k].getImage();
				}
				turn.setIcon(images[k]);
				
				break;
			}
		}
		else if(target == deal && dealPressed == 2){
			dealPressed++;
				for (int k = (int)(Math.random()*52);k < 52; images[k].getImage()) {
				if(dealer1.getIcon() == images[k] || dealer2.getIcon() == images[k] || community1.getIcon() == images[k]
				                  || community2.getIcon() == images[k] || community3.getIcon() == images[k] 
				                  || holeCard1.getIcon() == images[k] || holeCard2.getIcon() == images[k]
				                  || turn.getIcon() == images[k]){
					k = (int)(Math.random()*52);
					images[k].getImage();
				}
					river.setIcon(images[k]);
					
					deal.setEnabled(false);
					playAgain.setEnabled(true);
					break;
				}
			}
		if (target == playAgain){
			dealPressed = 0;
			play.setEnabled(true);
			deal.setEnabled(true);
			playAgain.setEnabled(false);
			holeCard1.setIcon(null);
			holeCard2.setIcon(null);
			dealer1.setIcon(null);
			dealer2.setIcon(null);
			community1.setIcon(null);
			community2.setIcon(null);
			community3.setIcon(null);
			turn.setIcon(null);
			river.setIcon(null);
		}
	}
}
	
		
	





this is my main class

import javax.swing.*;

public class Main extends JFrame {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		JFrame f = new JFrame("Texas Hold'em");
		CardImages ci = new CardImages();
		
		f.setSize(600, 500);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
		f.add(ci);
	}

}



Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>