I'm trying to build a program for school that plays tic tac toe. I DO NOT want to make it the standard 3x3. I was hoping to make a bigger grid (4x4 or 5x5) but with the same win conditions of three in a row.
I thought I would make a two dimensional array of text boxes that either mark X or O when clicked. I THINK that simply checking three in a row would be easy enough to see if there is a win BUT CAN ANYONE give me a clue how to do this for Diagonals?
My thoughts were to check an if statement that looks subtracts or adds one to each of the dimensions and IF there is a matching X or O then does it again by adding or subtracting again...then we should have a win.
HOWEVER, this becomes complicated and messy for those boxes starting on the edges.... ????

Nathan LanePosted Sep 10, 2008, 10:41 AM
Using that and zero-based arrays and array indexing, you can figure out where any x, y coordinate is now. Let's take a simple 3x3 array and transform it into a single-dimensional array: [ 0, 0, 0 ], [ 0, 0, 0 ], [ 0, 0, 0 ] == [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
Now let's say you want to find (x, y) == (0, 1) (first column, second row). Using F(x, y) = Array[ y * rowWidth + x ] we get F(0, 1) = Array[ 1 * 3 + 0 ] = Array[ 3 ] = [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ]. That was pretty simple. Now what about checking for three in a row?
Let's say we want to know when a player gets three in a row diagonally. What could that look like in our single-dimensional array? [ 1, 0, 0, 0, 1, 0, 0, 0, 1 ] and [ 0, 0, 1, 0, 1, 0, 1, 0, 0 ] are the two patterns, so now we know that when indexes 0, 4 and 8 are non-zero and when indexes 2, 4 and 6 are non-zero are the cases when we have three in a row diagonally. You might keep these patterns in an array or some place that you could use them to look for matches later during the game. Then you don't really have a need to check mathematically every time for three in a row, you can simply match every pattern.
So use pattern matching.
I hope that helps.
MANUEL ESCOBEDOPosted Dec 20, 2007, 12:05 AM
class TicTacToe
{
private:
char TicTacToe[3][3];
int auxCounter;
protected:
bool Ffull () {return auxCounter == 9;}
void CheckRow_0(char c, bool& win)
{
int counter = 0;
for (int i = 0; i < 3; i++)
if (TicTacToe[0][i] == c)
counter++;
if (counter == 3)
win = true;
}
void checkRow_1(char c, bool& win)
{
int counter = 0;
for (int i = 0; i < 3; i++)
if (TicTacToe[1][i] == c)
counter++;
if (counter == 3)
win = true;
}
void checkRow_2(char c, bool& win)
{
int counter = 0;
for (int i = 0; i < 3; i++)
if (TicTacToe[2][i] == c)
counter++;
if (counter == 3)
win = true;
}
void checkColumn_0(char c, bool& win)
{
int counter = 0;
for (int i = 0; i < 3; i++)
if (TicTacToe[i][0] == c)
counter++;
if (counter == 3)
win = true;
}
void checkColumn_1(char c, bool& win)
{
int counter = 0;
for (int i = 0; i < 3; i++)
if (TicTacToe[i][1] == c)
counter++;
if (counter == 3)
win = true;
}
void checkColumn_2 (char c, bool& win)
{
int counter = 0;
for (int i = 0; i < 3; i++)
if (TicTacToe[i][2] == c)
counter++;
if (counter == 3)
win = true;
}
void checkSlice (char c, bool& win)
{
int counter = 0;
for (int i = 0; i < 3; i++)
if (TicTacToe[i][i] == c)
counter++;
if (counter == 3)
win = true;
}
void checkSlice2 (char c, bool& win)
{
int counter = 0;
for (int j = 2; j > 0; j--)
if (TicTacToe[j][j] == c)
counter++;
if (counter == 3)
win = true;
}
public:
TicTacToe ()
{
auxCounter = 0;
}
void checkColumns(char c, bool& win)
{
checkColumn_0(c, win);
checkColumn_1(c, win);
checkColumn_2(c, win);
}
void CheckRows (char c, bool& win)
{
CheckRow_0(c, win);
checkRow_1(c, win);
checkRow_2(c, win);
}
void checkSlices (char c, bool& win)
{
checkSlice(c, win);
checkSlice2(c, win);
}
bool isChecked (int row, int col)
{
return TicTacToe[row][col] == 'O' || TicTacToe[row][col] == 'X';
}
void Set (int row, int col, char c)
{
TicTacToe[row][col] = c;
auxCounter++;
}
void Show ()
{
for (int i = 0; i < 3; i++)
{
cout << '\n';
for (int j = 0; j < 3; j++)
{ cout << TicTacToe[i][j] << ' '; }
}
cout << '\n';
}
__property bool full = {read = Ffull};
};