0% found this document useful (0 votes)
69 views8 pages

CS21 Machine Problem: Tactical Tic-Tac-Toe: Algorithm and Implementation

This document describes a machine problem to create an AI for the game Tic-Tac-Toe that cannot lose. It details an algorithm using three categories to determine the computer's move based on blocking winning positions or creating its own. The program utilizes a 1D array to represent the 3x3 board and prompts users to take turns entering tile numbers for their marks. It ensures valid inputs and implements the algorithm to achieve a draw at minimum each game.

Uploaded by

Nikki Fernandez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views8 pages

CS21 Machine Problem: Tactical Tic-Tac-Toe: Algorithm and Implementation

This document describes a machine problem to create an AI for the game Tic-Tac-Toe that cannot lose. It details an algorithm using three categories to determine the computer's move based on blocking winning positions or creating its own. The program utilizes a 1D array to represent the 3x3 board and prompts users to take turns entering tile numbers for their marks. It ensures valid inputs and implements the algorithm to achieve a draw at minimum each game.

Uploaded by

Nikki Fernandez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Fernandez, Nikki R.

2015 01108
THWSXY

CS21 Machine Problem: Tactical Tic-Tac-Toe

1.) Introduction:
Tic-Tac-Toe is commonly known as a pen-and-paper game
for two players assigned each with a symbol either X or O.
The players then take turns in drawing their marks on a 3x3
grid with the goal of forming 3 of their symbols in one
line either horizontally, vertically or diagonally.
It has been proven in some research that the game
could be perfectly played by one player and the worst case
could only lead to a draw. This machine problem would be an
imitation of the game Tic Tac Toe given an AI that
would never let the computer (CPU) lose the game whenever
it is to be played.

2.) Algorithm and Implementation:


Implementation:

The implementation of the program utilizes a 1D


array to store the so-called game board or the 3 x 3
grid that would be used for gameplay.

As usual, the game starts by asking the user


whether they want to let the computer (CPU) take the
first turn by entering "yes" or "no". [see Fig. 1]

Figure 1: Instructions / Opening of the game


The computer and the user then take turns to input
the number [1 9] of the tile where they want to
place their mark (as long as that tile is still
empty). [see Fig. 1]

Figure 2: Interface of the program

Algorithm:
Researching and playing a number of games paved the way
to an algorithm to know where the CPU should place its mark to
obtain a win or at least a draw to be used for this program.
The algorithm consists of 3 categories for every instance
encountered during gameplay with each category ranked or given
a priority.

Categories (Ranked):

1. Explicitly check tiles 5 and 9 if occupied by the opponent.


- Check tile 3 if it's empty.
- If it is empty, place CPU's mark on the tile to block the
player / user from making winning moves.

Figure 3: Category 1 ( Tiles 5 and 9)


2. Explicitly check these tiles if they are occupied by the
player / user:

1st: 1 and (6 or 8): If YES, check if tile 9 is blank then


place CPU's mark there.

2nd: 3 and (4 or 8): If YES, check if tile 7 is blank then


place CPU's mark there.

3rd: 7 and (6 or 2): If YES, check if tile 3 is blank then


place CPU's mark there.

4th: 9 and (4 or 2): If YES, check if tile 1 is blank then


place CPU's mark there.

Figure 4: 1st instance

Figure 5: 2nd instance

Figure 6: 3rd instance


Figure 7: 4th instance

3. Win / Block category:


Check whether 2 tiles (horizontally, vertically or
diagonally) are the same, the CPU should place the mark on
the tile to make a straight line to form a win if the marks
are Xs or a block if the marks are Os

3.) Game Design:


Shown below is the game design of the program. Just like a normal
board, it's only a simple design and there are no changes as to what
it actually looks like in other tic tac toe games.

Figure 8: Game board

4.) Register / Memory Tabulation:


Figure 9: Registers used at the start of the program

Figure 10: Registers used for the array / game board

Figure 11: Extra register used

5.) Label Meanings:


6.) Usage and User Interface:
The picture shown below is the user interface of the TIC TAC TOE
program in MARS
Figure 12: User interface

Figure 13: Game board

7.) Limitations:
Limitations of the program include the user input which only
accepts a "yes" or a "no" from the user. Although when a user enters a
word/string with "yes " and a different character at the end, the
program would still be able to accept it as the program reads the
first 3 letters that matches with "yes".
Another limitation is when the user is about to input a tile
number. The user should only be able to input a number ranging from [1
9] as the grid only has 9 tiles. The program will then ask the user
for another input that is within the range.
If the user would enter a number of a tile that is already
occupied, the program would also keep asking the user for another
input that is not yet occupied by any symbol.

8.) References:
Youtube.com -> Quasar Distant (Tutorial on Arrays):
- https://www.youtube.com/watch?v=BfHcogmKM20
- https://www.youtube.com/watch?v=Vb8kuvxc4NE
- https://www.youtube.com/watch?v=NxjLeh6Y2Q0
Stackoverflow.com
https://www.quora.com/Is-there-a-way-to-never-lose-at-Tic-Tac-Toe
http://www.asciitable.com
https://en.wikipedia.org/wiki/Tic-tac-toe

You might also like