图片笔记如上
———————————————————————————————————————————
文字笔记:
三子棋:
1.结尾加n
2.多加注释
3.单字符用单引号
4.比几小就模几
———————————————————————————————————————————代码展示:
1.头文件
#pragma once //头文件包含 #include#include #include //符号的定义 #define ROW 3 #define COL 3 //声明函数 //初始化棋盘 void InitBoard(char Board[ROW][COL],int row,int col); //打印棋盘 void PrintBoard(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 Iswin(char Board[ROW][COL], int row, int col); //四种结局 //*玩家 #电脑 c继续 f满了平局
2.函数的实现部分
#define _CRT_SECURE_NO_WARNINGS 1 #include "head.h" //实现函数 int Isfull(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++) { if (Board[i][j] == ' ') return 0; } } return 1; }//for内部只要一次就够了,外部要满足每一次 char Iswin(char Board[ROW][COL], int row, int col) { //判断棋盘是否满了 int ret = Isfull(Board, row, col); if (ret == 1) return 'f'; //行 int i = 0; int j = 0; for (i = 0; i < row; i++) { if (Board[i][0] == Board[i][1] && Board[i][0] == Board[i][2] && Board[i][0] != ' ') { return(Board[i][0]); } } //列 for (j = 0; j < row; j++) { if (Board[0][j] == Board[1][j] && Board[0][j] == Board[2][j] && Board[0][j] != ' ') { return(Board[0][j]); } } //对角线 if (Board[0][0] == Board[1][1] && Board[0][0] == Board[2][2] && Board[0][0] != ' ') return Board[0][0]; if (Board[2][0] == Board[1][1] && Board[1][1] == Board[0][2] && Board[2][0] != ' ') return Board[2][0]; return 'c'; } void Playermove(char Board[ROW][COL], int row, int col) { printf("玩家请输入棋子坐标:>"); int x = 0; int y = 0; while (1) { scanf("%d %d", &x, &y); //判断落子是否合法 if (x > 0 && x <= row && y > 0 && y <= col) { //判断落子位置是否被占用 if (Board[x - 1][y - 1] ==' ')//-1符合一般人习惯 { Board[x - 1][y - 1] = '*'; break; } else printf("坐标已经被占用,请重新输入坐标n"); } else printf("坐标不合法,请重新输入n"); } } void Computermove(char Board[ROW][COL], int row, int col) { printf("电脑走:>n"); while (1) { int x = rand() % row; int y = rand() % col;//随机函数头文件stdlid,比小就模几 if (Board[x][y] == ' ')//电脑不需要-1 { Board[x][y] = '#'; break; } } } void InitBoard(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 PrintBoard(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++) { printf(" %c ", Board[i][j]); if (j < col - 1) printf("|"); } printf("n"); if (i < row - 1) { int j = 0; for (j = 0; j < col; j++) { printf("---"); if (j < col - 1) printf("|"); } printf("n"); } } }
3.函数主体
#define _CRT_SECURE_NO_WARNINGS 1 #include "head.h" void game() { char ret =0;//接受游戏状态 //打印棋盘 char Board[ROW][COL]; //存储数据 InitBoard(Board, ROW, COL); //初始化棋盘 PrintBoard(Board, ROW, COL); //打印 while (1) { //玩家走 Playermove(Board, ROW, COL); PrintBoard(Board, ROW, COL); ret = Iswin(Board, ROW, COL); if (ret != 'c') break; //电脑走 Computermove(Board, ROW, COL); PrintBoard(Board, ROW, COL); ret = Iswin(Board, ROW, COL); if (ret != 'c') break;//函数外定义的函数,在函数内赋的值在函数外依然存在 } if (ret == '*') { printf("玩家胜利n"); } else if (ret == '#') { printf("电脑胜利n"); } else { printf("平局n"); } PrintBoard(Board,ROW,COL); } void Menu() { printf("######################################################n"); printf("################### 1.play ###################n"); printf("################### 0.exit ###################n"); printf("######################################################n"); } 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; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)