• Forum
  • Lounge
  • Simon Says Game in Java, I'm freaking ou

 
Simon Says Game in Java, I'm freaking out because I can't get any further

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//I ended up not using the online example at all, it was only making it more difficult
//this is my doing from top to bottom

package isay;
import javax.swing.*;//using this for the delay on the gameplay
import java.awt.*;//used for grid
import java.awt.event.*;//used for event listener
import java.util.Random;//using this for the random button selector

/**
 *
 * @author Sir Rudy Typealot
 */
public class ISay extends JFrame
{
    private final int windowTall=700;//height of window in pixels
    private final int windowWide=700;//width of window in pixels
    private boolean Blue=(false);
    private boolean Red=(false);
    private boolean Green=(false);
    private boolean Yellow=(false);
    private JButton[] ColorButton;    
    private JPanel[] BtnLayout;
    private JButton sequencer=new JButton();
    Dimension btnSize= new Dimension (125, 125);
   
  
    public ISay()
    {
        setTitle("I Say Let's Play!!!");//sets the title of the game
        setSize(windowWide, windowTall);//sets the dimensions of window
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//closes the window when x button is pressed 
     
        
      //this for section of code will create and add the 4 color buttons and it will also create the action listener for the corresponding buttons  
       ColorButton=new JButton[4];
       BtnLayout= new JPanel[5];
      
       for(int w=0; w<5; w++)
      {
       BtnLayout[w]=new JPanel();
      }
       
       sequencer=new JButton("Start/Reset");    
  
       for (int x=0; x<4; x++)
       {
           ColorButton[x]=new JButton();
           ColorButton[x].setPreferredSize(btnSize);
           ColorButton[x].addActionListener(new ColorButtonListener());
        }
     BtnLayout[0].add(ColorButton[0]);
     BtnLayout[1].add(ColorButton[1]);
     BtnLayout[2].add(ColorButton[2]);
     BtnLayout[3].add(ColorButton[3]);
     BtnLayout[4].add(sequencer);
     
     add(BtnLayout[0], BorderLayout.NORTH);
     add(BtnLayout[1], BorderLayout.WEST);
     add(BtnLayout[2], BorderLayout.SOUTH);
     add(BtnLayout[3], BorderLayout.EAST);
     add(BtnLayout[4], BorderLayout.CENTER);
     
     
     
     ColorButton[0].setBackground(Color.blue);  
     ColorButton[1].setBackground(Color.red);
     ColorButton[2].setBackground(Color.green);
     ColorButton[3].setBackground(Color.yellow);

     
       
        sequencer.addActionListener(new sequencerListener());//action listener for the sequence creator
        pack();
        setVisible(true);//displays the window
        setResizable(false);
    } 
    
    
    private class sequencerListener implements ActionListener
        {
         public void actionPerformed(ActionEvent e)
            {   
                int sequence[]=new int[10];
                Random rand=new Random();        
             
                
                for (int r = 0; r < 10; r++)
                {
                   sequence[r] = rand.nextInt(4)+1;
                   
                   if(sequence[r]==1)
                    {
                         
                        ColorButton[0].setBackground(Color.black);
                        ColorButton[1].setBackground(Color.red);
                        ColorButton[2].setBackground(Color.green);
                        ColorButton[3].setBackground(Color.yellow);                   
                 
                        System.out.print("Blue, ");
                        
                    }
                    if(sequence[r]==2)
                    {
                                              
                        ColorButton[0].setBackground(Color.blue);
                        ColorButton[1].setBackground(Color.black);
                        ColorButton[2].setBackground(Color.green);
                        ColorButton[3].setBackground(Color.yellow);
                
                        
                        System.out.print("Red, ");
                        
                    }
                    if(sequence[r]==3)
                    {
                        
                      
                        ColorButton[0].setBackground(Color.blue);
                        ColorButton[1].setBackground(Color.red);
                        ColorButton[2].setBackground(Color.black);
                        ColorButton[3].setBackground(Color.yellow);  
                
                      
                        System.out.print("Green, ");
                        
                    }
                    if (sequence[r]==4)
                    {
                        
                        
                        ColorButton[0].setBackground(Color.blue);
                        ColorButton[1].setBackground(Color.red);
                        ColorButton[2].setBackground(Color.green);
                        ColorButton[3].setBackground(Color.black);
                
                        System.out.print("Yellow, ");
                       
                    }
                       
                }
            }
            
        }
   
        private class ColorButtonListener implements ActionListener 
        {
            public void actionPerformed(ActionEvent e)
            {
              

              for(int x=0; x<10;x++)
              {

               if(e.getSource() == ColorButton[0])
               {
                Blue=true;
                
                ColorButton[0].setBackground(Color.black);
                ColorButton[1].setBackground(Color.red);
                ColorButton[2].setBackground(Color.green);
                ColorButton[3].setBackground(Color.yellow);

                e.setSource(0);
                
                Blue=false;
               }
            
            else if(e.getSource() == ColorButton[1])
            {        
                Red=true;
                ColorButton[0].setBackground(Color.blue);
                ColorButton[1].setBackground(Color.black);
                ColorButton[2].setBackground(Color.green);
                ColorButton[3].setBackground(Color.yellow);
                e.setSource(0);
                
                Red = false;
            }
            
            else if(e.getSource() == ColorButton[2])
            {
                Green=true;
                ColorButton[0].setBackground(Color.blue);
                ColorButton[1].setBackground(Color.red);
                ColorButton[2].setBackground(Color.black);
                ColorButton[3].setBackground(Color.yellow);
                e.setSource(0);
                
                Green = false;
            }
            
            else if(e.getSource() == ColorButton[3])
            {
                Yellow=true;
                
                ColorButton[0].setBackground(Color.blue);
                ColorButton[1].setBackground(Color.red);
                ColorButton[2].setBackground(Color.green);
                ColorButton[3].setBackground(Color.black);
                e.setSource(0);
                
                Yellow = false;
            }
            }
        }

        }
        
        
    public static void main(String[] args)
    {
        new ISay();
    }
    }


What are you stuck on? You can't just say "I'm stuck" and then wait for a reply ;)
I can't figure out how to show the sequence then let the user follow the sequence and verify he pressed the correct button. I don't know how to use the timer for the delay on the sequence.
Have you tried a simpler game first, or is this required for some class?
yeah, it's my final project for my java class. the only thing standing between me and my associates degree. I've been up all night working on it and have to turn it in an hour. I'm in a tough situation right now so any help is appreciated.
OK, do you have a general idea where in the code we should be looking?

Also, I hope you haven't been stuck like this since you first started months/weeks ago? If so I'm sorry you waited so long to ask for help.
Last edited on
Yeah, the sequencer action listener needs a timer to create a delay in order to allow the player enough time to see it. right now all it does is go straight to the last button in the sequence.

the other part is the button action listener, in that one i have to verify the users choice to check for accuracy.
Unfortunately I haven't done much AWT or Swing, and I've only messed with JavaFX a little, so you probably know more than me. But based on some quick research would javax.swing.Timer be helpful to you?

I found this:
http://stackoverflow.com/questions/7251625/how-to-create-a-delay-in-swing
Last edited on
Yes, the only problem is that I have to stick to material from the book. I wish I could use other material, I've found many useful examples online but none that I could use. I do appreciate you trying though.
Hm, and the book doesn't cover delays? What about Thread.sleep()? I know it's pretty ugly but as a last resort...
It has a paragraph on delays lol, and I can't use the Thread function. I'm hoping with what i have i can get a passing grade all i need is a 75. I'm about to head to school to see if there is anyone there for tutoring that might be able to help. sadly the last time i went for help i knew more than the tutors about programming. my school is transitioning from a technical school to an actual college so they have some catching up to do.
OK, good luck - I hope you can pass :\
Topic archived. No new replies allowed.