写一个程序需要多个文件,在这里,创建一个Test_SanZiqi.c的文件,一个Sanziqi.c的文件,还有一个SZQ.h的头文件。
如果可以看懂可以直接看三子棋这个目录,如果看不懂请从写主函数开始看!!!
主要作用声明函数,定义行和列,包含用到的函数的头文件
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 3 //定义行数
#define COL 3 //定义列数
//初始化棋盘
void IniBoard(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);
//判断输赢
char CheckWin(char board[ROW][COL], int row, int col);
测试代码,显示菜单,选择是否进入游戏,调用函数等。
#define _CRT_SECURE_NO_WARNINGS 1
#include"SZQ.h" //自己创建的头文件用双引号
//菜单
void menu()
{
printf("***************************\n");
printf("********* 1.start *********\n");
printf("********* 0.exit *********\n");
printf("***************************\n");
}
//游戏
void game()
{
char board[ROW][COL] = {
0 };
IniBoard(board, ROW, COL);//初始化棋盘
DisplayBoard(board, ROW, COL);//显示棋盘
int rec = CheckWin(board, ROW, COL);//判断输赢
while (1)
{
//玩家走
PlayerMove(board, ROW, COL);
if (rec != 'C')
{
break;
}
DisplayBoard(board, ROW, COL);
//电脑走
ComputerMove(board, ROW, COL);
if (rec != 'C')
{
break;
}
DisplayBoard(board, ROW, COL);
}
if (rec == '*')
{
printf("You Win!");
}
else if (rec == '#')
{
printf("You Lost!");
}
else if (rec == 'D')
{
printf("Draw!");
}
DisplayBoard(board, ROW, COL);
}
//测试模块
void test()
{
int input = 0;
do
{
menu();
printf("请输入选项:>");
scanf("%d", &input);
switch (input)
{
case 1: //进入游戏
game(); //游戏代码
break;
case 0: //退出游戏
printf("已退出!\n");
break;
default: //输入错误
printf("输入无效,请重新输入!\n");
}
} while (input);
}
int main()
{
test();
return 0;
}
在这个文件里实现函数
#define _CRT_SECURE_NO_WARNINGS 1
#include"SZQ.h"//在这里边也得包含自己建的头文件
//初始化棋盘
void IniBoard(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;
for (i = 0; i < row; i++)
{
//数据跟分割线
int j;
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
//下横线跟分割线
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;
while (1)
{
printf("Player *:");
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] = '*';
break;
}
else
{
printf("已有棋子!\n");
}
}
else
{
printf("输入错误!\n");
}
}
}
//电脑走
void ComputerMove(char board[ROW][COL], int row, int col)
{
printf("NPC #:");
int x = rand() % row;//随机数模行列数取余数
int y = rand() % col;
while (1)
{
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
//判断棋盘是否满了
int is_Full(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 1; //有空格
}
}
return 0;//满了
}
//判断输赢
char CheckWin(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 < row; 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[1][1];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
{
return board[1][1];
}
//判断平局(判断棋盘是否有空格)
if (is_Full(board, ROW, COL))
return 'C';
else
return 'D';
}
如果在用scanf函数进行输入时需要输入第二次才有结果并且结果是错误的请删除’’\n’'
int main()
{
test();//调用测试
return 0;
}
void menu()
{
printf("***************************\n");
printf("********* 1.start *********\n");
printf("********* 0.exit *********\n");
printf("***************************\n");
}
void test()
{
srand((unsigned int)time(NULL));//电脑走的时候获取当前时间产生的随机值,不然产生的随机数是固定的
int input = 0;
do
{
menu();//上去调用游戏菜单
printf("请输入选项:>");
scanf("%d", &input);
switch (input)
{
case 1: //进入游戏
game(); //游戏代码
break;
case 0: //退出游戏
printf("已退出!\n");
break;
default: //输入错误
printf("输入无效,请重新输入!\n");
}
} while (input);
}
void game()
{
char board[ROW][COL] = {
0 };
IniBoard(board, ROW, COL);//初始化棋盘
DisplayBoard(board, ROW, COL);//显示棋盘
int rec = CheckWin(board, ROW, COL);//判断输赢
while (1)
{
//玩家走
PlayerMove(board, ROW, COL);
if (rec != 'C')//判断是否有空格,有继续,无跳出
{
break;
}
DisplayBoard(board, ROW, COL);
//电脑走
ComputerMove(board, ROW, COL);
if (rec != 'C')
{
break;
}
DisplayBoard(board, ROW, COL);
}
if (rec == '*')//玩家赢
{
printf("You Win!");
}
else if (rec == '#')//电脑赢
{
printf("You Lost!");
}
else if (rec == 'D')//平局
{
printf("Draw!");
}
DisplayBoard(board, ROW, COL);
}
void IniBoard(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;
for (i = 0; i < row; i++)
{
//数据跟分割线
int j;
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
//下横线跟分割线
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;
while (1)
{
printf("Player *:");
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] = '*';
break;
}
else
{
printf("已有棋子!\n");
}
}
else
{
printf("输入错误!\n");
}
}
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = rand() % row;//随机数模行列数取余数
int y = rand() % col;
while (1)
{
if (board[x][y] == ' ')//如果是空格电脑下棋
{
board[x][y] = '#';
break;
}
}
}
//判断棋盘是否满了
int is_Full(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 1; //有空格
}
}
return 0;//满了
}
//判断输赢
char CheckWin(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 < row; 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[1][1];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
{
return board[1][1];
}
//判断平局(判断棋盘是否有空格)
if (is_Full(board, ROW, COL))
return 'C';//棋盘未满,继续下棋
else
return 'D';//棋盘满了,平局
}