分三个文件:
chess.h 头文件
chess.c 棋盘部分
test.c 测试部分
chess.h
//预处理如果没有定义则定义__0GAME_H__
#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#define ROWS 3 //宏替换行数
#define COLS 3 //宏替换列数
void init_board(char board[ROWS][COLS]); //初始化棋盘
void print_board(char board[ROWS][COLS]); //打印出棋盘
void player_move(char board[ROWS][COLS]); //玩家走
void computer_move(char board[ROWS][COLS]); //电脑走
int check_full(char board[ROWS][COLS]); //判断棋盘是否已满
char check_win(char board[ROWS][COLS]); //判断输赢
enum op
{
EXIT,
PLAY
};
#endif
chess.c
#include"chess.h"
void init_board(char board[ROWS][COLS])
{
int i = 0;
int j = 0;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
board[i][j] = ' ';
}
}
}
void print_board(char board[ROWS][COLS])
{
int i = 0;
int j = 0;
for (i = 0; i < ROWS; i++)
{
printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
if (i != 2)
printf("---|---|---\n");
}
}
int check_full(char board[ROWS][COLS])
{
int i = 0;
int j = 0;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
if (board[i][j] == ' ')
return ' ';
}
}
return 'q';
}
void player_move(char board[ROWS][COLS])
{
int x = 0;
int y = 0;
while (1)
{
printf("玩家下->\n");
printf("请输入x和y的坐标:\n");
scanf_s("%d%d", &x, &y);
if (board[x-1][y-1] == ' ')
{
board[x-1][y-1] = 'X';
break;
}
}
}
void computer_move(char board[ROWS][COLS])
{
int x = 0;
int y = 0;
int ret = check_full(board);
printf("电脑下->:\n");
Sleep(1000);
if (ret == ' ')
{
while (1)
{
x = rand() % 3;
y = rand() % 3;
if (board[x][y] == ' ')
{
board[x][y] = '0';
break;
}
}
}
}
char check_win(char board[ROWS][COLS]) //判断输赢
{
int i = 0;
char ret = 0;
for (i = 0; i < ROWS; i++)
{
if ((board[0][i] == board[1][i]) &&
(board[0][i] == board[2][i]) &&
((ret = board[0][i]) != ' '))
return ret;
}
for (i = 0; i < ROWS; i++)
{
if ((board[i][0] == board[i][1]) &&
(board[i][0] == board[i][2]) &&
((ret = board[i][0]) != ' '))
return ret;
}
for (i = 0; i < ROWS; i++)
{
if ((board[0][0] == board[1][1]) &&
(board[0][0] == board[2][2]) &&
((ret = board[0][0]) != ' '))
return ret;
}
for (i = 0; i < ROWS; i++)
{
if ((board[0][2] == board[1][1]) &&
(board[0][2] == board[2][0]) &&
((ret = board[0][2]) != ' '))
return ret;
}
return ' ';
}
test.c
#include"chess.h"
void menu()
{
printf("***********************\n");
printf("******* 1.play *******\n");
printf("******* 0.exit *******\n");
printf("***********************\n");
}
void play_game()
{
char board[ROWS][COLS];
int ret = 0;
srand((unsigned)time(NULL));
init_board(board);
print_board(board);
do
{
player_move(board);
print_board(board);
if ((check_win(board)) != ' ')
{
ret = check_win(board);
break;
}
if (check_full(board) == 'q')
{
ret = check_full(board);
break;
}
computer_move(board);
print_board(board);
if ((check_win(board)) != ' ')
{
ret = check_win(board);
break;
}
} while (check_full(board) != 'q');
if (ret == 'X')
printf("恭喜你,你赢了!\n");
if (ret == '0')
printf("哈哈,你真聪明,电脑赢了!\n");
if (ret == 'q')
printf("平局!\n");
}
void test()
{
int input = 0;
do
{
menu();
scanf_s("%d", &input);
switch (input)
{
case EXIT:
break;
case PLAY:
play_game();
}
} while (input);
}
int main()
{
test();
system("pause");
}
实现图片: