In: Computer Science
The objective of this assignment is to implement the tic-tac-toe
game with a C program.
The game is played by two players on a board defined as a 5x5 grid
(array). Each board
position can contain one of two possible markers, either ‘X’ or
‘O’. The first player plays with
‘X’ while the second player plays with ‘O’. Players place their
markers in an empty position of
the board in turns. The objective is to place 5 consecutive markers
of the same type in a line (a
line can be any row, any column or any diagonal). The first player
who manages to place 5
markers in a line wins. The game is played until one of the players
wins or until the board is full
with no player having 5 markers in a line (i.e., the result of the
game is a draw).
For Taking Input and setting input position used alphabets as position(See Output Image)
Code: (Text File also included in End)
Output:
Files:
Code:
#include <stdio.h>
#include <stdbool.h> // Including Boolean
void fillGrid(char grid[5][5]){
// Filling Grid
int pos = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++)
{
grid[i][j] = 'A'
+ pos; // Adding Alphabets for Reference
pos += 1;
}
}
}
void printGrid(char grid[5][5]) { // Method for print grid
for (int i = 0; i < 5; i++) {
printf("\n");
for (int j = 0; j < 5; j++)
{
printf("%c%s",
grid[i][j], " ");
}
}
}
// Method for check winner
bool won(char symbol, char grid[5][5]) {
bool diagWin = true; // Storing Status of
Diagonal Win
bool reverseDiagWin = true; // Storing Status of
Reverse Diagonal Win
for (int i = 0; i < 5; i++) { // Loop For
check Win
bool rowWin = true;
// Storing Status of Row Win
bool columnWin = true;
// Storing Status of ColumnWin
for (int j = 0; j
< 5; j++) { // Checking if row or column are same
rowWin =
rowWin && (grid[i][j] == symbol);
columnWin =
columnWin && (grid[j][i] == symbol);
}
if (rowWin || columnWin)
{ // If Won Return True
return
true;
}
// Row and Column failed, Now testing Daigonal
diagWin = diagWin
&& (grid[i][i] == symbol);
reverseDiagWin =
reverseDiagWin && (grid[i][5 - i - 1] == symbol);
}
if (diagWin || reverseDiagWin) { // If Won
Return True
return true;
} else {
return false;
}
}
bool validInput(char grid[5][5], char input) { // Checking if
Input is Valid
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++)
{
if(input ==
grid[i][j]){
return true;
}
}
}
return false;
}
int main() {
char grid[5][5]; // Grid Array
fillGrid(grid); // Fill Grid With Alphabets
printGrid(grid); // Printing Grid
bool turnX = true; // Storing Turn(X or O)
int turn = 0; // Storing turn count for end game
while (true) { //
Infinite loop(play until win or game end)
turn +=
1;
char symbol; // Symbol for turn
if (turnX) {
symbol = 'X';
} else {
symbol = 'O';
}
// Taking input and storing
printf("\n%c%s", symbol, " Poisition[A to Y] -->");
char val;
while(true){ // Infinity loop for correct input
scanf("%c", &val); // Taking input
if(val == '\n'){ // Empty Line or Enter
pressed
continue; // continue
}else if(validInput(grid, val)){ // Input
Valid
break; // Break loop
}else{ // Invalid Invalid
printf("%s\n", "Wrong Input,
Try Again!");
}
}
int location = val - 'A'; // Converting input into int
int row = location / 5; // Calcualting Row
int col = location % 5; // Calcualting Column
grid[row][col] = symbol; // Set Value
printGrid(grid); // Print Grid
if (won(symbol, grid)) { // Checking If Player Won
printf("\n%s%c\n", "Winner: ", symbol);
break;
}
if (turn == (5 * 5)) { // Checking if Game Over
printf("\n%s\n", "Winner: None");
break;
}
turnX = !turnX; // Changing Turm
}
return 0;
}