game of three chess

Step 1: Create the header file game.h

#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#define Row 3
#define Col 3

void ChessBoard(char arr[Row][Col], int row, int col);
void DisplayBoard(char arr[Row][Col], int row, int col);
void PlayerMove(char arr[Row][Col], int row, int col);
void ComputerMove(char arr[Row][Col], int row, int col);
char CheckWin(char arr[Row][Col], int row, int col);
int IsFull(char arr[Row][Col], int row, int col);
#endif//__GAME_H__

 

Step 2: Write game.c, including callable functions.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "game.h"
#include <string.h>

void ChessBoard(char arr[Row][Col], int row, int col)
{
memset(arr, ' ', row*col*sizeof(arr[0][0]));
}

void DisplayBoard(char arr[Row][Col], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf(" %c ",arr[i][j]);
if (j != (col - 1))
{
printf("|");
}
}
printf("\n");
if (i != (row - 1))
{
for (j = 0; j < col; j++)
{
printf("___", arr[i][j]);
if (j != (col - 1))
{
printf("|");
}
}
printf("\n");
}
else
{

for (j = 0; j < col; j++)
{
printf(" ");
if (j != (col - 1))
{
printf("|");
}
}
printf("\n");
}
}
}

void PlayerMove(char arr[Row][Col], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("Player moves:\n");
printf("Please input Coordinates: \n");
scanf("%d%d", &x, &y);
if ((x >= 1) && (x <= row) && (y >= 1) && (y <= col) )
{
if (arr[x - 1][y - 1] == ' ')
{
arr[x - 1][y - 1] = 'X';
break;
}
else
{
printf("Coordinates are occupied\n ");
}
}
else
{
printf("Illegal coordinates\n");
}
}
}

void ComputerMove(char arr[Row][Col], int row, int col)
{
printf("电脑走\n");
while (1)
{
int x = rand() % row;
int y = rand() % col;
if (arr[x][y] == ' ')
{
arr[x][y] = '0';
break;
}
}
}

char CheckWin(char arr[Row][Col], int row, int col)
{
int i = 0;
int j = 0;
int flag = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col-1; j++)
{
if ((arr[i][j] == arr[i][j+1]) && (arr[i][0]!=' '))
{
flag = 1;
}
else
{
flag = 0;
break;
}

}
if (flag == 1)
{
return arr[i][0];
}
}
for (j = 0; j < col; j++)
{
for (i = 0; i < row-1; i++)
{
if ((arr[i][j]==arr[i+1][j]) && (arr[0][j]!=' '))
{
flag = 1;
}
else
{
flag = 0;
break;
}

}
if (flag == 1)
{
return arr[0][j];
}
}
for (i = 0; i < row-1; i++)
{
if ((arr[i][i]== arr[i+1][i+1]) && (arr[0][0]!=' '))
{
flag = 1;
}
else
{
flag = 0;
break;
}
}
if (flag == 1)
{
return arr[0][0];
}
for (i = 0; i < row-1; i++)
{
if ((arr[i][row-i-1]==arr[i+1][row-i-2]) && (arr[0][row-1]!=' '))
{
flag = 1;
}
else
{
flag = 0;
break;
}
}
if (flag == 1)
{
return arr[0][row-1];
}
if (IsFull(arr, row, col) == 1)
{
return 'q';
}
return ' ';
}

int IsFull(char arr[Row][Col], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (arr[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}

 

Step 3: Write the test function main.c

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "game.h"

void menu()
{
printf("********************\n");
printf("***** 1.Play *****\n");
printf("***** 0.Exit *****\n");
printf("********************\n");
}

void game()
{
char arr[Row][Col] = {0};
char ret = 0;
ChessBoard(arr, Row, Col);//Initialize the board
DisplayBoard(arr, Row, Col);//On the screen Print the board
do
{
PlayerMove(arr, Row, Col);//Player moves
ret = CheckWin(arr, Row, Col);//Judgment win or lose
DisplayBoard(arr, Row, Col);//Print the board
if (ret != ' ')
{
break;
}
ComputerMove(arr, Row, Col);//Computer move
ret = CheckWin(arr, Row, Col);
DisplayBoard(arr, Row, Col);//Print board
} while (ret == ' ');
if (ret == 'X')
{
printf("Player wins\n");
}
if (ret == '0')
{
printf("Computer wins\n");
}
if (ret = = 'q')
{
printf("Tie\n");
}
}
void test()
{
int input = 0;
do
{
menu();
printf("Please choose:\n");
scanf("%d", &input);
switch ( input)
{
case 1:
game();
break;
case 0:
printf("Exit the game:\n");
break;
default:
printf("Wrong choice, please choose again:\n");
break;
}
} while (input);
}
int main()
{
srand((unsigned int)time(NULL));//Generate random number
test();
system("pause");
return 0;
}

 

The results are as follows:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325095099&siteId=291194637