thelavaking
Posts : 28 Join date : 2011-10-16
| Subject: Interesting.. Tue Mar 27, 2012 12:03 pm | |
| card - Code:
-
package gui.template; import javax.swing.*;
public class Card { String imageFileName; int value; Icon cardIcon; public Card(int num) { imageFileName = Integer.toString(num); imageFileName = "Deck\\" + imageFileName + ".png"; cardIcon = new ImageIcon(imageFileName); value=num; if(num % 13== 1) { value = 1; } else if(num % 13 >=2 && num % 13 <=10) { value = num % 13; } else { value = 10; } } }
guipanel - Code:
-
package gui.template; import java.awt.*; import javax.swing.*;
public class GUIJPanel extends JPanel { public GUIJPanel () { setLayout( null ); //setBackground( Color. YELLOW); } }
Guitemplate - Code:
-
package gui.template; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*;
public class GUITemplate extends JFrame { double pCard1; double pCard2; double pCard3; double pCard4; double pCard5; double cCard1; double cCard2; double cCard3; Card[] deck = new Card[53]; Card[] playerCard = new Card[6]; Card[] computerCard = new Card[4]; int pCards; int cCards = 3; int currentCard; int playerTotal; int computerTotal; JLabel pJLabel; JLabel cJLabel; JLabel resultsJLabel; JLabel cardsJLabel; JLabel pCard1JLabel; JLabel pCard2JLabel; JLabel pCard3JLabel; JLabel pCard4JLabel; JLabel pCard5JLabel; JLabel cCard1JLabel; JLabel cCard2JLabel; JLabel cCard3JLabel; JTextField cardsJTextField; JButton dealJButton; GUIJPanel panel; Container contentPane; public static void main( String[] args ) { GUITemplate app = new GUITemplate(); app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); app.start(); }
public void start() { currentCard = 1; createDeck(); shuffleDeck(); createUserInterface(); }
public void createDeck() { for (int i = 1; i <= 52; i++) { deck[i] = new Card(i); } } public void shuffleDeck() { currentCard = 1; int r; Card temp; for(int i =1; i <= 52; i++) { r = (int)(1 + Math.random()*52); temp = deck[i]; deck[i] = deck[r]; deck[r] = temp; } } public void clearHand() { for (int i = 1; i <= 5; i++) playerCard[i] = null; for (int i = 1; i <= 3; i++) computerCard[i] = null; pCard1JLabel.setIcon(null); pCard2JLabel.setIcon(null); pCard3JLabel.setIcon(null); pCard4JLabel.setIcon(null); pCard5JLabel.setIcon(null); cCard1JLabel.setIcon(null); cCard2JLabel.setIcon(null); cCard3JLabel.setIcon(null); playerTotal = 0; computerTotal = 0; } public void dealPlayer() { pCards = Integer.parseInt(cardsJTextField.getText()); for (int i = 1; i <= pCards; i++) { playerCard[i] = deck[currentCard]; currentCard++; System.out.println(currentCard); } if (playerCard[1] != null) pCard1JLabel.setIcon(playerCard[1].cardIcon); if (playerCard[2] != null) pCard2JLabel.setIcon(playerCard[2].cardIcon); if (playerCard[3] != null) pCard3JLabel.setIcon(playerCard[3].cardIcon); if (playerCard[4] != null) pCard4JLabel.setIcon(playerCard[4].cardIcon); if (playerCard[5] != null) pCard5JLabel.setIcon(playerCard[5].cardIcon); } public void dealComputer() { for (int i = 1; i <= cCards; i++ ) { computerCard[i] = deck[currentCard]; currentCard++; System.out.println(currentCard); } cCard1JLabel.setIcon(computerCard[1].cardIcon); cCard2JLabel.setIcon(computerCard[2].cardIcon); cCard3JLabel.setIcon(computerCard[3].cardIcon); if (currentCard >= 44) shuffleDeck(); } public void findPlayerTotal() { playerTotal = 0; boolean eleven = false; for (int i = 1; i <= pCards; i++) { if (playerCard[i].value == 11 && playerTotal <11) { playerTotal += 11; eleven = true; } else if(playerCard[i].value == 11 && playerTotal >= 11) playerTotal += 1; else playerTotal += playerCard[i].value; } if (playerTotal > 21 && eleven) playerTotal -= 10; } public void findComputerTotal() { computerTotal = 0; boolean eleven = false; for (int i = 1; i <= cCards; i++) { if (computerCard[i].value == 11 && computerTotal <11) { computerTotal += 11; eleven = true; } else if(computerCard[i].value == 11 && computerTotal >= 11) computerTotal += 1; else computerTotal += computerCard[i].value; } if (computerTotal > 21 && eleven) computerTotal -= 10; } public void determineResult() { String message = "You have " + playerTotal + " and i have " + computerTotal + " so "; if (playerTotal <= 21 && playerTotal > computerTotal) message += "you Win"; else if (playerTotal <= 21 && computerTotal > 21) message += "you Win"; else if (computerTotal <= 21 && computerTotal > playerTotal) message += "I Win"; else if (computerTotal <= 21 && playerTotal > 21) message += "I Win"; else message += "We Draw"; resultsJLabel.setText(message); } void createUserInterface() { contentPane = getContentPane(); panel = new GUIJPanel(); panel.setSize( 725, 425 ); contentPane.add( panel ); // JLabel Statements cardsJLabel = new JLabel(); cardsJLabel.setText( "How many cards do you want?" ); cardsJLabel.setBounds( 25, 25, 325, 50 ); cardsJLabel.setFont( new Font( "SansSerif", Font.PLAIN, 20 ) ); cardsJLabel.setHorizontalAlignment( JLabel.CENTER ); cardsJLabel.setForeground( Color.BLUE ); panel.add( cardsJLabel ); pJLabel = new JLabel(); pJLabel.setText( "PLAYER" ); pJLabel.setBounds( 25, 125, 175, 50 ); pJLabel.setFont( new Font( "SansSerif", Font.PLAIN, 20 ) ); pJLabel.setHorizontalAlignment( JLabel.CENTER ); pJLabel.setForeground( Color.BLUE ); panel.add( pJLabel ); cJLabel = new JLabel(); cJLabel.setText( "COMPUTER" ); cJLabel.setBounds( 25, 250, 175, 50 ); cJLabel.setFont( new Font( "SansSerif", Font.PLAIN, 20 ) ); cJLabel.setHorizontalAlignment( JLabel.CENTER ); cJLabel.setForeground( Color.BLUE ); panel.add( cJLabel ); pCard1JLabel = new JLabel(); pCard1JLabel.setText( "1" ); pCard1JLabel.setBounds( 225, 100, 75, 100 ); pCard1JLabel.setFont( new Font( "SansSerif", Font.PLAIN, 16 ) ); pCard1JLabel.setForeground( Color.BLUE ); panel.add( pCard1JLabel ); pCard2JLabel = new JLabel(); pCard2JLabel.setText( "2" ); pCard2JLabel.setBounds( 325, 100, 75, 100 ); pCard2JLabel.setFont( new Font( "SansSerif", Font.PLAIN, 16 ) ); pCard2JLabel.setForeground( Color.BLUE ); panel.add( pCard2JLabel ); pCard3JLabel = new JLabel(); pCard3JLabel.setText( "3" ); pCard3JLabel.setBounds( 425, 100, 75, 100 ); pCard3JLabel.setFont( new Font( "SansSerif", Font.PLAIN, 16 ) ); pCard3JLabel.setForeground( Color.BLUE ); panel.add( pCard3JLabel ); pCard4JLabel = new JLabel(); pCard4JLabel.setText( "4" ); pCard4JLabel.setBounds( 525, 100, 75, 100 ); pCard4JLabel.setFont( new Font( "SansSerif", Font.PLAIN, 16 ) ); pCard4JLabel.setForeground( Color.BLUE ); panel.add( pCard4JLabel ); pCard5JLabel = new JLabel(); pCard5JLabel.setText( "5" ); pCard5JLabel.setBounds( 625, 100, 75, 100 ); pCard5JLabel.setFont( new Font( "SansSerif", Font.PLAIN, 16 ) ); pCard5JLabel.setForeground( Color.BLUE ); panel.add( pCard5JLabel ); cCard1JLabel = new JLabel(); cCard1JLabel.setText( "1" ); cCard1JLabel.setBounds( 225, 225, 75, 100 ); cCard1JLabel.setFont( new Font( "SansSerif", Font.PLAIN, 16 ) ); cCard1JLabel.setForeground( Color.BLUE ); panel.add( cCard1JLabel ); cCard2JLabel = new JLabel(); cCard2JLabel.setText( "2" ); cCard2JLabel.setBounds( 325, 225, 75, 100 ); cCard2JLabel.setFont( new Font( "SansSerif", Font.PLAIN, 16 ) ); cCard2JLabel.setForeground( Color.BLUE ); panel.add( cCard2JLabel ); cCard3JLabel = new JLabel(); cCard3JLabel.setText( "3" ); cCard3JLabel.setBounds( 425, 225, 75, 100 ); cCard3JLabel.setFont( new Font( "SansSerif", Font.PLAIN, 16 ) ); cCard1JLabel.setForeground( Color.BLUE ); panel.add( cCard3JLabel ); resultsJLabel = new JLabel(); resultsJLabel.setText( "Result" ); resultsJLabel.setBounds( 25, 350, 675, 50 ); resultsJLabel.setFont( new Font( "SansSerif", Font.PLAIN, 20 ) ); resultsJLabel.setForeground( Color.BLUE ); panel.add( resultsJLabel ); // JTextField Statements cardsJTextField = new JTextField(); cardsJTextField.setBounds( 375, 25, 75, 50 ); cardsJTextField.setFont( new Font( "SansSerif", Font.PLAIN, 20 ) ); cardsJTextField.setHorizontalAlignment( JLabel.CENTER ); cardsJTextField.setForeground( Color.BLUE ); cardsJTextField.setEnabled( true ); panel.add( cardsJTextField );
dealJButton = new JButton( "Deal Cards" ); dealJButton.setBounds( 525, 25, 150, 50 ); panel.add( dealJButton ); dealJButton.addActionListener ( new ActionListener() { String message; public void actionPerformed( ActionEvent event ) { pCards = Integer.parseInt(cardsJTextField.getText()); if(pCards > 0 && pCards <= 5) { clearHand(); dealPlayer(); dealComputer(); findPlayerTotal(); findComputerTotal(); determineResult(); } } } ); setTitle( "Card Game" ); setSize( 733, 451 ); setLocation( 500, 400 ); setVisible( true ); } }
| |
|