Results 1 to 8 of 8
Like Tree2Likes
  • 2 Post By gimbal2

Thread: i need a bit of help

  1. #1
    jimmycool44 is offline Member
    Join Date
    Sep 2013
    Posts
    9
    Rep Power
    0

    Default i need a bit of help

    so im learning java,and tried to make a rock paper scissors games, it worked.
    i tried to add a score system,it didint work.
    there are no errors,but the label that is supposed to show the score doesnt update .-.

    code:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class RockPaperScisors extends JFrame {
        private JButton rock;
        private JButton paper;
        private JButton scissors;
        private JButton playButton;
        private JLabel label;
        int score = 0;
        private JLabel scoreLabel;
        
        private int player = 0,computer;
        
        public RockPaperScisors(){
            setLayout(new FlowLayout());
            
            rock = new JButton("rock");
            add(rock);
            
            paper = new JButton("paper");
            add(paper);
            
            scissors = new JButton("scissors");
            add(scissors);
            
            playButton = new JButton("Play");
            add(playButton);
            
            scoreLabel = new JLabel("Score:" + score);
            add(scoreLabel);
            
            label = new JLabel("");
            add(label);
            
            event e = new event();
            rock.addActionListener(e);
            
            events ev = new events();
            paper.addActionListener(ev);
            
            events3 eve = new events3();
            scissors.addActionListener(eve);
            
            events4 even = new events4();
             playButton.addActionListener(even);
            
            
        }
        
        public class event implements ActionListener {
            public void actionPerformed(ActionEvent e){
                player = 1;
            }
        }
        
        public class events implements ActionListener {
            public void actionPerformed(ActionEvent ev){
                player = 2;
            }
        }
        
        public class events3 implements ActionListener {
            public void actionPerformed(ActionEvent eve){
                player = 3;
            }
        }
        
        public class events4 implements ActionListener {
            public void actionPerformed(ActionEvent even){
                computer = (int)(Math.random()*3 + 1 );
                
                if (score < 0){
                    score = 0;
                }
                
                if (player == 0){
                    label.setText("Vyberte si utok!xD");
                }else if (player == 1 && computer == 2){
                    score = score - 1;
                    label.setText("Prehral si!Pocitac mal papier");
                }else if (player == 1 && computer == 3){
                    score = score + 1;
                    label.setText("Vyhral si!Pocitac mal noznice");
                }else if (player == 1 && computer == 1){
                    label.setText("remiza!");
                }else if (player == 2 && computer == 2){
                    label.setText("remiza");
                }else if(player == 2 && computer == 1){
                    score = score + 1;
                    label.setText("Vyhral si!Pocitac mal kamen");
                }else if (player == 2 && computer == 3){
                    score = score - 1;
                    label.setText("Prehral si!Pocitac mal noznice");
                }else if (player == 3 && computer == 3){
                    label.setText("remiza");
                }else if(player == 3 && computer == 1){
                    score = score - 1;
                    label.setText("Prehral si!Pocitac mal kamen");
                }else if (player == 3 && computer == 3){
                    score = score + 1;
                    label.setText("Vyhral si!Pocitac mal noznice");
                }
            }
        }
        
        public static void main(String args[]){
            RockPaperScisors gui = new RockPaperScisors();
            
            gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            gui.setVisible(true);
            gui.setTitle("RockPaperScisors");
            gui.setSize(400,300);
        }
    
    }
    thanks for any help.

    notice:i tried score++; , didint work eighter :(
    Last edited by jimmycool44; 09-11-2013 at 08:11 PM.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    902
    Rep Power
    2

    Default Re: i need a bit of help

    Every time you update the score variable you should update the label.

  3. #3
    jimmycool44 is offline Member
    Join Date
    Sep 2013
    Posts
    9
    Rep Power
    0

    Default Re: i need a bit of help

    oh thanks,i tought it would update it,well,still have lots to learn i guess :D

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,596
    Rep Power
    5

    Default Re: i need a bit of help

    Can you explain to yourself why you thought that? Because its kind of like drawing on a piece of paper with a red crayon, then replacing the crayon with a yellow crayon in your hand and expecting the color on the paper to change too.
    SurfMan and jim829 like this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    902
    Rep Power
    2

    Default Re: i need a bit of help

    Quote Originally Posted by gimbal2 View Post
    Because its kind of like drawing on a piece of paper with a red crayon, then replacing the crayon with a yellow crayon in your hand and expecting the color on the paper to change too.
    Your analogies never cease to amaze me :)

  6. #6
    jimmycool44 is offline Member
    Join Date
    Sep 2013
    Posts
    9
    Rep Power
    0

    Default Re: i need a bit of help

    Quote Originally Posted by gimbal2 View Post
    Can you explain to yourself why you thought that? Because its kind of like drawing on a piece of paper with a red crayon, then replacing the crayon with a yellow crayon in your hand and expecting the color on the paper to change too.
    Well mostly because i am a begginer to java,and when i updated the int i forgot i had to update the label as well,im still learning java so il probably make a lot more stupid mistakes like these anyways,the best way to learn is to learn from your mistakes.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,596
    Rep Power
    5

    Default Re: i need a bit of help

    You're absolutely right of course, but don't forget about the need to think logically about stuff.

    Don't get me wrong, I'm quite impressed that you managed to get as far as you have. Swing is tough and not really an API for novices to use for learning. Keep that in mind as you go forward.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    jimmycool44 is offline Member
    Join Date
    Sep 2013
    Posts
    9
    Rep Power
    0

    Default Re: i need a bit of help

    Quote Originally Posted by gimbal2 View Post
    You're absolutely right of course, but don't forget about the need to think logically about stuff.

    Don't get me wrong, I'm quite impressed that you managed to get as far as you have. Swing is tough and not really an API for novices to use for learning. Keep that in mind as you go forward.
    Thanks,and i will.I am glad i joined this forum,i alredy see great ppl :D

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •