Hey, im new here.. obviously still a newbie.. i got a few question..
This 4 file, when i try to run them it works, but then they become funny when u click 1 of the color at the bottom, i need to minimize and maximize it to fix it.. any idea wads wrg? i cant find out the reason.
And also at the end of the game.. i get this Exception in thread error.. , oh and also since this is not the whole thing, need to close it with terminate
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 10 at MastermindFrame.fillBoard(MastermindFrame.java:77) at MastermindFrame.paint(MastermindFrame.java:60) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JLayeredPane.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paintToOffscreen(Unknown Source) at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source) at javax.swing.RepaintManager$PaintManager.paint(Unknown Source) at javax.swing.RepaintManager.paint(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source) at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source) at sun.awt.SunGraphicsCallback.runComponents(Unknown Source) at java.awt.Container.paint(Unknown Source) at java.awt.Window.paint(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source) at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.Action; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; @SuppressWarnings("serial") public class Frame extends JFrame { // Game control actions private Action newAction; private Action quitAction; // User account control actions private Action loginAction; private Action registerAction; private Action logoffAction; private MastermindFrame mastermindframe; // Window listener private class FrameWindowListener extends WindowAdapter { /** Invokes quit action when closing window */ @Override public void windowClosing(WindowEvent we) { quitAction.actionPerformed(null); } } /** Create a new frame. */ public Frame() { mastermindframe = new MastermindFrame(); mastermindframe.reset(); mastermindframe.setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addWindowListener(new FrameWindowListener()); // Create menu bar JMenuBar menuBar = new JMenuBar(); add(menuBar, BorderLayout.NORTH); JMenu gameMenu = new JMenu("Game"); gameMenu.add(new JMenuItem(newAction)); gameMenu.add(new JMenuItem(quitAction)); JMenu userMenu = new JMenu("User"); userMenu.add(new JMenuItem(registerAction)); menuBar.add(gameMenu); menuBar.add(userMenu); JButton submit = new JButton( "Submit guess" ); submit.addActionListener(mastermindframe); addMouseListener(mastermindframe); setLayout(new BorderLayout()); setBackground( Color.white ); add(menuBar, BorderLayout.NORTH); getContentPane().add(mastermindframe, BorderLayout.CENTER); getContentPane().add(submit, BorderLayout.SOUTH); //setSize( new Dimension( 220, 450 ) ); setVisible( true ); this.setMinimumSize(new Dimension(400, 360)); // Create the playing board. //PlayBoard playBoard = new PlayBoard(null); //getContentPane().add(playBoard); setTitle("Mastermind"); pack(); } }
/** Game client entry point, where the game client start */ public class StartClient { /** Client entry point with no argument. */ public static void main(String[] args) { Frame frame = new Frame(); frame.setVisible(true); } }
import java.awt.Color; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.ArrayList; import javax.swing.JButton; import javax.swing.JPanel; public class MastermindFrame extends JPanel implements MouseListener, ActionListener{ private static MastermindFrame instance = new MastermindFrame(); private static final int ROWS = 10; private int dw, dh, currentColor, currentRow, turn; private Color guesses[][] = new Color[ROWS][4]; private Color responses[][] = new Color[10][4]; private Color colors[] = { Color.blue, Color.green, Color.yellow, Color.red, Color.black, Color.LIGHT_GRAY }; private int[] input = new int[4], bw = new int[2]; private Game Model = new Game(); //public JPanel Pane = new JPanel(); private boolean displayAnswer; public static MastermindFrame getInstance() { return instance; } /*public void addMouseListener(MouseListener m1) { Pane.addMouseListener(m1); }*/ /*public MastermindFrame() { this.setVisible(true); }*/ public void reset() { for (int i=0; i < ROWS; i++) for (int j=0; j < 4; j++) { guesses[i][j] = Color.white; responses[i][j] = Color.gray; } turn = 0; currentRow = 0; currentColor = 0; displayAnswer = false; } public void paint( Graphics g ) { dw = (int) getSize().width / 7; dh = (int) getSize().height / (ROWS+3); fillBoard( g ); drawBoard( g ); } private void fillBoard( Graphics g ) { if (displayAnswer) { int[] answer = Model.getanswer(); for (int j=0; j < 4; j++) { g.setColor( colors[answer[j]] ); g.fillRect( dw*j+dw, 0, dw, dh ); } } for (int i=0; i <= currentRow; i++) for (int j=0; j < 4; j++) { g.setColor( guesses[i][j] ); g.fillRect( dw*(j+1), dh*(i+1), dw, dh ); } for (int i=0; i < currentRow; i++) for (int j=0; j < 4; j++) { g.setColor( responses[i][j] ); g.fillRect( 5*dw+j%2*dw/2, dh*(i+1)+j/2*dh/2, dw/2, dh/2 ); } } private void drawBoard( Graphics g ) { g.setColor( Color.black ); // draw rows for (int i=0; i < ROWS+1; i++) g.drawLine( dw, dh*(i+1), 6*dw, dh*(i+1) ); // draw col for (int i=0; i < 6; i++) g.drawLine( dw*(i+1), dh, dw*(i+1), (ROWS+1)*dh ); // draw the peg line for (int i=0; i < ROWS; i++) g.drawLine( dw*5, (i+1)*dh+dh/2, dw*6, (i+1)*dh+dh/2 ); // draw the peg line g.drawLine( dw*5+dw/2, dh, dw*5+dw/2, dh*(ROWS+1) ); // draw the color for (int i=0; i < 6; i++) { g.setColor( colors[i] ); g.fillRect( i*dw+dw/2, dh*(ROWS+1)+dh/2, dw, dh ); } } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub int x = e.getX(), y = e.getY(); // click above the board if (y < dh) { displayAnswer = ! displayAnswer; repaint(); } else if (y < dh*(ROWS+1)+dh/2) { // place a color x = (x - dw) / dw; input[x] = currentColor; guesses[currentRow][x] = colors[currentColor]; repaint(); } else { // click below the board currentColor = (x - dw/2) / dw; // to select a color System.out.println( "color is " + currentColor ); int[] j = Model.getanswer(); for(int i=0; i<j.length; i++) { System.out.print(j[i]); } } } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getActionCommand().equals("Submit guess")) { Model.guess( input, bw ); for (int i=0; i < bw[0]; i++) responses[currentRow][i] = Color.black; for (int i=bw[0]; i < bw[0]+bw[1]; i++) responses[currentRow][i] = Color.white; currentRow++; turn++; if (bw[0] == 4) { if(turn >= 0 && turn <=3) { if(turn == 0) { //10 point }else if (turn == 1) { //9 }else if (turn == 2) { //8 }else if (turn == 3) { //7 } System.out.println("You are genius"); } else if(turn >=4 && turn <=7) { System.out.println("You are good"); if(turn == 4) { //6 }else if (turn == 5) { //5 }else if (turn == 6) { //4 }else if (turn == 7) { //3 } }else if (turn <= 9) { System.out.println("You are not bad"); if(turn == 8) { //score = 2 }else if (turn == 9) { //score = 1 } } ((JButton)e.getSource()).setText( "Go again?" ); Model.reset(); } } else { ((JButton)e.getSource()).setText( "Submit guess" ); reset(); } repaint(); } }
import java.io.Serializable; import java.util.ArrayList; import java.util.Random; /** * Game object describe a game, the object can be serialized and save to file * for resuming. * * @author Lin */ @SuppressWarnings("serial") public class Game implements Serializable { //static final + random no generator private Random rn = new Random(); public static final int NUMBER_OF_POSITIONS = 4; public static final int MAXIMUM_VALUE = 5; /** The secret code for the game */ //private final String secretCode; /** Guesses from user for this game */ private ArrayList<String> guesses; private boolean finished; //answer n stuff private static int[] answer = new int[NUMBER_OF_POSITIONS]; private int answerSums[] = new int[MAXIMUM_VALUE+1]; /** Creates a new game. */ public Game()//(String secretCode) { /*assert secretCode.length() == 4; this.secretCode = secretCode; this.guesses = new ArrayList<String>(); this.finished = false;*/ //random number generator for (int i=0; i < answer.length; i++) answer[i] = rn.nextInt( answerSums.length ); // Initialize the answer housekeeping array for (int i=0; i < answer.length; i++) answerSums[ answer[i] ]++; } /** * Add the attempt to guess history, and return the game state. * * @param guess * @return true if game is finished, false if not */ /*public boolean newGuess(String guess) { assert guess.length() == 4 && finished == false; guesses.add(guess); return finished = guess.equalsIgnoreCase(secretCode) || guesses.size() == 10; }*/ /** Accessor for secret code. */ public int[] getanswer() { return answer; } /*public String getSecretCode() { return this.secretCode; }*/ /** Accessor for attempts, return {@code null} if no attempt made. */ /*public String[] getGuesses() { if (guesses.isEmpty()) return null; return guesses.toArray(new String[0]); }*/ /** * Gets the feedback for a user attempt. * * @param guess * @return the feedback for the guess */ /*public String getFeedback(String guess) { assert guess.length() == 4; // TODO method uncompleted return "BBBB"; }*/ public void guess( int[] input, int[] bw ) { bw[0] = bw[1] = 0; // Initialize the input housekeeping array int[] inputSums = new int[answerSums.length]; for (int i=0; i < answer.length; i++) inputSums[ input[i] ]++; // Compute the total for (int i=0; i < answerSums.length; i++) bw[1] += (inputSums[i] < answerSums[i]) ? inputSums[i] : answerSums[i]; // Compute the black response for (int i=0; i < answer.length; i++) if (input[i] == answer[i]) bw[0]++; // Now, derive the white response bw[1] = bw[1] - bw[0]; } //reset code for new game public void reset() { for (int i=0; i < answer.length; i++) answer[i] = rn.nextInt( answerSums.length ); // Initialize the answer housekeeping array for (int i=0; i < answer.length; i++) answerSums[ answer[i] ]++; // TODO Auto-generated method stub } }