单文件完整源码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>#define ROW 9
#define COL 9
#define NUM 5//菜单
void menu()
{printf("************* 三子棋游戏 ************\n");printf("************1.play 0.exit************\n");
}//胜利打印
void Win()
{printf("************* 你赢啦 ************\n");
}//失败打印
void Defeat()
{printf("************* 你输啦 ************\n");
}//平局打印
void Draw()
{printf("************* 平局啦 ************\n");
}//初始化棋盘
void Initializeboard(char borad[ROW][COL], int row, int col)
{for (int i = 0; i < row; i++){for (int j = 0; j < col; j++){borad[i][j] = ' ';}}
}//打印棋盘
void Dispiayboard(char borad[ROW][COL], int row, int col)
{system("cls");for (int i = 0; i < row; i++){for (int j = 0; j < col; j++){printf(" %c ", borad[i][j]);if (j != col-1){printf("|");}}printf("\n");if (i != col-1){for (int n = 0; n < col; n++){printf("---");if (n != col-1){printf("|");}}printf("\n");}}
}//向左查同
int Cleft(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((y-1 >= 0) && (borad[x][y-1] == type)){count ++;count += Cleft(borad, type, x, y-1);}return count;
}//向右查同
int Cright(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((y+1 < COL) && (borad[x][y+1] == type)){count ++;count += Cright(borad, type, x, y+1);}return count;
}//向上查同
int Cover(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((x-1 >= 0) && (borad[x-1][y] == type)){count ++;count += Cover(borad, type, x-1, y);}return count;
}//向下查同
int Cdown(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((x+1 < ROW) && (borad[x+1][y] == type)){count ++;count += Cdown(borad, type, x+1, y);}return count;
}//向左斜左查同
int Cldial(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((x+1 < ROW) && (y-1 >= 0) && (borad[x+1][y-1] == type)){count ++;count += Cldial(borad, type, x+1, y-1);}return count;
}//向左斜右查同
int Cldiar(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((x-1 >= 0) && (y+1 < COL) && (borad[x-1][y+1] == type)){count ++;count += Cldiar(borad, type, x-1, y+1);}return count;
}//向右斜左查同
int Crdial(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((x-1 >= 0) && (y-1 >= 0) && (borad[x-1][y-1] == type)){count ++;count += Crdial(borad, type, x-1, y-1);}return count;
}//向右斜右查同
int Crdiar(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((x+1 < ROW) && (y+1 < COL) && (borad[x+1][y+1] == type)){count ++;count += Crdiar(borad, type, x+1, y+1);}return count;
}//平局判定
int Isfull(char borad[ROW][COL], int row, int col)
{for (int i = 0; i < row; i++){for (int j = 0; j < col; j++){if (borad[i][j] == ' '){return 1;}}}return 2;
}//判断输赢
int Iswin(char borad[ROW][COL], char type, int x, int y)
{//横向判定“---”int temp1 = 1;temp1 += Cleft(borad, type, x, y);temp1 += Cright(borad, type, x, y);//竖向判定“|”int temp2 = 1;temp2 += Cover(borad, type, x, y);temp2 += Cdown(borad, type, x, y);//左斜判定“/”int temp3 = 1;temp3 += Cldial(borad, type, x, y);temp3 += Cldiar(borad, type, x, y);//右斜判定“\”int temp4 = 1;temp4 += Crdial(borad, type, x, y);temp4 += Crdiar(borad, type, x, y);if ((temp1==NUM) || (temp2==NUM) || (temp3==NUM) || (temp4==NUM)){return 0;}else{//判断平局函数return Isfull(borad, ROW, COL);}
}//玩家下棋
int Playermove(char borad[ROW][COL], int row, int col)
{int x, y, z;//利用do while循环和状态值z实现坐标不合法时重新输入do{ z = 1;printf("请输入要下的坐标:\n");scanf("%d%d", &x, &y);//验证当前是否被占用或者超出范围if (borad[x-1][y-1] == ' '){//坐标可用改变状态值z以实现跳出循环z = 0;}else{printf("坐标不合法(被占用或者不在范围),请重新输入!\n");}} while (z);//将玩家下棋位置标记为“*”borad[x-1][y-1] = '*';//返回输赢结果return Iswin(borad, '*', x-1, y-1);
}//电脑下棋
int Computermove(char borad[ROW][COL], int row, int col)
{int x, y;while (1){x = rand()%row;y = rand()%col;if (borad[x][y] == ' '){borad[x][y] = '#';return Iswin(borad, '#', x, y);}}
}void game()
{//创建棋盘char borad[ROW][COL] = {0};//初始化棋盘Initializeboard(borad, ROW, COL);//打印棋盘Dispiayboard(borad, ROW, COL);while (1){//玩家下棋int ret1 = Playermove(borad, ROW, COL);if (ret1 == 0){Dispiayboard(borad, ROW, COL);printf("\n");Win();printf("\n\n\n");break;}else if (ret1 == 2){Dispiayboard(borad, ROW, COL);printf("\n");Draw();printf("\n\n\n");break;}//电脑下棋int ret2 = Computermove(borad, ROW, COL);//打印结果Dispiayboard(borad, ROW, COL);if (ret2 == 0){printf("\n");Defeat();printf("\n\n\n");break;}else if (ret2 == 2){printf("\n");Draw();printf("\n\n\n");break;}}
}//测试代码
void test()
{int a;srand((unsigned int) time(NULL));do{menu();scanf("%d", &a);switch (a){case 0:{printf("退出游戏!\n");break;}case 1:{game();break;}default:{printf("请输入正确指令!\n");}}} while (a);
}int main()
{test();return 0;
}
标准化项目源码:
test.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include "game.h"void menu()
{printf("******************************************* 三子棋游戏 *************************************");printf("************************************************************************************************************************");printf("******************************************* 1.Play 0.Exit *************************************");printf("************************************************************************************************************************");printf("************************************************************************************************************************");
}void Win()
{printf("************************************************************************************************************************");printf("************************************************************************************************************************");printf("********************************************* 你赢啦 ***************************************");printf("************************************************************************************************************************");printf("************************************************************************************************************************");
}void Defeat()
{printf("************************************************************************************************************************");printf("************************************************************************************************************************");printf("********************************************* 你输啦 ***************************************");printf("************************************************************************************************************************");printf("************************************************************************************************************************");
}void Draw()
{printf("************************************************************************************************************************");printf("************************************************************************************************************************");printf("********************************************* 平局啦 ***************************************");printf("************************************************************************************************************************");printf("************************************************************************************************************************");
}void game()
{char borad[ROW][COL] = {0};//初始化棋盘Initializeboard(borad, ROW, COL);//打印棋盘Dispiayboard(borad, ROW, COL);while (1){//玩家走int ret = Playermove(borad, ROW, COL);//清屏system("cls");//打印结果Dispiayboard(borad, ROW, COL);if (ret == 0){Win();break;}else if (ret == 2){Draw();break;}Sleep(1000);//电脑走int ret2 = Computermove(borad, ROW, COL);//清屏system("cls");//打印结果Dispiayboard(borad, ROW, COL);if (ret2 == 0){Defeat();break;}else if (ret2 == 2){Draw();break;}}
}void test()
{int a;srand((unsigned int)time(NULL));do{printf("请根据菜单选择(1开始游戏,0退出游戏):\n");scanf("%d", &a);switch (a){case 1:{system("cls");game();break;}case 0:{printf("退出游戏!\n");break;}default:{printf("请输入正确选项!\n");}}} while (a);
}int main()
{//打印菜单menu();//运行测试test();return 0;
}
game.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"//初始化棋盘
void Initializeboard(char borad[ROW][COL], int row, int col)
{int i = 0;int j = 0;for (i = 0; i < row; i++){for (j = 0; j < col; j++){borad[i][j] = ' ';}}
}//打印棋盘
void Dispiayboard(char borad[ROW][COL], int row, int col)
{printf("************************************************************************************************************************");printf("************************************************************************************************************************");printf("******************************************* 三子棋游戏 *************************************");printf("************************************************************************************************************************");printf("************************************************************************************************************************");printf("\n");printf("\n");int i = 0;int j = 0;for (i = 0; i < row; i++){for (j = 0; j < col; j++){printf(" %c ", borad[i][j]);if (j == col-1){break;}printf("|");}if (i == row-1){break;}printf("\n");for (int i = 0; i < col; i++){printf("---");if (i == col-1){break;}printf("|");}printf("\n");}printf("\n");printf("\n");printf("************************************************************************************************************************");
}//玩家走
int Playermove(char borad[ROW][COL], int row, int col)
{int x, y;while (1){printf("请输入坐标:\n");scanf("%d%d", &x, &y);if (x>0 && x<=row && y>0 && y<=col ){if (borad[x-1][y-1] == ' '){borad[x-1][y-1] = '*';int ret = Iswin(borad, '*', x-1, y-1);return ret;}else{printf("坐标被占用,请重新输入!\n");}}else{printf("坐标不在范围,请重新输入:\n");}}
}//电脑走
int Computermove(char borad[ROW][COL], int row, int col)
{int x, y;while (1){x = rand()%row;y = rand()%col;if (borad[x][y] == ' '){borad[x][y] = '#';int ret = Iswin(borad, '#', x, y);return ret;break;}}
}//判断输赢
int Iswin(char borad[ROW][COL], char type, int x, int y)
{//横向判定“---”int temp1 = 1;temp1 += Cleft(borad, type, x, y);temp1 += Cright(borad, type, x, y);//竖向判定“|”int temp2 = 1;temp2 += Cover(borad, type, x, y);temp2 += Cdown(borad, type, x, y);//左斜判定“/”int temp3 = 1;temp3 += Cldial(borad, type, x, y);temp3 += Cldiar(borad, type, x, y);//右斜判定“\”int temp4 = 1;temp4 += Crdial(borad, type, x, y);temp4 += Crdiar(borad, type, x, y);if ((temp1==NUM) || (temp2==NUM) || (temp3==NUM) || (temp4==NUM)){return 0;}else{//判断平局函数return Isfull(borad, ROW, COL);}
}//向左查同
int Cleft(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((y-1 >= 0) && (borad[x][y-1] == type)){count ++;count += Cleft(borad, type, x, y-1);}return count;
}//向右查同
int Cright(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((y+1 < COL) && (borad[x][y+1] == type)){count ++;count += Cright(borad, type, x, y+1);}return count;
}//向上查同
int Cover(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((x-1 >= 0) && (borad[x-1][y] == type)){count ++;count += Cover(borad, type, x-1, y);}return count;
}//向下查同
int Cdown(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((x+1 < ROW) && (borad[x+1][y] == type)){count ++;count += Cdown(borad, type, x+1, y);}return count;
}//向左斜左查同
int Cldial(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((x+1 < ROW) && (y-1 >= 0) && (borad[x+1][y-1] == type)){count ++;count += Cldial(borad, type, x+1, y-1);}return count;
}//向左斜右查同
int Cldiar(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((x-1 >= 0) && (y+1 < COL) && (borad[x-1][y+1] == type)){count ++;count += Cldiar(borad, type, x-1, y+1);}return count;
}//向右斜左查同
int Crdial(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((x-1 >= 0) && (y-1 >= 0) && (borad[x-1][y-1] == type)){count ++;count += Crdial(borad, type, x-1, y-1);}return count;
}//向右斜右查同
int Crdiar(char borad[ROW][COL], char type, int x, int y)
{int count = 0;if((x+1 < ROW) && (y+1 < COL) && (borad[x+1][y+1] == type)){count ++;count += Crdiar(borad, type, x+1, y+1);}return count;
}//平局判定
int Isfull(char borad[ROW][COL], int row, int col)
{for (int i = 0; i < row; i++){for (int j = 0; j < col; j++){if (borad[i][j] == ' '){return 1;}}}return 2;
}
game.h
#define ROW 9
#define COL 9
#define NUM 5#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <Windows.h>void Initializeboard(char borad[ROW][COL], int row, int col);
void Dispiayboard(char borad[ROW][COL], int row, int col);
int Playermove(char borad[ROW][COL], int row, int col);
int Computermove(char borad[ROW][COL], int row, int col);
int Iswin(char borad[ROW][COL], char type, int x, int y);
int Isfull(char borad[ROW][COL], int row, int col);
int Cleft(char borad[ROW][COL], char type, int x, int y);
int Cright(char borad[ROW][COL], char type, int x, int y);
int Cover(char borad[ROW][COL], char type, int x, int y);
int Cdown(char borad[ROW][COL], char type, int x, int y);
int Cldial(char borad[ROW][COL], char type, int x, int y);
int Cldiar(char borad[ROW][COL], char type, int x, int y);
int Crdial(char borad[ROW][COL], char type, int x, int y);
int Crdiar(char borad[ROW][COL], char type, int x, int y);