Connect 4

I couldn’t see anywhere where it says the games have to be an arcade game so here’s a board game for you. It seems to play reasonably well and offers all the features I could think of. What’s more it only took a few days to write! Any constructive opinions welcome :slight_smile:

Play Connect 4

Mike

PS any chance of a score for my retro arcade game Moon Base Alpha ?

Nice! I tried playing at “Not bad”-level and could win a few games, but the expert-level was extremely difficult! I’m curious, in what way you implemented the AI? Is the algorithm based on a mathematical function to calculate the next move?

Though to make the game experience more interesting I would like to see more animations. Like when I press “New game” maybe some kind of animation could be shown in the same way as when a player wins a game. And what does the “Play” option do? Also it would be fun to see some kind of statistics over wins and losses.

But overall I enjoyed playing the game.

a tip if you want your games to look polished and modern, don’t use java2d shapes especially with the default java colors. They instantly make a game look horrid and something from the early 1990’s, not to mention the aliasing. Its possible but tricky to make game look good java2d shapes. IMO its better to go with images, you can easily create basic shapes in something like inkscape and still have them look good (plus its really easy to make images look even better with antialising and gradients).

[quote=kapta]a tip if you want your games to look polished and modern, don’t use java2d shapes especially with the default java colors. They instantly make a game look horrid and something from the early 1990’s, not to mention the aliasing. Its possible but tricky to make game look good java2d shapes. IMO its better to go with images, you can easily create basic shapes in something like inkscape and still have them look good (plus its really easy to make images look even better with antialising and gradients).
[/quote]
I definitely need to learn more about images etc - sadly being a bit of an old hand means I’m also a bit old fashioned in my methods to. How much effort do you think it would take to convert the program?

[quote=fruitmaze ]Though to make the game experience more interesting I would like to see more animations.
[/quote]
One issue I’m having is trying to work out how to animate a piece sliding into the grid. I just wanted the wood image to show through the grid and to see the piece sliding down. I can easily do the animation part but I can’t work out how to get the piece to appear behind the grid as it falls.

[quote=fruitmaze ]what does the “Play” option do?
[/quote]
It makes the computer play the next move even if it’s not it’s turn. This is useful if you want the computer to play first or if you’re just stuck on what move to play next.

[quote=fruitmaze ]it would be fun to see some kind of statistics over wins and losses.
[/quote]
Would this be against all people or just against yourself? I’m not sure how often people would continue playing the program once they’ve had a few games - it’s a nice game to play but still just a distraction.

[quote=fruitmaze ]I’m curious, in what way you implemented the AI? Is the algorithm based on a mathematical function to calculate the next move?
[/quote]
It walks a “tree” of moves and then evaluates the positions it comes up with. It’s really no different to a chess program etc. It uses an alphabeta pruning algorithm (a faster version of minmax) and then extends the tree each time until it runs out of time. The evaluation of the individual positions is quite complex and I’m still not convinced by the weightings applied to each part of the evaluation. The weightings are currently being improved by genetic tuning. It also applies a small random factor to the initial moves just to ensure each game is different and interesting.

I’m impressed you won a few games even at the “not bad” level.

not much extra effort at all, your already using java2d, it has built in support for loading and drawing images. Theres tons of tutorials out there on how to load and draw images with java2d.

Draw the grid last.

Sadly doesn’t work - at least not the way I tried it. The grid is mad up of lots of squares with simple round circles in the middle of the square. If the circle is coloured red, then it’s a yellow piece, red if it’s a red piece and I currently use an off blue to signify an empty square. Doing this doesn’t allow me to have pieces sliding down the grid and it doesn’t allow me to view the wooden table through the grid “holes”. I figure it must be possible somehow but perhaps I’m missing something.

Use a gif or png, image, in which case you can make the hole in the block transparent.

Nice AI. I’ve trying to set up positions where placing one block gives two alternate winning positions, but so far the computer is spotting this.

I think statistics, wins and losses, for both 1 and 2-player mode. It would make it more interesting. Perhaps even a time counter if you play against a friend?

Like Alan_W said, I think you should use png-images for the graphics. The grid then has transparent holes. And a class Circle which keeps all data for a circle, and which also has a function move() that can slide the circle down, behind the grid.

Unfortunately Graphics2D has rather limited clip support. If you use an image as a buffer you can draw the grid and then chop arbitrary holes in it, but it’s starting to get a bit advanced. (The least-code way of doing it probably involves creating a BufferedImage, getting its AlphaRaster, wrapping that in another BufferedImage, and drawing black circles on it).

I suggest following the advice of others to start with, and then maybe coming back to do this kind of image manipulation once you’re comfortable with the basics of image rendering. (The main reason you might want to come back is that by making the holes in code you give yourself more freedom to tweak the layout without having to redo the graphics).

Well I’m now using images, transparency and animation! It definitely looks better for it. I also added a little bit of sound. The new version has replaced the old version.

The game play has been marginally improved on the higher levels and weakened on the lower levels. I’m not happy with how it currently plays the lower levels and I need to think about that side of things.

I’ll admit the graphics are a way from perfect but it’s definitely way better than it was but if anyone’s good with images and wants to help improve the game then the current images are:

Mike

It looks better now, but it is a bit buggy - if I double click on the game field, while the circle is falling down, I somehow change from green to red circles?

Hopefully fixed :slight_smile: