#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
using namespace std;
const int N = 15; //15*15的棋盘
const char ChessBoard = ' '; //棋盘标志
const char flag1 = 'o'; //玩家1或电脑标志
const char flag2 = 'x'; //玩家2标志
typedef struct Position{ //坐标
int row; //行
int col; //列
}Position;
class GoBang{ //五子棋类
public:
GoBang(){
InitChessBoard(); //初始化棋盘
}
void Play(){ //下棋
Position Play1; //玩家1或电脑
Position Play2; //玩家2
while (1){
int mode = ChoiceMode();
while (1){
if (mode == 1){ //电脑VS玩家
ComputerChess(Play1, flag1); //电脑走
if (GetVictory(Play1, 0, flag1)){ //0代表电脑,为真则表示电脑获胜
break;
}
PlayChess(Play2, 2, flag2); //玩家2走
if (GetVictory(Play2, 2, flag2)){ //2代表玩家2
break;
}
}
else{ //玩家1VS玩家2
PlayChess(Play1, 1, flag1); //玩家1走
if (GetVictory(Play1, 1, flag1)){ //玩家1赢
break;
}
PlayChess(Play2, 2, flag2); //玩家2走
if (GetVictory(Play2, 2, flag2)){ //玩家2赢
break;
}
}
}
cout << "======再来一局=======" << endl;
cout << "yes or no :";
char s[] = "yes";
cin >> s;
if (strcmp(s, "no") == 0){
break;
}
}
}
protected:
void InitChessBoard(){ //初始化棋盘
for (int i = 0; i < N + 1; ++i){
for (int j = 0; j < N + 1; ++j){
_ChessBoard[i][j] = ChessBoard;
}
}
}
int ChoiceMode(){ //选择模式
system("cls");
//系统调用,清屏
InitChessBoard(); //重新初始化棋盘
cout << "
$" << endl;
cout << "
\033[31m0、退出\033[0m
\0
33
[
31
𝑚
0
、
退
出
\0
33
[
0
𝑚
"<<endl;cout<<"
"<<
𝑒
𝑛
𝑑
𝑙
;
𝑐
𝑜
𝑢
𝑡
<<"
\033[33m1、电脑VS玩家\033[0m
"<<endl;cout<<"
"<<
𝑒
𝑛
𝑑
𝑙
;
𝑐
𝑜
𝑢
𝑡
<<"
\033[32m2、玩家VS玩家\033[0m
"<<endl;cout<<"
"<<
𝑒
𝑛
𝑑
𝑙
;
𝑐
𝑜
𝑢
𝑡
<<"
$$$" << endl;
while (1){
int i = 0;
cout << "\033[34m请选择模式:\033[0m";
cin >> i;
if (i == 0){ //退出
exit(1);
}
if (i == 1 || i == 2){
return i;
}
else{
cout << "\033[31m非法输入,请重新输入!\033[0m" << endl;
}
}
}
void PrintChessBoard(){ //打印棋盘
printf(" 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15\n");
printf(" |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n");
for (int i = 1; i < N + 1; ++i)
{
printf("%2d ", i);
printf("| %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c |\n", _ChessBoard[i][1], _ChessBoard[i][2], _ChessBoard[i][3], _ChessBoard[i][4], _ChessBoard[i][5], _ChessBoard[i][6], _ChessBoard[i][7], _ChessBoard[i][8], _ChessBoard[i][9], _ChessBoard[i][10], _ChessBoard[i][11], _ChessBoard[i][12], _ChessBoard[i][13], _ChessBoard[i][14], _ChessBoard[i][15]);
printf(" |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n");
}
cout << endl;
}
void ComputerChess(Position& pos, char flag){ //电脑走
//PrintChessBoard(); //打印棋盘
int x = 0;
int y = 0;
while (1){ //循环查找空位置
x = (rand() % N) + 1; //产生从1~N的随机数
srand((unsigned int)time(NULL));
y &#