configuring a scoreboard in game

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • laxbob410
    New Member
    • Apr 2010
    • 1

    configuring a scoreboard in game

    TRying to finish the memory game but i can't figure out how to implement the scoreboard to switch from players.

    My code gives player 1 a point when the boxes match, but i don't know how to make the code recognize player 2's turn and scoring.

    help?

    Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.*;
    import javax.swing.*;
    
    import static java.util.Collections.*; //uses shuffle
    
    
    public class MemoryGame1 extends JFrame implements ActionListener {
    
    	//variables
    	public int player1 = 0;
    	public int player2 = 0;
    	public JLabel Score1, Score2;
    	public JButton exit1, replay;//buttons to restart and leave the memory Game
    	public JButton[] memoryButton = new JButton[16];//for 16 boxes of buttons
    	public ArrayList<Integer> gameList = new ArrayList<Integer>();
    	//manipulate the size of the arrays
    	public int count = 0;
    	public int[] button = new int[2];
    	public int[] Value = new int[2];
    
    	public MemoryGame1() {
    		//calling methods
    		init(); //play board
    		buttlabels();//controls
    		panel();//
    		edit();//colors and fonts
    		Scramble();
    
    
    		setTitle("Memory Game for PC Programming");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setVisible(true);
    		setSize(850,700);
    	}
    	public void init() {
    		for (int i = 0; i < memoryButton.length; i++) {
    			memoryButton[i] = new JButton("?");
    			memoryButton[i].setFont(new Font("serif", Font.BOLD, 75));
    			memoryButton[i].addActionListener(this);
    			memoryButton[i].setBackground(Color.white);
    			//Play board buttons
    		}	
    	}
    	public void buttlabels(){//controls for buttons and labels
    		//titling the controls
    		exit1 = new JButton("Exit");
    		exit1.addActionListener(this);
    		replay = new JButton("Replay");
    		replay.addActionListener(this);
    
    		Score1 = new JLabel(" Player One Score: " + player1);
    		Score2 = new JLabel("Player Two Score: " + player2);
    	}
    	public void panel() {
    		Panel MemoryPanel = new Panel();
    		MemoryPanel.setLayout(new GridLayout(4, 4));
    		for (int i = 0; i < memoryButton.length; i++) //memory button length =16
    			//16 is the amount of boxes for the game
    		{
    			MemoryPanel.add(memoryButton[i]);
    		}
    		Panel buttonPanel = new Panel();//code for 
    		buttonPanel.add(replay);
    		buttonPanel.add(exit1);
    		buttonPanel.add(Score1);
    		buttonPanel.add(Score2);
    		buttonPanel.setBackground(Color.white);
    		buttonPanel.setLayout(new GridLayout(1, 0));
    
    		add(MemoryPanel, BorderLayout.CENTER);
    		add(buttonPanel, BorderLayout.NORTH);
    
    	}
    	public void edit() {
    		//edits labels and * with colors and fonts
    		exit1.setFont(new Font("Serif", Font.BOLD, 17));
    		replay.setFont(new Font("Serif", Font.BOLD, 17));
    	//*buttons
    		Score1.setFont(new Font("Serif", Font.BOLD, 15));
    		Score1.setForeground(Color.blue);
    		Score2.setFont(new Font("Serif", Font.BOLD, 15));
    		Score2.setForeground(Color.red);
    
    	}
    	public void Scramble() { //sets up what shows up behind the buttons and changes them
    		for (int i = 0; i < 2; i++) {
    			for (int ii = 1; ii < (memoryButton.length / 2) + 1; ii++) {
    				gameList.add(ii);
    			}
    		}
    		shuffle(gameList);
    
    		int number = 0;
    		
    		for (int a = 0; a < gameList.size(); a++) {
    			number++;
    	
    			System.out.print(" " + gameList.get(a));
    			if (number == 4) {
    				number = 0;
    				System.out.println();
    			}
    		}
    	}
    	
    	
    	public boolean sameValues(){//if cards are the same, stay
    		if (Value[0] == Value[1]) {//If one card matches other
    
    		
    		player1= player1 + 1 ;
    			Score1.setText (" Player One Score: " + player1);
    			return true;
    		}
    		
    		return false;}
    			
    	
    				
    	/*
    			player2= player2 + 1 ;
    			System.out.println(player2 + 1);
    			Score2.setText (" Player Two Score: " + player2);}
    		*/	
    	
    	
    
    	public void actionPerformed(ActionEvent MEM) {
    		if (exit1 == MEM.getSource()) {
    			System.exit(0);//closes program
    		}
    		if (replay == MEM.getSource()) {
    			//need action for replay
    			new MemoryGame1();
    		}
    		for (int i = 0; i < memoryButton.length; i++) {
    			if (memoryButton[i] == MEM.getSource()) {
    				memoryButton[i].setText("" +gameList.get(i));
    				memoryButton[i].setEnabled(false);
    				count++;
    
    				if (count == 3) {
    					if (sameValues()) {
    						memoryButton[button[0]].setEnabled(false);
    						memoryButton[button[1]].setEnabled(false);
    						 
    					}
    						else {
    						memoryButton[button[0]].setEnabled(true);
    						memoryButton[button[0]].setText("?");
    						memoryButton[button[1]].setEnabled(true);
    						memoryButton[button[1]].setText("?");
    						
    	
    					}
    					count = 1;
    				}
    				if (count == 1) {
    					button[0] = i;
    					Value[0] = gameList.get(i);
    
    				}
    				if (count == 2) {
    					button[1] = i;
    					Value[1] = gameList.get(i);
    
    				}
    			}
    		}
    		}
    	
    	
    
    
    	//main method, calls for the Game
    	public static void main(String[] args) {
    		new MemoryGame1();
    
    	}
    }
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Have some sort of flag eg: int currentPlayer, and swap the value from 1 to 2 or back when switching turns.

    Whenever you're updating score, check the flag's value.

    Comment

    Working...