## Orusaka

Okay, so I'm completly new to programming, and I've been using Sams Teach Yourself C++ in one hour a day over the last two days, admittingly taking in more than a days worth per day, though.

This morning I decided I was bored of reading theory, and decided to get my hands dirty actually writing a small program, which I figure I can expand on and fix to implement new features as I advance. Long story short, I was inspired by the books idea that one could use a multi-dimentional array to, for instance, store a chess-board. So, I decided to create a game of tic-tac-toe versus the computer.

Now, the problem I am having, is in the function I'm writing to handle the computer's moves. I can't seem to get my if statements to compare the array for a specific value, and I would be very thankful if anyone could explain how I might go about doing this a different way.

What I'm trying to do is this:

<code>
if (board[3][3] == {0,0,0,0,0,0,0,0,0})
</code>

This won't compile, however. It seems if-statements might be unable to compare an array to its own value in this way. The only other way I can of doing this would be to use:

<code>
if (board[0][0] == 0 && board[0][1] == 0 && </code etc...

This would be both time and space-consuming given the amount of options, and I assume there must be some sort of way of doing it like I tried to initially.

## VernonDozier 2,218

Close on the code tags. Here's how to do it:

[code]
paste code here
[/code]

You need a loop to check each element of the array individually.

``````#include <iostream>
using namespace std;

int main()
{
int board[3][3] = {0,0,0,0,0,0,0,0,0};

// check if board is all zeroes
bool allZeroes = true;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (board[i][j] != 0)
allZeroes = false;
}
}

if (allZeroes)
cout << "Board is all zeroes\n";
else
cout << "Board is not all zeroes\n";

cout << "Press any key to exit program\n";
cin.get ();
return 0;
}
``````

## Orusaka

Well, okay, I can see what you're doing there, but it wouldn't really be the way to go considering the way I've set up the program, admittingly not in the most proficient way. Remeber, I know next to nothing. But let's have a look at the entire source code, so I can try to explain why that would make things extremly convoluted and only solve one very small problem.

``````#include <iostream>

using namespace std;

void drawBoard();
void playerMove();
void computerMove();

unsigned short int board [3] [3] = {0};

int main()
{

drawBoard();

playerMove();

drawBoard();

computerMove();

drawBoard();

system("PAUSE");
return 0;
}

void drawBoard()
{
cout << board[0][0] << "|" << board[0][1] << "|" << board[0][2] << endl;
cout << "-|-|-" << endl;
cout << board[1][0] << "|" << board[1][1] << "|" << board[1][2] << endl;
cout << "-|-|-" << endl;
cout << board[2][0] << "|" << board[2][1] << "|" << board[2][2] << endl;
}

void playerMove()
{
unsigned short int row;
unsigned short int column;

cout << "\nYour turn!\nRow from top: ";
cin >> row;
cout << "Column from left: ";
cin >> column;
cout << endl;

if (row == 1 && column == 1)
board[0][0] = 1;
else if (row == 1 && column == 2)
board[0][1] = 1;
else if (row == 1 && column == 3)
board[0][2] = 1;
else if (row == 2 && column == 1)
board[1][0] = 1;
else if (row == 2 && column == 2)
board[1][1] = 1;
else if (row == 2 && column == 3)
board[1][2] = 1;
else if (row == 3 && column == 1)
board[2][0] = 1;
else if (row == 3 && column == 2)
board[2][1] = 1;
else if (row == 3 && column == 3)
board[2][2] = 1;
else if (row > 3 || column > 3)
{
cout << "No more than three rows or colums, please.\nTry Again!\n";
playerMove();
}
else
cout << "Something fucked up in the program.\n";

}

void computerMove()
{
if (board[3][3] == {0,0,0,0,0,0,0,0,0})
{
cout << "\nAh, so I shall start?\nVery well. Best be conservative, I reckon.\n\n";
board[1][1] = 2;
}
else if (board[3][3] == {0,0,0,0,1,0,0,0,0})
{
cout << "You've gone the conservative route, I see.\nNevertheless, you won't beat me!";
board[2][0] = 2;
}
else
{
cout << "debug: assigning value upper right\n";
board[0][2] = 2;
}
}``````

Now, the reason the functions are void and the board array a global variable, is because, as I've learned the hard way, a function cannot return an array, not a multidimentional one, anyway. But to the point at hand, as you can see, using a loop in the way you illustrated would make thing extremly convoluted when the very next thing I need to do I check for a slighty different value, and then I'd need to do that quite a few more times. Several hundred, probably. At that rate, it might possibly be better to just use && for every value in the array in the if-statement. Or possibly hold off on the AI idea until I've learnt some more advanced techniques to handle this kind of thing.

Thank you very much for your kind help, though.

## VernonDozier 2,218

A function can't return an array, but it can return a pointer to the first element in an array. See this post, lines 36 - 48. Or google "return an array C++" or something similar.

My advice would be to tweak my code so you have a function that compares two arrays. That may fit your needs better.

``````#include <iostream>
using namespace std;

bool ArraysAreTheSame (int board1[3][3], int board2[3][3])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (board1[i][j] != board2[i][j])
return false;
}
}

return true;
}

int main()
{
int board1[3][3] = {0,0,0,0,0,0,0,0,0};
int board2[3][3] = {1,0,0,0,0,0,0,0,0};
int board3[3][3] = {0,1,0,0,0,0,0,0,0};
int board4[3][3] = {0,1,0,0,0,0,0,0,0};

if (ArraysAreTheSame (board1, board2))
cout << "Boards 1 and 2 are the same\n";
else
cout << "Boards 1 and 2 are not the same\n";

if (ArraysAreTheSame (board3, board4))
cout << "Boards 3 and 4 are the same\n";
else
cout << "Boards 3 and 4 are not the same\n";

cout << "Press any key to exit program\n";
cin.get ();
return 0;
}``````