建议大家先看全部代码,再分块理解。代码不是很难,最主要是逻辑的严密,以及对问题的分块与分析,我要达到什么效果,我要怎么做大能达到这个效果?希望大家在学习c语言的过程中可以一步一步稳扎稳打,我觉得最关键的就是要自己动手,看别人写100遍都不如自己写一遍!!!加油加油!!!
在学习完数组之后,我们通过设计一个简单的小游戏—三子棋来加深对数组的理解。
为了使代码方便阅读,以及后期的测试与维护,我们选择使用多文件编程的方式
清楚了流程之后,现在我们来分模块设计这个程序。
void menu()
{
printf("******************************\n");
printf("******* 1.play ******\n");
printf("******* 0.exit ******\n");
printf("******************************\n");
}
void init_board(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] = ' ';
}
}
}
这里面我们使用了无参宏,定义了ROW COL方便我们后续对棋盘的一个扩充,比较灵活
void display_board(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 player_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家下棋:>\n");
while (1)
{
printf("请输入下棋的坐标:>\n");
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 computer_move(char board[ROW][COL], int row, int col)
{
printf("电脑下棋:>\n");
while (1)
{
int x = rand() % row;
int y = rand() % col;
//坐标是否被占用
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
在此处,我们通过简单的rand()随机生成坐标来实现电脑的随机下子。
srand((unsigned)time(NULL))//设置随机起点
a = rand()%100
我们就得到了随机的0~99个数字
比赛的情况有四种
- 玩家赢-return *
- 电脑赢-return #
- 平局了-return Q
- 游戏继续- return C
注意这里的设计很巧妙,我们通过返回在棋盘中连成线的棋子的个数,就避免讨论是谁赢了(字符与结果一一对应),使得程序更加简洁。
char whether_win(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
int k = 0;
//三列
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == board[i][j+1] && board[i][j+1] == board[i][j+2] && board[i][j] != ' ')
return board[i][0];
}
}
//三列
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == board[i][(j+1)%3] && board[i][(j+1)%3] == board[i][(j+2)%3] && board[i][j] != ' ')
return board[i][j];
}
}
//三行
for (i = 0; i < col; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == board[(i + 1)%3][j] && board[(i + 1)%3][j] == board[(i + 2)%3][j] && board[i][j] != ' ')
{
return board[i][j];
}
}
}
//两条对角线
for (i = 0; i < col; i++)
{
if (board[i][i] == board[(i + 1)%3][(i+1)%3] && board[(i + 1)%3][(i+1)%3] == board[(i + 2)%3][(i + 2)%3] && board[i][i] != ' ')
{
return board[i][i];
}
}
for (i = 0; i < col-2; i++)
{
for (j = row - 1; j >= 0; j--)
{
if (board[i][j] == board[(i + 1) % 3][j - 1] && board[(i + 1) % 3][j - 1] == board[(i + 2) % 3][j - 2] && board[i][j] != ' ')
{
return board[i][j];
}
}
}
//判断平局
if (whether_full(board, row, col) == 1)
{
return 'Q';
}
//继续
return 'C';
}
通过改变宏定义中ROW和COL的值我们可以得到更大的棋盘如下图
game.h
#pragma once
#include
#include
#include
#define ROW 3
#define COL 3
//打印棋盘
void display_board(char board[ROW][COL],int row,int col);
//初始化棋盘
void init_board(char board[ROW][COL], int row, int col);
//玩家下棋
void player_move(char board[ROW][COL], int row, int col);
//电脑下棋
void computer_move(char board[ROW][COL], int row, int col);
//判断游戏状态
char whether_win(char board[ROW][COL], int row, int col);
game.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void display_board(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 init_board(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 player_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家下棋:>\n");
while (1)
{
printf("请输入下棋的坐标:>\n");
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 computer_move(char board[ROW][COL], int row, int col)
{
printf("电脑下棋:>\n");
while (1)
{
int x = rand() % row;
int y = rand() % col;
//坐标是否被占用
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int whether_full(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 whether_win(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
int k = 0;
//三列
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == board[i][j+1] && board[i][j+1] == board[i][j+2] && board[i][j] != ' ')
return board[i][0];
}
}
//三列
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == board[i][(j+1)%3] && board[i][(j+1)%3] == board[i][(j+2)%3] && board[i][j] != ' ')
return board[i][j];
}
}
//三行
for (i = 0; i < col; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == board[(i + 1)%3][j] && board[(i + 1)%3][j] == board[(i + 2)%3][j] && board[i][j] != ' ')
{
return board[i][j];
}
}
}
//两条对角线
for (i = 0; i < col; i++)
{
if (board[i][i] == board[(i + 1)%3][(i+1)%3] && board[(i + 1)%3][(i+1)%3] == board[(i + 2)%3][(i + 2)%3] && board[i][i] != ' ')
{
return board[i][i];
}
}
for (i = 0; i < col-2; i++)
{
for (j = row - 1; j >= 0; j--)
{
if (board[i][j] == board[(i + 1) % 3][j - 1] && board[(i + 1) % 3][j - 1] == board[(i + 2) % 3][j - 2] && board[i][j] != ' ')
{
return board[i][j];
}
}
}
//判断平局
if (whether_full(board, row, col) == 1)
{
return 'Q';
}
//继续
return 'C';
}
test.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void menu()
{
printf("******************************\n");
printf("******* 1.play ******\n");
printf("******* 0.exit ******\n");
printf("******************************\n");
}
void game()
{
char ret = 0;
char board[ROW][COL] = { 0 };
init_board(board, ROW, COL);
display_board(board, ROW, COL);
while (1)
{
system("cls");
display_board(board, ROW, COL);
player_move(board, ROW, COL);
ret = whether_win(board, ROW, COL);
if (ret != 'C')
break;
display_board(board, ROW, COL);
computer_move(board, ROW, COL);
ret = whether_win(board, ROW, COL);
if (ret != 'C')
break;
display_board(board, ROW, COL);
}
if (ret == '*')
{
printf("玩家赢\n");
}
else if (ret == '#')
{
printf("电脑赢\n");
}
else if (ret == 'Q')
{
printf("平局\n");
}
display_board(board, ROW, COL);
}
//玩家赢-'*'
//电脑赢-'#'
//平局了-'Q'
//游戏继续-'C'
int main()
{
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");
break;
}
} while (input);
return 0;
}
这次的程序,在判断输赢的时候进行了优化,但是并没有达到最优,还可以在判断的时候在嵌套一层循环达到我们可以实现四子棋,五子棋。这样程序更加灵活,不过要是不怕麻烦可以继续在后面加判断条件,实现半自动n子棋