以数织图游戏C++代码
时间: 2023-10-08 19:14:29 浏览: 167
以下是一个简单的数织图游戏的 C++ 代码,其中包括了数织图的生成、打印和玩家填充的功能。请注意,该代码仅提供了基本框架,你可以根据自己的需求进行修改和扩展。
```
#include <iostream>
#include <vector>
using namespace std;
// 生成数织图的函数
vector<vector<int>> generateNonogram(int rows, int cols, vector<vector<int>>& rowHints, vector<vector<int>>& colHints) {
vector<vector<int>> nonogram(rows, vector<int>(cols, -1)); // 初始化为-1
// 根据行提示和列提示生成数织图
for (int i = 0; i < rows; i++) {
int index = 0;
for (int j = 0; j < cols; j++) {
if (nonogram[i][j] == -1) {
if (index < rowHints[i].size() && j == rowHints[i][index]) {
int len = rowHints[i][index + 1];
for (int k = j; k < j + len; k++) {
nonogram[i][k] = 1;
}
j += len;
index += 2;
}
}
}
}
for (int j = 0; j < cols; j++) {
int index = 0;
for (int i = 0; i < rows; i++) {
if (nonogram[i][j] == -1) {
if (index < colHints[j].size() && i == colHints[j][index]) {
int len = colHints[j][index + 1];
for (int k = i; k < i + len; k++) {
nonogram[k][j] = 1;
}
i += len;
index += 2;
}
}
}
}
return nonogram;
}
// 打印数织图的函数
void printNonogram(vector<vector<int>>& nonogram, vector<vector<int>>& rowHints, vector<vector<int>>& colHints) {
int rows = nonogram.size();
int cols = nonogram[0].size();
// 打印列提示
cout << " ";
for (int j = 0; j < cols; j++) {
int sum = 0;
for (int i = 0; i < colHints[j].size(); i += 2) {
sum += colHints[j][i + 1];
}
int space = max(0, (int)rowHints[0].size() * 2 - sum);
for (int k = 0; k < space / 2; k++) {
cout << " ";
}
for (int i = 0; i < colHints[j].size(); i += 2) {
int len = colHints[j][i + 1];
for (int k = 0; k < len; k++) {
cout << colHints[j][i];
}
for (int k = 0; k < 2; k++) {
cout << " ";
}
}
for (int k = 0; k < space - space / 2; k++) {
cout << " ";
}
}
cout << endl;
// 打印数织图和行提示
for (int i = 0; i < rows; i++) {
int sum = 0;
for (int k = 0; k < rowHints[i].size(); k += 2) {
sum += rowHints[i][k + 1];
}
int space = max(0, (int)colHints[0].size() * 2 - sum);
for (int k = 0; k < space / 2; k++) {
cout << " ";
}
for (int k = 0; k < rowHints[i].size(); k += 2) {
int len = rowHints[i][k + 1];
for (int j = 0; j < len; j++) {
cout << rowHints[i][k];
}
for (int j = 0; j < 2; j++) {
cout << " ";
}
}
for (int k = 0; k < space - space / 2; k++) {
cout << " ";
}
for (int j = 0; j < cols; j++) {
if (nonogram[i][j] == -1) {
cout << ".";
}
else if (nonogram[i][j] == 0) {
cout << " ";
}
else {
cout << "#";
}
cout << " ";
}
cout << endl;
}
}
int main() {
int rows, cols;
cout << "请输入数织图的行数:";
cin >> rows;
cout << "请输入数织图的列数:";
cin >> cols;
// 输入行提示和列提示
cout << "请按行输入行提示(以-1结束):" << endl;
vector<vector<int>> rowHints(rows);
for (int i = 0; i < rows; i++) {
int hint;
while (true) {
cin >> hint;
if (hint == -1) {
break;
}
rowHints[i].push_back(hint);
}
}
cout << "请按列输入列提示(以-1结束):" << endl;
vector<vector<int>> colHints(cols);
for (int j = 0; j < cols; j++) {
int hint;
while (true) {
cin >> hint;
if (hint == -1) {
break;
}
colHints[j].push_back(hint);
}
}
// 生成数织图并打印
vector<vector<int>> nonogram = generateNonogram(rows, cols, rowHints, colHints);
printNonogram(nonogram, rowHints, colHints);
// 让玩家填充数织图
while (true) {
int x, y, val;
cout << "请输入要填充的行列号和数字(以-1结束):";
cin >> x >> y >> val;
if (x == -1 || y == -1 || val == -1) {
break;
}
nonogram[x][y] = val;
printNonogram(nonogram, rowHints, colHints);
}
return 0;
}
```
在这个代码中,我们使用了 vector 容器来存储数织图、行提示和列提示。程序首先会让用户输入数织图的行列数和行列提示,然后根据这些提示生成数织图,并打印出数织图和行列提示。
程序会不断地询问玩家要填充的行列号和数字,直到玩家输入 -1。每次玩家输入后,程序会更新数织图,并打印出更新后的数织图和行列提示。
该代码只包含了基本的数织图游戏功能,你可以根据自己的需求进行修改和扩展。
阅读全文