TicTacToe - My first game

Well, I’ve been able to churn out my first game and it’s just TicTacToe. It has been more difficult than I would like to admit. But I’m really happy with the way it turned out. Here’s the download link for the jar and before you download it, please note that you cannot win against the computer :).


http://s17.postimage.org/sgtogzht7/tictac.jpg

Here’s some details about the game. I recently learnt Scala and wanted to implement the AI completely in Scala. For this I first created a simple text based game in Java with the move recommender engine in Scala. Once the text game worked fine, I used Slick to create the game and Inkscape to create the (lame) graphics.

There is a standard strategy for TicTacToe as given on Wikipedia. But I wanted to come up with my own strategy for it. So I didn’t bother to look at this link until far later when my AI wasn’t performing optimally.

I’ve developed a recursive score based AI which calculates all possible moves and assigns a score to every possible move. But this method led me to 3 issues - viz incorrect handling of

  1. forks
  2. immediate wins
  3. immediate losses

So, I’ve had to adapt a bit of the standard strategy to fix these issues. Forks were handled through hardcoding.

In any case, I’ve cut corners everywhere in this project. The code is an unmaintainable mess with hardcoded strings, numbers and pixel locations. But there are far better tictactoe implementations out there, so I didn’t want to make too much of it. It’s more or less a Proof of Concept.

I’m happy to have been able to integrate Java, Slick and Scala.

Btw, creating the jar has been a nightmare. Thanks to jarsplice, I was finally able to make it. The complete instructions on how I did it are here: http://stackoverflow.com/questions/13527869/could-not-find-main-method-from-given-launch-configuration-when-using-javasca

Oh me oh my, that is one big-ass jar file for such a small game. I looked into the file and I’m pretty sure the “slick.zip” is not necessary, so that’s 10 Mb less already.

Besides that it’s pretty good for a first game, with sounds and all. No bugs discovered :slight_smile:

Thanks for that. I removed slick.zip and now the .jar is ~10mb. Again, bulk of the file is because of the scala library which is taking up ~17mb uncompressed.

Thanks for trying out the game. I’m glad it worked, I didn’t test it on any machine other than mine.

lol, 10 mb is still huge for a tic tac toe game…everything on java4k is under 4 kilobytes and a lot more complex. Although I understand Scala is added.

Nice little game tho, keep on getting draws ;D

Well, I didn’t say it was an efficient game :). It was a good learning exercise on integrating Scala with Java. The algo I’m using is also probly not the most optimal either as you can see from the slight delay in playing the first move.

Thanks for trying it out. I was thinking of offering a prize for anyone who beats the computer :D. Because, if you can beat the computer, it’s probly a bug!

Actually the “AI” for an unbeatable TicTacToe game can be written in just a few lines of code in scala.

http://pastebin.com/V7d1sHSe


-   var result = if(a == b) true else false
+   var result = (a == b)


-   if(a == b) result = true
-       else result = false
+   var result = (a == b)

or… is this oddness mandatory in Scala?

I would really like it if you could make the computer beatable. ;D

Nope, you’re versions are valid Scala.

Well, the AI code in Scala for me takes ~130 lines. And there’s also a support file written in Java, so definitely not as compact. :slight_smile:

Ha ha, no you’re right. I do that myself quite often, it’s the curse of imperative programmers in the functional world :slight_smile:

Nothing easier, I’ll just make the computer play a random move everytime :D. Or to make it a definite loser, I’ll implement a method avoid3InARow() :smiley:

Good to know jarsplice worked, but if you use sbt, the one-jar plugin works very well. The sbt-assembly plugin is also good for making fat jars but I don’t think it has a story for native libs.

I would really like to use SBT, but it’s just not convenient if you’re using Eclipse. Doable definitely, but not convenient. After racking my brains for a few hours to make a jar, jarsplice was the only thing that worked for this project. I don’t understand why it’s so difficult to make a jar using Eclipse. They’ve got all the wizards, they just don’t work well if you use external libs

sbt and eclipse work great together:

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.0")

Stick that in ~/.sbt/plugins/eclipse.sbt then “sbt eclipse” should work for any project to generate eclipse project files. I’ve always found IDE export wizards to be way more cumbersome than they’re worth.

Yup, I use the eclipse sbt plugin now and then. I normally put it in a folder called ‘project’ in the project folder itself, but your method seems more convenient.

It works especially well with libraries on the repository like Akka, but not sure how to get it setup with a project using Java and Slick2D, or for that matter, libraries on my PC. Also not sure what the workflow should be like if I need to add more libraries at a later time. Do I run sbt and re-import or should I do it in Eclipse itself?

I’ll need to learn a bit more about build.sbt as well.

Since the current slick isn’t in maven anymore, it’s hard to make it a managed dependency, but any jar you stick in the lib/ folder will be picked up by sbt. However, it’s likely sbteclipse will only automatically add managed deps, so you’ll have to add them in your project in eclipse by hand. Maybe these days it adds a folder classpath container for lib/ but I’ve never tested that.

Oh and I believe sbt also adds lib/ to your native library path as well, so ‘sbt run’ shouldn’t need any special setup to pick up the native libs. It’s still your job to make it work outside of sbt though, at which time there’s jarsplice or one-jar (one-jar will use everything in /binlib as a native dep). If jarsplice has an API or CLI front end, maybe I could look into writing a sbt plugin for it.

Thanks for the info about sbt. I’ll try it out and see how it works with external libs i.e. unmanaged deps. No problems with Jarsplice, it’s one of the good tools to have in your Java arsenal IMO. Doesn’t look like it has any support for CLI:

I have just finished my fist game too! Its also tic tac toe.
All written in java with no scala or anything else.
Also mines 2 player and haven’t set about working on a 1 player version of the game yet. Will do that eventually.
Also mine doesn’t seem as pretty as yours. (shocking as that might be to you!)

Hey, welcome to the forum. Take it from one who has just completed a tic tac toe game, I know the first game is anything but trivial. See if you can get it to a 1-player version. The strategy is quite simple as given on wikipedia but implementing it is definitely not easy.

Awesome game!
I love how there is absolutely no delay between the time when the player plays and when the computer plays!
I have yet to win a game though :stuck_out_tongue:

Glad you liked it! :slight_smile:
Yes, I did consider putting in a delay, but felt it was unnecessary. This is tic tac toe and not chess :D. I wanted the game to be as quick as possible so that you can play as many times as you like with min time wasted