In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
这期内容当中小编将会给大家带来有关C语言实现三子棋实例代码怎么写,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
首先,我将该程序的实现分为3个板块,main,game.h,和game.c;
代码如下:
#define _CRT_SECURE_NO_WARNINGS#include"game.h" void menu(){ printf("****************************************\n"); printf("*********** 1.play 0.exit ************\n"); printf("****************************************\n");} void game(){ char board[ROW][COL] = {0}; Creatboard(board, ROW,COL);//创建棋盘 Displayboard(board,ROW,COL); //初始化棋盘 while (1) { PlayerMove(board,ROW,COL);//玩家移动 Displayboard(board, ROW, COL);//绘制 char i = Result(board, ROW, COL);//判断 switch (i) { case '*': printf("你赢了!"); Sleep(2000); Creatboard(board, ROW, COL); break; case '#': printf("你输了!"); Sleep(2000); Creatboard(board, ROW, COL); break; case 'p': printf("平局!"); Sleep(2000); Creatboard(board, ROW, COL); break; case 'c':; }//我没用鹏哥讲的;用的switch好像更麻烦了 system("cls");//清屏 ComputerMove(board, ROW, COL);//电脑移动 Displayboard(board, ROW, COL); switch (i) { case '*': printf("你赢了!"); Sleep(2000); Creatboard(board, ROW, COL); break; case '#': printf("你输了!"); Sleep(2000); Creatboard(board, ROW, COL); break; case 'p': printf("平局!"); Sleep(2000); Creatboard(board, ROW, COL); break; case 'c':; } }} void control(){ int n = 0; do { menu();//菜单页面 printf("请选择:>"); scanf("%d", &n); switch (n) { case 0: printf("退出游戏\n"); break; case 1: game(); break; default: printf("输入错误,请重新输入\n"); break; } } while (n);} int main(){ srand ((unsigned int)time(NULL));//时间戳 control(); return 0;}
这部分我用了switch之后比较麻烦,也没有结束游戏的退出程序,写的比较乱。
我将所用的头文件都放在了game.h里,着是鹏老师教的写大程序的方式,使程序结构一目了然。
#define ROW 3#define COL 3 #include#include#include//函数声明void Creatboard(char board[ROW][COL], int row, int col);void Displayboard(char board[ROW][COL],int row, int col);void PlayerMove(char board[ROW][COL], int row, int col);void ComputerMove(char board[ROW][COL], int row, int col);char Result(char board[ROW][COL], int row, int col);
对函数的定义是主要事项,判断三子棋当前是否输赢或是平局,以及棋盘绘制的规律,都是在看了几遍后才明白,我这个比较粗糙,有能力的可以自行修改一下绘制的图案。
#define _CRT_SECURE_NO_WARNINGS #include"game.h" void Creatboard(char board[ROW][COL], int row,int col){ int i = 0, j = 0; for (i = 0; i
< row; i++) { for (j = 0; j < col; j++) { board[i][j] = ' '; } }} void Displayboard(char board[ROW][COL], int row, int col)//绘制图案最好用循环,直接用printf按鹏哥的话说比较挫{ int i = 0, j = 0; for (i = 0; i < row; i++) { 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 PlayerMove(char board[ROW][COL], int row, int col)//下棋的位置是用坐标表示的{ int x, y; printf("玩家请下棋:>\n");again: scanf("%d %d", &x, &y); if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; } else { printf("illegal position, please re-enter\n"); goto again; }} void ComputerMove(char board[ROW][COL], int row, int col){ printf("\n"); while (1) { int x = rand() % row;//Generate a random number between 0 and 2 int y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } }} int Full(char board[ROW][COL], int row, int col) int i = 0, j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (board[i][j] == ' ') return 0; } } return 1;} char Result(char board[ROW][COL], int row, int col){ int i = 0, j = 0; for (i = 0; i < row; i++) { if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') return board[0][0]; } for (j = 0; j
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 227
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.