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? -
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();
-
-
}
-
}
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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,...
|
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...
|
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...
|
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...
|
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 .
| |
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...
|
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
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |