头文件 game.h
#pragma once
#define X 3
#define Y 3
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//简易菜单
void menu();
//游戏主程序
void game();
//展示棋盘
void display(char chess[X][Y]);
//玩家下棋
void player(char chess[X][Y], int x, int y);
//电脑下棋
void computer(char chess[X][Y], int x, int y);
//判断赢家
//'*'玩家胜
//'#'电脑胜
//'c'继续下棋
//'d'平局
char who_win(char chess[X][Y], int x, int y);
//判断平局
//返回1则平局,0则未平局
int draw(char chess[X][Y], int x, int y);
game.c文件
实现游戏玩法
#include "game.h"
void menu()
{
printf("#######################\n");
printf("####### 1. Play #######\n");
printf("####### 0. Exit #######\n");
printf("#######################\n");
}
void game()
{
//初始化棋盘
char chess[X][Y] = { 0 };
for (int i = 0; i < X; i++)
{
for (int j = 0; j < Y; j++)
{
chess[i][j] = ' ';
}
}
//随机数初始化
srand((unsigned int)time(NULL));
int ret = 0;
while (1)
{
display(chess);
player(chess, X, Y);
ret = who_win(chess, X, Y);
if (ret != 'c')
break;
computer(chess, X, Y);
ret = who_win(chess, X, Y);
if (ret != 'c')
break;
}
display(chess);
if (ret == 'd')
printf("Draw!\n");
else if (ret == '*')
printf("You Win!\n");
else if (ret == '#')
printf("Computer Win!\n");
}
void display(char chess[X][Y])
{
for (int i = 0; i < X; i++)
{
for (int j = 0; j < Y; j++)
{
printf(" %c ", chess[i][j]);
if ( j < Y-1 )
{
printf("|");
}
}
printf("\n");
if (i < X-1 )
{
printf("---+---+---\n");
}
}
}
void player(char chess[X][Y], int x, int y)
{
printf("It's your turn now!\n");
while (1)
{
printf("Please Input row and col:> ");
scanf_s("%d %d", &x, &y);
if (x >= 1 && x <= 3 && y >= 1 && y <= 3)
{
if (chess[x - 1][y - 1] == ' ')
{
chess[x - 1][y - 1] = '*';
break;
}
else
printf("Input wrong.Try again!\n");
}
else
{
printf("Input wrong.Try again!\n");
}
}
}
void computer(char chess[X][Y], int x, int y)
{
printf("It's the computer's turn!\n");
while (1)
{
x = rand() % 3;
y = rand() % 3;
if (chess[x][y] == ' ')
{
chess[x][y] = '#';
break;
}
}
}
char who_win(char chess[X][Y], int x, int y)
{
for (int i = 0; i < X; i++)
{
if (chess[i][1] == chess[i][2] && chess[i][1] == chess[i][0] && chess[i][0] != ' ')
return chess[i][0];
}
for (int j = 0; j < Y; j++)
{
if (chess[1][j] == chess[2][j] && chess[1][j] == chess[0][j] && chess[0][j] != ' ')
return chess[0][j];
}
if ((chess[0][0] == chess[1][1] && chess[1][1] == chess[2][2] && chess[1][1] != ' ') || (chess[0][2] == chess[1][1] && chess[1][1] == chess[2][0] && chess[1][1] != ' '))
return chess[1][1];
else if (draw(chess, x, y))
return 'd';
else
return 'c';
}
int draw(char chess[X][Y], int x, int y)
{
for (int i = 0; i < X; i++)
{
for (int j = 0; j < Y; j++)
{
if (chess[i][j] == ' ')
return 0;
}
}
return 1;
}
main.c文件
完成程序主框架
#include "game.h"
int main()
{
int choose = 0;
do
{
menu();
printf("Please Choose:> ");
scanf_s("%d", &choose);
switch (choose)
{
case 0:
printf("Game Over!\n");
break;
case 1:
printf("Game Start!\n");
game();
break;
default:
printf("Wrong!Choose again!\n");
break;
}
} while (choose);
return 0;
}
看着别人写很简单,实际写时仍然漏洞百出。对函数的理解还是欠缺,不能快速确定哪一块的功能可以打包为一个函数,经常反复判断导致程序逻辑错误。