作者前言
🎂 ✨✨✨✨✨✨🍧🍧🍧🍧🍧🍧🍧🎂
🎂 作者介绍: 🎂🎂
🎂 🎉🎉🎉🎉🎉🎉🎉 🎂
🎂作者id:老秦包你会, 🎂
简单介绍:🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂
喜欢学习C语言和python等编程语言,是一位爱分享的博主,有兴趣的小可爱可以来互讨 🎂🎂🎂🎂🎂🎂🎂🎂
🎂个人主页::小小页面🎂
🎂gitee页面:秦大大🎂
🎂🎂🎂🎂🎂🎂🎂🎂
🎂 一个爱分享的小博主 欢迎小可爱们前来借鉴🎂_______________________________________________________
______________________________________________________________________
目录
三子棋
分析
代码
_________________________________________________________
知识小插曲
1.sizeof 是操作符,计算占用内存的大小,单位是字节,计算的是,计算的对象是什么类型都行,
2.strlen是库函数,统计字符串的个数,计算的是\0之前出现的字符个数
小小解说
在上一篇博文中我简单的介绍了数组的创建和使用,也大概的介绍了数组名的作用,下面我们就来写一写这个数组的经典练习,
三子棋
分析
首先,我们玩游戏的时候都会有一个游戏界面,界面里面有菜单,那么我们玩三子棋也必须有菜单,
void meun()
{printf("******** 三子棋 *******\n");printf("**************************\n");printf("******** 0.exit *******\n");printf("******** 1.play *******\n");printf("**************************\n");}
这里就是菜单函数.
然后初始化一个数组,默认设计成空格
void my_initialization(char arr1[][COL], int row, int col)
{int i = 0;for (i = 0; i < row; i++){int j = 0;for (j = 0; j < col; j++){arr1[i][j] = ' ';}}
}
下面我们就要打印一下九空格
我们可以分为几步,进行打印
void my_Carving_a_chessboard(char arr1[][COL], int row, int col)
{int i = 0;printf("-------------\n");for (i = 0; i < row; i++){int j = 0;for (j = 0; j < col; j++){printf("| ");printf("%c ", arr1[i][j]);}printf("|\n");printf("-------------\n");}
}
当我们设计好这些后,就是玩家该下棋了,下棋要注意坐标,数组的范围,以及玩家的所想,下面我来代码演示
玩家和电脑下棋:
在这个过程中我们要知道玩家下过的地方电脑和玩家不能再下,电脑下给地方也一样,如果小可爱熟悉下棋,机会发现下棋者赢最快至少总共要下5个棋子,但为了让小可啊明白我这里就不做出这个判断了,
代码如下:
//电脑下棋
void comp_paly_chess(char arr1[][COL], int row, int col)
{while (1){int x = rand() % 3;int y = rand() % 3;if (arr1[x][y] == ' '){arr1[x][y] = '*';break;}}}void paly_chess(char arr1[][COL], int row, int col)
{//玩家下棋int x ;int y ;int num = 0;while (1){printf("玩家请输入坐标:>");scanf("%d %d", &x, &y);if (arr1[x - 1][y - 1] = ' ' && (x >= 1 && x <= 3) && (y >= 1 && y <= 3)){arr1[x - 1][y - 1] = '#';//判断谁赢了num = is_win(arr1, ROW, COL);if (num){break;}int flat = 0;// 判断是否是平局for (x = 0; x < row; x++){for (y = 0; y < col; y++){if (arr1[x][y] == ' '){flat++;}}}if (!flat){printf("平局\n");break;}//电脑下棋comp_paly_chess(arr1, ROW, COL);my_Carving_a_chessboard(arr1, ROW, COL);//判断谁赢了is_win(arr1, ROW, COL);if (num){break;}}else{printf("输入不符合要求,请重新输入\n");}}}
每下一步我都会判断一次,这样的效率很低,这里为了让大家理解就不做判断了
判断谁赢
代码:
int is_win(char arr1[][COL], int row, int col)
{//行胜int i = 0;for (i = 0; i < row; i++){int count = 0;int count1 = 0;int j = 0;for (j = 0; j < col; j++){if (arr1[i][j] == '#')count++;else if (arr1[i][j] == '*')count1++;}if (count == 3){printf("玩家胜利\n");return 1;}else if (count1 == 3){printf("电脑胜利\n");return 2;}}//列胜for (i = 0; i < row; i++){int count = 0;int count1 = 0;int j = 0;for (j = 0; j < col; j++){if (arr1[j][i] == '#')count++;else if (arr1[j][i] == '*')count1++;}if (count == 3){printf("玩家胜利\n");return 1;}else if (count1 == 3){printf("电脑胜利\n");return 2;}}//对角for (i = 0; i < row; i++){int count = 0;int count1 = 0;int j = 0;if (arr1[i][i] == '#')count++;else if (arr1[i][i] == '*')count1++;if (count == 3){printf("玩家胜利\n");return 1;}else if (count1 == 3){printf("电脑胜利\n");return 2;}}//斜角for (i = 0; i < row; i++){int count = 0;int count1 = 0;int j = 0;for (j = col - 1; j >= 0; j--){if (arr1[i][j] == '#')count++;else if (arr1[i][j] == '*')count1++;}if (count == 3){printf("玩家胜利\n");return 1;}else if (count1 == 3){printf("电脑胜利\n");return 2;}}return 0;
}
这里我是根据上图是行和列对角一一判断的,有很多重复的代码,但这样可以让小可爱们能理解,
下面我分享一下全部代码
头文件.h:
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 3
#define COL 3//游戏
void game();
//数组初始化
void my_initialization(char arr1[][COL], int row, int col);
//雕刻棋盘
void my_Carving_a_chessboard(char arr1[][COL], int row, int col);
//玩家和电脑下棋
void paly_chess(char arr1[][COL], int row, int col);
//电脑下棋
void comp_paly_chess(char arr1[][COL], int row, int col);
三子棋函数.c
#include "三子棋.h"
void game()
{char arr1[ROW][COL];//数组初始化my_initialization(arr1, ROW, COL);// 雕刻棋盘my_Carving_a_chessboard(arr1, ROW, COL);//打印棋盘//my_print_chess(arr1, ROW, COL);//电脑下棋//comp_paly_chess(arr1, ROW, COL);//玩家下棋和电脑下棋paly_chess(arr1, ROW, COL);}void my_initialization(char arr1[][COL], int row, int col)
{int i = 0;for (i = 0; i < row; i++){int j = 0;for (j = 0; j < col; j++){arr1[i][j] = ' ';}}
}void my_Carving_a_chessboard(char arr1[][COL], int row, int col)
{int i = 0;printf("-------------\n");for (i = 0; i < row; i++){int j = 0;for (j = 0; j < col; j++){printf("| ");printf("%c ", arr1[i][j]);}printf("|\n");printf("-------------\n");}
}
//电脑下棋
void comp_paly_chess(char arr1[][COL], int row, int col)
{while (1){int x = rand() % 3;int y = rand() % 3;if (arr1[x][y] == ' '){arr1[x][y] = '*';break;}}}void paly_chess(char arr1[][COL], int row, int col)
{//玩家下棋int x ;int y ;int num = 0;while (1){printf("玩家请输入坐标:>");scanf("%d %d", &x, &y);if (arr1[x - 1][y - 1] = ' ' && (x >= 1 && x <= 3) && (y >= 1 && y <= 3)){arr1[x - 1][y - 1] = '#';//判断谁赢了num = is_win(arr1, ROW, COL);if (num){break;}int flat = 0;// 判断是否是平局for (x = 0; x < row; x++){for (y = 0; y < col; y++){if (arr1[x][y] == ' '){flat++;}}}if (!flat){printf("平局\n");break;}//电脑下棋comp_paly_chess(arr1, ROW, COL);my_Carving_a_chessboard(arr1, ROW, COL);//判断谁赢了is_win(arr1, ROW, COL);if (num){break;}}else{printf("输入不符合要求,请重新输入\n");}}}
int is_win(char arr1[][COL], int row, int col)
{//行胜int i = 0;for (i = 0; i < row; i++){int count = 0;int count1 = 0;int j = 0;for (j = 0; j < col; j++){if (arr1[i][j] == '#')count++;else if (arr1[i][j] == '*')count1++;}if (count == 3){printf("玩家胜利\n");return 1;}else if (count1 == 3){printf("电脑胜利\n");return 2;}}//列胜for (i = 0; i < row; i++){int count = 0;int count1 = 0;int j = 0;for (j = 0; j < col; j++){if (arr1[j][i] == '#')count++;else if (arr1[j][i] == '*')count1++;}if (count == 3){printf("玩家胜利\n");return 1;}else if (count1 == 3){printf("电脑胜利\n");return 2;}}//对角for (i = 0; i < row; i++){int count = 0;int count1 = 0;int j = 0;if (arr1[i][i] == '#')count++;else if (arr1[i][i] == '*')count1++;if (count == 3){printf("玩家胜利\n");return 1;}else if (count1 == 3){printf("电脑胜利\n");return 2;}}//斜角for (i = 0; i < row; i++){int count = 0;int count1 = 0;int j = 0;for (j = col - 1; j >= 0; j--){if (arr1[i][j] == '#')count++;else if (arr1[i][j] == '*')count1++;}if (count == 3){printf("玩家胜利\n");return 1;}else if (count1 == 3){printf("电脑胜利\n");return 2;}}return 0;
}
三子棋模板
#include "三子棋.h"
void menu(void)
{printf("******** 三子棋 ******\n");printf("*********************************\n");printf("******** 0.exit ******\n");printf("******** 1.paly ******\n");printf("*********************************\n");}
int main()
{srand((unsigned)time(NULL));int input = 0;do{menu();printf("你已经在游戏界面,请选择:>");scanf("%d", &input);switch (input){case 1:printf("开始游戏\n");game();break;case 0:printf("退出游戏\n");break;default :printf("你的输入不符合规则,请重新输入\n");}} while (input);return 0;
}
总结:
三子棋的思路:
第一步游戏界面
第二步:打印棋盘
第三步:玩家和电脑对弈
这三步概纳了三子棋的主要思路,有不懂的小可爱可以私聊我