Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Java Game: Tic-Tac-Toe (Revised Game)

tic-tac-toe

  • Please log in to reply
6 replies to this topic

#1 Chall

Chall

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 349 posts
  • Location:Cedar Rapids, IA
  • Programming Language:Java
  • Learning:C, Java, C++, C#, Python, JavaScript, Assembly

Posted 11 October 2012 - 04:52 PM

Today, I created a new Java tutorial based on the very popular tutorial Tic-Tac-Toe. To get to the tutorial, simply click on the link above. The game I will be sharing uses a basic AI program, and if you don't use some of those impossible to beat moves, you will find it hard to beat. Here is the game I made:

Attached File  TicTacToe.jar   9.74KB   513 downloads 9.7kb

Requires Java 7 to run. If you want to play two player, run it with
java -jar TicTacToe.jar anythinghere

Picture:

Posted Image
  • 1
Speaks fluent Java

#2 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 11 October 2012 - 11:32 PM

Are that JLabels, or custom painting on jpanels or anything?
If it's custom painting, try enabling anti-aliasing if it isn't yet. The letter O also seems to be a bit off-center.
  • 0

#3 Chall

Chall

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 349 posts
  • Location:Cedar Rapids, IA
  • Programming Language:Java
  • Learning:C, Java, C++, C#, Python, JavaScript, Assembly

Posted 12 October 2012 - 04:15 AM

Painting on Canvas extension. I didn't know there was anti-aliasing. Also, since it was a tutorial, I wanted to keep it relatively more simple. The reason why the O is off is because it must be either larger or smaller slightly than the X. It paints the symbols dynamically, so there are no final coords of each image. I didn't want to add a separate case for the Os just because they were off.
  • 0
Speaks fluent Java

#4 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 12 October 2012 - 04:17 AM

Anti aliasing is pretty simple:
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
And then use g2 as if it was a normal Graphics object.
  • 0

#5 Chall

Chall

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 349 posts
  • Location:Cedar Rapids, IA
  • Programming Language:Java
  • Learning:C, Java, C++, C#, Python, JavaScript, Assembly

Posted 12 October 2012 - 12:17 PM

Okay, thanks. :) I think I will fix the O too. After I'm done with the final touches, I'm moving on to make a battleship game. Ain't that gunna be fun :)
  • 0
Speaks fluent Java

#6 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 12 October 2012 - 02:47 PM

It's probably harder for the computer to win battleship, as it's more RNG based.

For the tic-tac-toe. Make your AI start in the center, I think you can always win if you start in the center :) I noticed it started at the side for me.

For the letter O, the graphics object should have something like "getFontMetrics", so you can get the width of the 'O' before drawing it. Knowing the width, you can exactly tell at which X-position it should start.
  • 0

#7 Chall

Chall

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 349 posts
  • Location:Cedar Rapids, IA
  • Programming Language:Java
  • Learning:C, Java, C++, C#, Python, JavaScript, Assembly

Posted 12 October 2012 - 03:27 PM

Well, I probably shouldn't say this, but the first move of the AI is always random, and from that point on, if there is no move to block, or win to make, it will just make other random moves. I used to have a set strategy array, but it would have been too much for the tutorial. And I fixed the O in my source, I might re-upload today or tomorrow.
Oh, and if you start at the center, you don't always win. One of the best combos is this, numbered in order.
[0][0][1]
[0][0][0]
[0][2][3]
As if you can make this pattern on any vertex in 3 turns, and the 2 possible wins are not blocked, you will always win. Number 1 and 2 can swap order of placement, but 3 must always come last, otherwise the other player (and AI) will suspect that you are trying to make a win. :)
  • 0
Speaks fluent Java





Also tagged with one or more of these keywords: tic-tac-toe

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download