三子棋
- 三个文件(game.h;game.c;test.c)
game.h
#define ROW 3
#define COL 3
#include <stdio.h>
//头文件,才能使用time和srand
#include<stdlib.h>
#include <time.h>
//声明
void ClearBoard(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);
int IsFull(char board[ROW][COL], int row, int col);
// 1.玩家赢*
// 2.电脑赢0
// 3.平局Q
// 3.继续C
char IsWin(char board[ROW][COL], int row, int col);
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void ClearBoard(char board[ROW][COL], int row, int col)
{
int i, j;
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, j;
for (i = 0; i < row; i++)
{
//打印数据
//printf(" %c | %c | %c \n")
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if(j<(col-1))
{
printf("|");
}
}
printf("\n");
//打印棋盘
//printf("---|---|---|\n");
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
}
printf("\n");
}
}
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x, y;
printf("请玩家落子:\n");
while (1)
{
printf("请输入坐标:\n");
printf("---行---列---\n");
scanf("%d%d", &x, &y);
//判断坐标合法性
if (x - 1 >= 0 && x - 1 < row && y - 1 >= 0 && y - 1 < col)
{
//如果该坐标没有元素,则落子;如果改坐标有元素,则重新落子
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("该位置已经有子,请重新输入!\n ");
}
}
else
{
printf("该坐标错误,请重新输入!\n");
}
}
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x, y;
printf("电脑走\n");
while(1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '0';
break;
}
}
}
char IsWin(char board[ROW][COL], int row, int col)
{
int i;
//横三行
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[1][1];
}
if (1 == IsFull(board, ROW, COL))
{
return 'Q';
}
return 'C';
}
int IsFull(char board[ROW][COL], int row, int col)
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void game()
{
//创建棋盘
char board[ROW][COL] = { 0 };
//初始化棋盘,使里面全部为空格
ClearBoard(board, ROW, COL);
//展示棋盘
DisplayBoard(board, ROW, COL);
//下棋
char r;
while (1)
{
//玩家下棋
PlayerMove(board,ROW,COL);
DisplayBoard(board, ROW, COL);
//判断玩家是否赢
IsWin(board, ROW, COL);
r = IsWin(board, ROW, COL);
if (r != 'C')
{
break;
}
//电脑下棋
ComputerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
//判断电脑是否赢
r = IsWin(board, ROW, COL);
IsWin(board, ROW, COL);
if (r != 'C')
{
break;
}
}
if (r == '*')
{
printf("玩家赢!\n");
};
if (r == '0')
{
printf("电脑赢\n");
}
if (r == 'Q')
{
printf("平局\n");
}
}
void mean()
{
printf("*********************************\n");
printf("********* 1.PLAY 0 QUIT *********\n");
printf("*********************************\n");
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));//srand(随机数生成器)
do
{
mean();
printf("请选择>\n");
scanf("%d", &input);
switch (input)
{
case 1:
printf("三子棋\n");
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,请重新选择!\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
总结
- 头文件里(gema.h)可以放一些项目里经常需要的头文件(#include<stdio.h>)只需要引用game.h就能全部引用.
- rand()函数是个伪随机函数,需要添加srand((unsigned int)time(NULL));日后在学。
- 逻辑很重要,需要简单拆分。
- 三个文件
game.h 存放声明
test.c 引用函数
game.c 编写函数