Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: A Card Game

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default A Card Game

    when comparing the values of the card, the strings is sometimes correct sometimes wrong, like when i have A and the AI has 6, it will tell me the AI Won.
    i need help. Thanks!
    package JRPG;
     
    /**
     *
     * 
     */
    import javax.swing.*;
     
    public class Card {
     ImageIcon[]  aicardsic= new ImageIcon[5];
         ImageIcon[] anArrayOfImageIcons = {
            new ImageIcon("Images/2d.gif"),
            new ImageIcon("Images/3d.gif"),
            new ImageIcon("Images/4d.gif"),
            new ImageIcon("Images/5d.gif"),
            new ImageIcon("Images/6d.gif"),
            new ImageIcon("Images/7d.gif"),
            new ImageIcon("Images/8d.gif"),
            new ImageIcon("Images/9d.gif"),
            new ImageIcon("Images/td.gif"),
            new ImageIcon("Images/jd.gif"),
            new ImageIcon("Images/qd.gif"),
            new ImageIcon("Images/kd.gif"),
            new ImageIcon("Images/ad.gif"),
        };
     
     
       ImageIcon[] returnimages= new ImageIcon[5];
     
        private boolean drawn;
        public Card(int[] playnum, int[] ainum){
        setImages(playnum);
        setAiImage(ainum);
     
        }
     
     
        public boolean getDrawn(){
            return drawn;
        }
     
         public void setImages(int[] array){
     
         for (int i = 0; i < 5; i++) {
             switch (array[i]){
                 case 1:
                    returnimages[i]=anArrayOfImageIcons[0];
                     break;
                     case 2:
                    returnimages[i]=anArrayOfImageIcons[1];
                     break;
                         case 3:
                    returnimages[i]=anArrayOfImageIcons[2];
                     break;
                    case 4:
                    returnimages[i]=anArrayOfImageIcons[3];
                     break;
                      case 5:
                    returnimages[i]=anArrayOfImageIcons[4];
                     break;
                      case 6:
                    returnimages[i]=anArrayOfImageIcons[5];
                     break;
                       case 7:
                    returnimages[i]=anArrayOfImageIcons[6];
                     break;
                    case 8:
                    returnimages[i]=anArrayOfImageIcons[7];
                     break;
                     case 9:
                    returnimages[i]=anArrayOfImageIcons[8];
                     break;
                     case 10:
                   returnimages[i]=anArrayOfImageIcons[9];
                     break;
                     case 11:
                    returnimages[i]=anArrayOfImageIcons[10];
                     break;
                     case 12:
                    returnimages[i]=anArrayOfImageIcons[11];
                     break;
                      case 13:
                    returnimages[i]=anArrayOfImageIcons[12];
                     break;
         }}
     
     
     }
              public void setAiImage(int aicard[]){
     
                  for (int i = 0; i < 5; i++) {
     
     
             switch (aicard[i]){
                 case 1:
                    aicardsic[i]=anArrayOfImageIcons[0];
                     break;
                     case 2:
                    aicardsic[i]=anArrayOfImageIcons[1];
                     break;
                         case 3:
                    aicardsic[i]=anArrayOfImageIcons[2];
                     break;
                    case 4:
                    aicardsic[i]=anArrayOfImageIcons[3];
                     break;
                      case 5:
                    aicardsic[i]=anArrayOfImageIcons[4];
                     break;
                      case 6:
                    aicardsic[i]=anArrayOfImageIcons[5];
                     break;
                       case 7:
                    aicardsic[i]=anArrayOfImageIcons[6];
                     break;
                    case 8:
                    aicardsic[i]=anArrayOfImageIcons[7];
                     break;
     
                     case 9:
                    aicardsic[i]=anArrayOfImageIcons[8];
                     break;
                     case 10:
                   aicardsic[i]=anArrayOfImageIcons[9];
                     break;
                     case 11:
                    aicardsic[i]=anArrayOfImageIcons[10];
                     break;
                     case 12:
                    aicardsic[i]=anArrayOfImageIcons[11];
                     break;
                      case 13:
                    aicardsic[i]=anArrayOfImageIcons[12];
                     break;
         }}
     
     
     }
              public ImageIcon[] getPlayerImages(){
                  return returnimages;
              }
              public ImageIcon[] getAiImage(){
                  return aicardsic;
              }
     }
    package JRPG;
     
    import javax.swing.*;
     
    /**
     *
     * 
     */
     
    public class Game{
        private int comwins,playerwins,draw;
                int[] aicard=new int[5];
         int[] anArrayOfints = new int[13];
         ImageIcon[] images= new ImageIcon[5];
     ImageIcon[] AiCardIc=new ImageIcon[5];
         int[] arraysof5 = new int[5];
         boolean[] drawn = {false,false,false,false,false,false,false,false,false,false,false,false,false};
         private String Win;
    public void saveStats(){
        }
    public void saveCards(){
     
    }
     public int[] pickingCard(){
     
         for (int i = 0; i < 5; i++) {
     
                    arraysof5[i]= 1+(int)(Math.random()*13);
     
             do {
     
     
                       arraysof5[1]=1+(int)(Math.random()*13);
                   }
                 while (arraysof5[1]==arraysof5[0]);
             do {
     
     
     
                       arraysof5[2]= 1+(int)(Math.random()*13);
                   }
              while (arraysof5[2]==arraysof5[1]||arraysof5[2]==arraysof5[0]);
               do {
     
                       arraysof5[3]= 1+(int)(Math.random()*13);
     
              } while (arraysof5[3]==arraysof5[2]||arraysof5[3]==arraysof5[1]||arraysof5[3]==arraysof5[0]);
                  do{
                       arraysof5[4]= 1+(int)(Math.random()*13);
                    } while (arraysof5[4]==arraysof5[3]||arraysof5[4]==arraysof5[2]||arraysof5[4]==arraysof5[1]||arraysof5[4]==arraysof5[0]);   
     
         }
     
                    return arraysof5;
      }
     
    public void generateStats(){
    //    
    }
    public Game(){
     
      int[] a=setAICards();
        int[] arrays=pickingCard();
    Card c1 = new Card(arrays,a);
    images=c1.getPlayerImages();
    AiCardIc=c1.getAiImage();
     
    }
    public void showCards(){
          JOptionPane.showMessageDialog(null,"", "input", JOptionPane.INFORMATION_MESSAGE);
    }
     
    public int[] setAICards(){
        for (int i = 0; i < 5; i++) {
        aicard[i]=1+(int)(Math.random()*13);
        }
        return aicard;
    }
        public ImageIcon[] getGameImages(){
            return images;
        }
        public ImageIcon[] getAiCardImage(){
            return AiCardIc;
        }
        public int getComScore(){
            return comwins;
        }
        public int getPlayerScore(){
            return playerwins;
        }
        public int getDraw(){
           return draw;
        }
        public boolean[] getDrawn(){
            return drawn;
        }
        public void setDrawn(int a,boolean b){
            drawn[a] = b;
        }
     
                public String getWin(int a, int b){
            if (b>a) {
                comwins++;
              Win="AI Won!";
            }
     
            else if (b==a){
                draw++;
                 Win = "It is a Draw";
            }
            else {
          playerwins++;       Win = "Player Wins";
        }
        return Win;
        }
     
     
     
     
    }
    package JRPG;
     
    /**
     *
     * @author Ccc
     */
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class GameFrame extends JFrame implements ActionListener{
        private Game g1;
            private JButton player,com,restart,showcards,exit,Sound;
            public JButton[] pick= new JButton[5];
        private JLabel title,playlbl,comlbl,cardspick;
        private JPanel north,center,South,cardsp,East,West,insidenorth1,insidenorth2,total;
        private ImageIcon playeric,comIc,soic;
        ImageIcon[] aicardic=new ImageIcon[5];
        private String Name;
        private JTextArea result;
        private JTextField playername;
        private JMenuBar bar;
        private JMenu menu,customize;
        private JRadioButton red,green,black,gray,blue;
         ImageIcon[] images= new ImageIcon[5];
     
     
        public void actionPerformed(ActionEvent e){
            if (e.getSource()==pick[0]){
                pick[0].setEnabled(false);
                int[] a=g1.pickingCard();
                int[] b=g1.setAICards();
                String msg=g1.getWin(a[0],b[0]);
     
     
            JOptionPane.showMessageDialog(null, msg, "input", JOptionPane.INFORMATION_MESSAGE);
     
            }
        else  if (e.getSource()==pick[1]){
                pick[1].setEnabled(false);
                int[] a=g1.pickingCard();
                int[] b=g1.setAICards();
     
                g1.setDrawn(a[1],true);
                String msg1=g1.getWin(a[1],b[1]);
     
     
                aicardic=g1.getAiCardImage();
                com.setIcon(aicardic[1]);
               JOptionPane.showMessageDialog(null, msg1, "input", JOptionPane.INFORMATION_MESSAGE); 
        }
       else if (e.getSource()==pick[2]){
                   pick[2].setEnabled(false);
        int[] a=g1.pickingCard();
        int[] b=g1.setAICards();
                g1.setDrawn(a[2],true);
     
                String msg2=g1.getWin(a[2],b[2]);
     
                aicardic=g1.getAiCardImage();
                com.setIcon(aicardic[2]);
       JOptionPane.showMessageDialog(null, msg2, "input", JOptionPane.INFORMATION_MESSAGE);     
       }
       else if (e.getSource()==pick[3]){
                pick[3].setEnabled(false);
                int[] a=g1.pickingCard();
                int[] b=g1.setAICards();
     
                g1.setDrawn(a[3],true);
                String msg3=g1.getWin(a[3],b[3]);
     
                aicardic=g1.getAiCardImage();
                com.setIcon(aicardic[3]);
                JOptionPane.showMessageDialog(null, msg3, "input", JOptionPane.INFORMATION_MESSAGE);
            }
     else if (e.getSource()==pick[4]){
                pick[4].setEnabled(false);
                int[] a=g1.pickingCard();
                int[] b=g1.setAICards();
     
                g1.setDrawn(a[4],true);
                String msg4=g1.getWin(a[4],b[4]);
     
                aicardic=g1.getAiCardImage();
                com.setIcon(aicardic[4]);
                JOptionPane.showMessageDialog(null, msg4, "input", JOptionPane.INFORMATION_MESSAGE);
            }
     else if (e.getSource()==exit){
                 JOptionPane.showMessageDialog(null, "Thank you for using this program!!", "input", JOptionPane.INFORMATION_MESSAGE);
     
     
          dispose();
          System.exit(0);
      }
      else if (e.getSource()==restart){
           g1=new Game();
            aicardic=g1.getAiCardImage();
            images=g1.getGameImages();
             pick[0].setEnabled(true);
              pick[1].setEnabled(true);
               pick[2].setEnabled(true);
                pick[3].setEnabled(true);
                 pick[4].setEnabled(true);
            pick[0].setIcon(images[0]);
            pick[1].setIcon(images[1]);
            pick[2].setIcon(images[2]);
            pick[3].setIcon(images[3]);
            pick[4].setIcon(images[4]);
      }
              else if (e.getSource()==showcards){
     
     
     
      }
              else if (e.getSource()==red){
                north.setBackground(Color.RED);
                 South.setBackground(Color.RED);
                  East.setBackground(Color.RED);
                   West.setBackground(Color.RED);
                    center.setBackground(Color.RED);
              }
              else if (e.getSource()==green){
     north.setBackground(Color.GREEN);
                 South.setBackground(Color.GREEN);
                  East.setBackground(Color.GREEN);
                   West.setBackground(Color.GREEN);
                    center.setBackground(Color.GREEN);
              }
              else if (e.getSource()==blue){
                   north.setBackground(Color.BLUE);
                 South.setBackground(Color.BLUE);
                  East.setBackground(Color.BLUE);
                   West.setBackground(Color.BLUE);
                    center.setBackground(Color.BLUE);
              }
                else if (e.getSource()==black){
                              north.setBackground(Color.BLACK);
                 South.setBackground(Color.BLACK);
                  East.setBackground(Color.BLACK);
                   West.setBackground(Color.BLACK);
                    center.setBackground(Color.BLACK);
              }
               else if (e.getSource()==gray){
                              north.setBackground(Color.LIGHT_GRAY);
                 South.setBackground(Color.LIGHT_GRAY);
                  East.setBackground(Color.LIGHT_GRAY);
                   West.setBackground(Color.LIGHT_GRAY);
                    center.setBackground(Color.LIGHT_GRAY);
              }
        }
    public void setNorth(){
        Name = JOptionPane.showInputDialog(null, "Enter Name:", "input", JOptionPane.QUESTION_MESSAGE);
            north = new JPanel(new BorderLayout());
            insidenorth1 = new JPanel();
            insidenorth2 = new JPanel();
             title = new JLabel("Card Game");
             title.setFont(new Font("Arial",1,40));
             title.setForeground(Color.red);
              playername =new JTextField(Name,8);
              playername.disable();
     
           comlbl = new JLabel("Computer:");
           playlbl = new JLabel("Player:");
     
     
            insidenorth1.add(title,BorderLayout.CENTER);
            insidenorth2.add(playlbl);
            insidenorth2.add(playername);
            north.add(comlbl,BorderLayout.EAST);
           north.add(insidenorth1,BorderLayout.NORTH);
           north.add(insidenorth2,BorderLayout.WEST);
      add(north,BorderLayout.NORTH);
     
        }
    public void setWest(){
       West = new JPanel();
     
            playeric= new ImageIcon("Images/Mark.jpg");
            player = new JButton(playeric);
            West.add(player);
            //West.setBackground(Color.GREEN);
           add(West,BorderLayout.WEST);
    }
    public void setEast(){
        East = new JPanel();
        insidenorth1 = new JPanel();
           comIc= new ImageIcon("Images/cardback.png");
            com = new JButton(comIc);
            East.add(com);
     
     
            add(East,BorderLayout.EAST);
    }
        public void setCenter(){
     
            center = new JPanel();
            comIc= new ImageIcon();
            pick[0] = new JButton(images[0]);
            pick[1] = new JButton(images[1]);
            pick[2] = new JButton(images[2]);
            pick[3] = new JButton(images[3]);
            pick[4] = new JButton(images[4]);
            cardspick = new JLabel("pick your cards:");
            cardsp = new JPanel();
                     result =new JTextArea("Number of times "+Name+ " Won:"+g1.getPlayerScore()+"\n"
            + "Number of times the computer won:"+g1.getComScore()+"\n"
                    + "Number of times that were draw:"+g1.getDraw()+"\n");
                     result.disable();
                     result.setForeground(Color.BLACK);
                     result.setFont(new Font("sansserif", Font.BOLD, 14));
                           soic = new ImageIcon("sound");
     
     
     
            center.add(cardspick);
            pick[0].addActionListener(this);
            pick[1].addActionListener(this);
            pick[2].addActionListener(this);
            pick[3].addActionListener(this);
            pick[4].addActionListener(this);
            cardsp.add(pick[0]);
            cardsp.add(pick[1]);
            cardsp.add(pick[2]);
            cardsp.add(pick[3]);
            cardsp.add(pick[4]);
            center.add(cardsp);
            center.add(result);
     
            add(center,BorderLayout.CENTER);
        }
        public void setSouth(/*int com,int play,int draw*/){
               South = new JPanel();
               soic = new ImageIcon("onmedia.png");
            Sound= new JButton (soic);
            Sound.setForeground(Color.green);
            Sound.setBackground(Color.BLACK);
            Sound.addActionListener(this);
            restart = new JButton("restart");
            restart.setForeground(Color.green);
            restart.setBackground(Color.BLACK);
            showcards = new JButton("show cards");
            showcards.setForeground(Color.green);
            showcards.setBackground(Color.BLACK);
            showcards.addActionListener(this);
            exit = new JButton("exit");
            exit.addActionListener(this);
            exit.setForeground(Color.green);
            exit.setBackground(Color.BLACK);
            restart.addActionListener(this);
     
            //adding into west panel
            South.add(Sound);
            South.add(restart);
            South.add(showcards);
            South.add(exit);
            add(South,BorderLayout.SOUTH);
     
        }
        public void addMenu(){
                    bar = new JMenuBar();
                    menu = new JMenu("Menu");
                     customize = new JMenu("Customize");
       bar.add(menu);
       bar.add(customize);
     
     
            JMenuItem newAction = new JMenuItem("New");
            JMenuItem muteAction = new JMenuItem("Mute");
            JMenuItem exitAction = new JMenuItem("Exit");
           menu.add(newAction);
           menu.add(muteAction);
           menu.add(exitAction);
           ButtonGroup btg= new ButtonGroup();
            red= new JRadioButton("set background to Red");
            blue= new JRadioButton("set background to Blue");
            green= new JRadioButton("set background to Green");
            black= new JRadioButton("set background to Black");
           gray= new JRadioButton("set background to Gray");
           btg.add(red);
           btg.add(blue);
           btg.add(green);
           btg.add(black);
           btg.add(gray);
           gray.setSelected(true);
           customize.add(red);
           customize.add(blue);
          customize.add(green);
           customize.add(black);
           customize.add(gray);
           red.addActionListener(this);
           blue.addActionListener(this);
           black.addActionListener(this);
           gray.addActionListener(this);
           green.addActionListener(this);
     
            setJMenuBar(bar);
        }
        public void setFrame(){
                    setNorth();
            setCenter();
            setSouth();
            setWest();
            setEast();
            title.setBackground(Color.LIGHT_GRAY);
            north.setBackground(Color.LIGHT_GRAY);
                 South.setBackground(Color.LIGHT_GRAY);
                  East.setBackground(Color.LIGHT_GRAY);
                   West.setBackground(Color.LIGHT_GRAY);
                    center.setBackground(Color.LIGHT_GRAY);
            addMenu();
        }
        public GameFrame(){
     g1=new Game();
            aicardic=g1.getAiCardImage();
            images=g1.getGameImages();
            setFrame();
        }
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: A Card Game

    when i have A and the AI has 6, it will tell me the AI Won
    Please explain where in the code this test is being made.
    What are the rules for determining who wins?

    You should learn how to comment your code. There are no comments in it describing the logic and the rules that are being used to control the program flow.

    Try debugging your code by adding lots of printlns to show the logic flow and the values of variables as they change.

Similar Threads

  1. code for 3 card brag game
    By jay_tee92 in forum Object Oriented Programming
    Replies: 1
    Last Post: December 20th, 2011, 11:51 AM
  2. Card Game Problem.
    By d'fitz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 5th, 2011, 07:30 AM
  3. Card Game help....
    By macFs89H in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 2nd, 2011, 07:55 AM
  4. Card Game Problem....Need Help
    By macFs89H in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2011, 07:30 AM
  5. help on war card game project
    By sc0field1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 3rd, 2011, 04:51 AM