/*
** TttString is the object that contains the two dimensional
** Tic Tac Toe board in a a one dimentional form. The board
** is converted from:
** 1 2 3
** 4 5 6
** 7 8 9
** to
** 1 2 3 4 5 6 7 8 9
** and it is stored in an integer variable. The X is represented
** by a 1, the O by a 2, and an empty space by a 0.
*/
import java.io.*;
import java.lang.*;
class TttString
{
// the one dimensional board string.
public int tttString;
private int i;
final static int X = 1, O = 2, EMPTY = 0;
// locations of the x's and o's
private final static int LOCATIONS[] = {100000000, 10000000,
1000000, 100000, 10000,
1000, 100, 10, 1};
//===============================================================
// Constructor
//===============================================================
TttString()
{
// The tictactoe board is empty
ClearTttString();
}
//===============================================================
// Clear the content of the tic string
//===============================================================
public void ClearTttString()
{
tttString = 000000000;
}
//===============================================================
// Output the ticString to the screen
//===============================================================
public void OutTttString()
{
// convert the integer string to a character
// string
String sTttString = String.valueOf(tttString);
int stringLength;
// Add leading zeros to the string to make it look
// good for printing
stringLength = sTttString.length();
for (i = 0; i < 9 - stringLength; i++)
{
sTttString = "0" + sTttString;
}
System.out.println(sTttString);
}
//===============================================================
// Set the ticString with the location of the tictactoe
// move, and wether it's x = 1 or o = 2.
//===============================================================
public boolean SetTttString(int location, int xo)
{
// No locations beyond the board are allowed and
// No occupied locations can be overwritten.
if (location >= 1 && location <= 9 && GetXO(location) == EMPTY)
{
tttString = tttString + LOCATIONS[location - 1] * xo;
}
else
{
// No setting occured
return(false);
}
return(true);
}
//===============================================================
// Given the location tell me if there is an X or O or nothing
//===============================================================
public int GetXO(int location)
{
if (location >= 1 && location <=9)
{
return(tttString / LOCATIONS[location - 1] % 10);
}
else
{
System.out.println("Error: Attempt to address an incorrect location");
}
return(0);
}
}