Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: I need help with my game

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need help with my game

    So I'm working on this game for a school project and I have 2 problems
    1.I do not know how to display the players score in the panel
    2.Every single time the console reads the second button I click on as a 0 instead of the number underlining it

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.*;
    import javax.swing.border.*;
     
    public class Matchgame extends JPanel implements ActionListener {
     
    	private final JPanel Brd = new JPanel(new BorderLayout(3, 3));
    	String Winner;
    	private JButton[][] BoardSquares = new JButton[4][10];// 10 X 4 grid
    	int board[][] = {
    			{ 2, 11, 3, 2, 6, 5, 7, 9, 8, 1 },// numbers to fill Jbutton with
    			{ 10, 6, 1, 3, 8, 12, 13, 14, 15, 4 },
    			{ 17, 9, 10, 12, 11, 20, 13, 16, 4, 5 },
    			{ 19, 7, 17, 16, 14, 15, 19, 20, 18, 18 } };
     
    	private JPanel Board;// Sets JPanel
    	int guess1, guess2; // number value of the button pressed
    	public static boolean attempt1, attempt2, player1, player2; // players and
    	// attempts
    	int A1i, A1j, A2i, A2j;// numbers to put in place of board[i] and board[j]
    	private int rows = 4;// number of rows
    	private int cols = 10;// number of columns
    	private int turn = 20;// player turn
    	public int counter = -1;// counter
    	public int P1score = 0;// player 1 score
    	public int P2score = 0;// player 2 score
     
    	Matchgame() {
    		initialize();
    	}
     
    	public final void initialize() {
    		// set up the main GUI
     
    		Board = new JPanel(new GridLayout(rows, cols));
    		Board.setBorder(new LineBorder(Color.BLACK));
    		// Board.add(Score);
    		Brd.add(Board);
    		// create the board squares
    		for (int i = 0; i < BoardSquares.length; i++) {
    			for (int j = 0; j < BoardSquares[i].length; j++) {
    				JButton b = new JButton();// adds buttons
    				BoardSquares[i][j] = b;
    				b.addActionListener(this);
    			}
    		}
    		for (int i = 0; i < BoardSquares.length; i++) {
    			for (int j = 0; j < BoardSquares[0].length; j++) {
    				switch (j) {
    				default:
    					Board.add(BoardSquares[i][j]);
    				}
    			}
    		}
    	}
     
    	public final JComponent getBoard() {
    		return Board;
    	}
     
    	public final JComponent getGui() {
    		return Brd;
    	}
     
    	public static void main(String[] args) {
    		Runnable r = new Runnable() {
     
    			@Override
    			public void run() {
    				Matchgame cb = new Matchgame();
    				JFrame f = new JFrame("MATCH GAME");
    				f.add(cb.getGui());
    				f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    				// ensures the frame is the minimum size it needs to be
    				// in order display the components within it
    				f.setSize(500, 400);
    				// ensures the minimum size is enforced.
    				// f.setMinimumSize(f.getSize());
    				f.setVisible(true);
    			}
    		};
    		SwingUtilities.invokeLater(r);
    	}
     
    	public void sameValues() {
    		if (player1 = true) {
    			P1score++;
     
    		}
    		if (player2 = true) {
    			P2score++;
     
    		}
     
    	}
     
    	public void actionPerformed(ActionEvent arg0) {
    		if (turn % 2 == 0) {//if even then 
    			player1 = true;
    		} else {
    			player2 = true;
    		}
    		++counter;
    		if (counter == 0) {
    			attempt1 = true;
    		} else if (counter == 1) {
    			attempt2 = true;
     
    		}
    		if (counter == 2) {
     
    		if (guess1 == guess2) {
    				System.out.println("test" + guess1 + "test" + guess2);
    				sameValues();
     
    			} else if (guess1 != guess2) {
    				System.out.println("test" + guess1 + "test" + guess2);
    				BoardSquares[A1i][A1j].setText("");
    				BoardSquares[A2i][A2j].setText("");
     
    			}
     
    			counter = -1;
    			turn--;
    		}
     
    		for (int i = 0; i < BoardSquares.length; i++) {
    			for (int j = 0; j < BoardSquares[i].length; j++) {
    				if ((counter == 0) && (counter < 4)) {
    					if (arg0.getSource() == BoardSquares[i][j]) {
    						BoardSquares[i][j].setText("" + board[i][j]);
    						if (attempt1 == true) {
    							guess1 = board[i][j];
    							A1i = i;
    							A1j = j;
    						} else if (attempt2 == true) {
    							guess2 = board[i][j];
    							A2i = i;
    							A2j = j;
    						}
     
    					}
    				}
    			}
    		}
    		if (turn == 0) {
    			EndGame();
    		}
    	}
     
    	public void EndGame() {
    		if ((turn == 0) && (P1score > P2score)) {
    			Winner = JOptionPane
    					.showInputDialog("Player 1 won, enter anything to restart game!");
    			P1score = 0;
    			P2score = 0;
    		}
    		if ((turn == 0) && (P2score > P1score)) {
    			Winner = JOptionPane
    					.showInputDialog("Player 2 won, enter anything to restart game!");
    			P1score = 0;
    			P2score = 0;
    		}
    	}
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I need help with my game

    1.I do not know how to display the players score in the panel
    I suggest you add the playing board to a JFrame with another JPanel to display the score. The score can be printed most simply by using the g.printString( x, y, "String Here" ); in the scoring JPanel's paintComponent() method. You could add a shape of a different color behind the scores to emphasize them.
    2.Every single time the console reads the second button I click on as a 0 instead of the number underlining it
    Did you notice that selecting the second square actually requires 2 clicks? Is that the behavior you expected? I don't think so. It indicates that the logic in your actionPerformed() method is incorrect. It's also overcomplicated and uncommented. I suggest you think about how the game is played, write down the steps on paper, turn those into comments in your code, and program that.

    This code is incorrect (hint: consider the '='):
        public void sameValues() {
            if (player1 = true) {
                P1score++;
     
            }
            if (player2 = true) {
                P2score++;
     
            }
     
        }
    Comparing a boolean to true or false in a condition, like this:

    if (attempt1 == true)

    is unnecessary. Simply,

    if ( attempt1 )

    is sufficient.

    Having 2 booleans, attempt1 and attempt2, is unnecessary. The attempt is either attempt1 or !attempt1.

Similar Threads

  1. LWJGL Game: Odd behavior in game. Need help.
    By vividMario52 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 2nd, 2013, 05:43 PM
  2. Replies: 3
    Last Post: March 1st, 2013, 11:01 PM
  3. Help with Snake Game Java Code: It's Impossible to Lose the Game
    By haruspex_icis in forum What's Wrong With My Code?
    Replies: 20
    Last Post: December 17th, 2012, 12:21 PM
  4. Game Maker Language and Game Maker and Zelda Classic thread
    By Fira in forum Other Programming Languages
    Replies: 3
    Last Post: April 17th, 2012, 08:59 AM
  5. Simple game that requires me to load game settings from a file
    By 14fenix in forum Java Theory & Questions
    Replies: 5
    Last Post: December 1st, 2011, 09:21 PM