在函数声明调用的时候说明了日后写复杂的项目直接写在一个源文件中不建议所以今天这个三子棋分多个文件来写
首先来介绍用sest来作用游戏的开始和玩法,再然后用saq.h用来存放函数的声明和头文件最后使用szq.c来实现游戏
首先是游戏的开始逻辑
每个游戏都要有开始界面
然后就是开始界面之后的选则如果选则1就进入0就退出其他数字就重选,接下来就从game中来实现游戏
首先是打印棋盘让棋盘有一个3*3的位置进行下棋这里为了方便后续替换使用了宏定义
然后是玩家下棋部分
这是游戏判断部分
最后是源码
sest.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"szq.h"
//创建菜单
void menu()
{
printf("**********************\n");
printf("****** 1.play ******\n");
printf("****** 0.exit ******\n");
printf("**********************\n");
}
//创建游戏
void game()
{
int ret = 0;
char board[ROW][COL];//初始化棋盘
Initboard(board, ROW, COL);
Displayboard(board, ROW, COL);
while (1)
{
//玩家下棋
Playermove(board, ROW, COL);
Displayboard(board, ROW, COL);
ret = IsWin(board, ROW, COL);
if (ret != 'C')
{
break;
}
//判断输赢
Computermove(board, ROW, COL);
//电脑下棋
Displayboard(board, ROW, COL);
ret = IsWin(board, ROW, COL);
if (ret != 'C')
{
break;
}
}
//判断部分
if (ret == '*')
{
printf("玩家嬴\n");
}
else if (ret == '#')
{
printf("电脑嬴\n");
}
else
{
printf("平局\n");
}
}
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;
}
szq.h
#include <stdio.h>
#include <time.h>
#include <stdlib.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 Playermove(char board[ROW][COL], int row, int col);
//电脑下棋
void Computermove(char board[ROW][COL], int row, int col);
//判断输赢
char IsWin(char board[ROW], int row, int col);
szq.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"szq.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 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 Playermove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家要下棋:\n");
while (1)
{
printf("请输入下棋位置:");
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("电脑开始下棋>\n");
int x = 0;
int y = 0;
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
static 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;
}
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] && board[i][0] == board[i][0] && 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] && board[0][i] == board[2][i] && board[0][i] != ' ')
{
return board[i][0];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] == board[2][2] && board[0][0] != ' ')
{
return board[0][0];
}
if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[0][2] == board[2][0] && board[1][1] != ' ')
{
return board[1][1];
}
if (isFull(board, ROW, COL))
{
return 'Q';
}
return 'C';
}
以上就是本次的分享谢谢大家
标签:int,三子,后带,col,源码,board,printf,COL,ROW From: https://blog.csdn.net/2401_82857398/article/details/139837549