• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

TicTacToe game

Posts: 2
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
    Flip Alvez
    Posts: 2
    Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Ranch Hand
    Posts: 93
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
    Hey! Wanna see my flashlight? It looks like this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
      Bookmark Topic Watch Topic
    • New Topic