ID:1732137
 
So, I've been tasked with creating an "advanced" C program. But, the class isn't all that advanced so I can do something simple. My choice was to create a Tic-Tac-Toe game which will have an AI if I have enough time to do so. I mean, it's due tomorrow. Haha.

I'm just about done. Only issue is simplifying this, if possible, to make it look pretty for my professor's eyes.

Note: I've only briefly tested it. So, if you see something that may not work, then let me know.

BOARD_SQ_SIZE is a constant that is set to 3 since the board size is 3x3.

board is set to [3][3]. A two dimensional array.

playerTurn switches between X and O.

Return 1 if 3 blocks (equivalent to BOARD_SQ_SIZE) are found to be equal to playerTurn, indicating that that player won.
int CheckBoard(char board[][BOARD_SQ_SIZE], char (*playerTurn))
{
int rowCheck, //What row are we currently checking?
colCheck, //What column are we currently checking?
horChecks = 0, //How many blocks have we checked vertically or horizontally
vertChecks = 0,
diagRightChecks = 0, //How many blocks we have checked diagonally going to the right or to the left
diagRightVal = 0, //The starting point for the diagonal right value. We want to check the top left going to the bottom right for this one. So, start at 0 as an x value.
diagLeftChecks = 0,
diagLeftVal = BOARD_SQ_SIZE - 1; //The starting point for the diagonal right value. We want to check the bottom left going to the top right for this one. So, start at 2 as an x value.

for(rowCheck = 0; rowCheck < BOARD_SQ_SIZE; rowCheck++)
{
for(colCheck = 0; colCheck < BOARD_SQ_SIZE; colCheck++)
{
if(board[rowCheck][colCheck] == *playerTurn)
{
if(vertChecks + 1 < BOARD_SQ_SIZE)
{
vertChecks++;
}
else
{
return 1;
}

if(rowCheck == diagRightVal)
{
if(diagRightChecks + 1 < BOARD_SQ_SIZE)
{
diagRightVal++;
diagRightChecks++;
}
else
{
return 1;
}
}

if(rowCheck == diagLeftVal)
{
if(diagLeftChecks + 1 < BOARD_SQ_SIZE)
{
diagLeftVal++;
diagLeftChecks++;
}
else
{
return 1;
}
}
}

if(board[colCheck][rowCheck] == *playerTurn)
{
if(horChecks + 1 < BOARD_SQ_SIZE)
{
horChecks++;
}
else
{
return 1;
}
}
}
vertChecks = 0; //Reset because the column was checked and failed.
horChecks = 0; //Same
}

return 0;
}


Logical Error:
I don't think you need nested loops for this, you could use a bool value for that if you have in there as well.

When calling the function, pass the character you want to check (in this example 'x')

int check= 0 ;
for(int i = 0; i < size ; i++){
if(board[0][i] == 'x') check ++;
else break ;
}
if(rowcheck == size) // win condition

check = 0 ;
for(int i = 0; i < size ; i++){
if(board[i][0] == 'x') check++
else break ;
}
if(colcheck == size) // win condition

// and when doing the diagonals
for(int i = 0; i < size ; i++){
if(board[i][i] == 'x') check++
}

// you know the rest.
for(int i = 0 ; i < size ; i++){
if(board[size - i][i] == 'x') check ++
}
In response to Lugia319
[0][i] only checks Column 1, row 1 - 3. What is a good way of going about this "without" nested loops then?
Awww shoot, you're right. You might need a nested loop after all unless you wanna be like me and use goto.
In response to Lugia319
Lugia319 wrote:
Awww shoot, you're right. You might need a nested loop after all unless you wanna be like me and use goto.

Yeah, already finished it. I used a loop on the horizontal and vertical checks. This is very much neater though. Thanks. I was sleepy and couldn't think lol.

Also, I don't think there is goto in C, is there?
There is. It's not much different from DM

LoopBack: // note the colon
while(stuff){
// do stuff
}
goto LoopBack ;


goto is still ill-advised usage.
In response to Lugia319
Lugia319 wrote:
There is. It's not much different from DM

> LoopBack: // note the colon
> while(stuff){
> // do stuff
> }
> goto LoopBack ;
>


Nice. That is interesting. Very interesting.
Just several months ago, it so happens that I also have been experimenting with Tic-Tac-Toe for AI development. However, it isn't for any exercise or class. Instead, it is to experiment with very advanced AI (which I have yet to implement any AI yet).

The one I'm doing is being programmed in C++ of course.
In response to Bandock
Bandock wrote:
Just several months ago, it so happens that I also have been experimenting with Tic-Tac-Toe for AI development. However, it isn't for any exercise or class. Instead, it is to experiment with very advanced AI (which I have yet to implement any AI yet).

The one I'm doing is being programmed in C++ of course.

Ss4toby was telling me to add AI to my project. Apparently he did this in BYOND. I'll ask him about how it's done (logically) when I get home from my last 2.. hour... class. :/
In response to Xirre
Xirre wrote:
Bandock wrote:
Just several months ago, it so happens that I also have been experimenting with Tic-Tac-Toe for AI development. However, it isn't for any exercise or class. Instead, it is to experiment with very advanced AI (which I have yet to implement any AI yet).

The one I'm doing is being programmed in C++ of course.

Ss4toby was telling me to add AI to my project. Apparently he did this in BYOND. I'll ask him about how it's done (logically) when I get home from my last 2.. hour... class. :/

Ah, alright.
In response to Xirre
Xirre wrote:
Also, I don't think there is goto in C, is there?

More of an informational note, but 'goto' is historically one of the better ways to do exception handling" in C, so it can be somewhat important.