Magic shape card

content

One, the rules

Second, picture production

3. Code V1

Fourth, code V2


One, the rules

The correct set of cards is three cards with different four characteristics.

There is another version, if the three cards are exactly the same, it feels like an unlimited number of cards and unlimited rounds of play, which may be to see who plays first to a score.

Next I will implement the unlimited version.

Second, picture production

First, use PPT to quickly draw 27 pictures

Resize has been done with opencv here

Then use opencv to generate other graphs:

int main()
{
	for (int k = 1; k <= 27; k++)
	{
		string s = to_string(k);
		Mat img = imread("D:/set/img (" + s + ").png", 1);
		Mat img2 = Mat(Size(img.cols, img.rows * 2), img.type());
		for (int i = 0; i < img2.rows; i++)for (int j = 0; j < img.cols; j++)
		{
			img2.at<Vec3b>(i, j) = img.at<Vec3b>(i % img.rows, j);
		}
		s = "D:/set/" + to_string(k+100) + ".png";
		imwrite(s, img2);
	}
	return 0;
}

 In the same way, 27 pictures of three elements are obtained, a total of 81 pictures

 Then all the pictures of the same size

All are pictures with a width of 400 pixels and a height of 660 pixels.

Package download

3. Code V1

Use 81 pictures to make mini games.

Each time, if there is no solution, add 3 cards. If there is a solution, after removing these 3 cards, if there are less than 12 cards, add 3 cards.

Full code:



#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<opencv2/opencv.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/core/mat.hpp>
using namespace std;
using namespace cv;

#pragma comment(lib,"../x64/vc14/lib/opencv_world452.lib")
#pragma comment(lib,"../x64/vc14/lib/opencv_world452d.lib")


int row = 3;
int theRatio = 4;
int w = 400/ theRatio, h = 660/ theRatio, type = 16;
vector<int>v;
Mat imgs;
Mat allImg[81];

void draw(int k, int n)
{
	int col = v.size() / row, r = k / col, c = k % col;
	Mat img = allImg[n];
	for (int i = 0; i < img.rows; i++)for (int j = 0; j < img.cols; j++)
	{
		imgs.at<Vec3b>(i + r * h, j + c * w) = img.at<Vec3b>(i, j);
	}
}

void drawAll()//根据v来显示所有图片
{
	int col = v.size() / row;
	imgs = Mat(Size(w * col, h * row), type);
	for (int i = 0; i < row * col; i++)draw(i, v[i]);
}

void Init()
{
	for (int i = 0; i < 81; i++) {
		allImg[i] = imread("D:/set/img (" + to_string(i+1) + ").png", 1);
		resize(allImg[i], allImg[i], Size(w, h), 0, 0);
	}
	int col = v.size() / row;
	drawAll();
}

void add3()
{
	v.push_back(rand() % 81);
	v.push_back(rand() % 81);
	v.push_back(rand() % 81);
}

void Play()
{
	imshow("img", imgs);
	int a, b, c;
	waitKey(0);
	cout << "输入3张牌的序号(1-"<<v.size()<<"),0 代表无解\n";
	cin >> a;
	if (a <= 0 || a > v.size()) {
		add3();
		return Init();
	}
	cin >> b >> c;
	if (b <= 0 || b > v.size())return Play();
	if (c <= 0 || c > v.size())return Play();
	a--, b--, c--;
	vector<int>id = { a,b,c };
	sort(id.begin(), id.end());
	v.erase(v.begin() + id[2]);
	v.erase(v.begin() + id[1]);
	v.erase(v.begin() + id[0]);
	if (v.size() < 12)add3();
	drawAll();
}

int main()
{
	srand((unsigned)time(NULL));
	while(v.size() < 12)v.push_back(rand() % 81);
	int col = v.size() / row;
	imgs = Mat(Size(w * col, h * row), type);
	Init();
	while(true)Play();
	return 0;
}

Fourth, code V2

As an electronic version of the game, the advantage over the physical version is that it can be verified, so you can add verification.

Change the licensing mechanism to automatic verification, and automatically deal the cards when there is no solution.

At the same time, the bug was fixed, and the cards were scrambled after each draw, because one of the newly drawn cards must be a member of the correct deck.

Full code:



#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<opencv2/opencv.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/core/mat.hpp>
using namespace std;
using namespace cv;


#pragma comment(lib,"../x64/vc14/lib/opencv_world452.lib")
#pragma comment(lib,"../x64/vc14/lib/opencv_world452d.lib")


int row = 3;
int theRatio = 4;
int w = 400 / theRatio, h = 660 / theRatio, type = 16;
vector<int>v;
Mat imgs;
Mat allImg[81];

void add3()
{
	v.push_back(rand() % 81);
	v.push_back(rand() % 81);
	v.push_back(rand() % 81);
}

void draw(int k, int n)
{
	int col = v.size() / row, r = k / col, c = k % col;
	Mat img = allImg[n];
	for (int i = 0; i < img.rows; i++)for (int j = 0; j < img.cols; j++)
	{
		imgs.at<Vec3b>(i + r * h, j + c * w) = img.at<Vec3b>(i, j);
	}
}

bool check(int a, int b, int c) // 检查第a b c张牌, 范围是0-80
{
	if (a == b && b == c)return true;//三张牌完全相同
	// 检查3张牌的4个属性都不同
	int s = 81;
	while (s > 1) {
		s /= 3;
		if (a / s == b / s || b / s == c / s || a / s == c / s)return false;
		a %= s, b %= s, c %= s;
	}
	return true;
}
bool check()
{
	for (int i = 0; i < v.size(); i++)for (int j = i + 1; j < v.size(); j++)for (int k = j + 1; k < v.size(); k++) {
		if (check(v[i], v[j], v[k]))return true;
	}
	return false;
}
void fresh()
{
	while (!check())add3();
	for (int i = 0; i < 50; i++)
	{
		int a = rand() % v.size(), b = rand() % v.size();
		int tmp = v[a];
		v[a] = v[b], v[b] = tmp;
	}
	int col = v.size() / row;
	imgs = Mat(Size(w * col, h * row), type);
	for (int i = 0; i < row * col; i++)draw(i, v[i]);
}

void Init()
{
	for (int i = 0; i < 81; i++) {
		allImg[i] = imread("D:/set/img (" + to_string(i + 1) + ").png", 1);
		resize(allImg[i], allImg[i], Size(w, h), 0, 0);
	}
	int col = v.size() / row;
	fresh();
}

void Play()
{
	imshow("img", imgs);
	int a, b, c;
	waitKey(0);
	cout << "输入3张牌的序号(1-" << v.size() << ")\n";
	cin >> a >> b >> c;
	a--, b--, c--;
	vector<int>id = { a,b,c };
	sort(id.begin(), id.end());
	a = id[0], b = id[1], c = id[2];
	if (a < 0 || a >= v.size())return;
	if (b < 0 || b >= v.size())return;
	if (c < 0 || c >= v.size())return;
	if (a == b || b == c)return;
	if (!check(v[a], v[b], v[c]))return;
	v.erase(v.begin() + c);
	v.erase(v.begin() + b);
	v.erase(v.begin() + a);
	fresh();
}

int main()
{
	srand((unsigned)time(NULL));
	while (v.size() < 12)v.push_back(rand() % 81);
	int col = v.size() / row;
	imgs = Mat(Size(w * col, h * row), type);
	Init();
	while (true)Play();
	return 0;
}

Guess you like

Origin blog.csdn.net/nameofcsdn/article/details/123776601