/* http://www.javaprogrammingforums.com/whats-wrong-my-code/41805-java-matching-colours-game-second-click-not-working.html
When a button is clicked on the grid, it should be uncovered, and then you get a second click and this should be uncovered as well.
If they both match, both buttons should become black, if not then they should be covered to their initial blank colours (so grey).
Right now, the first button is covered and not the second.
Note: Needs lots of comments re the logic / should use enums
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.*;
@SuppressWarnings("serial")
public class OOCW2Last {
class RandomX { //<<<<<<<<< For debugging
int val = -1;
public int nextInt(int y) {
val++;
if(val > y)
val = 0;
return val;
}
} // end testing class
enum WhatColor {Original, Real, Black}; //<<<<<<<< added
public static final int WIDTH = 4; // Changed height???
public static final int HEIGHT = 4; // 5 gave 5x5 square ????
public static final int COLOURS = 4;
public static final int MAX_PER_COLOUR = 4;
public static final int BUT_SIZE = 100;
int player1Score = 0;
int butNumber1 = -1;
int butNumber2 = -1;
int player2Score = 0;
public int turn = 1; //???? WHAT values???
int player = 0;
boolean matchedColours;
int i = 0;
JLabel topLabel = new JLabel("Player 0 score: " +player1Score+" choose your first square");
JLabel botLabel = new JLabel("Player 1 score: " +player2Score+" wait for your turn ");
ColorButton[] arrayButtons = new ColorButton[WIDTH*HEIGHT];
Random rand = new Random();
//------------------------------------------------------
public static void main(String[] args)
{
OOCW2Last main = new OOCW2Last();
main.createGUI();
}
public void createGUI()
{
try {
UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
} catch (Exception e) {
e.printStackTrace();
}
JPanel panelMain = new JPanel();
JFrame mainGUI = new JFrame();
mainGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //<<<<<<<<<<<<
Color[] colors = new Color[8];
colors[0] = Color.blue;
colors[1] = Color.yellow;
colors[2] = Color.cyan;
colors[3] = Color.red;
colors[4] = Color.orange;
colors[5] = Color.green;
colors[6] = Color.white;
colors[7] = Color.pink;
topLabel.setBackground(Color.GREEN);
topLabel.setOpaque(true);
botLabel.setBackground(Color.RED);
botLabel.setOpaque(true);
panelMain.setLayout(new GridLayout(WIDTH,HEIGHT)); // rows, columns
for (int i = 0; i < WIDTH*HEIGHT; i++)
{
arrayButtons[i] = new ColorButton(i, BUT_SIZE, BUT_SIZE, this);
panelMain.add(arrayButtons[i]);
}
int x = MAX_PER_COLOUR;
int y = WIDTH*HEIGHT;
int z;
for (int i = 0; i < COLOURS; i++)
{
while(x > 0)
{
z = rand.nextInt(y);
while (arrayButtons[z].colorPicked == true){
z = rand.nextInt(y);
}
arrayButtons[z].realButtonColor = colors[i];
arrayButtons[z].colorPicked = true;
x --;
}
x = MAX_PER_COLOUR;
}
mainGUI.getContentPane().add(topLabel, BorderLayout.NORTH);
mainGUI.getContentPane().add(botLabel, BorderLayout.SOUTH);
mainGUI.getContentPane().add(panelMain,BorderLayout.CENTER);
mainGUI.pack();
mainGUI.setResizable(false);
mainGUI.setVisible(true);
}
public void message(JLabel botLabel, JLabel topLabel)
{
if(turn == 1)
{
if(player == 0)
{
topLabel.setText("Player 0 score: " +player1Score+" choose your first square");
botLabel.setText("Player 1 score: " +player2Score+ " wait for your turn ");
}
else
{
topLabel.setText("Player 0 score: " +player1Score+" wait for your turn ");
botLabel.setText("Player 1 score: " +player2Score+ " choose your first square ");
}
}
if(turn == 2)
{
if(player == 0)
{
topLabel.setText("Player 0 score: " +player1Score+" choose your second square");
botLabel.setText("Player 1 score: " +player2Score+ " wait for your turn ");
}
else
{
topLabel.setText("Player 0 score: " +player1Score+" wait for your turn ");
botLabel.setText("Player 1 score: " +player2Score+ " choose your second square");
}
}
if(turn == 3)
{
if(player == 0)
{
if(matchedColours)
{
topLabel.setText("Player 0 score: " +player1Score+" Well Done! Click a square to continue");
botLabel.setText("Player 1 score: " +player2Score+ " wait for your turn ");
}
else
{
topLabel.setText("Player 0 score: " +player1Score+" Failed! Click a square to continue");
botLabel.setText("Player 1 score: " +player2Score+ " wait for your turn ");
}
}
else
{
if(matchedColours)
{
topLabel.setText("Player 0 score: " +player2Score+ " wait for your turn ");
botLabel.setText("Player 1 score: " +player1Score+" Well Done! Click a square to continue");
}
else
{
topLabel.setText("Player 0 score: " +player2Score+ " wait for your turn ");
botLabel.setText("Player 1 score: " +player1Score+" Failed! Click a square to continue");
}
}
}
}
public boolean buttonClicked(int iButton)
{
message(botLabel, topLabel);
System.out.println("bC "+iButton + ", turn="+turn +", btn1="+butNumber1 + ", btn2="+butNumber2); //<<<
boolean changeColor = true; // <<<<<<<<< ADDED
if(turn==1)
{
butNumber1 = iButton;
turn++;
message(botLabel, topLabel);
}
else if(turn==2)
{
turn++;
butNumber2 = iButton;
message(botLabel, topLabel);
if(arrayButtons[butNumber1].realButtonColor == (arrayButtons[butNumber2].realButtonColor))
{
matchedColours = true;
arrayButtons[butNumber1].setColor(WhatColor.Black); //flip(false);
arrayButtons[butNumber2].setColor(WhatColor.Black); //flip(false);
message(botLabel, topLabel);
changeColor = false;
}
else
{
butNumber2 = iButton;
matchedColours = false;
// Need to wait some before changing the colors<<<<<<<<<<<<<<<
arrayButtons[butNumber1].setColor(WhatColor.Original); //setBackground(null);
// arrayButtons[butNumber2].setColor(WhatColor.Original); //setBackground(null); /**issue may be here, something to do with butNumber2**/
(new WaitThenSetColor(butNumber2)).execute(); //<<< second will show for a short time
// changeColor = false; Now want color to be changed so user can see for a few seconds
message(botLabel, topLabel);
}
turn = 1;
}
else if(turn==3) //????? when
{
message(botLabel, topLabel);
}
return changeColor;
}
class WaitThenSetColor extends SwingWorker<String, Object> {
int btnId;
public WaitThenSetColor(int btnId){
this.btnId = btnId;
}
@Override
public String doInBackground() {
try{Thread.sleep(1000);}catch(Exception x){}
return "";
}
@Override
protected void done() {
try {
arrayButtons[btnId].setColor(WhatColor.Original);
} catch (Exception ignore) {
}
}
}
//--------------------------------------------------------------------
class ColorButton extends JButton implements ActionListener
{
OOCW2Last objectMain;
int buttonNumber;
Color realButtonColor;
Color drawColor;
JFrame guiFrame = new JFrame();
int buttonState = 0; //????? what values?
boolean colorPicked = false;
public ColorButton(int buttonN, int height, int width,
OOCW2Last mainClassObject)
{
try {
UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
} catch (Exception e) {
e.printStackTrace();
}
objectMain = mainClassObject;
buttonNumber = buttonN;
this.setBackground(drawColor);
setMinimumSize(
new Dimension(width, height) );
setPreferredSize(
new Dimension(width, height) );
addActionListener(this);
}
// public void flip(Boolean b) // Sets real color or sets color black <<<< Should be two different methods: setReal and setBlack
public void setColor(WhatColor wc)
{
// if (b == true)
switch(wc) {
case Real:
{
drawColor = realButtonColor;
buttonState = 1; //??? what does this mean
this.setBackground(realButtonColor);
System.out.println("setColor real "+ buttonNumber + " to " + realButtonColor +", state=1"); //<<<<<<<
}
break;
// if (b == false)
case Black:
{
drawColor = Color.BLACK; /*grey*/
buttonState = 0; //????
this.setBackground(Color.BLACK);
System.out.println("setColor black "+buttonNumber + " to black, state=0"); //<<<<<<<
// Should this deactivate this button?
removeActionListener(this); //<<<<<<<<<<<<<
}
break;
case Original:
System.out.println("setColor original "+buttonNumber + ", state="+buttonState); //<<<<<<<
setBackground(null);
break;
}
}
public void setButtonColor(Color realColor)
{
setBackground(realColor);
}
public void drawButton()
{
if (buttonState != 0) //????
{
setButtonColor(drawColor);
}
}
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println("aP " + buttonNumber + ", state="+buttonState + ", rBtnColor="+realButtonColor); //<<<<<<<<
boolean changeColor = objectMain.buttonClicked(buttonNumber);
if (buttonState == 0 && changeColor) //????
{
setColor(WhatColor.Real); //flip(true); //???? only call
}
drawButton();
}
}
}
/*
Running: java.exe -client OOCW2Last
aP 0, state=0, rBtnColor=java.awt.Color[r=0,g=0,b=255]
bC 0, turn=1, btn1=-1, btn2=-1
flip true 0 to java.awt.Color[r=0,g=0,b=255], state=1
aP 4, state=0, rBtnColor=java.awt.Color[r=255,g=255,b=0] << does not match
bC 4, turn=2, btn1=0, btn2=-1
>>>set background null
flip true 4 to java.awt.Color[r=255,g=255,b=0], state=1 <<<<<< should not happen
Running: java.exe -client OOCW2Last
aP 1, state=0, rBtnColor=java.awt.Color[r=0,g=0,b=255]
bC 1, turn=1, btn1=-1, btn2=-1
flip true 1 to java.awt.Color[r=0,g=0,b=255], state=1
aP 2, state=0, rBtnColor=java.awt.Color[r=0,g=0,b=255] <<<<<<< Matches
bC 2, turn=2, btn1=1, btn2=-1
flip false 1 to black, state=0 <<<<< called from bC
flip false 2 to black, state=0
flip true 2 to java.awt.Color[r=0,g=0,b=255], state=1 <<<<<<< should not happen called from aP
0 error(s)
Running: java.exe -client OOCW2Last <<<<<<<<<< Using Random colors <<<<<<<<<<
aP 1, state=0, rBtnColor=java.awt.Color[r=0,g=255,b=255]
bC 1, turn=1, btn1=-1, btn2=-1
setColor real 1 to java.awt.Color[r=0,g=255,b=255], state=1
aP 5, state=0, rBtnColor=java.awt.Color[r=0,g=255,b=255]
bC 5, turn=2, btn1=1, btn2=-1
setColor black 1 to black, state=0
setColor black 5 to black, state=0
aP 0, state=0, rBtnColor=java.awt.Color[r=255,g=255,b=0]
bC 0, turn=1, btn1=1, btn2=5
setColor real 0 to java.awt.Color[r=255,g=255,b=0], state=1
aP 4, state=0, rBtnColor=java.awt.Color[r=255,g=255,b=0]
bC 4, turn=2, btn1=0, btn2=5
setColor black 0 to black, state=0
setColor black 4 to black, state=0
aP 2, state=0, rBtnColor=java.awt.Color[r=0,g=255,b=255]
bC 2, turn=1, btn1=0, btn2=4
setColor real 2 to java.awt.Color[r=0,g=255,b=255], state=1
aP 6, state=0, rBtnColor=java.awt.Color[r=255,g=255,b=0]
bC 6, turn=2, btn1=2, btn2=4
setColor original 2, state=1
setColor real 6 to java.awt.Color[r=255,g=255,b=0], state=1
setColor original 6, state=1
aP 3, state=0, rBtnColor=java.awt.Color[r=255,g=0,b=0]
bC 3, turn=1, btn1=2, btn2=6
setColor real 3 to java.awt.Color[r=255,g=0,b=0], state=1
aP 7, state=0, rBtnColor=java.awt.Color[r=0,g=255,b=255]
bC 7, turn=2, btn1=3, btn2=6
setColor original 3, state=1
setColor real 7 to java.awt.Color[r=0,g=255,b=255], state=1
setColor original 7, state=1
0 error(s)
*/