目录
1.创造棋盘
2.玩家下棋
3.电脑下棋
4.判断输赢
赢的判断(也是最难的):
行的判断:
列的判断:
主副对角线的判断(花费时间最长):
平局或者继续:
最后判断的全部代码
三个文件的代码
头文件的引用及函数的声明
测试部分
函数的实现
一个文件的代码
写完文章完的话
今天我要分享一个小游戏,五子棋游戏(由三子棋改编)。
比较重要的图片
首先用一个通用的do while游戏模式
void meun()
{
printf("##############################\n");
printf("########### 1. play ##########\n");
printf("########### 0. exit ##########\n");
printf("##############################\n");
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
meun();
printf("请输入你的选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误请重新输入\n");
break;
}
} while (input);
}
int main(void)
{
test();
return 0;
}
然后给出game()函数怎么做
先用三子棋为例
红色为一个二维数组的各个元素的位置,蓝色为空格。先让数组元素赋值为空格。以便之后再人为赋值或者电脑赋值。
//数组元素的初始化
void InitBoard(char show[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
show[i][j] = ' ';
}
}
}
//设置棋盘
void DisplayBoard(char show[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
printf("#");
for (i = 0; i < row; i++)
{
printf(" %d ", i + 1);
if (i != row - 1)
{
printf(" ");
}
}
printf("\n");
for (i = 0; i < row; i++)
{
printf("%d", i + 1);
for (j = 0; j < col; j++)
{
printf(" %c ", show[i][j]);
if (j != col - 1)
{
printf("|");
}
else
{
printf("\n");
}
}
if (i != row - 1)
{
printf(" ");
for (j = 0; j < col; j++)
{
printf("---");
if (j != col - 1)
{
printf("|");
}
else
{
printf("\n");
}
}
}
}
}
选择一个坐标,如果这个位置合法且没有被下过就下这里。注意:玩家并不都是程序员,所以访问数组元素的时候要注意下标。例如,玩家输入1 1,其实访问的是[0][0]下标的元素。
void PlayerBoard(char show[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入你的坐标:>");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col && show[x - 1][y - 1] == ' ')
{
show[x - 1][y - 1] = '*';
break;
}
else
{
printf("输入错误,请重新输入\n");
}
}
}
我这里的电脑下棋是随机的,用两个随机数作为坐标下棋.
用的是常用的rand()函数,用srand()撒播随机种子,srand()函数的参数用的是(unsigned int)time(NULL)。这里用了两个头文件#include
但是还要让这个随机数在一个范围内随机,所以需要用一个操作限制随机数——取余。例如,
10不在1-5之前,则10%5 + 1就在这个区域了。加一是让取余为0的数字进入范围。
//这里需要引用的头文件
#include
#include
//撒播随机种子
srand((unsigned int)time(NULL));
//电脑下棋
void ComputerBoard(char show[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % row + 1;//限制随机数
y = rand() % col + 1;
if (x >= 1 && x <= row && y >= 1 && y <= col && show[x - 1][y - 1] == ' ')
{
show[x - 1][y - 1] = '#';
break;
}
}
}
不管是人还是电脑,下完棋都要判断输赢尽快告诉玩家,中止游戏。
判断无非有四种情况:玩家赢,电脑赢,平局,继续。
人和电脑并不是下一步就停止了,所以用一个循环来然人和电脑不停地玩回合战。
总有一个时刻要分出输赢或平局,继续。用一个if语句来判断战局情况。
所以要设置一个函数来返回一个字符来展示战局情况。
在这个函数里,比较重要的是判断输赢,其次是平局,然后是继续。
判断输赢,其实只用判断赢了没有就可以(反正我也不会判断怎么输),所以在玩家或电脑每下一步棋就判断一次就行。其中还要对每一行、每一列、每个对角线(主副对角线)进行判断。
先是每一行用一个大循环进行行移动,用count记录该行是否满足条件,再用一个循环进行行内有无满足条件的,如果有就让count++,continue。非则让count赋回原值,知道count满足游戏规则的数值EASY_COUNT(就是你要玩几子棋).
思路和行一样
//逐行判断
for (i = 0; i < row; i++)
{
int count = 1;
for (j = 0; j < col - 1; j++)
{
if (show[i][j] == show[i][j + 1] && show[i][j] != ' ')
{
count++;
if (count == EASY_COUNT)
{
return show[i][j];
}
continue;
}
count = 1;
}
}
//逐列判断
for (i = 0; i < col; i++)
{
int count = 1;
for (j = 0; j < row - 1; j++)
{
if (show[j][i] == show[j + 1][i] && show[j][i] != ' ')
{
1; count++;
if (count == EASY_COUNT)
{
return show[j][i];
}
continue;
}
count = 1;
}
}
首先让对角线的判断分为两个模式(这两个模式是都要进行的):
一、从左上的右下我简称为右下。
二、从左下到右上我简称为右上。
并不是所有对角线都要进行判断。
如果玩的是三子棋,红色箭头的对角线就是没有必要判断的。不要着急,你可能会问不是五子棋吗?怎么又变三子棋了?你可以让棋盘变大,你可以改变EASY_COUNT的值让这个游戏变成5子棋,6子棋。
然后我要引用一个小算法减少不必要的时间浪费。
一个正方形边长是用n个数组成。
例如玩三子棋。你会发现如果横2竖2斜边是2,横3竖3斜边是3,横4竖4斜边是4,横5竖5斜边是5.没错这就是规律。现在玩三子棋那么我需要判断的对角线至少有三个能数的子。用右下(从左上到右下),应该分为两个阶段:红色和蓝色
先分析红色,限制行数应该是行数row减去EASY_COUNT的值。好吧蓝色你应该也会了,但是注意一点,蓝色比红色少一个对角线。
现在判断右上(从左下到右上):
先分析红色,这个简单,就是行数为EASY_COUNT这一行到行数为row那一行。蓝色:列数col减去EASY_COUNT的值为限制列数。
说了这么多,该代码了,但是要提醒一点注意我描述下标是玩家认为的下标,并不是真实的下标,还是坐标1 1,其实是[0][0]元素的访问。
//主副对角线
//1. 右上
k = EASY_COUNT - 1;
for (k; k < row; k++)
{
int count = 1;
for (i = k, j = 0; i != 0 && j != col - 1; i--, j++)
{
if (show[i][j] == show[i - 1][j + 1] && show[i][j] != ' ')
{
count++;
if (count == EASY_COUNT)
{
return show[i][j];
}
continue;
}
count = 1;
}
}
k = col - EASY_COUNT;
for (k; k > 0; k--)
{
int count = 1;
for (i = row - 1, j = k; i != 0 && j != col - 1; i--, j++)
{
if (show[i][j] == show[i - 1][j + 1] && show[i][j] != ' ')
{
count++;
if (count == EASY_COUNT)
{
return show[i][j];
}
continue;
}
count = 1;
}
}
//右下
k = row - EASY_COUNT;
for (k; k >= 0; k--)
{
int count = 1;
for (i = k, j = 0; i != row - 1 && j != col - 1; i++, j++)
{
if (show[i][j] == show[i + 1][j + 1] && show[i][j] != ' ')
{
count++;
if (count == EASY_COUNT)
{
return show[i][j];
}
continue;
}
count = 1;
}
}
k = col - EASY_COUNT;
for (k; k > 0; k--)
{
int count = 1;
for (i = 0, j = k; i != row - 1 && j != col - 1; i++, j++)
{
if (show[i][j] == show[i + 1][j + 1] && show[i][j] != ' ')
{
count++;
if (count == EASY_COUNT)
{
return show[i][j];
}
continue;
}
count = 1;
}
}
这个比较简单,平局我用了一个函数IsFull()。
int IsFull(char show[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 (show[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
if (1 == IsFull(show, ROW, COL))
{
return 'Q';
}
else
{
return 'C';
}
int IsFull(char show[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 (show[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
char IsWin(char show[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
int k = 0;
//逐行判断
for (i = 0; i < row; i++)
{
int count = 1;
for (j = 0; j < col - 1; j++)
{
if (show[i][j] == show[i][j + 1] && show[i][j] != ' ')
{
count++;
if (count == EASY_COUNT)
{
return show[i][j];
}
continue;
}
count = 1;
}
}
//逐列判断
for (i = 0; i < col; i++)
{
int count = 1;
for (j = 0; j < row - 1; j++)
{
if (show[j][i] == show[j + 1][i] && show[j][i] != ' ')
{
1; count++;
if (count == EASY_COUNT)
{
return show[j][i];
}
continue;
}
count = 1;
}
}
//主副对角线
//1. 右上
k = EASY_COUNT - 1;
for (k; k < row; k++)
{
int count = 1;
for (i = k, j = 0; i != 0 && j != col - 1; i--, j++)
{
if (show[i][j] == show[i - 1][j + 1] && show[i][j] != ' ')
{
count++;
if (count == EASY_COUNT)
{
return show[i][j];
}
continue;
}
count = 1;
}
}
k = col - EASY_COUNT;
for (k; k > 0; k--)
{
int count = 1;
for (i = row - 1, j = k; i != 0 && j != col - 1; i--, j++)
{
if (show[i][j] == show[i - 1][j + 1] && show[i][j] != ' ')
{
count++;
if (count == EASY_COUNT)
{
return show[i][j];
}
continue;
}
count = 1;
}
}
//右下
k = row - EASY_COUNT;
for (k; k >= 0; k--)
{
int count = 1;
for (i = k, j = 0; i != row - 1 && j != col - 1; i++, j++)
{
if (show[i][j] == show[i + 1][j + 1] && show[i][j] != ' ')
{
count++;
if (count == EASY_COUNT)
{
return show[i][j];
}
continue;
}
count = 1;
}
}
k = col - EASY_COUNT;
for (k; k > 0; k--)
{
int count = 1;
for (i = 0, j = k; i != row - 1 && j != col - 1; i++, j++)
{
if (show[i][j] == show[i + 1][j + 1] && show[i][j] != ' ')
{
count++;
if (count == EASY_COUNT)
{
return show[i][j];
}
continue;
}
count = 1;
}
}
if (1 == IsFull(show, ROW, COL))
{
return 'Q';
}
else
{
return 'C';
}
}
#include
#include
#include
#define ROW 10//棋盘行数
#define COL 10//棋盘列数
#define EASY_COUNT 5//你要玩的几子棋
void InitBoard(char show[ROW][COL], int row, int col);
void DisplayBoard(char show[ROW][COL], int row, int col);
void PlayerBoard(char show[ROW][COL], int row, int col);
void ComputerBoard(char show[ROW][COL], int row, int col);
char IsWin(char show[ROW][COL], int row, int col);
game.h是我自己的头文件的名字
#include"game.h"
void meun()
{
printf("##############################\n");
printf("########### 1. play ##########\n");
printf("########### 0. exit ##########\n");
printf("##############################\n");
}
void game()
{
char show[ROW][COL] = { 0 };
InitBoard(show, ROW, COL);
DisplayBoard(show, ROW, COL);
char ret = '\0';
while (1)
{
PlayerBoard(show, ROW, COL);
DisplayBoard(show, ROW, COL);
ret = IsWin(show, ROW, COL);
if (ret != 'C')
{
break;
}
ComputerBoard(show, ROW, COL);
DisplayBoard(show, ROW, COL);
ret = IsWin(show, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf("玩家赢\n");
}
else if (ret == '#')
{
printf("电脑赢\n");
}
else
{
printf("平局\n");
}
}
void meun()
{
printf("##############################\n");
printf("########### 1. play ##########\n");
printf("########### 0. exit ##########\n");
printf("##############################\n");
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
meun();
printf("请输入你的选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误请重新输入\n");
break;
}
} while (input);
}
int main(void)
{
test();
return 0;
}
#include"game.h"
void InitBoard(char show[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
show[i][j] = ' ';
}
}
}
void DisplayBoard(char show[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
printf("#");
for (i = 0; i < row; i++)
{
printf(" %d ", i + 1);
if (i != row - 1)
{
printf(" ");
}
}
printf("\n");
for (i = 0; i < row; i++)
{
printf("%d", i + 1);
for (j = 0; j < col; j++)
{
printf(" %c ", show[i][j]);
if (j != col - 1)
{
printf("|");
}
else
{
printf("\n");
}
}
if (i != row - 1)
{
printf(" ");
for (j = 0; j < col; j++)
{
printf("---");
if (j != col - 1)
{
printf("|");
}
else
{
printf("\n");
}
}
}
}
}
void PlayerBoard(char show[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入你的坐标:>");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col && show[x - 1][y - 1] == ' ')
{
show[x - 1][y - 1] = '*';
break;
}
else
{
printf("输入错误,请重新输入\n");
}
}
}
void ComputerBoard(char show[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % row + 1;
y = rand() % col + 1;
//printf("%d %d\n", x, y);
if (x >= 1 && x <= row && y >= 1 && y <= col && show[x - 1][y - 1] == ' ')
{
show[x - 1][y - 1] = '#';
break;
}
}
}
int IsFull(char show[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 (show[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
char IsWin(char show[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
int k = 0;
//逐行判断
for (i = 0; i < row; i++)
{
int count = 1;
for (j = 0; j < col - 1; j++)
{
if (show[i][j] == show[i][j + 1] && show[i][j] != ' ')
{
count++;
if (count == EASY_COUNT)
{
return show[i][j];
}
continue;
}
count = 1;
}
}
//逐列判断
for (i = 0; i < col; i++)
{
int count = 1;
for (j = 0; j < row - 1; j++)
{
if (show[j][i] == show[j + 1][i] && show[j][i] != ' ')
{
1; count++;
if (count == EASY_COUNT)
{
return show[j][i];
}
continue;
}
count = 1;
}
}
//主副对角线
//1. 右上
k = EASY_COUNT - 1;
for (k; k < row; k++)
{
int count = 1;
for (i = k, j = 0; i != 0 && j != col - 1; i--, j++)
{
if (show[i][j] == show[i - 1][j + 1] && show[i][j] != ' ')
{
count++;
if (count == EASY_COUNT)
{
return show[i][j];
}
continue;
}
count = 1;
}
}
k = col - EASY_COUNT;
for (k; k > 0; k--)
{
int count = 1;
for (i = row - 1, j = k; i != 0 && j != col - 1; i--, j++)
{
if (show[i][j] == show[i - 1][j + 1] && show[i][j] != ' ')
{
count++;
if (count == EASY_COUNT)
{
return show[i][j];
}
continue;
}
count = 1;
}
}
//右下
k = row - EASY_COUNT;
for (k; k >= 0; k--)
{
int count = 1;
for (i = k, j = 0; i != row - 1 && j != col - 1; i++, j++)
{
if (show[i][j] == show[i + 1][j + 1] && show[i][j] != ' ')
{
count++;
if (count == EASY_COUNT)
{
return show[i][j];
}
continue;
}
count = 1;
}
}
k = col - EASY_COUNT;
for (k; k > 0; k--)
{
int count = 1;
for (i = 0, j = k; i != row - 1 && j != col - 1; i++, j++)
{
if (show[i][j] == show[i + 1][j + 1] && show[i][j] != ' ')
{
count++;
if (count == EASY_COUNT)
{
return show[i][j];
}
continue;
}
count = 1;
}
}
if (1 == IsFull(show, ROW, COL))
{
return 'Q';
}
else
{
return 'C';
}
}
我知道有人看不懂,那我就写在一个.c文件上吧,就是说你让代码复制,然后粘贴到你的编译器的.c文件里就可以直接用了。
#include
#include
#include
#define ROW 10//棋盘行数
#define COL 10//棋盘列数
#define EASY_COUNT 5//你要玩的几子棋
void InitBoard(char show[ROW][COL], int row, int col);
void DisplayBoard(char show[ROW][COL], int row, int col);
void PlayerBoard(char show[ROW][COL], int row, int col);
void ComputerBoard(char show[ROW][COL], int row, int col);
char IsWin(char show[ROW][COL], int row, int col);
void InitBoard(char show[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
show[i][j] = ' ';
}
}
}
void DisplayBoard(char show[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
printf("#");
for (i = 0; i < row; i++)
{
printf(" %d ", i + 1);
if (i != row - 1)
{
printf(" ");
}
}
printf("\n");
for (i = 0; i < row; i++)
{
printf("%d", i + 1);
for (j = 0; j < col; j++)
{
printf(" %c ", show[i][j]);
if (j != col - 1)
{
printf("|");
}
else
{
printf("\n");
}
}
if (i != row - 1)
{
printf(" ");
for (j = 0; j < col; j++)
{
printf("---");
if (j != col - 1)
{
printf("|");
}
else
{
printf("\n");
}
}
}
}
}
void PlayerBoard(char show[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入你的坐标:>");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col && show[x - 1][y - 1] == ' ')
{
show[x - 1][y - 1] = '*';
break;
}
else
{
printf("输入错误,请重新输入\n");
}
}
}
void ComputerBoard(char show[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % row + 1;
y = rand() % col + 1;
//printf("%d %d\n", x, y);
if (x >= 1 && x <= row && y >= 1 && y <= col && show[x - 1][y - 1] == ' ')
{
show[x - 1][y - 1] = '#';
break;
}
}
}
int IsFull(char show[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 (show[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
char IsWin(char show[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
int k = 0;
//逐行判断
for (i = 0; i < row; i++)
{
int count = 1;
for (j = 0; j < col - 1; j++)
{
if (show[i][j] == show[i][j + 1] && show[i][j] != ' ')
{
count++;
if (count == EASY_COUNT)
{
return show[i][j];
}
continue;
}
count = 1;
}
}
//逐列判断
for (i = 0; i < col; i++)
{
int count = 1;
for (j = 0; j < row - 1; j++)
{
if (show[j][i] == show[j + 1][i] && show[j][i] != ' ')
{
1; count++;
if (count == EASY_COUNT)
{
return show[j][i];
}
continue;
}
count = 1;
}
}
//主副对角线
//1. 右上
k = EASY_COUNT - 1;
for (k; k < row; k++)
{
int count = 1;
for (i = k, j = 0; i != 0 && j != col - 1; i--, j++)
{
if (show[i][j] == show[i - 1][j + 1] && show[i][j] != ' ')
{
count++;
if (count == EASY_COUNT)
{
return show[i][j];
}
continue;
}
count = 1;
}
}
k = col - EASY_COUNT;
for (k; k > 0; k--)
{
int count = 1;
for (i = row - 1, j = k; i != 0 && j != col - 1; i--, j++)
{
if (show[i][j] == show[i - 1][j + 1] && show[i][j] != ' ')
{
count++;
if (count == EASY_COUNT)
{
return show[i][j];
}
continue;
}
count = 1;
}
}
//右下
k = row - EASY_COUNT;
for (k; k >= 0; k--)
{
int count = 1;
for (i = k, j = 0; i != row - 1 && j != col - 1; i++, j++)
{
if (show[i][j] == show[i + 1][j + 1] && show[i][j] != ' ')
{
count++;
if (count == EASY_COUNT)
{
return show[i][j];
}
continue;
}
count = 1;
}
}
k = col - EASY_COUNT;
for (k; k > 0; k--)
{
int count = 1;
for (i = 0, j = k; i != row - 1 && j != col - 1; i++, j++)
{
if (show[i][j] == show[i + 1][j + 1] && show[i][j] != ' ')
{
count++;
if (count == EASY_COUNT)
{
return show[i][j];
}
continue;
}
count = 1;
}
}
if (1 == IsFull(show, ROW, COL))
{
return 'Q';
}
else
{
return 'C';
}
}
void meun()
{
printf("##############################\n");
printf("########### 1. play ##########\n");
printf("########### 0. exit ##########\n");
printf("##############################\n");
}
void game()
{
char show[ROW][COL] = { 0 };
InitBoard(show, ROW, COL);
DisplayBoard(show, ROW, COL);
char ret = '\0';
while (1)
{
PlayerBoard(show, ROW, COL);
DisplayBoard(show, ROW, COL);
ret = IsWin(show, ROW, COL);
if (ret != 'C')
{
break;
}
ComputerBoard(show, ROW, COL);
DisplayBoard(show, ROW, COL);
ret = IsWin(show, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf("玩家赢\n");
}
else if (ret == '#')
{
printf("电脑赢\n");
}
else
{
printf("平局\n");
}
}
void meun()
{
printf("##############################\n");
printf("########### 1. play ##########\n");
printf("########### 0. exit ##########\n");
printf("##############################\n");
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
meun();
printf("请输入你的选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误请重新输入\n");
break;
}
} while (input);
}
int main(void)
{
test();
return 0;
}
如有错误可直接指出,希望和编程之路与伙伴共同进步。
最后,制作不易,觉得不错可以点赞。