//**************************************Classes Imported**********************************************************
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.event.*;
import java.awt.Dimension;
import java.awt.event.ActionListener;
//import java.io.InputStream;
import java.util.Random;
import javax.swing.*;
public class Mastermind extends JFrame implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
int width;
int height;
int numColors;
int change;
static int pairs2;
int y=0;
static JButton guess;
final Color[] COLORS = { Color.green, Color.blue, Color.orange, Color.yellow,Color.red, };
JButton[][] colouredPegs;
JButton[][] whites;
JButton[][] blacks;
JButton[] computerGuess;
int [] guesses = new int [4];
int [] [] game = new int [10] [4];
int [] [] bw = new int [10] [2];
int[][] arraycopyguess;
int[] arraycopyrandom;
JPanel colouredPanel = new JPanel();
JPanel whitesPanel = new JPanel();
JPanel blacksPanel = new JPanel();
JPanel computerGuessPanel = new JPanel();
JPanel panel2 = new JPanel();
static Color choose(int i)
{
if (i==0) return Color.red;
if (i==1) return Color.green;
if (i==2) return Color.blue;
if (i==3) return Color.orange;
else return Color.yellow;
}
public Mastermind(int h, int w, int c) {
width=w;height=h; numColors=c;
colouredPegs= new JButton[height][width];
whites= new JButton[height][width];
blacks= new JButton[height][width];
computerGuess= new JButton[width];
colouredPanel.setLayout(new GridLayout(height,width));
blacksPanel.setLayout(new GridLayout(height,width));
whitesPanel.setLayout(new GridLayout(height,width));
computerGuessPanel.setLayout(new GridLayout(1,width));
colouredPanel.setBorder(BorderFactory.createEmptyBorder(20, 20,
20, 20));
whitesPanel.setBorder(BorderFactory.createEmptyBorder(20, 20,
20, 20));
blacksPanel.setBorder(BorderFactory.createEmptyBorder(20, 20,
20, 20));
Random r = new Random();
guess = new JButton("Guess");
guess.addActionListener(this);
for (int n = 0; n < 4; n++)
{
guesses[n] = r.nextInt(5);
computerGuess[n]=new JButton();
computerGuess[n].setBackground(choose(guesses[n]));
computerGuessPanel.add(computerGuess[n]);
//System.out.println(guesses[n]);
}
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
{
colouredPegs [i][j]= new JButton();
colouredPegs [i][j].setBackground(choose(i));
colouredPegs [i][j].addActionListener(this);
whites [i][j]= new JButton();
whites [i][j].setVisible(false);
whites [i][j].setBackground(Color.white);
blacks [i][j]= new JButton();
blacks [i][j].setVisible(false);
blacks [i][j].setBackground(Color.black);
colouredPanel.add(colouredPegs [i][j]);
whitesPanel.add(whites [i][j]);
blacksPanel.add(blacks [i][j]);
if (i>0)
colouredPegs[i][j].setVisible(false);
}
for (int i = 0; i < 10; i++)
for (int j = 1; j < 5; j++)
{
}
setLayout(new BorderLayout());
add(blacksPanel, "West");
add(colouredPanel, "Center");
add(whitesPanel, "East");
JPanel guessPanel = new JPanel();
guessPanel.setLayout(new FlowLayout());
guessPanel.add(guess);
add(guessPanel,"South");
JPanel topPanel = new JPanel();
topPanel.setBorder(BorderFactory.createEmptyBorder(20, 20,
20, 20));
topPanel.setLayout(new GridLayout(1,3));
topPanel.add(new JLabel("Blacks",JLabel.CENTER));
topPanel.add(computerGuessPanel);
topPanel.add(new JLabel("Whites",JLabel.CENTER));
add(topPanel,"North");
setDefaultCloseOperation(3);
setTitle("Mastermind");
setMinimumSize(new Dimension(width*50,height*50));
pack();
setResizable(false);
setVisible(true);
}
int increments =0; // adds 1 to the array
public void actionPerformed(ActionEvent e) {
for (int i=0;i<10;i++)
for (int j=0;j<4;j++)
if (e.getSource() == colouredPegs[i][j])
{
if(game[i][j] >=4)
game[i][j] = 0;
else
game[i][j]++;
colouredPegs [i][j].setBackground(choose(game [i] [j]));
// System.out.println(game[i][j]);
}
// System.out.println(game[0][3]);
//**************************************Guess Action Listener**********************************************************
guess.addActionListener (new ActionListener() {// start guess action listener
int row=0;
int count=0;
int count2=0;
int newrowvisible=1;
public void actionPerformed(ActionEvent e) { // start a e
int tt=0;
for(int j = 0; j < width; j++)
{ // start for loop
if(game[count][j] == guesses[j])
{ // start if statement
//ensures each is each(Match numbers)-both increment
tt++;//counts how many times number match
//convert used ones so if 1 is picked it's turn so no white picks up
game[count][j]=7;
guesses[j]=8;
} // end if
} // end for loop
//Displays the black button using the counter
for (int j = 0; j < tt; j++)
{ // start for loop
blacks [count][j].setVisible(true);
} // end for loop
if (tt==4)
{ // start if
for (int p = 0; p < width; p++)
{ // start for loop
computerGuess[p].setVisible(true);
} //end for loop
JOptionPane.showMessageDialog(null," Correct! You Have Guessed The Right Colours. ");
System.exit(3);
} // end if
int ss=0;
for(int k = 0; k < width; k++)
{ // tsart for
for(int l = 0; l < width; l++)//with 2 for loops
if(game[count][k]==guesses[l])//takes first from orig and compares against all 6 other white
{ // start if
//same converting helps here so once converted others can't use it
game[count][k]=7;
guesses[l]=8;
ss++;
} // end if
} // end loop
System.out.println(ss);
for (int j = 0; j < ss; j++)
{ // start for loop
whites [count][j].setVisible(true);
} // end for loop
count++;
for(int s=0;s<4;s++)
{ // start for loop
colouredPegs[newrowvisible][s].setVisible(true);
} // end for loop
newrowvisible++;
}
});
}
//**************************************Main Method**********************************************************
public static void main(String[] args) {
new Mastermind(10,4,5);
}
}
8 Replies - 968 Views - Last Post: 16 November 2009 - 07:45 PM
#1
Code nearly Working..but cant find increment problem
Posted 16 November 2009 - 06:35 PM
basically, iv rewritten my mastermind game and its nearly working..im just having issues incrementing count properly ..any advice?
Replies To: Code nearly Working..but cant find increment problem
#2
Re: Code nearly Working..but cant find increment problem
Posted 16 November 2009 - 06:38 PM
I think you need to put it inside of your loop....
This post has been edited by theautokustomizer: 16 November 2009 - 06:39 PM
#3
Re: Code nearly Working..but cant find increment problem
Posted 16 November 2009 - 06:44 PM
the int count in the guess action event as this checks the rows etc...im stuck on where to place count++; as my code dosent function properly
I was thinking maybe it needs to go to the top ..because as the guess button is clicked..count can increment but this dosent work??
I was thinking maybe it needs to go to the top ..because as the guess button is clicked..count can increment but this dosent work??
#4
Re: Code nearly Working..but cant find increment problem
Posted 16 November 2009 - 07:01 PM
Have you tried putting at the beginning of your 'for' loop?
#5
Re: Code nearly Working..but cant find increment problem
Posted 16 November 2009 - 07:05 PM
which for loop? as i have quite a few
#6
Re: Code nearly Working..but cant find increment problem
Posted 16 November 2009 - 07:09 PM
In your actionperformed method
#7
Re: Code nearly Working..but cant find increment problem
Posted 16 November 2009 - 07:15 PM
Nope dosent work..as the button is pressed..its going to increment straight away to 2 ..iv set -1 so each time it increments but must be something else thats the problem ..im confuzed lol
#8
Re: Code nearly Working..but cant find increment problem
Posted 16 November 2009 - 07:30 PM
Found the error...its not the increment..i need a copy of the game array
#9
Re: Code nearly Working..but cant find increment problem
Posted 16 November 2009 - 07:45 PM
Ah ha, well, I never ran it, I just threw it out there at a quick go threw, lol. Glad to hear you got it...
This post has been edited by theautokustomizer: 16 November 2009 - 07:46 PM
Page 1 of 1

New Topic/Question
Reply



MultiQuote


|