井字棋游戏要求在3乘3棋盘上,每行都相同或者每列都相同再或者对角线相同,则胜出.因此我们可以使用一个二维数组来表示棋盘,判断胜负只需要判断数组元素是否相同即可.具体我们可以分为以下几步来做:
1.创建维数组并进行初始化,如果仅仅是一个二维数组来表示棋盘,看起来不是很清楚,因此我们可以对棋盘边框用符号打印出来进行优化一下:
//初始化棋盘
void init(char board[max_row][max_col]) {
for (int row = 0; row < max_row; row++) {
for (int col = 0; col < max_col; col++) {
board[row][col] =' ';
}
}
srand((unsigned int)time(0));
}
//打印棋盘
void print(char board[max_row][max_col]) {
system("cls");//每次打印之前清除掉上一次的结果.
for (int a = 0; a < max_col; a++) {
printf("+---+---+---+\n");
for (int b = 0; b < max_row; b++) {
printf("| %c ", board[a][b]);
}
printf("| \n");
}
printf("+---+---+---+\n");
}
2.进行玩家落子,同样是使用数组操作,让玩家输入相对应的坐标,在二维数组的该位置打印'x'来表示玩家下的棋子
void play(char board[max_row][max_col]) {
printf("玩家落子阶段!\n");
int a;
int b;
while (1) {
printf("请输入想要落子的坐标: ");
scanf("%d %d", &a, &b);
//判定玩家落子坐标是否在有效范围内
if (a < 0 || b < 0 || a >= max_row || b >= max_col) {
printf("输入坐标有误请重新输入!\n");
continue;
}
//判定落子处是否已经存在了棋子
if (board[b][a] != ' ') {
printf("此处已被落子无法再落子,请重新输入!\n");
continue;
}
board[b][a] = 'x'; //用'x'表示玩家落子
break;
}
}
3.玩家落子结束后让电脑在二维数组中随机落子,使用rand函数产生0到2的随机数进行赋值操作,进而实现电脑随机落子
void computer(char board[max_row][max_col]) {
printf("电脑落子阶段!\n");
while (1) {
int a = rand() % max_row; //产生0到2的随机数
int b = rand() % max_col;
if (board[a][b] != ' ') { //判断所下位置是否已经有子
continue;
}
board[a][b] = 'o';
break;
}
4.判断胜负,当玩家落子结束,或者电脑落子结束后,判断是否存在某行或某列或者对角线有相同的元素,如果有则直接判定结果,如果没有则轮到下家落子
char check(char board[max_row][max_col]) {
//检测所有行是否相等
for (int cow = 0; cow < max_row; cow++) {
if (board[cow][0] != ' ' && board[cow][0] == board[cow][1] && board[cow][0]==board[cow][2] ) {
return board[cow][0];
}
}
//检测所有列是否相等
for (int col = 0; col < max_col; col++) {
if (board[0][col] != ' ' && board[0][col] == board[1][col] && board[0][col]==board[2][col] ) {
return board[0][col];
}
}
//检测对角线是否相等
if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[0][0] == board[2][2]) {
return board[0][0];
}
if (board[0][2] != ' ' && board[0][2] == board[1][1] && board[0][2] == board[2][0]) {
return board[0][2];
}
if (pingju(board)) {
return 'q'; //棋子已满和棋
}
return 'a'; //棋盘未满
}
//判断棋盘是否棋子已满,如果满了返回1,未满返回0.
int pingju(char board[max_row][max_col]) {
for (int row = 0; row < max_row; row++) {
for (int col = 0; col < max_col; col++) {
if (board[row][col] == ' ') {
return 0;
}
}
}
return 1;
}
5.在主函数中调用以上函数,进行操作:
int main() {
char board[max_row][max_col] = {0};//数组初始化为0
char winner = 'n';
init(board); //将棋盘中元素初始化为空格
while (1) {
print(board); //打印棋盘
play(board); //玩家落子
winner = check(board); //判断胜负
if (winner != 'a') {
break;
}
computer(board); //电脑落子
winner = check(board); //判断胜负
if (winner != 'a') {
break;
}
}
if (winner == 'x') {
print(board);
printf("你赢了!");
}
else if (winner == 'o') {
print(board);
printf("你输了!");
}
else if (winner == 'q') {
print(board);
printf("和棋!");
}
return 0;
在主函数中利用while循环来实现玩家与电脑的轮流落子,如果当一方落子之后胜负已出,则跳出循环直接打印最终结果.
完整代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#define max_row 3
#define max_col 3
//初始化棋盘
void init(char board[max_row][max_col]) {
for (int row = 0; row < max_row; row++) {
for (int col = 0; col < max_col; col++) {
board[row][col] =' ';
}
}
srand((unsigned int)time(0));
}
//打印棋盘
void print(char board[max_row][max_col]) {
system("cls");//每次打印之前清除掉上一次的结果.
for (int a = 0; a < max_col; a++) {
printf("+---+---+---+\n");
for (int b = 0; b < max_row; b++) {
printf("| %c ", board[a][b]);
}
printf("| \n");
}
printf("+---+---+---+\n");
}
//玩家落子
void play(char board[max_row][max_col]) {
printf("玩家落子阶段!\n");
int a;
int b;
while (1) {
printf("请输入想要落子的坐标: ");
scanf("%d %d", &a, &b);
//判定玩家落子坐标是否在有效范围内
if (a < 0 || b < 0 || a >= max_row || b >= max_col) {
printf("输入坐标有误请重新输入!\n");
continue;
}
//判定落子处是否已经存在了棋子
if (board[a][b] != ' ') {
printf("此处已被落子无法再落子,请重新输入!\n");
continue;
}
board[a][b] = 'x'; //用'x'表示玩家落子
break;
}
}
//
void computer(char board[max_row][max_col]) {
printf("电脑落子阶段!\n");
while (1) {
int a = rand() % max_row; //产生0到2的随机数
int b = rand() % max_col;
if (board[a][b] != ' ') { //判断所下位置是否已经有子
continue;
}
board[a][b] = 'o';
break;
}
//
}
//
char check(char board[max_row][max_col]) {
//检测所有行是否相等
for (int cow = 0; cow < max_row; cow++) {
if (board[cow][0] != ' ' && board[cow][0] == board[cow][1] && board[cow][0]==board[cow][2] ) {
return board[cow][0];
}
}
//检测所有列是否相等
for (int col = 0; col < max_col; col++) {
if (board[0][col] != ' ' && board[0][col] == board[1][col] && board[0][col]==board[2][col] ) {
return board[0][col];
}
}
//检测对角线是否相等
if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[0][0] == board[2][2]) {
return board[0][0];
}
if (board[0][2] != ' ' && board[0][2] == board[1][1] && board[0][2] == board[2][0]) {
return board[0][2];
}
if (pingju(board)) {
return 'q';
}
return 'a';
}
//判断棋盘是否棋子已满,如果满了返回1,未满返回0.
int pingju(char board[max_row][max_col]) {
for (int row = 0; row < max_row; row++) {
for (int col = 0; col < max_col; col++) {
if (board[row][col] == ' ') {
return 0;
}
}
}
return 1;
}
int main() {
char board[max_row][max_col] = {0};//数组初始化为0
char winner = 'n';
init(board); //将棋盘中元素初始化为空格
while (1) {
print(board); //打印棋盘
play(board); //玩家落子
winner = check(board); //判断胜负
if (winner != 'a') {
break;
}
computer(board); //电脑落子
winner = check(board); //判断胜负
if (winner != 'a') {
break;
}
}
if (winner == 'x') {
print(board);
printf("你赢了!");
}
else if (winner == 'o') {
print(board);
printf("你输了!");
}
else if (winner == 'q') {
print(board);
printf("和棋!");
}
return 0;
}