跳到正文

休闲·娱乐

当蒟蒻OIer因无法摸鱼而养鱼之2.0——跳棋

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

文章信息

发布
2023年8月7日
收录
2025年12月12日
更新
2026年11月20日
篇幅
约 20 分钟

版本

  1. v1当前

在机房里呆了一天好烦啊

于是随手写了个跳棋(

操作方式什么的程序里有

C++
#define mp make_pair
#include <algorithm>
#include <cctype>
#include <chrono>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <map>
#include <random>
#include <vector>
#include <windows.h>
#include <conio.h>
using namespace std;
const int N = 20;
const char BMap[N][N]{
	"                   ",
	"     X             ",
	"     XX            ",
	"     XXX           ",
	"     XXXX          ",
	" XXXXOOOOOXXXX     ",
	"  XXXOOOOOOXXX     ",
	"   XXOOOOOOOXX     ",
	"    XOOOOOOOOX     ",
	"     OOOOOOOOO     ",
	"     XOOOOOOOOX    ",
	"     XXOOOOOOOXX   ",
	"     XXXOOOOOOXXX  ",
	"     XXXXOOOOOXXXX ",
	"          XXXX     ",
	"           XXX     ",
	"            XX     ",
	"             X     ",
	"                   ",
	"                   "};
namespace Base
{
	mt19937_64 gen(chrono::system_clock::now().time_since_epoch().count());
	int Rand(int l = 0, int r = 2147483647)
	{
		uniform_int_distribution<> rd(l, r);
		return rd(gen);
	}
	void Move(int x, int y)
	{
		HANDLE hout;
		COORD coord;
		coord.X = y;
		coord.Y = x + 2;
		hout = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleCursorPosition(hout, coord);
	}
	const int FBlack = 0, FBlue = 1, FGreen = 2, FCyan = 3, FRed = 4, FPurple = 5, FYellow = 6, FWhite = 7;
	const int FLBlack = 8, FLBlue = 9, FLGreen = 10, FLCyan = 11, FLRed = 12, FLPurple = 13, FLYellow = 14, FLWhite = 15;
	const int BBlack = 0, BBlue = 16, BGreen = 32, BCyan = 48, BRed = 64, BPurple = 80, BYellow = 96, BWhite = 112;
	const int BLBlack = 128, BLBlue = 144, BLGreen = 160, BLCyan = 176, BLRed = 192, BLPurple = 208, BLYellow = 224, BLWhite = 240;
	void Color(int c) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c); }
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1 : 0)
	bool Check(char ch) { return KEY_DOWN(ch); }
#undef KEY_DOWN
	void Clear() { system("cls"); }
#define gc getch
};
using namespace Base;
namespace CHNCheckers
{
	char mp0[N][N];
	int col[N][N];
	vector<pair<int, int>> Goal[6];
	vector<pair<int, int>> Chess[6];
	const int W = 6, mx[W]{-1, -1, 0, 0, 1, 1}, my[W]{-1, 0, -1, 1, 0, 1};
	int p[7];
	int GetChess(int x) { return (x >> 8) & 15; }
	int GetType(int x) { return (x >> 4) & 15; }
	int GetID(int x) { return x & 15; }
	int Merge(int c, int t, int i) { return (c << 8) | (t << 4) | i; }
	void InitPlayer(int n)
	{
		for (int i = 0; i < 6; ++i)
			Chess[i].clear(), Goal[i].clear();
		if (1) // * P1->P6
		{
			Goal[0].push_back(mp(14, 10));
			Goal[0].push_back(mp(14, 11));
			Goal[0].push_back(mp(14, 12));
			Goal[0].push_back(mp(14, 13));
			Goal[0].push_back(mp(15, 11));
			Goal[0].push_back(mp(15, 12));
			Goal[0].push_back(mp(15, 13));
			Goal[0].push_back(mp(16, 12));
			Goal[0].push_back(mp(16, 13));
			Goal[0].push_back(mp(17, 13));
		}
		if (1) // * P2->P5
		{
			Goal[1].push_back(mp(10, 14));
			Goal[1].push_back(mp(11, 14));
			Goal[1].push_back(mp(11, 15));
			Goal[1].push_back(mp(12, 14));
			Goal[1].push_back(mp(12, 15));
			Goal[1].push_back(mp(12, 16));
			Goal[1].push_back(mp(13, 14));
			Goal[1].push_back(mp(13, 15));
			Goal[1].push_back(mp(13, 16));
			Goal[1].push_back(mp(13, 17));
		}
		if (1) // * P3->P4
		{
			Goal[2].push_back(mp(10, 5));
			Goal[2].push_back(mp(11, 5));
			Goal[2].push_back(mp(11, 6));
			Goal[2].push_back(mp(12, 5));
			Goal[2].push_back(mp(12, 6));
			Goal[2].push_back(mp(12, 7));
			Goal[2].push_back(mp(13, 5));
			Goal[2].push_back(mp(13, 6));
			Goal[2].push_back(mp(13, 7));
			Goal[2].push_back(mp(13, 8));
		}
		if (1) // * P4->P3
		{
			Goal[3].push_back(mp(5, 10));
			Goal[3].push_back(mp(5, 11));
			Goal[3].push_back(mp(5, 12));
			Goal[3].push_back(mp(5, 13));
			Goal[3].push_back(mp(6, 11));
			Goal[3].push_back(mp(6, 12));
			Goal[3].push_back(mp(6, 13));
			Goal[3].push_back(mp(7, 12));
			Goal[3].push_back(mp(7, 13));
			Goal[3].push_back(mp(8, 13));
		}
		if (1) // * P5->P2
		{
			Goal[4].push_back(mp(5, 1));
			Goal[4].push_back(mp(5, 2));
			Goal[4].push_back(mp(5, 3));
			Goal[4].push_back(mp(5, 4));
			Goal[4].push_back(mp(6, 2));
			Goal[4].push_back(mp(6, 3));
			Goal[4].push_back(mp(6, 4));
			Goal[4].push_back(mp(7, 3));
			Goal[4].push_back(mp(7, 4));
			Goal[4].push_back(mp(8, 4));
		}
		if (1) // * P6->P1
		{
			Goal[5].push_back(mp(1, 5));
			Goal[5].push_back(mp(2, 5));
			Goal[5].push_back(mp(2, 6));
			Goal[5].push_back(mp(3, 5));
			Goal[5].push_back(mp(3, 6));
			Goal[5].push_back(mp(3, 7));
			Goal[5].push_back(mp(4, 5));
			Goal[5].push_back(mp(4, 6));
			Goal[5].push_back(mp(4, 7));
			Goal[5].push_back(mp(4, 8));
		}
		if (n == 2)
		{
			Chess[0] = Goal[5], p[1] = 0;
			Chess[5] = Goal[0], p[2] = 5;
		}
		else if (n == 3)
		{
			Chess[0] = Goal[5], p[1] = 0;
			Chess[3] = Goal[2], p[2] = 3;
			Chess[4] = Goal[1], p[3] = 4;
		}
		else if (n == 4)
		{
			Chess[1] = Goal[4], p[1] = 1;
			Chess[2] = Goal[3], p[2] = 2;
			Chess[3] = Goal[2], p[3] = 3;
			Chess[4] = Goal[1], p[4] = 4;
		}
		else if (n == 6)
		{
			Chess[0] = Goal[5], p[1] = 0;
			Chess[1] = Goal[4], p[2] = 1;
			Chess[2] = Goal[3], p[3] = 2;
			Chess[3] = Goal[2], p[4] = 3;
			Chess[4] = Goal[1], p[5] = 4;
			Chess[5] = Goal[0], p[6] = 5;
		}
	}
	void DrawMap()
	{
		memcpy(mp0, BMap, sizeof(mp0));
		for (int i = 0; i < N; ++i)
			for (int j = 0; j < N; ++j)
				if (mp0[i][j] == 'O')
					col[i][j] = BWhite;
				else if (mp0[i][j] == ' ')
					col[i][j] = 0;
		for (int i = 0; i < 6; ++i)
			for (auto j : Goal[i])
				col[j.first][j.second] = (i + 1) << 4;
	}
	void DrawPlayer(int n)
	{
		for (int i = 1; i <= n; ++i)
		{
			int j = p[i];
			for (int k = 0; k < 10; ++k)
			{
				auto l = Chess[j][k];
				mp0[l.first][l.second] = k + 48;
				col[l.first][l.second] |= (j + 1) | 8;
			}
		}
	}
	pair<int, int> Address(int x, int y) { return make_pair(x, y * 2 + N - x); }
	void PrintMap()
	{
		for (int i = 0; i < N; ++i)
			for (int j = 0; j < N; ++j)
			{
				auto k = Address(i, j);
				Move(k.first, k.second);
				Color(col[i][j]);
				cout << mp0[i][j];
			}
		Color(FWhite);
	}
	void Print(int n)
	{
		DrawMap();
		DrawPlayer(n);
		PrintMap();
	}
	bool Over(int x)
	{
		bool f = false;
		for (auto i : Chess[x])
		{
			f = false;
			for (auto j : Goal[x])
				if (i == j)
				{
					f = true;
					break;
				}
			if (!f)
				return false;
		}
		return true;
	}
	bool CheckSpace(int x, int y)
	{
		if (BMap[x][y] != 'O' && BMap[x][y] != 'X')
			return true;
		return false;
	}
	bool CheckDist(int x, int y)
	{
		if (CheckSpace(x, y))
			return false;
		for (int i = 0; i < 6; ++i)
			for (auto j : Chess[i])
				if (j == mp(x, y))
					return true;
		return false;
	}
	bool CheckMove(int x, int c, int w, bool d)
	{
		if (d)
		{
			pair<int, int> &a = Chess[x][c];
			pair<int, int> b = a, c;
			int d;
			b.first += mx[w], b.second += my[w];
			for (d = 1; !CheckDist(b.first, b.second) && !CheckSpace(b.first, b.second); ++d)
				b.first += mx[w], b.second += my[w];
			if (CheckSpace(b.first, b.second))
				return false;
			c = b;
			c.first += mx[w], c.second += my[w];
			for (int e = 1; e < d && !CheckDist(c.first, c.second) && !CheckSpace(c.first, c.second); ++e)
				c.first += mx[w], c.second += my[w];
			if (CheckSpace(c.first, c.second) || CheckDist(c.first, c.second))
				return false;
			a = c;
			return true;
		}
		else
		{
			pair<int, int> &a = Chess[x][c];
			if (CheckDist(a.first + mx[w], a.second + my[w]) || CheckSpace(a.first + mx[w], a.second + my[w]))
				return false;
			a.first += mx[w], a.second += my[w];
			return true;
		}
	}
	void MoveChess(int i, int n)
	{
		Move(-2, 0);
		Color((p[i] + 1) | 8);
		cout << "Player " << i << ':';
		Color(FWhite);
		bool d;
		char ch;
		int w, c;
Chess:
		// Chess
		Move(-1, 0);
		cout << "Please select your chess(0~9):                                                      ";
		ch = gc();
		if (isdigit(ch))
			c = ch - 48;
		else
		{
			Move(-2, 9);
			Color(FLRed);
			cout << "Input Error                                                            ";
			Color(FWhite);
			goto Chess;
		}
Type:
		// Type
		Move(-1, 0);
		cout << "Walk or Jump (W/J)?                                                                 ";
		ch = toupper(gc());
		if (ch == 'W')
			d = false;
		else if (ch == 'J')
			d = true;
		else
		{
			Move(-2, 9);
			Color(FLRed);
			cout << "Input Error                                                                         ";
			Color(FWhite);
			goto Type;
		}
Way:
		// Way
		Move(-1, 0);
		cout << "Northwest(W), Northeast(E), West(A), East(D), Southwest(Z), Southeast(X) or Stay(S)?";
		ch = toupper(gc());
		if (ch == 'W')
			w = 0;
		else if (ch == 'E')
			w = 1;
		else if (ch == 'A')
			w = 2;
		else if (ch == 'D')
			w = 3;
		else if (ch == 'Z')
			w = 4;
		else if (ch == 'X')
			w = 5;
		else if (ch == 'S')
			w = 6;
		else
		{
			Move(-2, 9);
			Color(FLRed);
			cout << "Input Error                                                                         ";
			Color(FWhite);
			goto Way;
		}
		if (w != 6)
		{
			bool f = CheckMove(p[i], c, w, d);
			Move(-2, 9);
			if (!f)
			{
				Color(FLRed);
				cout << "Move Failed                                                                         ";
				Color(FWhite);
				goto Way;
			}
			else
			{
				Color(FLGreen);
				cout << "Move Succesfully                                                                    ";
				Print(n);
				Color(FWhite);
			}
			if (d)
				goto Way;
		}
	}
	int Play(int n)
	{
		int i = n;
		bool f;
		Print(n);
		while (Over(p[i]) == 0)
		{
			if (i++ == n)
				i = 1;
			Move(-2, 0);
			MoveChess(i, n);
			Print(n);
		}
		return i;
	}
	void GameOver(int i)
	{
		Clear();
		Color(FLRed);
		cout << "Game Over\n";
		Color((p[i] + 1) | 8);
		cout << "Winner: Player " << i;
		Color(FWhite);
		system("pause");
	}
	void Game()
	{
        system("mode con lines=30 cols=100");
		Clear();
		int n;
		cout << "Please enter the number of players (2/3/4/6):";
		n = gc() - 48;
		while (n != 2 && n != 3 && n != 4 && n != 6)
		{
			Clear();
			cout << "Please enter the number of players (2/3/4/6):";
			n = gc() - 48;
		}
		Clear();
		InitPlayer(n);
		int win = Play(n);
		GameOver(win);
	}
}
int main()
{
	CHNCheckers::Game();
	return 0;
}
原文
抓取于
  1. liangkairui

    四阔以