• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Connect Four game (edited version)

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

I received some complaints on my previous post regarding the clarity of my code. Of course they were justified. I will try again here, removing all unnecessary code for this question. So again:

Our assignment is to make a version of the classic 'Connect Four' game, where we need to construct a GUI that shows the current state of the game. Running it right now results in a board with empty spaces only. Also after choosing a column to place a chip, the board remains empty.

Question: How do we get the board filled with the right color of chips in the right columns and rows? So in this case a red oval in case player 1 makes a move and a blue oval for player 2.

We know the 'ArcsPanel'-class (almost at the bottom of the code) is (totally) wrong. We tried various approaches, but we don't know what to do to make it right.

We hope it's somewhat more clear right now. Thank you for your time.

Main 'ConnectPanel'-class:


P.S.: Because of removal of several parts of the code, it isn't entirely correct anymore (for instance no player can win) and still a bit of a mess in some parts. Though it should suffice for giving an idea of what we're trying to do.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lennart van Ham wrote:I received some complaints on my previous post regarding the clarity of my code. Of course they were justified. I will try again here, removing all unnecessary code for this question....


Fair enough. I've locked your other one to avoid confusion. In future, could you mark your threads as "resolved" when you no longer need them?

Thanks.

Winston
 
Lennart van Ham
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:In future, could you mark your threads as "resolved" when you no longer need them?


Of course, excuse me. I did search for it but did not know how. I will next time.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lennart van Ham wrote:Of course, excuse me. I did search for it but did not know how. I will next time.


No probs. In answer to your question, I can't help you much with the GUI stuff, because I'm no Swing expert, but I can offer this piece of advice:
Keep your "game logic" completely separate from your GUI.

Reason: The game of Connect4 (or "Connect-n") is the same whether you're dealing with physical pieces, a GUI, or a simple keyboard and terminal, so your "game management" shouldn't care one whit how it's being displayed.

GUIs should ONLY be concerned with displaying stuff and reacting to user "input" - in whatever form it comes (scrollbars, mouse clicks, buttons etc).
The trouble is that they're so complex in their own right that you can get distracted by all those visual components and pointer devices, and start designing the game around the display, rather than the way it should be - design the game first, and then build the GUI around it.

My approach is simple:
Design the game around a very simple "terminal"-style API - keyboard (Scanner) and a basic character-based display that simply re-displays the "board" after every move - and make absolutely sure THAT works before I write my first line of GUI code.

After that, it's simply a case of translating my "display" into GUI components and my keyboard input into "reactions", and I can concentrate on the stuff that's important to GUIs, safe in the knowledge that the rest of my program (ie, the "game logic") will work just fine.

HIH

Winston
 
Lennart van Ham
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:My approach is simple:
Design the game around a very simple "terminal"-style API - keyboard (Scanner) and a basic character-based display that simply re-displays the "board" after every move - and make absolutely sure THAT works before I write my first line of GUI code.


I understand that what you mean at this point is creating a game which gives simple, non graphical console output (in case of using Eclipse) and after that forming a GUI version, correct me if I'm wrong. Well that actually is exactly the thing we're trying to do. The game itself is completely finished, except for the GUI part, at which we don't know how to start and/or proceed.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lennart van Ham wrote:The game itself is completely finished, except for the GUI part, at which we don't know how to start and/or proceed.


So presumably you know:
  • What you need to display, based on your "game" classes.
  • The "reactions" (or moves) that a player can make.
  • What those reactions need to do to change the state of the game (presumably methods already written in your game classes).
  • (Maybe most important) What constitutes a "winning" position (again, from your game classes, NOT your GUI).
  • And all you need to do is implement it.

    Like I say, I can't be much help there, because I'm no Swing guru, but it should be a simple translation exercise.

    Winston
     
    Lennart van Ham
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Gutkowski wrote:

    Lennart van Ham wrote:The game itself is completely finished, except for the GUI part, at which we don't know how to start and/or proceed.


    So presumably you know:... ...it should be a simple translation exercise.


    That's right, we know all that. Sure it would be simple, but we're completely new to this part of Java/Eclipse. That's why we'd very much appreciate some help from someone who is more in on the swing part of Java.

    But thanks for your time anyway .
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Lennart van Ham wrote:That's right, we know all that...but we're completely new to this part of Java/Eclipse. That's why we'd very much appreciate some help from someone who is more in on the swing part of Java.


    Okey-doke. I'll move this thread to a forum that might provide you with more answers then. Hope it helps, and good luck with your project.

    Winston
     
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    In which case everything which is not related to the display should be moved to other classes. You do not play the game via the Swing components, you play it via games classes. All the Swing components should do is provide a display.
     
    Bartender
    Posts: 5465
    212
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well, the GUI part can be made short and easy, if you have a bit of experience.
    And having a decent gui can make testing very much easier.

    But, when it comes to helping getting the supplied code up and running:
    I'm afraid that code is beyond rescue. So I decided, reluctantly, to give
    it a big overhaul. Realize that the next code is only one way of doing it.
    Also, see how little code is involved.

    But it doesn't come free! Study it, think why I removed nearly all 'statics',
    and find out what's happening here. And it goes without saying that you
    give the method 'checkForResult' a body!

    Greetz,
    Piet
     
    Sheriff
    Posts: 17644
    300
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You should call setColor before you call fillOval. The other problem that I can see is that you're using theSeed to control which color you're going to draw. Not good and it's probably wrong because this variable has too wide a scope. The color of the chip to draw will change for every cell on the board whereas theSeed does not change as you loop through your board and draw each cell. Your current model of the board as int values that have specific meanings (1 = RED, 2 = BLUE, other = WHITE) is not ideal. I would suggest you eliminate the need to map the board data by using Color directly, like so:

    This allows you to deal directly with the colors instead of having to map int values to Color values. The logic to draw the board should look something like this:

    The drawCell method on the boardUI object, which would be your JPanel subclass, would have the four lines of code that you have duplicated unnecessarily in the paintComponent method. When you see code that's duplicated like that with very small variations, you should always think of putting the duplicated code in a method and parameterizing the things that vary. Duplication is a bad thing in a program. See the DRY Principle
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Junilu Lacar wrote:You should call setColor before you call fillOval....


    @Lennart: Do you see what I mean now?

    What Junilu has said (and I'm quite sure he's right) has nothing whatsoever to do with the game of Connect-4, but everything to do with how GUI code works (and very possibly, how Java GUI code works).

    Swing (or "webby" stuff like JSPs or Struts) are floundering elephants compared to the elegant design of a game like Connect-4 (or MasterMind, or Chess...) because they are concerned solely with display and reaction; not how the game works.

    So it will benefit you a lot to design your program on precisely the same basis.

    Winston
     
    Lennart van Ham
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you all for your replies. To start it is clear to me now I need to change almost all of my code. This is getting me rather furious though, since I attend an University studie and my code is fully based on a code we received from our mentors, with almost only 'statics'. Besides from that, I do understand why you guys say it is 'beyond rescue' right now (apart from the fact I removed all unnecessary components for this question). I will make sure all components except for the 'swing' will be in different classes.

    In reply to Piet Souris, thank you for your effort. The only thing I think I didn't mention clearly is that we need to add those chips by typing the desired column in 'Console', instead of clicking with your mouse. The 'MouseClicked' version can be found multiple times on the internet. Though the version we want isn't to be found anywhere.
     
    Piet Souris
    Bartender
    Posts: 5465
    212
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi Lennart,

    thanks for the explanation. I hope you took no offence my writing about 'rescue'
    I did not mean that the code per se was bad, but that so much could be said
    about it that it could take quite a long time.

    Anyway: most important remark that I would like to make is: make sure that
    the frame isn't covering the whatever input box you'll use. When I first ran it in
    NetBeans, on my laptop, the frame completely covered the console, so I had no
    idea what to do other than clicking.

    Success with the project!

    Greetz,
    Piet
     
    Junilu Lacar
    Sheriff
    Posts: 17644
    300
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Lennart, I understand your frustration and I'm also disappointed when I see these kinds of things coming from teachers. I think the problem you have been given is beyond the learning progression level where you would still use all statics. On the other hand, communication between the teacher and student is important and you should try to give feedback to the mentors you have so they can discuss alternative approaches with you. Good luck.
     
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    how can we create a connectFour with GUI in java, but there is no updated picture or files. the output will show the button with 'X' or 'O' on grid
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Welcome to the Ranch

    I suggest you start by writing down the rules of the game. That forms the program specification .
     
    Piet Souris
    Bartender
    Posts: 5465
    212
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    jiang gangjunjiang wrote:how can we create a connectFour with GUI in java, but there is no updated picture or files. the output will show the button with 'X' or 'O' on grid


    If you look back in this topic, a complete GUI-version is given. It is dated, there are some things I would not do now, but it is a working version.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic