#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3
#define COL 3
//菜单
void meau();
//游戏
void game();
//初始化数组
void InitBoard(char pch[ROW][COL], int row, int col);
//打印三子棋棋盘
void PrintChess(char pch[ROW][COL], int row, int col);
//玩家下棋
void PlayerChess(char pch[ROW][COL], int row, int col);
//电脑下棋
void ComputerChess(char pch[ROW][COL], int row, int col);
//判决胜负
//'#'电脑赢,'*'玩家赢,'Q'平局,'C'继续游戏
char IsWin(char pch[ROW][COL], int row, int col);
//判断二维数组是否满
int IsFull(char pch[ROW][COL], int row, int col);
函数实现文件 game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void meau()
{
printf("***********************************************\n");
printf("************* 欢迎来到三子棋游戏!*************\n");
printf("***********************************************\n");
printf("************* 1 进入游戏 **************\n");
printf("************* 0 退出游戏 **************\n");
printf("***********************************************\n");
}
void game()
{
char board[ROW][COL];//定义字符二维数组
InitBoard(board, ROW, COL);//初始化数组,将里面填满空格
PrintChess(board, ROW, COL);//打印棋盘
//下棋
char ret = 0;
while (1)
{
PlayerChess(board, ROW, COL);
PrintChess(board, ROW, COL);//打印棋盘
//判决胜负
ret = IsWin(board, ROW, COL);
if (ret != 'C')
break;
ComputerChess(board, ROW, COL);
PrintChess(board, ROW, COL);//打印棋盘
ret = IsWin(board, ROW, COL);
if (ret != 'C')
break;
}
if (ret == '*')
printf("玩家获胜!\n");
else if (ret == '#')
printf("电脑获胜!\n");
else
printf("平局!\n");
}
//初始化数组
void InitBoard(char pch[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
pch[i][j] = ' ';
}
}
}
//打印三子棋棋盘
void PrintChess(char pch[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 ",pch[i][j]);
if (j < col - 1)
printf("|");
}
printf("\n");
for (j = 0; j < col; j++)
{
if (i < row - 1)
{
printf("---");
if (j < col - 1)
printf("|");
}
}
printf("\n");
}
}
//玩家下棋
void PlayerChess(char pch[ROW][COL], int row, int col)
{
printf("玩家下棋>\n");
int x = 0;
int y = 0;
while (1)
{
printf("请输入目的坐标:>");
scanf("%d%d", &x, &y);
if ((x >= 1 && x <= row) && (y >= 1 && y <= col))
{
if (pch[x - 1][y - 1] == ' ')
{
pch[x - 1][y - 1] = '*';
break;
}
else
printf("棋盘目的坐标位置被占用,请重新输入坐标。\n");
}
else
{
printf("坐标越界,请重新输入!\n");
}
}
}
//电脑下棋
void ComputerChess(char pch[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑下棋>\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (pch[x][y] == ' ')
{
pch[x][y] = '#';
break;
}
}
}
//判决胜负
//'#'电脑赢,'*'玩家赢,'Q'平局,'C'继续游戏
char IsWin(char pch[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)//行
{
if ((pch[i][0] == pch[i][1] && pch[i][1] == pch[i][2]) && pch[i][1] != ' ')
return pch[i][1];
}
for (i = 0; i < col; i++)//列
{
if ((pch[0][i] == pch[1][i] && pch[1][i] == pch[2][i]) && pch[1][i] != ' ')
return pch[1][i];
}
if ((pch[0][0] == pch[1][1] && pch[1][1] == pch[2][2]) && pch[1][1] != ' ')//对角线
return pch[1][1];
else if ((pch[0][2] == pch[1][1] && pch[1][1] == pch[2][0]) && pch[1][1] != ' ')
return pch[1][1];
else if (IsFull(pch, row, col))//判断后如果没有分出胜负如果满了则平局
return 'Q';
else
return 'C';
}
//判断二维数组是否满
int IsFull(char pch[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
if (pch[i][j] == ' ')
return 0;
}
}
return 1;
}
测试文件 test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
meau();//菜单
printf("请根据菜单输入:>");
scanf("%d", &input);
switch (input)
{
case 0:
printf("退出游戏!\n");
break;
case 1:
printf("进入游戏三子棋!\n");
game();
break;
default:
printf("输入不合法,请重新输入!\n");
break;
}
}while (input);
}