Code:
#include <stdio.h>#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define ROW 6
#define COL 6
int makeMove(char *theBoard , int player, const char *PIECES, int turn, char *savedGameBoard);
void printBoard(char theBoard [ROW][COL]);
int horizontalCheck(char *board);
int verticalCheck(char *board);
int diagonalCheck(char *board);
int checkFour(char *board, int a, int b, int c, int d);
int checkWin(char *board);
int main(void)
{
// Message to start the game
printf("welcome!\n\n");
char gameBoard[ROW][COL];
char savedGameBoard[100][ROW][COL];
// Reset the board before we start the game
memset(gameBoard, ' ', ROW * COL);
const char *PIECES = "XO";
int turn;
int endGame = 0;
for(turn = 0; turn < ROW * COL && !endGame; turn++)
{
strcpy( savedGameBoard[turn], gameBoard);
printBoard(gameBoard);
while(!makeMove(gameBoard, turn % 2, PIECES, turn, savedGameBoard))
{
printBoard(gameBoard);
printf("Invalid input, this column is full. Please choose another one:\n");
}
endGame = checkWin(gameBoard);
}
printBoard(gameBoard);
if(turn == ROW * COL && !endGame)
{
printf("It's a tie!");
}
else
{
turn--;
printf("Player %d (%c) wins!\n", turn % 2 + 1, PIECES[turn % 2]);
}
return 0;
}
int makeMove(char *theBoard , int player, const char *PIECES, int turn, char *savedGameBoard)
{
int row, col = 0;
bool flag=false;
printf("Player %d (%c):\nEnter number coordinate: (0 to undo move) ", player + 1, PIECES[player]);
while(1)
{
// Check if it's an integer
if(1 != scanf("%d", &col))
{
printf("\nInvalid input, bye-bye!\n");
exit(0);
}
// Check if entered 0
if(col==0)
flag=true;
// Check if it's in bound
if(col < 1 || col > 7 )
{
while(getchar() != '\n');
printBoard(theBoard);
printf("Invalid input, the number must be between 1 to 7:\n");
}
else
{
break;
}
}
// If entered 0, and it's not the first turn bring back the previous board
if(flag)
{
if(turn==0)
{
printf("You can't undo the move.");
}
else
{
strcpy(theBoard, savedGameBoard[turn-1]);
printf("You undo the last move, \n");
printBoard(theBoard);
return 1;
}
}
// Enter the new char to the board
col--;
for(row = ROW - 1; row >= 0; row--)
{
if(theBoard[COL * row + col] == ' ')
{
theBoard[COL * row + col] = PIECES[player];
return 1;
}
}
return 0;
}
// This function print the board currently status
void printBoard(char theBoard [ROW][COL])
{
int countCol[7] = { 1, 2, 3, 4, 5, 6, 7 };
int i,j,t;
printf("\n\nThe board status is: \n");
for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
{
printf("| %c |", theBoard[i][j]);
}
printf("\n");
}
printf(" 1 2 3 4 5 6 7\n");
}
int horizontalCheck(char *board){
int row, col, idx;
const int WIDTH = 1;
for(row = 0; row < ROW; row++){
for(col = 0; col < COL - 3; col++){
idx = COL * row + col;
if(checkFour(board, idx, idx + WIDTH, idx + WIDTH * 2, idx + WIDTH * 3)){
return 1;
}
}
}
return 0;
}
int verticalCheck(char *board){
int row, col, idx;
const int HEIGHT = 7;
for(row = 0; row < ROW - 3; row++){
for(col = 0; col < COL; col++){
idx = COL * row + col;
if(checkFour(board, idx, idx + HEIGHT, idx + HEIGHT * 2, idx + HEIGHT * 3)){
return 1;
}
}
}
return 0;
}
int diagonalCheck(char *board){
int row, col, idx, count = 0;
const int DIAG_RGT = 6, DIAG_LFT = 8;
for(row = 0; row < ROW - 3; row++){
for(col = 0; col < COL; col++){
idx = COL * row + col;
if(count <= 3 && checkFour(board, idx, idx + DIAG_LFT, idx + DIAG_LFT * 2, idx + DIAG_LFT * 3) || count >= 3 && checkFour(board, idx, idx + DIAG_RGT, idx + DIAG_RGT * 2, idx + DIAG_RGT * 3)){
return 1;
}
count++;
}
count = 0;
}
return 0;
}
int checkFour(char *board, int a, int b, int c, int d){
return (board[a] == board[b] && board[b] == board[c] && board[c] == board[d] && board[a] != ' ');
}
int checkWin(char *board){
return (horizontalCheck(board) || verticalCheck(board) || diagonalCheck(board));
}