整个游戏可以分为以下几个环节
1.打印一个游戏菜单
2.玩游戏
(1)玩家走一步
(2)电脑走一步
每走一步对结果进行显示,其中游戏的结果为玩家赢,电脑赢,以及平局
代码显示如下:
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("*******************************\n");
printf("******** 0.退出 *********\n");
printf("******** 1.玩游戏 *********\n");
}
void game()
{
char board[ROWS][COLS] = { 0 };
char ret = 0;
init_board(board, ROWS, COLS);
display_board(board, ROWS, COLS);
while (1)
{
player_move(board, ROWS, COLS);
ret = IsWin(board, ROWS, COLS);
if (ret != ' ')
break;
display_board(board, ROWS, COLS);
computer_move(board, ROWS, COLS);
ret = IsWin(board, ROWS, COLS);
if (ret != ' ')
break;
display_board(board, ROWS, COLS);
}
if (ret=='X')
printf("玩家赢\n");
else if (ret=='0')
printf("电脑赢\n");
else if (ret == "Q")
printf("平局\n");
display_board(board, ROWS, COLS);
}
int main()
{
int input = 0;
srand((int)time(NULL));
do
{
menu();
printf("请选择>\n");
scanf("%d", &input);
switch (input)
{
case 0:
break;
case 1:
game();
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void display_board(char board[ROWS][COLS], 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("---");
if (j < col - 1)
printf("|");
}
printf("\n");
}
}
}
void init_board(char board[ROWS][COLS], int row, int col)
{
memset(&board[0][0], ' ', col*row*sizeof(board[0][0]));
}
void player_move(char board[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家走:>\n");
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("输入坐标非法,请重新输入\n");
}
}
}
void computer_move(char board[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑走:>\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y]==' ')
{
board[x][y] = '0';
break;
}
}
}
char IsWin(char board[ROWS][COLS], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][2] == board[i][2] && board[i][1] != ' ')
return board[i][1];
}
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
return board[1][i];
}
if (board[0][0] == board[1][1] && board[2][2] == board[1][1] && board[1][1] != ' ')
return board[1][1];
if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ')
return board[1][1];
if(IsFull(board, row, col) == 1)
{
return 'Q';
}
return ' ';
}
char IsFull(char board[ROWS][COLS], 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;
}
}
game.h
#define _CRT_SECURE_NO_WARNINGS 1
#ifndef _GAME_H_
#define _GAME_H_
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define ROWS 3
#define COLS 3
void menu();
void game();
void init_board(char board[ROWS][COLS], int row, int col);
void display_board(char board[ROWS][COLS], int row, int col);
void player_move(char board[ROWS][COLS], int row, int col);
void computer_move(char board[ROWS][COLS], int row, int col);
char IsWin(char board[ROWS][COLS], int row, int col);
char IsFull(char board[ROWS][COLS], int row, int col);
#endif//_GAME_H_