Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Java:Tutorial - Tic-Tac-Toe

tic-tac-toe

  • Please log in to reply
60 replies to this topic

#49 ultimate99

ultimate99

    CC Newcomer

  • New Member
  • PipPip
  • 10 posts

Posted 27 April 2012 - 02:29 AM

Great tutorial! Very interesting indeed.
But my concern is what if I want the program to be buttonless, meaning console only, what should I do?
  • 0

#50 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 27 April 2012 - 07:25 AM

Great tutorial! Very interesting indeed.
But my concern is what if I want the program to be buttonless, meaning console only, what should I do?

Leave out the GUI. Don't handle Button/Mouse events, just handle keyboard input using an object that'll read from System.in.
  • 0

#51 ultimate99

ultimate99

    CC Newcomer

  • New Member
  • PipPip
  • 10 posts

Posted 27 April 2012 - 07:27 AM

Leave out the GUI. Don't handle Button/Mouse events, just handle keyboard input using an object that'll read from System.in.


Yes, I tried that. Left out buttons, JFrame, active listener..etc, but it messes up all the code.
  • 0

#52 lethalwire

lethalwire

    while(false){ ... }

  • Senior Member
  • PipPipPipPipPipPip
  • 766 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:PHP

Posted 27 April 2012 - 07:31 AM

You'll probably need to rewrite most of the code yourself. Removing the GUI components won't get you a working program.

If you're having trouble, feel free to post your problem in the http://forum.codecal.../126-java-help/ section.
  • 0

#53 MesutErgul

MesutErgul

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts
  • Programming Language:C, Java, C++, C#, PHP, (Visual) Basic, Python, JavaScript, Perl, Ruby, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Lisp, Pascal, Transact-SQL, Assembly, ActionScript, Fortran, Bash
  • Learning:Objective-C

Posted 25 June 2012 - 11:33 PM

One try without game trees:
1-to win(your double)
2-if not, not to lose(opponent's double)
3-if not, do you already have a fork(have a double double)
4-if not, if opponent has a fork
i-search in blocking points for possible double and fork(ultimate win)
ii-if not search forks in blocking points(which gives the opponent the most losing possibilities:check losing points for opponent )
iii-if not only blocking points(not to lose)
5-if not search for double and fork(ultimate win)
6-if not search only for forks which gives opponent the most losing possibilities:check losing points for opponent
7-if not search only for a double
8-if not dead end, tie, random.
9-if not(it means your first move)
i-if it's the first move of the game;
ix-give the opponent the most losing possibility(the algorithm results in only corners which gives 7 losing point possibility to opponent)
iy-or for breaking boredom just random.
ii-if it's second move of the game;
iix-find only the not losing points(gives a little more options)
iiy-or find the points in this list which has the best winning chance(it can be boring,cause it results in only all corners or adjacent corners or center)

Note: When you have double and forks, check if your double gives the opponent a double.if it gives, check if that your new mandatory point is included in your fork list.
  • 1

#54 tpPacino

tpPacino

    CC Regular

  • Member
  • PipPipPip
  • 29 posts
  • Location:Sarajevo, Bosnia and Herzegovina
  • Programming Language:C, Java, C++, PHP, (Visual) Basic, PL/SQL, Pascal, Others
  • Learning:Objective-C, C#, JavaScript, Ruby, Haskell, Others

Posted 26 June 2012 - 03:57 AM

It is an amazing tutorial, just the AI is bad code. You could use the Minimax algorithm, designed for games. There are already a lot of implementations of it around the Internet, using C++ to make tic tac toe. It's easily exported to Java...
  • 0

#55 DanielTan

DanielTan

    CC Regular

  • Member
  • PipPipPip
  • 25 posts

Posted 22 July 2013 - 04:29 AM

You also could have use a 2d array to store int values to mean cross and circles, then do summation to see if they add up to the winning scores

 

i.e. cross =>1, circle =>9, cross win=>3, circle win=>27 

 

instead of using boolean calculation because they are three states: null, circle, cross,since you're already using an array to set up the buttons.
I tried it in python and it works fine. Also it keeps the code clean because you don't have to manually check every row/column.


  • 0

#56 TheEyesHaveIt

TheEyesHaveIt

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 15 August 2013 - 06:37 AM

Hi, I was wondering if it is acceptable to put all the code for checking for victory inside the actionPerformed method?  

 

Not sure if it's better to make a new method or new class for checking victory.


Edited by TheEyesHaveIt, 15 August 2013 - 06:38 AM.

  • 0

#57 Polymath

Polymath

    CC Lurker

  • New Member
  • Pip
  • 8 posts

Posted 14 April 2014 - 07:28 PM

Thank you so much John, im using this to get a better grasp on some things i already know. Its great seeing different ways to do similar things. Im especially ecstatic about the AI, i always wanted to code AI but never knew how, however now i have an opportunity. You've been a great help.

 

Thanks, Polymath


  • 0

#58 Polymath

Polymath

    CC Lurker

  • New Member
  • Pip
  • 8 posts

Posted 18 April 2014 - 11:36 AM

Thanks so much for this tutorial , I've replicated something very similar to it by my-self and your code was a great help.

However, i am unsure as to how the winConditions 2-D array works with the program. You used {}{} to place values in the array, however could someone explain how these values are checked in the "Determine who won" for-loop.

 

I looked all over the internet, and i only saw curly braces ({}) being used once in a 2-D array with the comma separating the different indexes. How and why do you have so many?

 

Thanks, Polymath


  • 0

#59 ravital

ravital

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 10 June 2014 - 04:49 PM


I looked all over the internet, and i only saw curly braces ({}) being used once in a 2-D array with the comma separating the different indexes. How and why do you have so many?

 

Thanks, Polymath

John defined a two-dimensional array.  The braces each have 3 elements in them.  There are 9 pairs of braces.  This means the array has 8 rows and 3 columns.  Looking at his code:

 

private int[][] winCombinations = new int[][] {
            {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, //horizontal wins
            {1, 4, 7}, {2, 5, 8}, {3, 6, 9}, //virticle wins
            {1, 5, 9}, {3, 5, 7}  

 

This means that if you wanted to access the three values 2, 5, 8 (as one of the winning combinations), you would address each one of those three as:

 

winCombinations[4][0] // this one = 2

winCombinations[4][1] // this one = 5

winCombinations[4][2] // this one = 8

 

Rows and columns in an array are "zero-based" meaning you start counting them as zero.  First row - first column is [0][0], first row - second column is [0][1], and so on.


  • 1

#60 Ritwik I the programmer

Ritwik I the programmer

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts
  • Programming Language:Java
  • Learning:Java, C++, Python

Posted 12 June 2014 - 01:53 AM

ravital has explained it pretty well. I would just like to add that a 2d array in java is an array of 1D arrays. So, {1, 2, 3} is a 1d array, and so are {4, 5, 6}, {7, 8, 9}, and so on, while winCombination is defined as an array whose elements are these predefined 1d arrays.


Edited by Ritwik I the programmer, 12 June 2014 - 01:54 AM.

  • 0

I can believe, but why should I?






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