双人五子棋
双人五子棋 C++ 官方解析版。只支持最新的 VC++ 的 C++23 (部分)版本。如果你需要使用 GNU 进行运行,请自行解析至 C++14。
本代码遵循 CC-BY-NC (署名-非商业使用)协议。
#include <iostream>
#include <cstring>
#include <string>
#include <format>
#include <ctime>
#include <cstdlib>
#include <windows.h>
#include <conio.h>
#include <print>
using namespace std;
const int kMaxN = 25;
string map[kMaxN][kMaxN], n1, n2;
void init() {
system("cls");
system("title 双人五子棋-好渴鹅");
#ifdef UNICODE
SetConsoleTitleW(L"双人五子棋-好渴鹅");
#else
SetConsoleTitleA("双人五子棋-好渴鹅");
#endif
fill(map[0], map[0] + kMaxN * kMaxN, "十 ");
}
void readPlayer() {
print("你是玩家 ①,请输入你的名字:");
cin >> n1;
print("你是玩家 ②,请输入你的名字:");
cin >> n2;
}
void printMap() {
println(" 1 2 3 4 5 6 7 8 9 10 11 12 13");
for (int i = 1; i <= 13; i++) {
print("{} {}", i, (i < 10 ? " " : ""));
for (int j = 1; j <= 13; j++) {
print("{}", map[i][j]);
}
cout << endl;
}
cout << endl;
}
bool isVictory() {
for (int i = 1; i <= 13; i++) {
for (int j = 1; j <= 13; j++) {
if (map[i][j] == map[i][j + 1] &&
map[i][j + 1] == map[i][j + 2] &&
map[i][j + 2] == map[i][j + 3] &&
map[i][j + 3] == map[i][j + 4] &&
map[i][j] != "十 ") {
println("横向胜利!{}{}", i, j);
return 1;
}
if (map[i][j] == map[i + 1][j] &&
map[i + 1][j] == map[i + 2][j] &&
map[i + 2][j] == map[i + 3][j] &&
map[i + 3][j] == map[i + 4][j] &&
map[i][j] != "十 ") {
println("竖向胜利!{}{}", i, j);
return 1;
}
if (map[i][j] == map[i + 1][j + 1] &&
map[i + 1][j + 1] == map[i + 2][j + 2] &&
map[i + 2][j + 2] == map[i + 3][j + 3] &&
map[i + 3][j + 3] == map[i + 4][j + 4] &&
map[i][j] != "十 ") {
println("左斜线胜利!{}{}", i, j);
return 1;
}
if (j - 4 >= 1) {
if (map[i][j] == map[i + 1][j - 1] &&
map[i + 1][j - 1] == map[i + 2][j - 2] &&
map[i + 2][j - 2] == map[i + 3][j - 3] &&
map[i + 3][j - 3] == map[i + 4][j - 4] &&
map[i][j] != "十 ") {
println("右斜线胜利!{}{}", i, j);
return 1;
}
}
}
}
return 0;
}
bool hasDrew(int x, int y) {
return map[x][y] == "○ " || map[x][y] == "● " ? 1 : 0;
}
bool isMap(int x, int y) {
if (x < 1 || x > 13 || y < 1 || y > 13) {
return 0;
}
return 1;
}
string toChar(int x) {
return x & 1 ? "○ " : "● ";
}
void draw(int x, int y, string c) {
map[x][y] = c;
}
void printPlayer(int p) {
string name = (p & 1 ? n1 : n2);
println("到你了,{}!请问需要下到哪里?", name);
println("请在一行内输入坐标,并用空格隔开:");
int x = 0, y = 0;
cin >> x >> y;
while (hasDrew(x, y) || !isMap(x, y)) {
println("超出界限 / 坐标已经下过了 / 输入错误!");
println("请在一行内输入坐标,并用空格隔开:");
cin >> x >> y;
}
draw(x, y, toChar(p));
system("cls");
}
void play() {
for (size_t i = 1; i <= 11451419198105201314; i++) {
printMap();
if (isVictory()) {
print("{} 赢了!", (i & 1 ? n2 : n1));
return;
}
printPlayer(i);
}
}
int main() {
init();
readPlayer();
play();
system("pause");
return 0;
}

浙公网安备 33010602011771号