how to make a tic tac toe using the c language program using two dimensional array?
How to make tic tac toe using c language program?
#include %26lt;iostream.h%26gt;
#include %26lt;stdlib.h%26gt;
void drawBoard(int board[][3]);
int getScore(int board[][3], int*, int*);
int gameover(int board[][3], int);
int main(int argc, char *argv[])
{
enum player { computer = 3, human = 2, tie = 1 };
int board[3][3] = { {0,0,0},{0,0,0},{0,0,0} };
int game = 0, turn = 0;
while (1)
{
int choice, row, column, howMany, newScore;
int biggestScore[9][3];
for(int i = 0; i %26lt; 9; i++) // Set biggestScore[9][3] to all zeros.
for (int j = 0; j %26lt; 3; j++)
biggestScore[i][j] = 0;
if(turn == 9) // No more sqares left
if(gameover(board, tie)) break;
else { game++; turn = 0; continue; }
else if((game + turn) % 2 == 0)
{
drawBoard(board);
cout %26lt;%26lt; "Select a square (1-9): ";
cin %26gt;%26gt; choice;
if(choice %26gt;= 1 %26amp;%26amp; choice %26lt;= 9)
{
// Change user input to row/column for use in 2D board array.
row = (int) (choice - 1) / 3;
column = (choice - 1) % 3;
// Check user's input
if(board[row][column]) { cout %26lt;%26lt; "Space taken, select again.\n"; continue; }
else { board[row][column] = human; turn++; }
if(getScore(board, %26amp;row, %26amp;column) %26gt; 50)
if(gameover(board, human)) break;
else { game++; turn = 0; continue; }
}
}
else
{
for(int x = 0; x %26lt; 3; x++)
for(int y = 0; y %26lt; 3; y++)
if(!board[x][y]) // Square is empty
{
newScore = getScore(board, %26amp;x, %26amp;y);
if(newScore %26gt; biggestScore[0][0])
howMany = 0;
if(newScore %26gt;= biggestScore[0][0])
{
biggestScore[howMany][0] = newScore;
biggestScore[howMany][1] = x;
biggestScore[howMany++][2] = y;
}
}
int random = rand() % howMany;
row = biggestScore[random][1];
column = biggestScore[random][2];
if(biggestScore[0][0] %26gt; 50)
{
board[row][column] = computer;
if(gameover(board, computer)) break;
else { game++; turn = 0; continue; }
}
else
{
board[row][column] = computer;
turn++;
}
} // End of if(choice %26gt; 0 ...
} // End of while(!quit) { ...
return 0;
} // End of main() { ...
void drawBoard(int board[][3])
{
char piece;
// Add a 'system("clear")' or 'system("cls")' if you want a clean screen
// I left this out becuase of portability between windows/unix.
// You may also create a simple for() loop that prints newlines for the same effect.
for (int i = 2; i %26gt; -1; i--)
{
for (int j = 0; j %26lt; 3; j++)
{
if(!board[i][j]) piece = (char) (i * 3 + j) + 49;
else if(board[i][j] == 2) piece = 'X';
else piece = 'O';
cout %26lt;%26lt; "| " %26lt;%26lt; piece %26lt;%26lt; " ";
}
cout %26lt;%26lt; "|\n";
}
} // End of drawBoard() ...
int getScore(int board[][3],int *x,int *y)
{
int score;
// The scores that a square will be given if the proper conditions are met:
// Modify these to see the results but remember that a score of over 50
// assumes a win; so be careful, or change that number in above code.
// Also remember that when the computer is getting these scores, it is
// assuming that it has not placed the piece yet on that square.
int scores[9] = {/*No Pieces on line*/ 0,
/*Undefined*/ 0,
/*One human piece*/ 4,
/*One computer piece*/ 3,
/*Two human pices*/ 8,
/*One of each*/ 2,
/*Two computer OR three human*/ 50,
/*Two human and one computer */ 1,
/*Two computer and one human */ 1 };
// Get horizontal score:
score = scores[(board[*x][0] + board[*x][1] + board[*x][2])];
// Get vertical score:
score += scores[(board[0][*y] + board[1][*y] + board[2][*y])];
// Get diagonal score (if possible)
if(*x == *y) // Check for '/' diagonal
score += scores[(board[0][0] + board[1][1] + board[2][2])];
if(*x + *y == 2) // Check for '\' diagonal
score += scores[(board[2][0] + board[1][1] + board[0][2])];
return score;
} // End of getScore() ...
int gameover(int board[][3], int player)
{
char again;
char name[3][9] = { "Computer", "Nobody", "You" };
drawBoard(board);
cout %26lt;%26lt; name[player % 3] %26lt;%26lt; " Won\nPlay again? (Y/N) ";
cin %26gt;%26gt; again;
if(again == 'Y' || again == 'y')
{
//Reset board
for (int i = 0; i %26lt; 3; i++)
for (int j = 0; j %26lt; 3; j++)
board[i][j] = 0;
return 0;
}
else
return 1;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment