蒟蒻的第二个小游戏:国际象棋(1)。 但是蒟蒻还没做完,所以麻烦各位dalao、julao在作品完成后, 搞一些操作,测试一下蒟蒻的代码。
然而,因为蒟蒻的代码太长了,所以可能会分三个左右的云剪切板。 由于某谷性能的原因,每个云剪切板只放 行代码。
上代码:
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <windows.h>
using namespace std;
const int N = 10;
const int n = 8;
const int madx[N] = {-1, 1, -2, 2, -2, 2, -1, 1};
const int mady[N] = {2, 2, 1, 1, -1, -1, -2, -2};
const bool chess_color[N * N] = {
true, false, true, false, true, false, true, false,
false, true, false, true, false, true, false, true,
true, false, true, false, true, false, true, false,
false, true, false, true, false, true, false, true,
true, false, true, false, true, false, true, false,
false, true, false, true, false, true, false, true,
true, false, true, false, true, false, true, false,
false, true, false, true, false, true, false, true
};
int color[N][N], player;//1白2黑
int game[N][N], op, nowx, nowy, nextx, nexty;
string s;
bool huan_qi;
void chess(int x){
if (x == 1){
cout << "王";
}
else if (x == 2){
cout << "后";
}
else if (x == 3){
cout << "车";
}
else if (x == 4){
cout << "马";
}
else if (x == 5){
cout << "象";
}
else if (x == 6){
cout << "兵";
}
return;
}
void update(){
system("cls");
printf(" ");
for (int i = 1; i <= n; i++){
printf(" %c", (char)(i + 'a' - 1));
}
printf("\n -----------------------------------------\n");
for (int i = 1; i <= n; i++){
printf(" %d|", 9 - i);
for (int j = 1; j <= n; j++){
if (game[i][j] == 0){
printf(" 无 ");
}
else{
if (color[i][j] == 1){
printf("白");
}
else{
printf("黑");
}
chess(game[i][j]);
}
printf("|");
}
printf("%d\n |", 9 - i);
for (int j = 1; j <= n; j++){
if (chess_color[(i - 1) * n + j - 1] == true){
cout << "白";
}
else{
cout << "黑";
}
cout << "格|";
}
printf("\n -----------------------------------------\n");
}
printf(" ");
for (int i = 1; i <= n; i++){
printf(" %c", (char)(i + 'a' - 1));
}
printf("\n\n");
return;
}
void sleep_print(string x, int T){
for (int i = 0; i < int(x.length()); i++){
cout << x[i];
Sleep(T);
}
return;
}
void init(){
game[1][5] = game[8][5] = 1;
game[1][4] = game[8][4] = 2;
game[1][1] = game[1][8] = game[8][1] = game[8][8] = 3;
game[1][2] = game[1][7] = game[8][2] = game[8][7] = 4;
game[1][3] = game[1][6] = game[8][3] = game[8][6] = 5;
for (int j = 1; j <= n; j++){
color[1][j] = color[2][j] = 2;
color[7][j] = color[8][j] = 1;
game[2][j] = game[7][j] = 6;
}
return;
}
void clear(){
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
game[i][j] = 0;
color[i][j] = 0;
}
}
return;
}
void print_player(int x){
if (x == 1){
printf("白");
}
else{
printf("黑");
}
return;
}
void read_pos(int *x, int *y){
cin >> s;
if (int(s.length()) != 2){
*x = -1;
*y = -1;
return;
}
if (s[0] < '1' || s[0] > '8'){
*x = -1;
*y = -1;
return;
}
if (s[1] < 'a' || s[1] > 'h'){
*x = -1;
*y = -1;
return;
}
*x = 9 - (s[0] - '0');
*y = (s[1] - 'a') + 1;
return;
}
void chi(int play, int x, int y, int T){
print_player(play);
Sleep(T);
sleep_print("方的", T);
chess(x);
Sleep(T);
sleep_print("吃掉了", T);
print_player(3 - play);
Sleep(T);
sleep_print("方的", T);
chess(y);
Sleep(T);
printf("\n");
Sleep(T);
return;
}
int main(){
sleep_print("前言:\n", 20);
sleep_print("因为作者太弱了\n", 20);
sleep_print("所以这个游戏并不支持:\n", 20);
sleep_print(" 1.多人线上对战\n 2.人机对战\n等功能\n", 20);
Sleep(100);
sleep_print("\n而且,棋盘会比较大\n", 20);
sleep_print("所以作者建议把这个对话框最大化\n", 20);
Sleep(100);
sleep_print("\n所以你准备好了吗?\n", 20);
sleep_print("输入任意开始游戏!\n", 20);
cin >> s;
sleep_print("让我们开始吧!\n", 20);
Sleep(1000);
init();
player = 1;
while (true)
{
update();
sleep_print("你接下来要做什么操作?\n", 20);
Sleep(50);
sleep_print("要退出游戏输入0\n", 20);
sleep_print("要开始下棋输入1\n", 20);
// sleep_print("要自定义残局输入2\n", 20);
scanf("%d", &op);
Sleep(100);
if (op == 0)
{
system("cls");
sleep_print("欢迎你下次再来!", 20);
Sleep(1000);
break;
}
else if (op == 1)
{
update();
player = 1;
sleep_print("下面是坐标的输入格式:\n", 10);
sleep_print("输入两个字符\n", 20);
sleep_print("第一个是数字,代表行号\n", 20);
sleep_print("第二个是字母,代表列号\n", 20);
Sleep(50);
sleep_print("记下后输入任意继续...\n", 20);
cin >> s;
sleep_print("开始吧!\n", 10);
Sleep(500);
while (true){
while (true){
update();
sleep_print("现在是", 10);
print_player(player);
Sleep(10);
sleep_print("方走\n请问", 10);
print_player(player);
Sleep(10);
sleep_print("方,你要移动哪一个棋子?\n", 10);
read_pos(&nowx, &nowy);
if (nowx == -1 && nowy == -1){
sleep_print("你输入的不合法\n", 20);
Sleep(300);
continue;
}
if (game[nowx][nowy] == 0){
sleep_print("这个格子上没有棋子\n", 20);
Sleep(300);
continue;
}
if (color[nowx][nowy] != player){
sleep_print("这个棋子不是你的\n", 20);
Sleep(300);
continue;
}
break;
}
huan_qi = false;
while (true){
update();
sleep_print("你将要移动", 10);
printf("%d", 9 - nowx);
Sleep(10);
printf("%c", (char)(nowy - 1 + 'a'));
Sleep(10);
sleep_print("上的棋子\n", 10);
print_player(player);
Sleep(10);
sleep_print("方,你要换新的棋子吗?\n", 10);
sleep_print("要换新的棋子输入1\n", 10);
scanf("%d", &op);
if (op == 1){
sleep_print("已经放下那颗棋子\n", 10);
Sleep(200);
huan_qi = true;
break;
}
print_player(player);
Sleep(10);
sleep_print("方,你要把这一个棋子移动到哪里?\n", 10);
read_pos(&nextx, &nexty);
if (nextx == -1 && nexty == -1){
sleep_print("你输入的不合法\n", 20);
Sleep(300);
continue;
}
if (nextx == nowx && nexty == nowy){
sleep_print("不能移动到起点\n", 20);
Sleep(300);
continue;
}
int absx = nowx - nextx;
if (absx < 0){
absx = -absx;
}
int absy = nowy - nexty;
if (absy < 0){
absy = -absy;
}
if (game[nowx][nowy] == 1){
if (absx > 1 || absy > 1){
sleep_print("这个棋子无法移动到这一格\n", 10);
Sleep(200);
continue;
}
game[nowx][nowy] = 0;
color[nowx][nowy] = 0;
if (game[nextx][nexty] != 0){
if (color[nextx][nexty] != player){
chi(player, 1, game[nextx][nexty], 20);
Sleep(300);
}
else{
sleep_print("这个位置上是你的棋子\n", 10);
sleep_print("你不能吃掉自己的棋子\n", 10);
Sleep(300);
continue;
}
}
game[nextx][nexty] = 1;
color[nextx][nexty] = player;
break;
}
else if (game[nowx][nowy] == 2){
bool flag = false;
int dx = 0, dy = 0;
if (absx == absy){
if (nowx < nextx){
dx = 1;
}
else{
dx = -1;
}
if (nowy < nexty){
dy = 1;
}
else{
dy = -1;
}
flag = true;
}
else if (absx == 0 || absy == 0){
if (absx == 0){
if (nowy < nexty){
dy = 1;
}
else{
dy = -1;
}
}
else if (absy == 0){
if (nowx < nextx){
dx = 1;
}
else{
dx = -1;
}
}
flag = true;
}
if (flag == false){
sleep_print("这个棋子无法移动到这一格\n", 10);
Sleep(200);
continue;
}
game[nowx][nowy] = 0;
color[nowx][nowy] = 0;
int tmpx = nowx;
int tmpy = nowy;
flag = true;
while (nowx != nextx || nowy != nexty){
nowx += dx;
nowy += dy;
if ((nowx != nextx || nowy != nexty) && game[nowx][nowy] != 0){
flag = false;
sleep_print("后不能越子\n", 20);
Sleep(200);
break;
}
}
if (flag == false){
nowx = tmpx;
nowy = tmpy;
game[nowx][nowy] = 2;
color[nowx][nowy] = player;
continue;
}
if (game[nextx][nexty] != 0){
if (color[nextx][nexty] == player){
sleep_print("这个位置上有你的棋子\n", 10);
sleep_print("你不能吃掉自己的棋子\n", 10);
Sleep(300);
nowx = tmpx;
nowy = tmpy;
game[nowx][nowy] = 2;
color[nowx][nowy] = player;
continue;
}
else{
chi(player, 2, game[nextx][nexty], 20);
Sleep(300);
}
}
game[nextx][nexty] = 2;
color[nextx][nexty] = player;
break;
}
else if (game[nowx][nowy] == 3){
int dx = 0, dy = 0;
if (absx != 0 && absy != 0){
sleep_print("这个棋子无法移动到这一格\n", 20);
Sleep(300);
continue;
}
if (absx == 0){
if (nowy < nexty){
dy = 1;
}
else{
dy = -1;