目录
(0)游戏思路
三子棋的实现;
1.游戏一把结束继续,不退出
2.三个文件处理:game.h,game.c,text.c
3.步骤:
(1)打印菜单
(2)初始数组
(3)打印棋盘(数据 + 分割线)
(4)玩家下棋
(5)电脑下棋
(6)判断输赢
(1)打印菜单
#include <stdio.h>
void menu()
{
printf("****************************\n");
printf("*********1:开始游戏*********\n");
printf("*********0:退出游戏*********\n");
printf("****************************\n");
printf("*******请输入相应数字*******\n");
}
int main()
{
int input = 0;
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1:
game();
case 0:
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}
【说明】
1.用input进行输入,1进入游戏,0退出游戏,其他值重新输入,则使用switch语句
2.重复进行游戏,则使用do while语句循环
3.用input的值判断真假,便于循环与终止
(2)初始数组
主函数:
#include "game.h"
void game()
{
//创建数组
char board[ROW][COL] = {0};
//初始化数组
Initboard(board, ROW, COL);
}
game.h文件:
#pragma once
#include <stdio.h>
#define ROW 3
#define COL 3
Initboard(char board[ROW][COL], int row, int col);
game.c文件:
#include "game.h"
Initboard(char board[ROW][COL], int row, int col)
{
int i = 0;
for ( i = 0; i < row; i++)
{
int j = 0;
for ( j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
【说明】
1.创建二维数组来实现三子棋,即3x3
2.用宏定义ROW与COL为3,从而重复使用,不使代码上出现很多3,便于美观与方便
3.二维数组函数接受类型不用char* board的指针类型,否则后面无法进行引用
4.双循环初始化数组为空格
(3)打印棋盘(数据 + 分割线)
game.c文件:
Displayboard(char board[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 ", board[i][j]);
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
for ( j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
}
}
【说明】
1. 实现此棋盘
2.为实现可以打印任意大小棋盘,即改变ROW,COL的值即可,所以使用循环打印
3.即此为一个单位,用循环打印实现棋盘
4.注意打印\n实现换行
(4)玩家下棋
//玩家下棋
Playerboard(char board[ROW][COL], int row, int col)
{
int x, y = 0;
while (1)
{
printf("请输入坐标,俩数字,中间用空格隔开:>");
scanf("%d %d", &x, &y);
if (x > 0 && x < row + 1 && y > 0 && y < col + 1)
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("输入错误,请重新输入\n");
}
}
}
【说明】
1.此处为了实现玩家输入,即改变坐标的值
2.为了防止玩家乱输入,则需要对x,y设置一个限制
3.玩家不是程序员,所以从1开始输入,那么坐标则需要-1
(5)电脑下棋
//电脑下棋
Computerboard(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑下棋\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
【说明】
1.电脑下棋生成随机值,则需要目标格无子时方可下棋
2.x,y的随机值应该在循环内,保证随机值错误时重新随机
3. * rand需要srand((unsignded int)time(NULL)来支撑
同时需要头文件#include <stdlib.h> #include <time.h>
(6)判断输赢*
game.c文件:
//判断输赢
int Isfull(char board[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 (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++)
{
int j = 0;
if (board[i][j] == board[i][j+1] && board[i][j] == board[i][j+2] && board[i][j] != ' ')
{
return board[i][j];
}
}
//一列赢
for (i = 0; i < col; i++)
{
int j = 0;
if (board[j][i] == board[j][i + 1] && board[j][i] == board[j][i + 2] && board[j][i] != ' ')
{
return board[j][i];
}
}
//斜线赢
if (board[1][1] == board[2][2] && board[2][2] == board[0][0] && board[1][1] != ' ')
{
return board[1][1];
}
if (board[1][1] == board[0][2] && board[0][2] == board[2][0] && board[1][1] != ' ')
{
return board[1][1];
}
//平局
if (Isfull(board, ROW, COL) == 1)
{
return 'Q';
}
//继续游戏
return 'C';
}
【说明】
1.通过game.c的Iswin函数返回值来确定谁赢,并在主函数里进行判断,如图
while (1)
{
//玩家下棋
Playerboard(board, ROW, COL);
Displayboard(board, ROW, COL);
//判断输赢
ret = Iswin(board, ROW, COL);
if (ret != 'C')
{
break;
}
//电脑下棋
Computerboard(board, ROW, COL);
Displayboard(board, ROW, COL);
//判断输赢
ret = Iswin(board, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == '#')
{
printf("电脑赢\n");
}
if (ret == '*')
{
printf("玩家赢\n");
}
if (ret == 'Q')
{
printf("平局\n");
}
2.一直下到出结果,所以循环语句
3.为了方便,先判断是否继续游戏,若已有结果,则跳出循环进行判断结果
(7)总代码
//game.h头文件
#pragma once
#include <stdio.h>
#define ROW 3
#define COL 3
#include <stdlib.h>
#include <time.h>
//初始化棋盘
void Initboard(char board[ROW][COL], int row, int col);
//打印棋盘
void Displayboard(char board[ROW][COL], int row, int col);
//玩家下棋
void Playerboard(char board[ROW][COL], int row, int col);
//电脑下棋
void Computerboard(char board[ROW][COL], int row, int col);
//判断输赢
char Iswin(char board[ROW][COL], int row, int col);
//主函数
#define _CRT_SECURE_NO_WARNINGS 1
void menu()
{
printf("****************************\n");
printf("*********1:开始游戏*********\n");
printf("*********0:退出游戏*********\n");
printf("****************************\n");
}
#include "game.h"
void game()
{
srand((unsigned int)time(NULL));
int ret = 0;
//创建数组
char board[ROW][COL] = {0};
//初始化数组
Initboard(board, ROW, COL);
//打印棋盘
Displayboard(board, ROW, COL);
while (1)
{
//玩家下棋
Playerboard(board, ROW, COL);
Displayboard(board, ROW, COL);
//判断输赢
ret = Iswin(board, ROW, COL);
if (ret != 'C')
{
break;
}
//电脑下棋
Computerboard(board, ROW, COL);
Displayboard(board, ROW, COL);
//判断输赢
ret = Iswin(board, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == '#')
{
printf("电脑赢\n");
}
if (ret == '*')
{
printf("玩家赢\n");
}
if (ret == 'Q')
{
printf("平局\n");
}
}
int main()
{
int input = 0;
do
{
menu();
printf("请输入:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
case 0:
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}
//game.c文件
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
//初始化数组
void Initboard(char board[ROW][COL], int row, int col)
{
int i = 0;
for ( i = 0; i < row; i++)
{
int j = 0;
for ( j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
//打印棋盘
void Displayboard(char board[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 ", 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 Playerboard(char board[ROW][COL], int row, int col)
{
int x, y = 0;
while (1)
{
printf("请输入坐标,俩数字,中间用空格隔开:>");
scanf("%d %d", &x, &y);
if (x > 0 && x < row + 1 && y > 0 && y < col + 1)
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("输入错误,请重新输入\n");
}
}
}
//电脑下棋
void Computerboard(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑下棋\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
//判断输赢
int Isfull(char board[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 (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++)
{
int j = 0;
if (board[i][j] == board[i][j+1] && board[i][j] == board[i][j+2] && board[i][j] != ' ')
{
return board[i][j];
}
}
//一列赢
for (i = 0; i < col; i++)
{
int j = 0;
if (board[j][i] == board[j][i + 1] && board[j][i] == board[j][i + 2] && board[j][i] != ' ')
{
return board[j][i];
}
}
//斜线赢
if (board[1][1] == board[2][2] && board[2][2] == board[0][0] && board[1][1] != ' ')
{
return board[1][1];
}
if (board[1][1] == board[0][2] && board[0][2] == board[2][0] && board[1][1] != ' ')
{
return board[1][1];
}
//平局
if (Isfull(board, ROW, COL) == 1)
{
return 'Q';
}
//继续游戏
return 'C';
}
(8)运行实例
今天就总结到此,感谢您的垂阅,敬请指教,谢谢。