1、所用函数声明部分(在头文件中)
//game.h
#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define ROW 3
#define COL 3
enum OPTION
{
EXIT,
PLAY
};
#endif //__GAME_H__
void Initboard(char board[ROW][COL], int row, int col);
void Displayboard(char board[ROW][COL],int row, int col);
void PlayerMove(char board[ROW][COL], int row, int col);
void ComputerMove(char board[ROW][COL], int row, int col);
char CheckWin(char board[ROW][COL], int row, int col);
2、实现部分
//game.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void Initboard(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
void Displayboard(char board[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 ",board[i][j]);
if (j < col - 1)
printf("|");
}
printf("\n");
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---", board[i][j]);
if (j < col - 1)
printf("|");
}
}printf("\n");
}
}
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家走:");
while (1)
{
printf("请输入坐标:");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x-1][y-1] == ' ')
{
board[x-1][y-1] = 'X';
break;
}
else
{
printf("坐标被占用\n");
}
}
else
{
printf("坐标非法请重新输入");
}
}
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑走:\n");
while (1)
{
x = rand() % row;
y = ranf() % col;
if (board[x][y] == ' ')
{
board[x][y] = '0';
break;
}
}
}
static int isfull(char board[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 (board[i][j] == ' ')
return 0;
}
}
return 1;
}
char CheckWin(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
{
return board[i][0];
}
}
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
{
return board[0][i];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
{
return board[0][0];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
{
return board[0][2];
}
if (isfull(board, row, col))
{
return 'Q';
}
return ' ';
}
3、测试部分
//test.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void menu()
{
printf("******欢迎进去三子棋系统******\n");
printf("******************************\n");
printf("********1.play 0.exit*********\n");
printf("******************************\n");
printf("******************************\n");
}
void game()
{
char win = 0;
char board[ROW][COL] = {0};
Initboard(board,ROW,COL);
Displayboard(board, ROW, COL);
while (1)
{
PlayerMove(board, ROW, COL);
Displayboard(board, ROW, COL);
win=CheckWin(board, ROW, COL);
if (win != ' ')
break;
ComputerMove(board, ROW, COL);
Displayboard(board, ROW, COL);
win = CheckWin(board, ROW, COL);
if (win != ' ')
break;
}
if (win == 'X')
{
peintf("玩家赢\n");
}
else if (win == '0')
{
peintf("电脑赢\n");
}
else
{
printf("平局\n");
}
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case PLAY:
game();
break;
case EXIT:
printf("退出游戏\n");
break;
default:
printf("选择错误请重新选择\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}