三子棋,小时候拿粉笔画着玩,现在拿C语言写着玩,来看一下怎么写吧!
首先创建一个“三子棋”的项目,这个项目中包括一个头文件game.h和两个源文件test.c、game.c。
- “game.h”:game.h头文件中包含game.c中定义的所有函数的声明和整个项目需要调用的头文件。如下:
#ifndef __GAME_H__
#define __GAME_H__
//需要引用的头文件
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#define ROW 3 //定义行数
#define COL 3 //定义列数
//所需函数
void InitBoard(char board[ROW][COL], int row, int col);//初始化棋盘
void DisplayBoard(char board[ROW][COL], int row, int col);//打印棋盘
void ComputerMov(char board[ROW][COL],int row,int col);//电脑走
void PlayMov(char board[ROW][COL], int row,int col);//玩家走
char Iswin(char board[ROW][COL], int row, int col);//判断输赢
int IsFull(char board[ROW][COL], int row, int col);//判断棋盘是否放满
#endif //__GAME_H__
- “test.c”:test.c源文件为测试程序,它里面包含了main函数、打印菜单menu函数、游戏game函数。
#include"game.h"
//打印菜单
void menu()
{
printf("**************************\n");
printf("***** 1.play *****\n");
printf("***** 0.exit *****\n");
printf("**************************\n");
}
//制定游戏规则
void game()
{
char board[ROW][COL] = {0};//先创建一个二维数组来表示棋盘
char ret = '0';
InitBoard(board, ROW, COL);
DisplayBoard(board, ROW, COL);
do
{
ComputerMov(board, ROW, COL);
DisplayBoard(board, ROW, COL);
if (Iswin(board, ROW, COL) != ' ')
{
break;
}
PlayMov(board, ROW, COL);
DisplayBoard(board, ROW, COL);
if (Iswin(board, ROW, COL) != ' ')
{
break;
}
ret = Iswin(board, ROW, COL);
} while (ret==' ');
//判断输赢及平局:Q->平局;*->电脑赢;#->玩家赢
if (Iswin(board, ROW, COL) == 'Q')
{
printf("平局,你们的实力旗鼓相当!\n");
}
if (Iswin(board, ROW, COL) == '*')
{
printf("电脑赢了!别灰心,再努力一下!\n");
}
if(Iswin(board, ROW, COL) == '#')
{
printf("恭喜你,你赢了!\n");
}
DisplayBoard(board, ROW, COL);//打印出最终棋盘
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));//生成一个随机数
do
{
menu();
printf("请选择->");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,请重新选择->\n");
break;
}
} while (input);
return 0;
}
- “game.c”:game.c源文件中包含game函数所实现的功能函数,如下:
初始化棋盘函数: InitBoard(char board[ROW][COL], int row, int col);
打印棋盘函数:DisplayBoard(char board[ROW][COL],int row,int col);
电脑走棋函数:ComputerMov(char board[ROW][COL],int row,int,col);
玩家走棋函数:PlayMov(char board[ROW][COL],int row,int col);
判断输赢函数:Iswin(char board[ROW][COL],int row,int col);
棋盘是否满函数:IsFull(char board[ROW][COL],int row,int col);
#include"game.h"
//初始化棋盘
void InitBoard(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
//void InitBoard(char board[ROW][COL], int row, int col)
//{
// memset(&board[0][0], ' ', row*col * sizeof(board[0][0]));//把特定的内存设置为字符
//}
//打印棋盘
void DisplayBoard(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
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 ComputerMov(char board[ROW][COL], int row, int col)
{
printf("电脑走\n");
int x = 0;
int y = 0;
while (1)
{
x= rand() % row;//随机数生成电脑坐标
y= rand() % col;
if (board[x][y] == ' ')//如果输入坐标为空
{
board[x][y] = '*';
break;
}
}
}
//玩家走棋
void PlayMov(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("玩家走\n");
printf("请输入坐标:");
scanf("%d%d", &x, &y);
if ((x > 0) && (x <= row) && (y > 0) && (y <= col))
{
if (' ' == board[x - 1][y - 1])
{
board[x - 1][y - 1] = '#';
break;
}
else
{
printf("您输入的坐标已被占用,请重新输入坐标->\n");
}
}
else
{
printf("您输入的坐标不合法,请重新输入->\n");
}
}
}
//判断输赢
char Iswin(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)//行相等
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2])
{
return board[i][0];
}
}
for (i = 0; i < col; i++)//列相等
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i])
{
return board[0][i];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2])
{
return board[1][1];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0])
{
return board[1][1];
}
if (1 == IsFull(board, ROW, COL))
{
return 'Q';//平局
}
return ' ';
}
//判断棋盘是否已满
int IsFull(char board[ROW][COL], 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;//棋盘已满
}
下面来看看结果吧: