• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Memory game making Computer the second player

Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone. I'm trying to make a Java Swing memory game program and I'm quite stuck when it comes to making the Computer be a player.

This is the code for the GamePanel where I check if the two cards chosen are the same or not (starts on Line 90).

Note: I have removed some stuff like imports etc to make the code a bit smaller.
Right now if start the program and successfully make pairs(when it's the Player's turn) it works normally. Computer chooses 2 random cards, if they're a pair they're replaced with the Empty image. Otherwise, they're flipped back to normal. If I make a wrong pair though all cards get flipped and the game is unplayable.
I know this is a lot I'm asking, but I'm getting pretty desperate right here. Some guidance would be greatly appreciated.

Anyway, thanks in advance for your time. If there are parts that aren't clear enough let me know.
Dim Kyrtan
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rest of the Classes:

Card class code.

User class code.

Users are instansiated here.
Posts: 64638
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Thank you for posting nicely indented code, but please leave the imports in the code; then readers can copy and paste the code and run it immediately.

I am afraid I think you have gone round this the wrong way. I think you should have a version of the game that you can run from the terminal/command line, and only write the GUI as a display when that is working.
Don't use Strings for difficulty. Use an enum instead.
Never write == true or == false. Both are poor style and error‑prone if you write = by mistake.
boolean b = ...;
Never if (b == false) ...
Always if (!b) ...
Never if (b == true) ...
Always if (b) ...
Don't use setXXX() and getXXX() methods like that. It isn't object‑oriented to add scores like that. Write a scorePoints() method and call player.scorePoints(10);
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!