检验学习成果最好的方式是实践,在学习完C语言的函数和数组以后,就可以简易的实现一些小游戏,本文将介绍如何实现三字棋小游戏。欢迎探讨
三子棋的规则是在3X3的棋盘里,率先将自己的三个棋子连成一条直线为赢家,程序实现的核心是玩家的落子以及输赢的判断。
核心流图:当前棋盘->玩家1移动->当前棋盘->输赢判断->玩家2移动->当前棋盘->判断输赢->玩家1移动······
玩家输入坐标将棋子落入棋盘中,即为向一个二维数组赋值。需要注意的时,输入的坐标不能超出数组范围,并且不能占用被输入的位置。
void PlayerMove(char board[ROW][COL], int row, int col)
{
printf("玩家走:>\n");
int x = 0;
int y = 0;
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 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");
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
}
printf("\n");
}
}
只要我们找到三个连成一条直线的棋子即可,这里选三个棋子中中间那个即可。然后判断是不是满足条件。
说明:
返回*,表示输入*的玩家赢;
返回#,表示输入#的玩家赢;
返回 Q,表示平局;
返回C,表示游戏继续。
char IsWin(char board[ROW][COL], int row, int col){
int i, j, count = 0;
for(i=0;i<row;i++)
for (j = 0; j < col; j++) {
if (board[i][j] == '*') {
if (i-1>=0&&i+1<row&&board[i-1][j] == '*' && board[i + 1][j] == '*')
return '*';
if (j-1>=0&&j+1<col&&board[i][j-1] == '*' && board[i][j + 1] == '*')
return '*';
if (i-1 >=0 && i + 1 < row && j - 1 >= 0 && j + 1 < col&& board[i-1][j-1] == '*' && board[i + 1][j + 1] == '*')
return '*';
if (i-1 >= 0 && i + 1 < row && j - 1 >= 0 && j + 1 < col && board[i - 1][j + 1] == '*' && board[i + 1][j - 1] == '*')
return '*';
}
if (board[i][j] == '#') {
if (i - 1 >= 0 && i + 1 < row && board[i - 1][j] == '#' && board[i + 1][j] == '#')
return '#';
if (j - 1 >= 0 && j + 1 < col && board[i][j - 1] == '#' && board[i][j + 1] == '#')
return '#';
if (i-1 >= 0 && i + 1 < row && j - 1 >= 0 && j + 1 < col && board[i - 1][j - 1] == '#' && board[i + 1][j + 1] == '#')
return '*';
if (i-1 >= 0 && i + 1 < row && j - 1 >= 0 && j + 1 < col && board[i - 1][j + 1] == '#' && board[i + 1][j - 1] == '#')
return '#';
}
if (board[i][j] == ' ')
count++;
}
if (count == 0)
return 'Q';
else
return 'C';
}
#define _CRT_SECURE_NO_WARNINGS
#include
#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 Player1Move(char board[ROW][COL], int row, int col);
void Player2Move(char board[ROW][COL], int row, int col);
char IsWin(char board[ROW][COL], int row, int col);
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");
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
}
printf("\n");
}
}//棋盘打印函数,打印出当前棋盘
void Player1Move(char board[ROW][COL], int row, int col)
{
printf("玩家1走:>\n");
int x = 0;
int y = 0;
while (1)
{
printf("请输入坐标:>");
scanf("%d%d", &x, &y);//2 1 -- > 1 0
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 Player2Move(char board[ROW][COL], int row, int col)
{
printf("玩家1走:>\n");
int x = 0;
int y = 0;
while (1)
{
printf("请输入坐标:>");
scanf("%d%d", &x, &y);//2 1 -- > 1 0
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");
}
}
}
char IsWin(char board[ROW][COL], int row, int col) {
int i, j, count = 0;
for (i = 0; i < row; i++)
for (j = 0; j < col; j++) {
if (board[i][j] == '*') {
if (i - 1 >= 0 && i + 1 < row && board[i - 1][j] == '*' && board[i + 1][j] == '*')
return '*';
if (j - 1 >= 0 && j + 1 < col && board[i][j - 1] == '*' && board[i][j + 1] == '*')
return '*';
if (i - 1 >= 0 && i + 1 < row && j - 1 >= 0 && j + 1 < col && board[i - 1][j - 1] == '*' && board[i + 1][j + 1] == '*')
return '*';
if (i - 1 >= 0 && i + 1 < row && j - 1 >= 0 && j + 1 < col && board[i - 1][j + 1] == '*' && board[i + 1][j - 1] == '*')
return '*';
}
if (board[i][j] == '#') {
if (i - 1 >= 0 && i + 1 < row && board[i - 1][j] == '#' && board[i + 1][j] == '#')
return '#';
if (j - 1 >= 0 && j + 1 < col && board[i][j - 1] == '#' && board[i][j + 1] == '#')
return '#';
if (i - 1 >= 0 && i + 1 < row && j - 1 >= 0 && j + 1 < col && board[i - 1][j - 1] == '#' && board[i + 1][j + 1] == '#')
return '*';
if (i - 1 >= 0 && i + 1 < row && j - 1 >= 0 && j + 1 < col && board[i - 1][j + 1] == '#' && board[i + 1][j - 1] == '#')
return '#';
}
if (board[i][j] == ' ')
count++;
}
if (count == 0)
return 'Q';
else
return 'C';
}
int main()
{
char board[ROW][COL];
InitBoard(board, ROW, COL);
DisplayBoard(board, ROW, COL);
char ret = 0;
while (1)
{
Player1Move(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = IsWin(board, ROW, COL);
if (ret != 'C')
{
break;
}
Player2Move(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");
}
return 0;
}
⭐C语言实现扫雷小游戏⭐