File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Game Development and the fly likes TicTacToe game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "TicTacToe game" Watch "TicTacToe game" New topic

TicTacToe game

Flip Alvez

Joined: May 30, 2011
Posts: 2

Hi every one! Im a total java noob, and ive got to do a Tic tac toe game for my java class and Im getting a lot of difficulties ,and since Ive been a lurker of this forum for a while now, I decided to post here haha . Ive already searched for a tons of code of how to do a tic tac toe java game but i cant understand them very well and the most part of code that ive seen is much more complicated for what i want, I just want a simple tic tac toe game that executes in the console haha..
basically the game must have 2 game modes : SinglePlayer and TwoPlayers. The singlePlayer mode must have three levels of difficulty : Easy, Medium and Hard.
I decided to start with the two player mode but im having issues with how to execute the game with the players 'X' and 'O' and put it on the BoardGame.
I was thinking of doing a method that recieves the position of where I want to put the symbol and then alternate that symbol with a counter.. but i dont know how lol.. im thinking of doing a boardGame class that contains the board and the plays methods, a player class that contains the players and the Game modes and a TicTacToe class that just executes the game..

Heres my BoardGame class:

Thanks in advance.
P.s - Sorry about my english, its a bit rusty.
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 260
You want methods to :
  • initialise the board and set whose turn it is
  • find the next free square
  • to see if the game is finished (won or drawn)
  • to swap sides

  • Obviously this won't play well but it will allow you to get a program to play. To get it to play slightly better you'll have to prefer the centre and corners before playing the sides. To get it to play much better then you'll need to look up MinMax searches and selecting the best move.

    PS why do you use pos (1-9) to input a move but then store it in a 3*3 array? wouldn't it be simpler to just have a position array of length 9 and then store the board in that?

    Arcade : Alien Swarm
    Board : Chess - Checkers - Connect 4 - Othello
    Flip Alvez

    Joined: May 30, 2011
    Posts: 2

    yeah thats a good ideia, its simpler . Thanks : )
    I will try to do a method that recieves the position that i want to store in the board and the player 'X' or 'O' and then i post here the results hehe
    Ashish Schottky
    Ranch Hand

    Joined: Dec 29, 2009
    Posts: 93
    At original poster:
    Its generally a bad idea to implement magic numbers.
    what you can do is

    may be you can make some classes.
    1)Main class could control the game-flow, it the overall management of the game. It might contain

    2)AI class
    where the details about AI will go,
    if you want to make three levels, make sure you treat the maximum depth in accordance to your level.
    Say easy,maxPly=2 medium maxPly=5,hard maxPly=9.
    If your implementation is correct then your code would be unbeatable at the hardest level(Converse, if your AI is beatable on level 9 then for sure there is some bug ;) )

    This is just the tip of advice I can offer.
    Anyways if you would like me to explain you more regarding the same, just drop me a PM and then I would even start to blog on how to write tic-tac-toe.
    It would surely improve my english writing skills.
    I agree. Here's the link:
    subject: TicTacToe game
    It's not a secret anymore!