用户:
蒟蒻OIer1048576查看:7 回复:11 评论:7 创建时间:2021-02-28T17:31:16
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
char board[7][7] = {
{0,0,'g','k','g',0,0},
{0,0,'s',0,'s',0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,'S',0,'S',0,0},
{0,0,'G','K','G',0,0},
};
enum Piece {
WHITE_KING = 'K',
WHITE_GUARD = 'G',
WHITE_SOILDER = 'S',
BLACK_KING = 'k',
BLACK_GUARD = 'g',
BLACK_SOILDER = 's',
nothing = '\000'
};
bool check(const char * data , char target) {
for (int i = 0; i < 7; ++i) {
for (int j = 0; j < 7; ++j) {
if (board[i][j] == target) return;
}
}
wcout << data << "赢了!";
}
void print() {
cout << " 0 1 2 3 4 5 6 \n";
for (int i = 0; i < 7; ++i) {
cout << i << " ";
for (int j = 0; j < 7; ++j) {
if (board[i][j]) cout << board[i][j] << " ";
else cout << "0 ";
}
cout << endl;
}
}
int main() {
while (1) {
//White:
if (check("黑棋", 'K')) return 0;
print();
wcout << "白棋的移动:";
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
cout << endl;
if (board[x1][y1] == nothing) {
wcout << "此处无子\n";
continue;
}
if (board[x1][y1] > 'a') {
wcout << "此处为黑子\n";
continue;
}
if (board[x2][y2] < 'Z' && board[x2][y2] > 'A') {
wcout << "别对着友军开炮啊!\n";
continue;
}
if (board[x1][y1] == WHITE_KING) { // 国王:八方一格,被吃输棋。
int xmove = abs(x2 - x1);
int ymove = abs(y2 - y1);
if (ymove > xmove) swap(xmove, ymove);
if (xmove == 1 && ymove < 2) {
wcout << "移动成功!\n";
board[x1][y1] = nothing;
board[x2][y2] = WHITE_KING;
}
else {
wcout << "你走错了!\n";
continue;
}
}
if (board[x1][y1] == WHITE_GUARD) { // 侍卫:斜走一格
int xmove = abs(x2 - x1);
int ymove = abs(y2 - y1);
if (ymove > xmove) swap(xmove, ymove);
if (xmove == 1 && ymove == 1) {
wcout << "移动成功!\n";
board[x1][y1] = nothing;
board[x2][y2] = WHITE_GUARD;
}
else {
wcout << "你走错了!\n";
continue;
}
}
if (board[x1][y1] == WHITE_SOILDER) { // 国王:八方一格,被吃输棋。
int xmove = abs(x2 - x1);
int ymove = abs(y2 - y1);
if (ymove > xmove) swap(xmove, ymove);
if (xmove == 1 && ymove < 2) {
wcout << "移动成功!\n";
board[x1][y1] = nothing;
board[x2][y2] = WHITE_SOILDER;
}
else {
wcout << "你走错了!\n";
continue;
}
}
// Black:
if (check("白棋", 'k')) return 0;
wcout << "白棋的移动:";
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
cout << endl;
if (board[x1][y1] == nothing) {
wcout << "此处无子\n";
continue;
}
if (board[x1][y1] < 'Z') {
wcout << "此处为白子\n";
continue;
}
if (board[x2][y2] > 'a') {
wcout << "别对着友军开炮啊!\n";
continue;
}
if (board[x1][y1] == BLACK_KING) { // 国王:八方一格,被吃输棋。
int xmove = abs(x2 - x1);
int ymove = abs(y2 - y1);
if (ymove > xmove) swap(xmove, ymove);
if (xmove == 1 && ymove < 2) {
wcout << "移动成功!\n";
board[x1][y1] = nothing;
board[x2][y2] = BLACK_KING;
}
else {
wcout << "你走错了!\n";
continue;
}
}
if (board[x1][y1] == BLACK_GUARD) { // 侍卫:斜走一格
int xmove = abs(x2 - x1);
int ymove = abs(y2 - y1);
if (ymove > xmove) swap(xmove, ymove);
if (xmove == 1 && ymove == 1) {
wcout << "移动成功!\n";
board[x1][y1] = nothing;
board[x2][y2] = BLACK_GUARD;
}
else {
wcout << "你走错了!\n";
continue;
}
}
if (board[x1][y1] == BLACK_SOILDER) { // 国王:八方一格,被吃输棋。
int xmove = abs(x2 - x1);
int ymove = abs(y2 - y1);
if (ymove > xmove) swap(xmove, ymove);
if (xmove == 1 && ymove < 2) {
wcout << "移动成功!\n";
board[x1][y1] = nothing;
board[x2][y2] = BLACK_SOILDER;
}
else {
wcout << "你走错了!\n";
continue;
}
}
}
}
都是些简单重复,不要被吓到
蒟蒻OIer1048576K = 白棋 - 国王 - 八方走一格 - 被吃输棋
G = 白棋 - 侍卫 - 斜走一格
S = 白棋 - 士兵 - 直走一格
k = 黑棋 - 国王 - 八方走一格 - 被吃输棋
g = 黑棋 - 侍卫 - 斜走一格
s = 黑棋 - 士兵 - 直走一格
0 = 空位
点赞0
评论
蒟蒻OIer1048576大量debug后:
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
char board[7][7] = {
{0,0,'g','k','g',0,0},
{0,0,'s',0,'s',0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,'S',0,'S',0,0},
{0,0,'G','K','G',0,0},
};
enum Piece {
WHITE_KING = 'K',
WHITE_GUARD = 'G',
WHITE_SOILDER = 'S',
BLACK_KING = 'k',
BLACK_GUARD = 'g',
BLACK_SOILDER = 's',
nothing = '\000'
};
bool check(const char* data, char target) {
for (int i = 0; i < 7; ++i) {
for (int j = 0; j < 7; ++j) {
if (board[i][j] == target) return true;
}
}
wcout << data << "赢了!";
return false;
}
void print() {
cout << " 0 1 2 3 4 5 6 \n";
for (int i = 0; i < 7; ++i) {
cout << i << " ";
for (int j = 0; j < 7; ++j) {
if (board[i][j]) cout << board[i][j] << " ";
else cout << "0 ";
}
cout << endl;
}
}
int main() {
while (1) {
//White:
if (check("黑棋", 'K')) return 0;
print();
wcout << "白棋的移动:";
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
cout << endl;
if (board[x1][y1] == nothing) {
wcout << "此处无子\n";
continue;
}
if (board[x1][y1] > 'a') {
wcout << "此处为黑子\n";
continue;
}
if (board[x2][y2] < 'Z' && board[x2][y2] > 'A') {
wcout << "别对着友军开炮啊!\n";
continue;
}
if (board[x1][y1] == WHITE_KING) { // 国王:八方一格,被吃输棋。
int xmove = abs(x2 - x1);
int ymove = abs(y2 - y1);
if (ymove > xmove) swap(xmove, ymove);
if (xmove == 1 && ymove < 2) {
wcout << "移动成功!\n";
board[x1][y1] = nothing;
board[x2][y2] = WHITE_KING;
}
else {
wcout << "你走错了!\n";
continue;
}
}
if (board[x1][y1] == WHITE_GUARD) { // 侍卫:斜走一格
int xmove = abs(x2 - x1);
int ymove = abs(y2 - y1);
if (ymove > xmove) swap(xmove, ymove);
if (xmove == 1 && ymove == 1) {
wcout << "移动成功!\n";
board[x1][y1] = nothing;
board[x2][y2] = WHITE_GUARD;
}
else {
wcout << "你走错了!\n";
continue;
}
}
if (board[x1][y1] == WHITE_SOILDER) { // 国王:八方一格,被吃输棋。
int xmove = abs(x2 - x1);
int ymove = abs(y2 - y1);
if (ymove > xmove) swap(xmove, ymove);
if (xmove == 1 && ymove < 2) {
wcout << "移动成功!\n";
board[x1][y1] = nothing;
board[x2][y2] = WHITE_SOILDER;
}
else {
wcout << "你走错了!\n";
continue;
}
}
// Black:
if (check("白棋", 'k')) return 0;
wcout << "黑棋的移动:";
cin >> x1 >> y1 >> x2 >> y2;
cout << endl;
if (board[x1][y1] == nothing) {
wcout << "此处无子\n";
continue;
}
if (board[x1][y1] < 'Z') {
wcout << "此处为白子\n";
continue;
}
if (board[x2][y2] > 'a') {
wcout << "别对着友军开炮啊!\n";
continue;
}
if (board[x1][y1] == BLACK_KING) { // 国王:八方一格,被吃输棋。
int xmove = abs(x2 - x1);
int ymove = abs(y2 - y1);
if (ymove > xmove) swap(xmove, ymove);
if (xmove == 1 && ymove < 2) {
wcout << "移动成功!\n";
board[x1][y1] = nothing;
board[x2][y2] = BLACK_KING;
}
else {
wcout << "你走错了!\n";
continue;
}
}
if (board[x1][y1] == BLACK_GUARD) { // 侍卫:斜走一格
int xmove = abs(x2 - x1);
int ymove = abs(y2 - y1);
if (ymove > xmove) swap(xmove, ymove);
if (xmove == 1 && ymove == 1) {
wcout << "移动成功!\n";
board[x1][y1] = nothing;
board[x2][y2] = BLACK_GUARD;
}
else {
wcout << "你走错了!\n";
continue;
}
}
if (board[x1][y1] == BLACK_SOILDER) { // 国王:八方一格,被吃输棋。
int xmove = abs(x2 - x1);
int ymove = abs(y2 - y1);
if (ymove > xmove) swap(xmove, ymove);
if (xmove == 1 && ymove < 2) {
wcout << "移动成功!\n";
board[x1][y1] = nothing;
board[x2][y2] = BLACK_SOILDER;
}
else {
wcout << "你走错了!\n";
continue;
}
}
}
}
点赞0
评论