Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (468)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  TicTacToe - My first game  (Read 4722 times)
0 Members and 1 Guest are viewing this topic.
Offline plastygrove

Junior Member


Medals: 1
Projects: 2



« Posted 2012-11-23 14:07:33 »

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 Smiley.



https://www.dropbox.com/s/voc7obrlqctgxda/tictactoe.jar

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


Offline Gjallar

JGO Coder


Medals: 13
Projects: 1


Follower of Nurgle


« Reply #1 - Posted 2012-11-23 14:15:30 »

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  Smiley
Offline plastygrove

Junior Member


Medals: 1
Projects: 2



« Reply #2 - Posted 2012-11-23 14:27:36 »

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. Not bugs discovered  Smiley

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 119
Projects: 4
Exp: 3 years



« Reply #3 - Posted 2012-11-23 18:23:55 »

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 Grin
Offline plastygrove

Junior Member


Medals: 1
Projects: 2



« Reply #4 - Posted 2012-11-23 19:39:06 »

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 Grin

Well, I didn't say it was an efficient game Smiley. 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 Cheesy. Because, if you can beat the computer, it's probly a bug!
Offline DrZoidberg

Senior Member


Medals: 10



« Reply #5 - Posted 2012-11-30 23:05:29 »

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

http://pastebin.com/V7d1sHSe
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 613
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2012-12-01 02:01:33 »

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

http://pastebin.com/V7d1sHSe

1  
2  
3  
4  
5  
6  
7  
-   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?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline masteryoom

JGO Coder


Medals: 5
Projects: 2


If you look closely, you might see it turning...


« Reply #7 - Posted 2012-12-01 03:16:18 »

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

Smiley
Offline actual

JGO Coder


Medals: 24



« Reply #8 - Posted 2012-12-01 04:27:33 »

or... is this oddness mandatory in Scala?

Nope, you're versions are valid Scala.
Offline plastygrove

Junior Member


Medals: 1
Projects: 2



« Reply #9 - Posted 2012-12-01 04:28:20 »

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

http://pastebin.com/V7d1sHSe

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. Smiley

or... is this oddness mandatory in Scala?

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

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

Nothing easier, I'll just make the computer play a random move everytime Cheesy. Or to make it a definite loser, I'll implement a method avoid3InARow() Cheesy
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie

JGO Kernel


Medals: 200



« Reply #10 - Posted 2012-12-01 05:09:54 »

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.

Offline plastygrove

Junior Member


Medals: 1
Projects: 2



« Reply #11 - Posted 2012-12-01 06:17:33 »

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
Offline sproingie

JGO Kernel


Medals: 200



« Reply #12 - Posted 2012-12-01 07:43:31 »

sbt and eclipse work great together:

1  
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.

Offline plastygrove

Junior Member


Medals: 1
Projects: 2



« Reply #13 - Posted 2012-12-01 08:23:19 »

sbt and eclipse work great together:
1  
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.0")


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.
Offline sproingie

JGO Kernel


Medals: 200



« Reply #14 - Posted 2012-12-01 23:04:41 »

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.
Offline plastygrove

Junior Member


Medals: 1
Projects: 2



« Reply #15 - Posted 2012-12-02 07:00:19 »

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:

http://stackoverflow.com/questions/10998264/command-line-parameters-with-jarsplice
Offline souldown

Innocent Bystander





« Reply #16 - Posted 2012-12-04 08:53:52 »

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!)
Offline plastygrove

Junior Member


Medals: 1
Projects: 2



« Reply #17 - Posted 2012-12-05 06:32:40 »

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.
Offline Hagex

Junior Newbie





« Reply #18 - Posted 2013-01-07 11:25:51 »

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  Tongue
Offline plastygrove

Junior Member


Medals: 1
Projects: 2



« Reply #19 - Posted 2013-01-08 03:31:38 »

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  Tongue

Glad you liked it! Smiley
Yes, I did consider putting in a delay, but felt it was unnecessary. This is tic tac toe and not chess Cheesy. 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
Offline JohnsonCorp

Senior Newbie





« Reply #20 - Posted 2013-01-21 07:12:10 »

Feels like AI is cheating or something. Is it possible to win?
Offline sproingie

JGO Kernel


Medals: 200



« Reply #21 - Posted 2013-01-21 07:20:24 »

Feels like AI is cheating or something. Is it possible to win?

That either deserves applause or a facepalm, not sure which.   Cranky
Offline plastygrove

Junior Member


Medals: 1
Projects: 2



« Reply #22 - Posted 2013-01-21 18:52:29 »

Feels like AI is cheating or something. Is it possible to win?

Nope, like I said, AI can never lose. If it loses (or cheats) then there's a bug Smiley.

That either deserves applause or a facepalm, not sure which.   Cranky

Heh, I agree with you Cheesy
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #23 - Posted 2013-01-25 01:37:04 »

Feels like AI is cheating or something. Is it possible to win?

That either deserves applause or a facepalm, not sure which.   Cranky
Considering his other high quality comments, I'm pretty sure it's the second.... Cranky

Pages: [1]
  ignore  |  Print  
 
 

 
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!