Join 149,443 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,251 people online right now. Registration is fast and FREE... Join Now!
Hello , I am currently doing an assignment where I have to programm the Mastermind Game. My question is , I have 6 buttons from which the colors are chosen. My problem is when the user clicks on the button ,then starting with the first button [0][0],the color clicked should be assigned to the buttons - at least the first four for starters. This is a snippet of the code
CODE
public void actionPerformed(ActionEvent event) { for (int i = 0; i < color_buttons.length; i++) { color_buttons[i].addActionListener(this); if (event.getSource() == color_buttons[i]) {
for (int k = 0; k <= 0; k++) { // for(int j=0;j<=0;j++){ int j = 0; if (j == 0) guess_buttons[j][k].setBackground(color_selected[i]); k++;
is never reached. That is because of k<=0. So change that condition to k<=4 or some other value you think that is appropriate. That's it for now, if there are still problems post the rest of the code.
This post has been edited by PennyBoki: 24 Jun, 2007 - 06:00 AM
is never reached. That is because of k<=0. So change that condition to k<=4 or some other value you think that is appropriate. That's it for now, if there are still problems post the rest of the code.
I have changed the code a bit- but I still haven't quite got it. When I click on the button I get the color I want on the the first button (I have an array of buttns [4][8], columns and rows). Now when I click on another color which should show on the second button ([1][0]) , I get another color but still on the first button. I think I should increase the counter for the second position of the column but dont know how.
CODE
public void actionPerformed(ActionEvent event) {
for (int i = 0; i < color_buttons.length; i++) { color_buttons[i].addActionListener(this); if (event.getSource() == color_buttons[i]) {
for (int k = 0; k <1; k++) { for (int j = 0; j <1; j++) { guess_buttons[j][k].setBackground(color_selected[i]);
There is a way but the point is that the whole world could see so that if someone has similar problem she/he could find guidance here. This is a free community(one of the things I like most about this place) so everybody should share. That is where I stand sorry.
private final static int number_of_trials = 8; // number of trials
// inititalise with 1
public MasterMindGui() {
super(); setTitle("MasterMind");
rows = 8; columns = 4; button_panel = new JPanel(new GridLayout(8, 1)); guess_panel = new JPanel(new GridLayout(rows, columns, 10, 10)); text_panel = new JPanel(new GridLayout(rows, columns, 10, 10));
guess_buttons = new JButton[4][8]; for (int i = 0; i < columns; i++) { for (int j = 0; j < rows; j++) { guess_buttons[i][j] = new JButton(); guess_buttons[i][j].setEnabled(false); guess_panel.add(guess_buttons[i][j]);
} } result_box = new JButton[4][8]; for (int i = 0; i < columns; i++) { for (int j = 0; j < rows; j++) { result_box[i][j] = new JButton(" "); result_box[i][j].setEnabled(false); result_box[i][j].setBackground(Color.WHITE); text_panel.add(result_box[i][j]); } }
for (int i = 0; i < 4; i++) { int comp_color = (int) (Math.random() * 6); unknown_color[i] = comp_color; }
color_buttons[0] = new JButton(); color_selected[0] = new Color(240, 0, 0); button_panel.add(color_buttons[0]); color_buttons[0].setBackground(color_selected[0]);
color_buttons[1] = new JButton(); color_selected[1] = new Color(255, 255, 0); button_panel.add(color_buttons[1]); color_buttons[1].setBackground(color_selected[1]);
color_buttons[2] = new JButton(); color_selected[2] = new Color(141, 182, 205); button_panel.add(color_buttons[2]); color_buttons[2].setBackground(color_selected[2]);
color_buttons[3] = new JButton(); color_selected[3] = new Color(0, 0, 0); button_panel.add(color_buttons[3]); color_buttons[3].setBackground(color_selected[3]);
color_buttons[4] = new JButton(); color_selected[4] = new Color(255, 165, 0); button_panel.add(color_buttons[4]); color_buttons[4].setBackground(color_selected[4]);
color_buttons[5] = new JButton(); color_selected[5] = new Color(124, 252, 0); button_panel.add(color_buttons[5]); color_buttons[5].setBackground(color_selected[5]);
for (int i = 0; i < color_buttons.length; i++) { color_buttons[i].addActionListener(this);
} start_button = new JButton("Start"); reset_button= new JButton("Reset");
public void actionPerformed(ActionEvent event) { for (int i = 0; i < color_buttons.length; i++) { color_buttons[i].addActionListener(this); if (event.getSource() == color_buttons[i]) {
for (int k = 0; k < 8; k++) { for (int j = 0; j < 4; j++) { guess_buttons[j][k].setBackground(color_selected[i]);
for (int k = 0; k < 8; k++) { for (int j = 0; j < 4; j++) { guess_buttons[j][k].setBackground(color_selected[i]);
}
all of the buttons are painted with one color. Try it without the for loops , declare k and j as a global variables and set them to 0; then after every event increase j++ up to 3 then increase k++ and set j=0 and again .... ... ... and just stop when k reaches 8
This post has been edited by PennyBoki: 24 Jun, 2007 - 02:40 PM
hi Penny , thanks a lot, it does work , I tried that out but for some reason I never quite get beyond the second row. I'm getting an arrayindexoutofboundsexception, so obviously there is something wrong with my array and I think it's my color_selected[i] array. Still haven't figured out what it is though.
now i"ll make this very simple: post the new code along with the main function and every thing in it, I do try to help here and when I say the new code I mean all of it.