接下来手把手教你用C语言实现三子棋
第一步
建立一个项目 ——》在项目中建立三个文件:text.c game.c game.h
第二步
分析三子棋实现步骤 :
1.交互页面:输入1 —— 玩 输入0 —— 退出游戏
2.建立棋盘
3. 下棋
5.判断结果
第三步
代码实现:
test.c
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void menu() { printf("**********************\n"); printf("*** 1.play 0.exti ***\n"); printf("**********************\n"); } void Gobang() { srand((unsigned)time(NULL)); char tmp = 'C'; char board[Line][Row] = { 0 };//棋盘 //1.初始化棋盘 initial(board, Line, Row); //2.打印棋盘 print(board, Line, Row); while (1) { //3.下棋(玩家先下,电脑再下) //玩家下 player(board, Line, Row); tmp = judge(board, Line, Row); if (tmp != 'C') break; print(board, Line, Row);//打印棋盘 //电脑下 computer(board, Line, Row); tmp = judge(board, Line, Row); if (tmp != 'C') break; print(board, Line, Row);//打印棋盘 //4.判断输赢 } if (tmp == '*') printf("玩家赢\n"); else if (tmp == '#') printf("电脑赢\n"); else printf("平局\n"); print(board, Line, Row);//打印棋盘 } int main() { int input = 0; do { menu(); printf("请输入你的选择 >> "); scanf("%d", &input); switch (input) { case 1:Gobang(); break;//开始游戏 case 0:printf("退出游戏\n"); break; default:printf("请重新输入\n"); break; } } while (input!=0); system("pause"); }
game.h
#include<windows.h> #include<stdio.h> #include<stdlib.h> #include<time.h> #define Line 5//行 #define Row 5//列 //初始化棋盘 void initial(char board[Line][Row], int line, int row); //打印棋盘 void print(char board[Line][Row], int line, int row); //玩家下棋 void player(char board[Line][Row], int line, int row); //电脑下棋 void computer(char board[Line][Row], int line, int row); //判断输赢 char judge(char board[Line][Row], int line, int row);
game.c
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" //将棋盘初始化为空 void initial(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(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++) { printf(" %c ", board[i][j]); if (j < row - 1) { printf("|"); } } printf("\n"); for (j = 0; j < row; j++) { if (i < line - 1) { printf("---"); if (j < row - 1) { printf("|"); } } } printf("\n"); } } //玩家下棋 void player(char board[Line][Row], int line, int row) { int i = 0; int j = 0; while (1) { printf("请玩家下棋 >> "); scanf("%d%d", &i, &j); if (board[i-1][j-1] = ' ' && i-1 < line && j-1 < row && i - 1 >= 0 && j - 1 >= 0) { board[i-1][j-1] = '*'; break; } else { printf("输入错误请重新输入\n"); } } } //电脑下棋 void computer(char board[Line][Row], int line, int row) { while (1) { int i = rand() % line; int j = rand() % row; printf("请电脑下棋 >> \n"); if (board[i][j] == ' ') { board[i][j] = '#'; break; } } } //判断输赢 char judge(char board[Line][Row], int line, int row) { int i = 0; int j = 0; int t = 0; //判断行低级版本 //for (i = 0; i < line; i++) //{ // if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ') // { // return board[i][0]; // } //} //判断行高级版本 for (i = 0; i < line; i++) { for (j = 0; j < row; j++) { if (j < row - 2 && board[i][j] != ' ' && board[i][j] == board[i][j + 1] && board[i][j + 1] == board[i][j + 2]) { return board[i][j]; } } } //判断列低级版本 //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][i]; // } //} //判断列高级版本 for (i = 0; i < row; i++) { for (j = 0; j < line; j++) { if (j < line - 2 && board[j][i] != ' ' && board[j][i] == board[j + 1][i] && board[j + 1][i] == board[j+ 2][i]) { return board[j][i]; } } } //判断对角线低级版本 //if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ' || board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ') //{ // return board[1][1]; //} //判断对角线高级版本 //正对角线 for (i = 0, j = 0; i < line, j < row; i++, j++) { if (i < line - 2 && j < row - 2 && board[i][j] != ' '&&board[i][j] == board[i + 1][j + 1] && board[i][j] == board[i + 2][j + 2]) { return board[i][j]; } } //反对角线 for (i = 0; i < line; i++) { for (j = 0; j < row; j++) { if (j >= 2 && board[i][j] != ' ' && board[i][j] == board[i + 1][j - 1] && board[i][j] == board[i + 2][j - 2]) { return board[i][j]; } } } //平局 for (i = 0; i < line; i++) { for (j = 0; j < row; j++) { if (board[i][j] == ' ') { t = 1; } } } if (t != 1) { return 'D'; } return 'C'; }