Hey everyone, so I made a terminal based Connect 4 game, that works fairly well. (at least for what I want it to do) I'm trying to build a GUI for it, but I'm having some problems.
This is the code I've got for my connect 4 game so far, for your reference...
So, my ultimate goal is to have this logic translated into a GUI. My plan is to have a button for each column, and translate the button push into the necessary input. I have the buttons mostly figured out, but my real issue here is displaying the game board. I want to display the gameboard as a grid of white circles, that turn red and black when they are selected. That's the one thing I can't seem to find any information on how to do.
This is what I have for the GUI so far...
So far, the only thing I have is the six buttons, and they are not tied to anything other than a system.out to see if they work. I'm wanting to display the "gameboard" underneath the six buttons, and have the console output of my connect 4 game translated into the GUI.
I am a suspicious so‑and‑so, and I assume code as complicated as what you have shown us is incorrect. If you had spent twenty minutes thinking about the concepts behind the game, you could have reduced that to well under 20% of its current length, and made the code nice and easy to read.
Expunge all memory of
addActionListener(this); from your mind. It is poor non‑oriented code and error‑prone and look how horrible that block of if statements looks. Add a separate Listener to each button; you will probably need a private class implementing ActionListener.
What is the public interface of the connect 4 app? It should have methods to drop a counter into a particular column, signal a win, show the different colours of the counters, etc. The GUI is there to serve the app, and the structure of the GUI should match the app. If you have a method to drop a counter into column n then you can have a button at the top of each column which calls
myGameApp.dropCounterIntoColumn(n); Work out that concept and you won't find it difficult. Set up an array of panels or labels and use the paintComponent method with g.fillOval(...) to draw the different coloured counters. Maybe, give the panels a method so you can call addCounter(Color.RED); on them.
What's that smell? Hey, sniff this tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library