怎么利用C语言实现AI五子棋游戏
本篇内容介绍了“怎么利用C语言实现AI五子棋游戏”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
目录
一.如何实现
二.实现代码及分析
(1)菜单的制作
(2)棋盘的初始化和打印
(3)玩家下子
(4)电脑下子
(5)判断输赢
三.整个代码
(1)test.c文件下代码:
(2)game.c下的代码
(3)game.h下的代码
四.具体效果
棋盘打印以及选择先后手
玩家和电脑下子
判断输赢
一.如何实现
说明:由于本文只是对初学C语言的人学习,所以将不会涉及任何算法,电脑将采用随机下子的方式。(后期会为大家介绍Alpha-Beta剪枝算法实现人工智能AI)
主要部分:
(1)菜单
(2)打印棋盘
(3)玩家下子
(4)电脑下子
(5)判断输赢
二.实现代码及分析
(1)菜单的制作
运用do…while循环调用菜单,根据用户选择实现玩游戏和退出游戏
(2)棋盘的初始化和打印
棋盘采用标准的15*15的格子,我们可以宏定义ROW和COL分别为15和15来表示行和列。
分别封装两个函数对棋盘进行初始化和打印
初始化:(这里将棋盘初始化为空格,可初始化为其他)
打印棋盘:
效果如下:
(3)玩家下子
void PlayerMove(char board[ROW][COL], int row, int col) {int x, y;while (1) {printf("玩家走:\n");printf("请输入坐标:\n");scanf("%d %d", &x, &y);if (x >= 1 && x <= row && y >= 1 && y <= col) {if (board[x - 1][y - 1] == ' ') {board[x - 1][y - 1] = 'O';break;}else {printf("坐标被占用,请重新输入\n");}}else {printf("坐标非法,请重新输入\n");}}}
玩家下子主要采用的思路是,判断棋盘上是否有子,已经输入的坐标是否合法。
(4)电脑下子
void ComputerMove(char board[ROW][COL], int row, int col) {int x, y;printf("电脑走\n");while (1) {x = rand() % row;y = rand() % col;if (board[x][y] == ' ') {board[x][y] = 'X';break;}}}
与玩家下子相同,此处采用的随机下子,后期可进行优化(比如:极大极小值算法、Alpha-Beta剪枝算法等)
(5)判断输赢
通过遍历整个棋盘,观察是否有连五子情况出现,代码如下:
char iswin(char board[ROW][COL], int row, int col) {//行int ren=0,dian=0,i,j;for (i = 0;i < row;i++) {ren = 0;for (j = 0;j < col;j++) {if (board[i][j] == 'O')ren++;elseren = 0;if (ren >= 5)return 'o';}}for (i = 0;i < row;i++) {dian = 0;for (j = 0;j < col;j++) {if (board[i][j] == 'X')dian++;elsedian = 0;if (dian >= 5)return 'x';}}//列ren = dian = 0;for (i = 0;i < row;i++) {ren = 0;for (j = 0;j < col;j++) {if (board[j][i] == 'O')ren++;elseren = 0;if (ren >= 5)return 'o';}}for (i = 0;i < row;i++) {dian = 0;for (j = 0;j < col;j++) {if (board[j][i] == 'X')dian++;elsedian = 0;if (dian >= 5)return 'x';}}//右下ren = dian =i=j= 0;for (int k = 0;k < row;k++) {i = k;j = 0;ren = 0;while (i < row && j < col) {if (board[i][j] == 'O') {ren++;}else {ren = 0;}i++;j++;if (ren >= 5)return 'o';}}i = j = 0;for (int k = 0;k < row;k++) {i = k;j = 0;dian = 0;while (i < row && j < col) {if (board[i][j] == 'X') {dian++;}elsedian = 0;i++;j++;if (dian >= 5)return 'x';}}//右上for (int k = row;k >=0;k--) {j = col;i = k;ren = 0;while (i >=0 && j >=0) {if (board[i][j] == 'O') {ren++;}elseren = 0;i--;j--;if (ren >= 5)return 'o';}}for (int k = row;k >= 0;k--) {i = k;j = col;dian = 0;while (i >= 0 && j >= 0) {if (board[i][j] == 'X') {dian++;}elsedian = 0;i--;j--;if (dian >= 5)return 'o';}}for (int k = row;k >= 0;k--) {i = k;j = 0;dian = 0;while (i >= 0 && j <col) {if (board[i][j] == 'O') {dian++;}elsedian = 0;i--;j++;if (dian >= 5)return 'o';}}for (int k = row;k >= 0;k--) {i = k;j = 0;dian = 0;while (i >= 0 && j <col) {if (board[i][j] == 'X') {dian++;}elsedian = 0;i--;j++;if (dian >= 5)return 'x';}}for (int k = 0;k < row;k++) {i = k;j = col;dian = 0;while (i < row && j >= 0) {if (board[i][j] == 'O') {dian++;}elsedian = 0;i++;j--;if (dian >= 5)return 'o';}}for (int k = 0;k <row;k++) {i = k;j = col;dian = 0;while (i <row && j >= 0) {if (board[i][j] == 'X') {dian++;}elsedian = 0;i++;j--;if (dian >= 5)return 'x';}}for (int k = row;k >= 0;k--) {i = k;j = 0;dian = 0;while (i >= 0 && j <col) {if (board[i][j] == 'O') {dian++;}elsedian = 0;i--;j++;if (dian >= 5)return 'o';}}for (int k = row;k >= 0;k--) {i = k;j = 0;dian = 0;while (i >= 0 && j <col) {if (board[i][j] == 'X') {dian++;}elsedian = 0;i--;j++;if (dian >= 5)return 'x';}}for (int k = 0;k < row;k++) {i = k;j = col;dian = 0;while (i < row && j >= 0) {if (board[i][j] == 'O') {dian++;}elsedian = 0;i++;j--;if (dian >= 5)return 'o';}}for (int k = 0;k <row;k++) {i = k;j = col;dian = 0;while (i <row && j >= 0) {if (board[i][j] == 'X') {dian++;}elsedian = 0;i++;j--;if (dian >= 5)return 'x';}}return 'c';//左}
三.整个代码
(1)test.c文件下代码:
#define _CRT_SECURE_NO_WARNINGS 1#include"game.h"void menu() {printf("**************************\n");printf("******* 1. play *********\n");printf("******* 0. exit *********\n");printf("**************************\n");}void game() {//棋盘数组char board[ROW][COL],ret;//初始化棋盘InitBoard(board, ROW, COL);//打印棋盘PrintBoard(board, ROW, COL);//下棋int a;printf("1.先手 0.后手");scanf("%d", &a);if(a==1)PlayerMove(board, ROW, COL);elseComputerMove(board, ROW, COL);while (1) {if (a == 0) {//PlayerMove(board, ROW, COL);PlayerMove(board, ROW, COL);PrintBoard(board, ROW, COL);ret=iswin(board, ROW, COL);if (ret != 'c') {break;}//ComputerMove(board, ROW, COL);ComputerMove(board, ROW, COL);PrintBoard(board, ROW, COL);ret = iswin(board, ROW, COL);if (ret != 'c') {break;}}else {//ComputerMove(board, ROW, COL);ComputerMove(board, ROW, COL);PrintBoard(board, ROW, COL);ret = iswin(board, ROW, COL);if (ret != 'c') {break;}//PlayerMove(board, ROW, COL);PlayerMove(board, ROW, COL);PrintBoard(board, ROW, COL);ret = iswin(board, ROW, COL);if (ret != 'c') {break;}}}if (ret == 'o')printf("玩家赢\n");else if (ret == 'x')printf("电脑赢\n");elseprintf("平局\n");}int main() {int input;srand((unsigned int)time(NULL));do {menu();scanf("%d", &input);switch (input) {case 1:game();break;case 0:printf("退出游戏\n");break;default:printf("选择错误\n");break;}} while (input);return 0;}
(2)game.c下的代码
#define _CRT_SECURE_NO_WARNINGS 1#include"game.h"void InitBoard(char board[ROW][COL], int row, int col) {for (int i = 0;i < row;i++) {for (int j = 0;j < col;j++) {board[i][j] = ' ';}}}void PrintBoard(char board[ROW][COL], int row, int col) {for (int i = 0;i < row;i++) {printf(" ");printf("%2d", i+1);}printf("\n");for (int i = 0;i < row;i++) {//打印数据printf("%2d", i + 1);for (int j = 0;j < col;j++) {printf(" %c ", board[i][j]);if (j < col - 1)printf("|");}printf("\n");//打印分割行if (i < row - 1) {printf(" ");for (int 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;while (1) {printf("玩家走:\n");printf("请输入坐标:\n");scanf("%d %d", &x, &y);if (x >= 1 && x <= row && y >= 1 && y <= col) {if (board[x - 1][y - 1] == ' ') {board[x - 1][y - 1] = 'O';break;}else {printf("坐标被占用,请重新输入\n");}}else {printf("坐标非法,请重新输入\n");}}}void ComputerMove(char board[ROW][COL], int row, int col) {int x, y;printf("电脑走\n");while (1) {x = rand() % row;y = rand() % col;if (board[x][y] == ' ') {board[x][y] = 'X';break;}}}char iswin(char board[ROW][COL], int row, int col) {//行int ren=0,dian=0,i,j;for (i = 0;i < row;i++) {ren = 0;for (j = 0;j < col;j++) {if (board[i][j] == 'O')ren++;elseren = 0;if (ren >= 5)return 'o';}}for (i = 0;i < row;i++) {dian = 0;for (j = 0;j < col;j++) {if (board[i][j] == 'X')dian++;elsedian = 0;if (dian >= 5)return 'x';}}//列ren = dian = 0;for (i = 0;i < row;i++) {ren = 0;for (j = 0;j < col;j++) {if (board[j][i] == 'O')ren++;elseren = 0;if (ren >= 5)return 'o';}}for (i = 0;i < row;i++) {dian = 0;for (j = 0;j < col;j++) {if (board[j][i] == 'X')dian++;elsedian = 0;if (dian >= 5)return 'x';}}//右下ren = dian =i=j= 0;for (int k = 0;k < row;k++) {i = k;j = 0;ren = 0;while (i < row && j < col) {if (board[i][j] == 'O') {ren++;}else {ren = 0;}i++;j++;if (ren >= 5)return 'o';}}i = j = 0;for (int k = 0;k < row;k++) {i = k;j = 0;dian = 0;while (i < row && j < col) {if (board[i][j] == 'X') {dian++;}elsedian = 0;i++;j++;if (dian >= 5)return 'x';}}//右上for (int k = row;k >=0;k--) {j = col;i = k;ren = 0;while (i >=0 && j >=0) {if (board[i][j] == 'O') {ren++;}elseren = 0;i--;j--;if (ren >= 5)return 'o';}}for (int k = row;k >= 0;k--) {i = k;j = col;dian = 0;while (i >= 0 && j >= 0) {if (board[i][j] == 'X') {dian++;}elsedian = 0;i--;j--;if (dian >= 5)return 'o';}}for (int k = row;k >= 0;k--) {i = k;j = 0;dian = 0;while (i >= 0 && j <col) {if (board[i][j] == 'O') {dian++;}elsedian = 0;i--;j++;if (dian >= 5)return 'o';}}for (int k = row;k >= 0;k--) {i = k;j = 0;dian = 0;while (i >= 0 && j <col) {if (board[i][j] == 'X') {dian++;}elsedian = 0;i--;j++;if (dian >= 5)return 'x';}}for (int k = 0;k < row;k++) {i = k;j = col;dian = 0;while (i < row && j >= 0) {if (board[i][j] == 'O') {dian++;}elsedian = 0;i++;j--;if (dian >= 5)return 'o';}}for (int k = 0;k <row;k++) {i = k;j = col;dian = 0;while (i <row && j >= 0) {if (board[i][j] == 'X') {dian++;}elsedian = 0;i++;j--;if (dian >= 5)return 'x';}}return 'c';//左}
(3)game.h下的代码
#pragma once#define ROW 15#define COL 15#include<stdio.h>#include<stdlib.h>#include<time.h>//初始化棋盘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);
四.具体效果
1.棋盘打印以及选择先后手
2.玩家和电脑下子
3.判断输赢
此处我们看到玩家赢了,可是电脑实在太蠢了,所以还有很多地方需要添加的
“怎么利用C语言实现AI五子棋游戏”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341