跳到正文

个人记录

【fsc原创】象棋

fsc0190约 20 分钟
原文
文章信息与版本

文章信息

发布
2026年10月6日
收录
2026年10月6日
更新
2026年11月19日
篇幅
约 20 分钟

版本

  1. v4当前
  2. v3
  3. v2
  4. v1

代码量 10.2k 耗时 2.5h

觉得好玩就点个赞吧

注意:绝杀之后不会显示绝杀,而是无法移动 不管怎么动都会显示不正确的操作,这个bug以后有时间会修掉

C++
#include<bits/stdc++.h>
#include<conio.h>
#include<cstdlib>
#include<ctime>
using namespace std;
int a[10][9];
int turn = 1;
bool gameover = 0;
void longstop ()
{
	auto now1 = chrono::system_clock::now();
	auto ms1 = chrono::duration_cast<std::chrono::milliseconds>(now1.time_since_epoch()).count();
	while (1)
	{
		auto now2 = chrono::system_clock::now();
		auto ms2 = chrono::duration_cast<std::chrono::milliseconds>(now2.time_since_epoch()).count();
		if (ms2-ms1 >= 2000)
		{
			break;
		}
	}
}
void inita()
{
    for (int i=0;i<10;i++)
    {
        for (int j=0;j<9;j++)
        {
            a[i][j] = 0;
		}
	}
    a[0][0] = -5;
	a[0][1] = -4;
	a[0][2] = -3;
	a[0][3] = -2;
	a[0][4] = -1;
    a[0][5] = -2;
	a[0][6] = -3;
	a[0][7] = -4;
	a[0][8] = -5;
    a[2][1] = -6;
	a[2][7] = -6;
    a[3][0] = -7;
	a[3][2] = -7;
	a[3][4] = -7;
	a[3][6] = -7;
	a[3][8] = -7;
    a[9][0] = 5;
	a[9][1] = 4;
	a[9][2] = 3;
	a[9][3] = 2;
	a[9][4] = 1;
    a[9][5] = 2;
	a[9][6] = 3;
	a[9][7] = 4;
	a[9][8] = 5;
    a[7][1] = 6;
	a[7][7] = 6;
    a[6][0] = 7;
	a[6][2] = 7;
	a[6][4] = 7;
	a[6][6] = 7;
	a[6][8] = 7;
}
int gtcmp (int piece)
{
    if (piece == 0)
	{
		return 0;
	}
    return piece > 0 ? 1 : -1;
}
int gttp (int piece)
{
    if (piece == 0)
	{
		return 0;
	}
    return piece > 0 ? piece : -piece;
}
void print()
{
    system("cls");
    cout << "   0   1   2   3   4   5   6   7   8" << endl;
    cout << " -------------------------------------" << endl;
    for (int i=0;i<10;i++)
	{
        cout << i << "|";
        for (int j=0;j<9;j++)
		{
            int p = a[i][j];
            char c = ' ';
            if (p == 0)
			{
				c = '+';
			}
            else if (p == 1)
			{
				c = 'K';
			}
            else if (p == 2)
			{
				c = 'A';
			}
            else if (p == 3)
			{
				c = 'E';
			}
            else if (p == 4)
			{
				c = 'H';
			}
            else if (p == 5)
			{
				c = 'R';
			}
            else if (p == 6)
			{
				c = 'C';
			}
            else if (p == 7)
			{
				c = 'S';
			}
            else if (p == -1)
			{
				c = 'k';
			}
            else if (p == -2)
			{
				c = 'a';
			}
            else if (p == -3)
			{
				c = 'e';
			}
            else if (p == -4)
			{
				c = 'h';
			}
            else if (p == -5)
			{
				c = 'r';
			}
            else if (p == -6)
			{
				c = 'c';
			}
            else if (p == -7)
			{
				c = 's';
			}
            cout << " " << c << " |";
        }
        cout << endl;
        if (i == 4)
		{
			cout << " -------------------------------------" << endl;
		}
        else if (i < 9)
		{
			cout << " |   |   |   |   |   |   |   |   |" << endl;
		}
    }
    cout << " -------------------------------------" << endl;
    cout << "大写为红方,小写为黑方 K/k=王, R/r=车, H/h=马, C/c=炮, E/e=象, A/a=士, S/s=兵" << endl;
}
bool ina (int r,int c)
{
    return r >= 0 && r < 10 && c >= 0 && c < 9;
}
bool vaild (int fr,int fc,int tr,int tc)
{
    int piece = a[fr][fc];
    int tar = a[tr][tc];
    int camp = gtcmp(piece);
    int type = gttp(piece);
    if (gtcmp(tar) == camp)
	{
		return 0;
	}
    int r = tr-fr;
    int c = tc-fc;
    int ar = abs(r);
    int ac = abs(c);
    if (type == 1)
	{
        if (tc < 3 || tc > 5)
        {
        	return 0;
		}
        if (tr < 7 || tr > 9)
        {
        	return 0;
		}
        if (ar+ac != 1)
        {
        	return 0;
		}
    }
    else if (type == -1)
	{
        if (tc < 3 || tc > 5)
        {
        	return 0;
		}
        if (tr < 0 || tr > 2)
        {
        	return 0;
		}
        if (ar+ac != 1)
        {
        	return 0;
		}
    }
    else if (type == 2)
	{
        if (tc < 3 || tc > 5)
        {
        	return 0;
		}
        if (tr < 7 || tr > 9)
        {
        	return 0;
		}
        if (ar != 1 || ac != 1)
        {
        	return 0;
		}
    }
    else if (type == -2)
	{
        if (tc < 3 || tc > 5)
        {
        	return 0;
		}
        if (tr < 0 || tr > 2)
        {
        	return 0;
		}
        if (ar != 1 || ac != 1)
        {
        	return 0;
		}
    }
    else if (type == 3)
	{
        if (ar != 2 || ac != 2)
        {
        	return 0;
		}
        if (tr < 5)
        {
        	return 0;
		}
        if (a[fr+r/2][fc+c/2] != 0)
        {
        	return 0;
		}
    }
    else if (type == -3)
	{
        if (ar != 2 || ac != 2)
        {
        	return 0;
		}
        if (tr > 4)
        {
        	return 0;
		}
        if (a[fr+r/2][fc+c/2] != 0)
        {
        	return 0;
		}
    }
    else if (type == 4 || type == -4)
	{
        if (!((ar == 2 && ac == 1) || (ar == 1 && ac == 2)))
        {
        	return 0;
		}
        if (ar == 2)
		{
            if (a[fr+r/2][fc] != 0)
	        {
	        	return 0;
			}
        }
		else
		{
            if (a[fr][fc+c/2] != 0)
	        {
	        	return 0;
			}
        }
    }
    else if (type == 5 || type == -5)
	{
        if (fr != tr && fc != tc)
        {
        	return 0;
		}
        if (fr == tr)
		{
            int step = c > 0 ? 1 : -1;
            for (int k=fc+step;k!=tc;k+=step)
			{
		        if (a[fr][k] != 0)
		        {
		        	return 0;
				}
            }
        }
		else
		{
            int step = r > 0 ? 1 : -1;
            for (int k=fr+step;k!=tr;k+=step)
			{
                if (a[k][fc] != 0)
		        {
		        	return 0;
				}
            }
        }
    }
    else if (type == 6 || type == -6)
	{
        if (fr != tr && fc != tc)
        {
        	return 0;
		}
        int cnt = 0;
        if (fr == tr)
		{
            int step = c > 0 ? 1 : -1;
            for (int k=fc+step;k!=tc;k+=step)
			{
                if (a[fr][k] != 0)
                {
                	cnt++; 
				}
            }
        }
		else
		{
            int step = r > 0 ? 1 : -1;
            for (int k=fr+step;k!=tr;k+=step)
			{
                if (a[k][fc] != 0)
                {
                	cnt++; 
				}
            }
        }
        if (tar == 0)
		{
            if (cnt != 0)
	        {
	        	return 0;
			}
        }
		else
		{
            if (cnt != 1)
	        {
	        	return 0;
			}
        }
    }
    else if (type == 7)
	{
        if (r > 0)
        {
        	return 0;
		}
        if (fr > 4)
		{
            if (ac != 0 || r != -1)
	        {
	        	return 0;
			}
        }
		else
		{
            if (ar+ac != 1)
	        {
	        	return 0;
			}
        }
    }
    else if (type == -7)
	{
        if (r < 0)
        {
        	return 0;
		}
        if (fr < 5)
		{
            if (ac != 0 || r != 1)
	        {
	        	return 0;
			}
        }
		else
		{
            if (ar+ac != 1)
	        {
	        	return 0;
			}
        }
    }
    else
	{
        return 0;
    }
    return 1;
}
bool checkFlyingGeneral(int fr, int fc, int tr, int tc)
{
    int tempTarget = a[tr][tc];
    a[tr][tc] = a[fr][fc];
    a[fr][fc] = 0;
    bool valid = true;
    int redKingR = -1, redKingC = -1;
    int blackKingR = -1, blackKingC = -1;
    for(int i=0; i<10; i++)
	{
        for(int j=3; j<=5; j++)
		{
            if(a[i][j] == 1)
			{
				redKingR = i;
				redKingC = j;
			}
            if (a[i][j] == -1)
			{
				blackKingR = i;
				blackKingC = j;
			}
        }
    }
    if (redKingR != -1 && blackKingR != -1 && redKingC == blackKingC)
	{
        int minR = min(redKingR, blackKingR);
        int maxR = max(redKingR, blackKingR);
        bool hasBlocker = false;
        for(int i = minR+1;i<maxR;i++)
		{
            if(a[i][redKingC] != 0)
			{
                hasBlocker = 1;
                break;
            }
        }
        if (hasBlocker == 0)
		{
			valid = 0;
		}
    }
    a[fr][fc] = a[tr][tc];
    a[tr][tc] = tempTarget;
    return valid;
}
void mv (int fr,int fc,int tr,int tc)
{
    a[tr][tc] = a[fr][fc];
    a[fr][fc] = 0;
}
void check ()
{
    bool rflag = 0;
    bool bflag = 0;
    for (int i=0;i<10;i++)
	{
        for (int j=0;j<9;j++)
		{
            if (a[i][j] == 1) rflag = 1;
            if (a[i][j] == -1) bflag = 1;
        }
    }
    if (rflag == 0)
	{
        cout << "黑方获胜!" << endl;
        gameover = 1;
    }
	else if (bflag == 0)
	{
        cout << "红方获胜!" << endl;
        gameover = 1;
    }
}
int main()
{
	cout << "欢迎游玩象棋 fsc出品 必属精品" << endl;
	longstop();
	cout << "游玩之前,能关注luogu:fsc0190和bilibili:Oierfsc吗plzplz,这对我有很大的激励去制作新游戏" << endl;
	longstop();
	cout << "按任意键以继续";
    _getch();
    system("cls");
    inita();
    while (gameover == 0) 
	{
        print();
        string turnName = (turn == 1) ? "红" : "黑";
        cout << "回合: " << turnName << endl;
        cout << "输入要移动的子的行号、列号和要将他移动到的位置的行号、列号" << endl;
        int fr,fc,tr,tc;
        if (!(cin >> fr >> fc >> tr >> tc))
		{
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "输入错误,请输入数字" << endl;
            system("pause");
            continue;
        }
        if (ina(fr,fc) == 0 || ina(tr,tc) == 0)
		{
            cout << "坐标超出范围" << endl;
            system("pause");
            continue;
        }
        int piece = a[fr][fc];
        if (piece == 0)
		{
            cout << "这里没有棋子" << endl;
            system("pause");
            continue;
        }
        if (gtcmp(piece) != turn)
		{
            cout << "这不是你方的棋子" << endl;
            system("pause");
            continue;
        }
        if (vaild(fr,fc,tr,tc))
		{
            if (checkFlyingGeneral(fr,fc,tr,tc) == 0)
			{
                cout << "将帅不能见面" << endl;
                system("pause");
                continue;
            }
            mv(fr,fc,tr,tc);
            check();
            if (gameover == 0)
			{
                turn = -turn;
            }
        }
		else
		{
            cout << "不正确的操作,请重新操作" << endl;
            system("pause");
        }
    }
    cout << "将杀!";
    system("pause");
    return 0;
}
原文