赞
踩
本篇适用于C语言初学者。
目录
- #include<stdio.h>
- #include <stdlib.h>
- #include <time.h>
-
- #define ROW 3
- #define COL 3
-
- void InitBorad(char borad[ROW][COL], int row, int col);
-
- void DisplayBorad(char borad[ROW][COL], int row, int col);
-
- void PlayerMove(char borad[ROW][COL], int row, int col);
-
- void ComputerMove(char borad[ROW][COL], int row, int col);
-
- char IsWin(char borad[ROW][COL], int row, int col);
-
-
- void InitBorad(char borad[ROW][COL], int row, int col)
- {
- int i = 0;
- for (i = 0; i < row; i++)
- {
- int j = 0;
- for (j = 0; j < col; j++)
- {
- borad[i][j] = ' ';
- }
- }
- }
-
- void DisplayBorad(char borad[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 ", borad[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 borad[ROW][COL], int row, int col)
- {
- int x = 0;
- int y = 0;
- while (1)
- {
- printf("玩家下棋:>");
- scanf("%d %d", &x, &y);
- if (x >= 1 && x <= row && y >= 1 && y <= col)
- {
- if (borad[x - 1][y - 1] == ' ')
- {
- borad[x - 1][y - 1] = '*';
- break;
- }
- else
- {
- printf("该位置已被占有,请重新选择!\n");
- }
- }
- else
- {
- printf("输入错误,请重新选择!\n");
- }
- }
- }
-
- void ComputerMove(char borad[ROW][COL], int row, int col)
- {
- printf("电脑下棋:>\n");
- while (1)
- {
- int x = rand() % row;
- int y = rand() % col;
- if (borad[x][y] == ' ')
- {
- borad[x][y] = '#';
- break;
- }
- }
- }
-
- //'*'玩家赢
- //'#'电脑赢
- //'C'继续游戏
- //'Q'平局
- //char IsWin(char borad[ROW][COL], int row, int col)
- //{
- // int i = 0;
- // for (i = 0; i < row; i++)
- // {
- // if (borad[i][0] == borad[i][1] && borad[i][1] == borad[i][2] && borad[i][0] != ' ')
- // return borad[i][0];
- // }
- // for (i = 0; i < col; i++)
- // {
- // if (borad[0][i] == borad[1][i] && borad[1][i] == borad[2][i] && borad[0][i] != ' ')
- // return borad[0][i];
- // }
- // if (borad[0][0] == borad[1][1] && borad[1][1] == borad[2][2] && borad[0][0] != ' ')
- // return borad[0][0];
- // if (borad[0][2] == borad[1][1] && borad[1][1] == borad[2][0] && borad[0][2] != ' ')
- // return borad[0][2];
- // for (i = 0; i < row; i++)
- // {
- // int j = 0;
- // for (j = 0; j < col; j++)
- // {
- // if (borad[i][j] == ' ')
- // return 'C';
- // }
- // }
- // return 'Q';
- //}
-
- //判断优化
- char IsWin(char borad[ROW][COL], int row, int col)
- {
- int i = 0;
- int flag = 1;
- //行
- for (i = 0; i < row; i++)
- {
- flag = 1;
- int n = 0;
- if (borad[i][0] != ' ')
- {
- while (n < row - 1)
- {
- if (borad[i][n] != borad[i][n + 1])
- {
- flag = 0;
- break;
- }
- n++;
- }
- if (flag)
- return borad[i][0];
- }
- }
- //列
- for (i = 0; i < col; i++)
- {
- flag = 1;
- int n = 0;
- if (borad[0][i] != ' ')
- {
- while (n < col - 1)
- {
- if (borad[n][i] != borad[n + 1][i])
- {
- flag = 0;
- break;
- }
- n++;
- }
- if (flag)
- return borad[0][i];
- }
- }
-
- //左对角线
- if (borad[0][0] != ' ')
- {
- flag = 1;
- int n = 0;
- while (n < row - 1)
- {
- if (borad[n][n] != borad[n + 1][n + 1])
- {
- flag = 0;
- break;
- }
- n++;
-
- }
- if (flag)
- return borad[0][0];
- }
- //右对角线
- int r = 0;
- int c = col - 1;
- if (borad[r][c] != ' ')
- {
- flag = 1;
- while (r < row - 1)
- {
- if (borad[r][c] != borad[r + 1][c - 1])
- {
- flag = 0;
- break;
- }
- r++;
- c--;
- }
- if (flag)
- return borad[r][c];
- }
- //继续
- for (i = 0; i < row; i++)
- {
- int j = 0;
- for (j = 0; j < col; j++)
- {
- if (borad[i][j] == ' ')
- return 'C';
- }
- }
- //平局
- return 'Q';
- }
-
-
- void menu()
- {
- printf("**********************************\n");
- printf("********** 1.play ***********\n");
- printf("********** 0.exit ***********\n");
- printf("**********************************\n");
- }
-
-
- void game()
- {
- char borad[ROW][COL] = { 0 };
- char ch = 0;
- //初始化
- InitBorad(borad, ROW, COL);
- //打印棋盘
- DisplayBorad(borad, ROW, COL);
- //玩家下棋
- while (1)
- {
- //玩家下棋
- PlayerMove(borad, ROW, COL);
- DisplayBorad(borad, ROW, COL);
- //判断
- ch = IsWin(borad, ROW, COL);
- if (ch != 'C')
- break;
- //电脑下棋
- ComputerMove(borad, ROW, COL);
- DisplayBorad(borad, ROW, COL);
- ch = IsWin(borad, ROW, COL);
- if (ch != 'C')
- break;
- }
- if (ch == '*')
- printf("玩家赢!\n");
- else if (ch == '#')
- printf("电脑赢!\n");
- else if (ch == 'Q')
- printf("平局!\n");
- }
-
- int main()
- {
- int input = 0;
- srand((unsigned int)time(NULL));
- 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;
- }
- #include<stdio.h>
- #include <stdlib.h>
- #include <time.h>
-
- //定义表示符常量分别表示行(ROW)和列(COL)
- #define ROW 3
- #define COL 3
- //初始化棋盘
- void InitBorad(char borad[ROW][COL], int row, int col);
- //打印棋盘
- void DisplayBorad(char borad[ROW][COL], int row, int col);
- //玩家移动
- void PlayerMove(char borad[ROW][COL], int row, int col);
- //电脑移动
- void ComputerMove(char borad[ROW][COL], int row, int col);
- //判断输赢实现
- char IsWin(char borad[ROW][COL], int row, int col);
- //游戏菜单
- void menu()
- {
- printf("**********************************\n");
- printf("********** 1.play ***********\n");
- printf("********** 0.exit ***********\n");
- printf("**********************************\n");
- }
-
-
- void game()
- {
- //定义一个模拟棋盘的二维数组
- char borad[ROW][COL] = { 0 };
-
- char ch = 0;
- //调用函数初始化棋盘(二维数组)
- InitBorad(borad, ROW, COL);
- //调用函数打印棋盘(二维数组)
- DisplayBorad(borad, ROW, COL);
- while (1)
- {
- //调用函数玩家下棋
- PlayerMove(borad, ROW, COL);
- DisplayBorad(borad, ROW, COL);
- //进行输赢判断
- ch = IsWin(borad, ROW, COL);
- //C表示继续游戏
- if (ch != 'C')
- break;
- //调用函数电脑下棋
- ComputerMove(borad, ROW, COL);
- //进行输赢判断
- DisplayBorad(borad, ROW, COL);
- ch = IsWin(borad, ROW, COL);
- if (ch != 'C')
- break;
- }
- // * 表示玩家赢
- if (ch == '*')
- printf("玩家赢!\n");
- // # 表示电脑赢
- else if (ch == '#')
- printf("电脑赢!\n");
- //Q表示平局
- else if (ch == 'Q')
- printf("平局!\n");
- }
- //主函数(程序入口)
- int main()
- {
- //存储选择的变量
- int input = 0;
- //使用time()库(time.h)函数和srand()库(stdlib.h)函数确定随机数生成起点,将time()返回值强制转换成无符号整型(unsigned int)。
- srand((unsigned int)time(NULL));
- 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;
- }
- //利用嵌套循环实现对棋盘(二维数组)的初始化,初始化为空格
- void InitBorad(char borad[ROW][COL], int row, int col)
- {
- int i = 0;
- for (i = 0; i < row; i++)
- {
- int j = 0;
- for (j = 0; j < col; j++)
- {
- borad[i][j] = ' ';
- }
- }
- }
- //利用嵌套循环实现对棋盘的打印
- void DisplayBorad(char borad[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 ", borad[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 borad[ROW][COL], int row, int col)
- {
- int x = 0;
- int y = 0;
- while (1)
- {
- printf("玩家下棋:>");
- //输入想要下棋地方的坐标:行从1开始,列从1开始(数组下标从0开始,因此需要对输入数据减1)
- scanf("%d %d", &x, &y);
- //对输入信息所对应的坐标进行判断和处理
- if (x >= 1 && x <= row && y >= 1 && y <= col)
- {
- if (borad[x - 1][y - 1] == ' ')
- {
- borad[x - 1][y - 1] = '*';
- break;
- }
- else
- {
- printf("该位置已被占有,请重新选择!\n");
- }
- }
- else
- {
- printf("输入错误,请重新选择!\n");
- }
- }
- }
- //电脑下棋实现细节
- void ComputerMove(char borad[ROW][COL], int row, int col)
- {
- printf("电脑下棋:>\n");
- while (1)
- {
- //代码主体部分已通过srand()设置随机数生成起点,此处调用rand()库(stdlib.h)函数生成两个随机数,分别作为二维数组的行下标和列下标
- int x = rand() % row;
- int y = rand() % col;
- //对坐标进行判断和处理
- if (borad[x][y] == ' ')
- {
- borad[x][y] = '#';
- break;
- }
- }
- }
-
- //'*'玩家赢
- //'#'电脑赢
- //'C'继续游戏
- //'Q'平局
- //此注释内容为原始判断方法实现内容
- //char IsWin(char borad[ROW][COL], int row, int col)
- //{
- // int i = 0;
- // for (i = 0; i < row; i++)
- // {
- // if (borad[i][0] == borad[i][1] && borad[i][1] == borad[i][2] && borad[i][0] != ' ')
- // return borad[i][0];
- // }
- // for (i = 0; i < col; i++)
- // {
- // if (borad[0][i] == borad[1][i] && borad[1][i] == borad[2][i] && borad[0][i] != ' ')
- // return borad[0][i];
- // }
- // if (borad[0][0] == borad[1][1] && borad[1][1] == borad[2][2] && borad[0][0] != ' ')
- // return borad[0][0];
- // if (borad[0][2] == borad[1][1] && borad[1][1] == borad[2][0] && borad[0][2] != ' ')
- // return borad[0][2];
- // for (i = 0; i < row; i++)
- // {
- // int j = 0;
- // for (j = 0; j < col; j++)
- // {
- // if (borad[i][j] == ' ')
- // return 'C';
- // }
- // }
- // return 'Q';
- //}
-
- //判断优化
- //'*'玩家赢
- //'#'电脑赢
- //'C'继续游戏
- //'Q'平局
- char IsWin(char borad[ROW][COL], int row, int col)
- {
- int i = 0;
- int flag = 1;
- //行的判断实现
- //循环遍历行
- for (i = 0; i < row; i++)
- {
- //作为实现判断依据的变量,假设游戏已经结束。
- flag = 1;
-
- //作为遍历一行内容的变量
- int n = 0;
- if (borad[i][0] != ' ')
- {
- while (n < row - 1)
- {
- if (borad[i][n] != borad[i][n + 1])
- {
- //如果本行三个落棋地方不一样,置flag变量为0,表示游戏未结束
- flag = 0;
- break;
- }
- n++;
- }
- //如果游戏结束,返回达成胜利条件坐标所存的字符作为判断玩家or电脑赢的依据
- if (flag)
- return borad[i][0];
- }
- }
- //列
- //具体实现细节同行
- for (i = 0; i < col; i++)
- {
- flag = 1;
- int n = 0;
- if (borad[0][i] != ' ')
- {
- while (n < col - 1)
- {
- if (borad[n][i] != borad[n + 1][i])
- {
- flag = 0;
- break;
- }
- n++;
- }
- //如果游戏结束,返回达成胜利条件坐标所存的字符作为判断玩家or电脑赢的依据
- if (flag)
- return borad[0][i];
- }
- }
-
- //左对角线
- //左对角线规律:行加一,列加一。 剩余实现原理同行
- if (borad[0][0] != ' ')
- {
- flag = 1;
- int n = 0;
- while (n < row - 1)
- {
- if (borad[n][n] != borad[n + 1][n + 1])
- {
- flag = 0;
- break;
- }
- n++;
-
- }
- //如果游戏结束,返回达成胜利条件坐标所存的字符作为判断玩家or电脑赢的依据
- if (flag)
- return borad[0][0];
- }
- //右对角线
- //右对角线规律:开始时行为零(数组)列为col - 1,此后行加一,列减一,直至行为数组下标最大值或者列为0。 剩余实现原理同行
- int r = 0;
- int c = col - 1;
- if (borad[r][c] != ' ')
- {
- flag = 1;
- while (r < row - 1)
- {
- if (borad[r][c] != borad[r + 1][c - 1])
- {
- flag = 0;
- break;
- }
- r++;
- c--;
- }
- //如果游戏结束,返回达成胜利条件坐标所存的字符作为判断玩家or电脑赢的依据
- if (flag)
- return borad[r][c];
- }
- //继续
- //前面判断并未生效,因此说明此时未有赢家诞生,对数组进行遍历,看看是否还有可以落棋的地方来判断是否游戏继续或者平局
- for (i = 0; i < row; i++)
- {
- int j = 0;
- for (j = 0; j < col; j++)
- {
- //如果有 ' ' 证明有地方可以落棋,返回 'C' 表示游戏继续
- if (borad[i][j] == ' ')
- return 'C';
- }
- }
- //平局
- //如果上述判断均未生效,且遍历数组发现没有可以落棋的坐标返回'Q'表示平局
- return 'Q';
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。