Tic Tac Toe

To skip explanations and to access the board, click here.

This is an easy implementation of a Tic Tac Toe game for two players. The players, "X" and "O", play alternately. That player who succeeds first to have three of his symbols in a line wins. If both players play optimal, the result is a draw.

The fields of the game are declared as Java Script buttons. These buttons activate in the case of a click the function "clicked" in the script code at the beginning of the page.

There are three types of players already implemented: Random, Easy and Medium. The task is to add the Good player. The following should be done:
  1. Implement the strategy for the "good" player by writing the function "goodmove();". For this, the following information on the implementation should be taken into account:
            The variable "fields" contains the current board of the
            game:
                      fields[0]  fields[1]  fields[2]
                      fields[3]  fields[4]  fields[5]
                      fields[6]  fields[7]  fields[8]
    
            The values are "-", "X" and "O" as displayed on the screen.
    
            The variable movenumber tells how many moves have already been made.
    
            The local variables player and counterplayer inform on the
            names ("X" and "O") of the player and his opponent.
    
            The other implemented strategies can be used as an example
            of how can it be done. In particular one can also work with
            copies of the board as the variable fcopy in the function
            mediummove().
    
            wincheck(fcopy) determines which player is winning the
            game as it is on the board in the variable fcopy. The
            return values are:
              "X": player "X" wins
              "O": player "O" wins
              "D": draw
              " ": game not yet terminated, a further move has to be done 
    The suggest move button for the good player can be used to test the implemented button before doing whole games or tournaments.
  2. The good player should never loose and furthermore occasionally win against the Random, Easy and Medium players. These players follow randomized strategies and do more or less often bad moves when they can be defeated; on the other hand, they might also make good moves and then the good player can only obtain a draw. So the good player should exploit winning situations without giving himself in danger to loose a game.
  3. Add buttons in order to start the other combinations of tournaments which are not yet there. Look at the already existing buttons in order to see how such buttons are programmed. The parameter 'G' in the call of the function (instead of 'R','E','M') stands for "good". Java Script has two types of quotes in order to permit quoting inside quotes, as needed here.
  4. Run several hundred of games of tournaments in order to make sure that the good player never looses and occasionally beats the weaker players. Good versus good should always end in draws, otherwise the strategy for the good player is not yet completed. The additional window contains the protocols of all the games in tournaments so that a tournament can be inspected for finding rare events (like defeats of a not yet perfect good player).
Tic Tac Toe Board
Press the below buttons to play. The first nine buttons are the Tic Tac Toe field. Note that when the computer playes "O", the user has nevertheless to start the game by making one "X" move after pressing the button to activate the computer program to move.





Suggesting a move


Computer plays "X"


Computer plays "O"


Tournaments

Moves made: