473,806 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

configuring a scoreboard in game

1 New Member
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?

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.util.*;
  5. import javax.swing.*;
  6.  
  7. import static java.util.Collections.*; //uses shuffle
  8.  
  9.  
  10. public class MemoryGame1 extends JFrame implements ActionListener {
  11.  
  12.     //variables
  13.     public int player1 = 0;
  14.     public int player2 = 0;
  15.     public JLabel Score1, Score2;
  16.     public JButton exit1, replay;//buttons to restart and leave the memory Game
  17.     public JButton[] memoryButton = new JButton[16];//for 16 boxes of buttons
  18.     public ArrayList<Integer> gameList = new ArrayList<Integer>();
  19.     //manipulate the size of the arrays
  20.     public int count = 0;
  21.     public int[] button = new int[2];
  22.     public int[] Value = new int[2];
  23.  
  24.     public MemoryGame1() {
  25.         //calling methods
  26.         init(); //play board
  27.         buttlabels();//controls
  28.         panel();//
  29.         edit();//colors and fonts
  30.         Scramble();
  31.  
  32.  
  33.         setTitle("Memory Game for PC Programming");
  34.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  35.         setVisible(true);
  36.         setSize(850,700);
  37.     }
  38.     public void init() {
  39.         for (int i = 0; i < memoryButton.length; i++) {
  40.             memoryButton[i] = new JButton("?");
  41.             memoryButton[i].setFont(new Font("serif", Font.BOLD, 75));
  42.             memoryButton[i].addActionListener(this);
  43.             memoryButton[i].setBackground(Color.white);
  44.             //Play board buttons
  45.         }    
  46.     }
  47.     public void buttlabels(){//controls for buttons and labels
  48.         //titling the controls
  49.         exit1 = new JButton("Exit");
  50.         exit1.addActionListener(this);
  51.         replay = new JButton("Replay");
  52.         replay.addActionListener(this);
  53.  
  54.         Score1 = new JLabel(" Player One Score: " + player1);
  55.         Score2 = new JLabel("Player Two Score: " + player2);
  56.     }
  57.     public void panel() {
  58.         Panel MemoryPanel = new Panel();
  59.         MemoryPanel.setLayout(new GridLayout(4, 4));
  60.         for (int i = 0; i < memoryButton.length; i++) //memory button length =16
  61.             //16 is the amount of boxes for the game
  62.         {
  63.             MemoryPanel.add(memoryButton[i]);
  64.         }
  65.         Panel buttonPanel = new Panel();//code for 
  66.         buttonPanel.add(replay);
  67.         buttonPanel.add(exit1);
  68.         buttonPanel.add(Score1);
  69.         buttonPanel.add(Score2);
  70.         buttonPanel.setBackground(Color.white);
  71.         buttonPanel.setLayout(new GridLayout(1, 0));
  72.  
  73.         add(MemoryPanel, BorderLayout.CENTER);
  74.         add(buttonPanel, BorderLayout.NORTH);
  75.  
  76.     }
  77.     public void edit() {
  78.         //edits labels and * with colors and fonts
  79.         exit1.setFont(new Font("Serif", Font.BOLD, 17));
  80.         replay.setFont(new Font("Serif", Font.BOLD, 17));
  81.     //*buttons
  82.         Score1.setFont(new Font("Serif", Font.BOLD, 15));
  83.         Score1.setForeground(Color.blue);
  84.         Score2.setFont(new Font("Serif", Font.BOLD, 15));
  85.         Score2.setForeground(Color.red);
  86.  
  87.     }
  88.     public void Scramble() { //sets up what shows up behind the buttons and changes them
  89.         for (int i = 0; i < 2; i++) {
  90.             for (int ii = 1; ii < (memoryButton.length / 2) + 1; ii++) {
  91.                 gameList.add(ii);
  92.             }
  93.         }
  94.         shuffle(gameList);
  95.  
  96.         int number = 0;
  97.  
  98.         for (int a = 0; a < gameList.size(); a++) {
  99.             number++;
  100.  
  101.             System.out.print(" " + gameList.get(a));
  102.             if (number == 4) {
  103.                 number = 0;
  104.                 System.out.println();
  105.             }
  106.         }
  107.     }
  108.  
  109.  
  110.     public boolean sameValues(){//if cards are the same, stay
  111.         if (Value[0] == Value[1]) {//If one card matches other
  112.  
  113.  
  114.         player1= player1 + 1 ;
  115.             Score1.setText (" Player One Score: " + player1);
  116.             return true;
  117.         }
  118.  
  119.         return false;}
  120.  
  121.  
  122.  
  123.     /*
  124.             player2= player2 + 1 ;
  125.             System.out.println(player2 + 1);
  126.             Score2.setText (" Player Two Score: " + player2);}
  127.         */    
  128.  
  129.  
  130.  
  131.     public void actionPerformed(ActionEvent MEM) {
  132.         if (exit1 == MEM.getSource()) {
  133.             System.exit(0);//closes program
  134.         }
  135.         if (replay == MEM.getSource()) {
  136.             //need action for replay
  137.             new MemoryGame1();
  138.         }
  139.         for (int i = 0; i < memoryButton.length; i++) {
  140.             if (memoryButton[i] == MEM.getSource()) {
  141.                 memoryButton[i].setText("" +gameList.get(i));
  142.                 memoryButton[i].setEnabled(false);
  143.                 count++;
  144.  
  145.                 if (count == 3) {
  146.                     if (sameValues()) {
  147.                         memoryButton[button[0]].setEnabled(false);
  148.                         memoryButton[button[1]].setEnabled(false);
  149.  
  150.                     }
  151.                         else {
  152.                         memoryButton[button[0]].setEnabled(true);
  153.                         memoryButton[button[0]].setText("?");
  154.                         memoryButton[button[1]].setEnabled(true);
  155.                         memoryButton[button[1]].setText("?");
  156.  
  157.  
  158.                     }
  159.                     count = 1;
  160.                 }
  161.                 if (count == 1) {
  162.                     button[0] = i;
  163.                     Value[0] = gameList.get(i);
  164.  
  165.                 }
  166.                 if (count == 2) {
  167.                     button[1] = i;
  168.                     Value[1] = gameList.get(i);
  169.  
  170.                 }
  171.             }
  172.         }
  173.         }
  174.  
  175.  
  176.  
  177.  
  178.     //main method, calls for the Game
  179.     public static void main(String[] args) {
  180.         new MemoryGame1();
  181.  
  182.     }
  183. }
  184.  
Apr 28 '10 #1
1 3031
jkmyoung
2,057 Recognized Expert Top Contributor
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.
Apr 28 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

138
6618
by: theodp | last post by:
--> From http://www.techdirt.com/articles/20040406/1349225.shtml Microsoft Patents Saving The Name Of A Game Contributed by Mike on Tuesday, April 6th, 2004 @ 01:49PM from the yeah,-that's-non-obvious dept. theodp writes "As if there weren't enough dodgy patents, here's an excerpt from one granted to Microsoft Tuesday for a 'Method and apparatus for displaying information regarding stored data in a gaming system': 'When saving a game,...
10
4380
by: Ed Stevens | last post by:
A tale of woe, and a question . . . Last week my boss said to me "we've installed DB2 Connect on this Solaris box. Make it work." Now, I've barely seen DB2 Connect on Windows, having fumbled thru one install and config, with a lot of handholding from IBM Support. I'm comfortable in unix but have never dealt with DB2 Connect there. (my main job is as an Oracle DBA, and most of my db's are on Solaris or AIX) I've found the install...
1
1745
by: Ron Weldy | last post by:
So far, every book I have looks at on ASP.NET jumps right into creating a project. There is no discussion about setting up the environment, using a local IIS install (I think it can all be ran on XP now, right?) vs. having a development server, etc. I will want to eventually maintain several websites using ASP.NET and SQL Server and I have my own development box with W2003 server and IIS6 running. Anybody got some good reference material...
1
2892
by: fowle040 | last post by:
I underlined and bold print my files. I need to know how to make this code into a working game. The object of the game is to have two players 1- belle and 2-beast. I want them to lose and gain strength and health, when 0 die. I need to know how to make them interact. I need to know what to do with teraform (creek, pasture, brick road) how to add. I need the public interface to look like the forest. classes need to function the program...
2
1922
by: yogesh | last post by:
i developing a game in c++, for that i got the socket program is working successfull in freebsd , but the same program is shifted to the Redhat linux 9.0 . i change the ip address and the port is also same i can compile the code successfull. but if i run the server socket file It not assigning the port , the port i am liking is 8002 .
5
13917
pbmods
by: pbmods | last post by:
Configuring PHP 5.2.3 with GD support for Mac OS X PHP 5.2.3 does not seem to want to configure with GD support on Mac OS X for some reason. When configuring, you may notice this error: configure: error: GD build test failed. Please check the config.log for details. Checking config.log yields the following: configure:42434: gcc -o conftest -g -O2 -no-cpp-precomp -L/usr/local/lib -L/usr/local/lib conftest.c -L -lfreetype -lpng...
1
2621
by: rada.lambretha | last post by:
Configuring Linux as a Firewall * Making installation choices * Introducing iptables * Using iptables commands * Simplifying things with firewall GUIs * Adding proxy functionality As Linux gains increasing acceptance in corporate datacenters and
2
5319
by: LilMeechc20 | last post by:
Hello, I have a group assignment that I have to do. We have to write a Tic Tac Toe game. One person in my group has managed to write the code for a multiplayer (human -vs- human) game and I managed to write a code for a single player (human -vs- computer) game. My problem is, now we want to merge the two games with options to pick which game you would like to play. However, the games were written totally different styles and I can't...
0
10617
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10109
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5545
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3849
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.