#include <iostream>
#include <conio.h>
#include <stdlib.h>

int main()
{
const int w = 20;//棋盘宽
const int h = 20;//棋盘高
const int s = w * h;//棋盘大小
const int kg = 0;//空格(无子)
const int hz = 1;//黑子
const int bz = 2;//白子
int c = hz;//当前落子

char map[s] = {};//棋盘数组
int x = 0, y = 0;//英雄坐标

while (1)
{
system("cls");
for (int i = 0; i < s; ++i)
{
if (i == x + y * w)//英雄在棋盘或棋子之上
{
switch (map[i])
{
case kg:std::cout<<"◎";break;
case hz:std::cout<<"□";break;
case bz:std::cout<<"■";break;
}
}
else //英雄不在棋盘或棋子之上
{
switch (map[i])
{
case kg:
{
if (i == 0)
std::cout<<"┏";
else if (i == w - 1)
std::cout<<"┓";
else if (i == s - w)
std::cout<<"┗";
else if (i ==s - 1)
std::cout<<"┛";
else if (i % w == 0)
std::cout<<"┣";
else if (i % w == h - 1)
std::cout<<"┫";
else if (i / w == 0)
std::cout<<"┳";
else if (i / w == h -1)
std::cout<<"┻";
else if (i < s)
std::cout<<"╋";
}break;
case hz:std::cout<<"○";break;
case bz:std::cout<<"●";break;
}
}
if(i % w == w - 1)
std::cout<<std::endl;
}

//输入
int input = _getch();

if ((input == 'w' || input == 'W') && y > 0)
y -= 1;
else if ((input == 's' || input == 'S') && y < h - 1)
y += 1;
else if ((input == 'a' || input == 'A') && x > 0)
x -= 1;
else if ((input == 'd' || input == 'D') && x < w - 1)
x += 1;
else if (input == ' ' && map[x + y * w] == kg)
{
map[x + y * w] = c;
//胜负判断

int now = x + y * w;

//方法1(代码太繁琐,有bug)
横向寻子
//int sum = 1;
//for (int i = now + 1; map[i] == c; i += 1)
// sum += 1;
//for (int i = now - 1; map[i] == c; i -= 1)
// sum += 1;
//if (sum >= 5)
//{
// std::cout<<(c == hz ? "黑子胜利": "白子胜利")<<std::endl;
// break;
//}
纵向寻子
//sum = 1;
//for (int i = now + w; map[i] == c; i += w)
// sum += 1;
//for (int i = now - w; map[i] == c; i -= w)
// sum += 1;
//if (sum >= 5)
//{
// std::cout<<(c == hz ? "黑子胜利": "白子胜利")<<std::endl;
// break;
//}
斜向寻子
//sum = 1;
//for (int i = now + w + 1; map[i] == c; i += w + 1)
// sum += 1;
//for (int i = now - (w + 1); map[i] == c; i -= w + 1)
// sum += 1;
//if (sum >= 5)
//{
// std::cout<<(c == hz ? "黑子胜利": "白子胜利")<<std::endl;
// break;
//}
//
反斜向寻子
//sum = 1;
//for (int i = now + 1 - w; map[i] == c; i += 1 - w)
// sum += 1;
//for (int i = now - (1 - w); map[i] == c; i -= 1 - w)
// sum += 1;
//if (sum >= 5)
//{
// std::cout<<(c == hz ? "黑子胜利": "白子胜利")<<std::endl;
// break;
//}

//方法2(代码简洁,有bug)
//const int f[] = {1, w, w + 1, w - 1};
//for (int j = 0; j < 4; ++j)
//{
// int sum = 1;
// for (int i = now + f[j]; map[i] == c; i += f[j])
// sum += 1;
// for (int i = now - f[j]; map[i] == c; i -= f[j])
// sum += 1;
// if (sum >= 5)
//{
// std::cout<<(c == hz ? "黑子胜利": "白子胜利")<<std::endl;

// do
// {
// std::cout<<"按Y重新开始,按N退出游戏...";
// input = _getch();
// } while (input != 'y' && input != 'Y' && input != 'n' && input != 'N');

// if (input == 'y' || input == 'Y')
// {
// c = bz;//当前落子
// for (int i = 0; i < s; ++i)
// map[i] = kg;
// x = y = 0;//英雄坐标
// }
// else
// return;
// break;
//}
//}

方法3(代码简洁,无bug)
const char fx[] = {1, 0, 1, 1};
const char fy[] = {0, 1, 1, -1};
for (int j = 0; j < 4; ++j)
{
int sum = 1;
for (int dx = x + fx[j], dy = y + fy[j]; ;dx += fx[j], dy += fy[j])
{
if (dx >= 0 && dx < w && map[dx + dy * w] == c)
sum += 1;
else
break;
}
for (int dx = x - fx[j], dy = y - fy[j]; ;dx -= fx[j], dy -= fy[j])
{
if (dx >= 0 && dx < w && map[dx + dy * w] == c)
sum += 1;
else
break;
}

if (sum >= 5)
{
std::cout<<(c == hz ? "黑子胜利": "白子胜利")<<std::endl;

do
{
std::cout<<"按Y重新开始,按N退出游戏...\n";
input = _getch();
} while (input != 'y' && input != 'Y' && input != 'n' && input != 'N');

if (input == 'y' || input == 'Y')
{
c = bz;//当前落子
for (int i = 0; i < s; ++i)
map[i] = kg;
x = y = 0;//英雄坐标
}
else
return 0;
break;
}
}
c = (c == hz ? bz : hz);
}
}
system("pause");
}