C语言实现三子棋(井字棋)算法
作者:爱编辑
这篇文章主要为大家详细介绍了C语言实现三子棋(井字棋)算法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了C语言实现三子棋算法,供大家参考,具体内容如下
游戏文件主干(test.c):
#include"game.h" void menu()//游戏菜单 { printf("************************************************\n"); printf("********** 1.play *********\n"); printf("********** 0.exit *********\n"); printf("************************************************\n"); } void game()//游戏玩法实现主体(粗略) { printf("三子棋:>\n"); char board[LINE][ROW]; Init_checkerboard(board, LINE, ROW);//初始化棋盘 Print_checkerboard(board, LINE, ROW);//打印棋盘 char ret = 0; while (1)//条件为1可以一直循环 { Player_game(board, LINE, ROW);//玩家行动 Print_checkerboard(board, LINE, ROW);//玩家行动后的棋盘 ret = Iswin(board, LINE, ROW);//判断是否出现胜负 if (ret != 'c')//出现胜负就跳出循环 { break; } Commputer_game(board, LINE, ROW);//电脑回合 Print_checkerboard(board, LINE, ROW);//电脑行动后的棋盘 ret = Iswin(board, LINE, ROW);//判断是否出现胜负 if (ret != 'c') { break; } } if (ret == '*') { printf("玩家胜利!\n"); } else if (ret == '#') { printf("电脑胜利!\n"); } else { 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("输入数字无效,请重新输入"); break; } } while (input); return 0; }
游戏主干(game.c):
#include"game.h"//游戏主干(详细) void Init_checkerboard(char board[LINE] [ROW], int line, int row) { int i = 0; int j = 0; for ( i = 0; i <line; i++) { for ( j = 0; j <row; j++) { board[i][j]=' '; } } }//棋盘的初始化 void Print_checkerboard(char board[LINE][ROW], int line, int row) { int i = 0; for (i = 0; i < line; i++) { int j = 0; for (j = 0; j < line; j++) { printf(" %c ",board[i][j]); if (j < row - 1) { printf("|"); } } printf("\n"); if (i < row - 1) { for (j = 0; j < row; j++) { printf("---"); if (j < row - 1) { printf("|"); } } } printf("\n"); } }//生成3-3的棋盘 void Player_game(char board[LINE][ROW], int line, int row) { printf("玩家下棋:>"); int x = 0; int y = 0; while (1) { printf("请输入想要下棋的位置坐标:>"); scanf("%d%d",&x,&y); if (x >= 1 && x <= line && y >= 1 && y <= row) { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; break; } else { printf("坐标被占用,请重新输入\n"); } } else { printf("非法坐标,超出范围\n"); } } }//玩家行动的限制与实现 void Commputer_game(char board[LINE][ROW], int line, int row) { int x = 0; int y = 0; while (1) { x = rand() % line; y = rand() % row; if (board[x][y]==' ') { board[x][y] = '#'; break; } } }//电脑行动的限制与实现 char con(char board[LINE][ROW], int line, int row) { int i = 0; int j = 0; for (i = 0; i < line; i++) { for (j = 0; j < row; j++) { if (board[i][j] == ' ') { return 'a'; } } } }//判断游戏是否继续的实现 char rank(char board[LINE][ROW], int line, int row) { int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < line; j++) { if (board[j][i] == '*' && board[j][i] == board[j+1][i] && board[j+2][i] == board[j][i]) { return 'q'; } else if (board[j][i] == '#' && board[j][i] == board[j + 1][i] && board[j + 2][i] == board[j][i]) { return 'w'; } } } }//判断一列的胜负条件 char Iswin(char board[LINE][ROW], int line, int row)//判断胜负的条件 { int i = 0; int j = 0; for (i = 0; i < line; i++) { for (j = 0; j < row; j++) { //判断行的胜负条件 if (board[i][j] == board[i][j + 1] && board[i][j] == '*' && board[i][j] == board[i][j + 2]) { return '*'; } else if (board[i][j] == '#' && board[i][j] == board[i][j + 1] && board[i][j] == board[i][j + 2]) { return '#'; } //判断列 else if (rank(board,LINE,ROW)=='q') { return '*'; } else if (rank(board, LINE, ROW) == 'w') { return '#'; } //对角线判断 else if ((board[i][j] == board[i + 1][j + 1] && board[i][j] == board[i + 2][j + 2] && board[i][j] == '*') || (board[i][j + 2] == board[i + 1][j + 1] && board[i + 2][j] == board[i][j + 2] && board[i][j + 2] == '*')) { return '*'; } else if ((board[i][j] == board[i + 1][j + 1] && board[i][j] == board[i + 2][j + 2] && board[i][j] == '#') || (board[i][j + 2] == board[i + 1][j + 1] && board[i + 2][j] == board[i][j + 2] && board[i][j + 2] == '#')) { return '#'; } else if (con(board, LINE, ROW) == 'a') { return 'c'; } else { return 'p'; } } } return 0; }
游戏的头文件(game.h):
#pragma once//保证被重复调用后依然只执行一次 #include<stdio.h> #include<stdlib.h> #include<time.h> #define LINE 3 #define ROW 3 //初始化棋盘: extern void Init_checkerboard(char board[LINE][ROW], int line, int row); //打印棋盘: extern void Print_checkerboard(char board[LINE][ROW], int line, int row); //玩家走: extern void Player_game(char board[LINE][ROW], int line, int row); //电脑走: extern void Commputer_game(char board[LINE][ROW], int line, int row); //判断胜负: extern char Iswin(char board[LINE][ROW], int line, int row);
代码运行图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。