英文:
How can i define arrays as 2D pointers?
问题
你需要将这些数组转换为指针并使用动态内存分配。以下是已经进行转换的代码段:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#define SIZE 100
using namespace std;
int num, number = 1, x, y, startValue = 0, points = 0, chartSize, openCards = 0;
char* ptr1 = new char[SIZE*SIZE];
char* ptr2 = new char[SIZE * SIZE];
int* ptr3 = new int[SIZE * SIZE];
char rl, (*chart)[SIZE] = reinterpret_cast<char(*)[SIZE]>(ptr1), (*letters)[SIZE] = reinterpret_cast<char(*)[SIZE]>(ptr2);
int (*assigned)[SIZE] = reinterpret_cast<int(*)[SIZE]>(ptr3);
void dynamicChart() {
srand(time(NULL));
cout << "Please enter the size of the chart(2,4,6,etc.):" << endl;
cin >> chartSize;
while (chartSize % 2 != 0 || chartSize <= 0) {
cout << "Invalid chart size. Please enter another value." << endl;
cin >> chartSize;
}
int spaces = chartSize * chartSize, assigned[SIZE][SIZE] = { 0 };
while (spaces != 0) {
rl = 'A' + rand() % 26;
int count = 0;
while (count < 2) {
int r1 = rand() % chartSize;
int r2 = rand() % chartSize;
if (!assigned[r1][r2]) {
letters[r1][r2] = rl;
assigned[r1][r2] = 1;
count++;
}
}
spaces -= 2;
}
for (int i = 0; i < chartSize; i++) {
for (int j = 0; j < chartSize; j++) {
chart[i][j] = '*';
}
}
}
void drawChart() {
cout << "\n\n\n\n";
cout << " ";
for (int i = 1; i < chartSize + 1; i++) {
cout << i;
cout << "\t ";
}
cout << "\n";
for (int i = 0; i < chartSize; i++) {
cout << number;
number++;
for (int j = 0; j < chartSize; j++) {
cout << "|";
cout << " " << chart[i][j];
cout << "\t";
}
cout << endl;
}
number = 1;
}
// 其余部分保持不变,只需修改数组的访问方式即可。
int main() {
// mainMenu() 和其他功能的调用保持不变。
return 0;
}
这些更改将数组转换为指针,使得您可以使用动态内存分配。请注意,我使用了 reinterpret_cast
来将指针类型转换为正确的数组类型。您需要根据这个示例来修改其他部分,以便使用新的指针而不是数组。希望这可以帮助您解决问题!如果您需要进一步的帮助,请随时提问。
英文:
#include <iostream>
#include <string>
#include <stdlib.h>
#include <windows.h>
#define SIZE 100
using namespace std;
int num, number = 1, a, b, x, y, startValue = 0, points = 0, chartSize, openCards = 0;
char* ptr1 = new char[SIZE*SIZE];
char* ptr2 = new char[SIZE * SIZE];
int* ptr3 = new int[SIZE * SIZE];
char rl, chart[SIZE][SIZE], letters[SIZE][SIZE], (*ptr1)[SIZE][SIZE] = &chart, (*ptr2)[SIZE][SIZE] = &letters;
void dynamicChart() {
srand(time(NULL));
cout << "Please enter the size of the chart(2,4,6,etc.):" << endl;
cin >> chartSize;
while (chartSize % 2 != 0 || chartSize <= 0) {
cout << "Invalid chart size.Please enter another value.\n";
cin >> chartSize;
}
int spaces = chartSize * chartSize, assigned[SIZE][SIZE] = { 0 },(*ptr3)[SIZE][SIZE] = &assigned;
while (spaces != 0) {
rl = 'A' + rand() % 26;
int count = 0;
while (count < 2) {
int r1 = rand() % chartSize;
int r2 = rand() % chartSize;
if (!(*(*(*ptr3 + r1) + r2))) {
*(*(*ptr2 + r1) + r2) = rl;
*(*(*ptr3 + r1) + r2) = 1;
count++;
}
}
spaces -= 2;
}
for (int i = 0; i < chartSize; i++) {
for (int j = 0; j < chartSize; j++) {
*(*(*ptr1 + i) + j) = '*';
}
}
}
void drawChart() {
cout << "\n\n\n\n";
cout << " ";
for (int i = 1; i < chartSize + 1; i++) {
cout << i;
cout << "\t ";
}
cout << "\n";
for (int i = 0; i < chartSize; i++) {
cout << number;
number++;
for (int j = 0; j < chartSize; j++) {
cout << "|";us
cout << " " << *(*(*ptr1 + i) + j);
cout << "\t";
}
cout << endl;
}
number = 1;
}
void mainMenu() {
cout << "Play(1)\tQuit(0)" << endl;
cin >> num;
while (num != 0 && num != 1) {
cout << "Please enter a valid input." << endl;
cin >> num;
}
if (num) {
startValue = 1;
}
}
void playGame1() {
bool validInput = false;
while (!validInput) {
cout << "Points: " << points << endl;
cout << "Enter a coordinate:\t\tQuit(0)" << endl;
cin >> x;
if (x == 0) {
cout << "Quitting...";
Sleep(1000);
system("CLS");
exit(1);
}
cin >> y;
if (y == 0) {
cout << "Quitting...";
Sleep(1000);
system("CLS");
exit(1);
}
if (x > chartSize || y > chartSize || x <= 0 || y <= 0) {
cout << "Invalid coordinates. Please try again.\n";
}
else {
x--;
y--;
if (*(*(*ptr1 + x) + y) == '*') {
*(*(*ptr1 + x) + y) = *(*(*ptr2 + x) + y);
}
else {
cout << "This coordinate is occupied.Please enter another coordinate.\n";
playGame1();
}
validInput = true;
}
}
}
void playGame2() {
bool validInput = false;
while (!validInput) {
cout << "Points: " << points << endl;
cout << "Enter a coordinate:\t\tQuit(0)" << endl;
cin >> a;
if (a == 0) {
cout << "Quitting...";
Sleep(1000);
system("CLS");
exit(1);
}
cin >> b;
if (b == 0) {
cout << "Quitting...";
Sleep(1000);
system("CLS");
exit(1);
}
if (a > chartSize || b > chartSize || a <= 0 || b <= 0) {
cout << "Invalid coordinates. Please try again.\n";
}
else {
a--;
b--;
if (*(*(*ptr1 + a) + b) == '*') {
*(*(*ptr1 + a) + b) = *(*(*ptr2 + a) + b);
}
else {
cout << "This coordinate is occupied.Please enter another coordinate.\n";
playGame2();
}
validInput = true;
}
}
}
void control() {
if (*(*(*ptr1 + a) + b) == chart[x][y]) {
openCards += 2;
Sleep(1000);
points += 3;
system("CLS");
}
else {
points -= 1;
Sleep(1000);
*(*(*ptr1 + a) + b) = chart[x][y] = '*';
system("CLS");
}
}
int main() {
mainMenu();
system("CLS");
if (startValue) {
dynamicChart();
system("CLS");
while (1) {
drawChart();
playGame1();
system("CLS");
drawChart();
playGame2();
system("CLS");
drawChart();
control();
if (openCards == chartSize * chartSize) {
drawChart();
cout << "You won! With " << points << " points.";
Sleep(3000);
system("CLS");
break;
}
}
}
else {
cout << "You have quitted.";
}
return 0;
}
This is a memory game that i am working on. I need to change the arrays to pointers and use dynamic memory allocation but i am not beeing able to do it. I am having error message "operand of '*' must be a pointer but has type 'int'." whenever i try to use my pointers.
I tried to use pointer to pointer but it didn't worked out.Instead i had many more errors.
答案1
得分: -2
以下是翻译好的代码部分:
// 分配2D数组
char** ptr1 = new char*[chartSize];
for (int i = 0; i < chartSize; i++) {
ptr1[i] = new char[chartSize];
}
// 初始化2D数组元素
for (int i = 0; i < chartSize; i++) {
for (int j = 0; j < chartSize; j++) {
ptr1[i][j] = '*';
}
}
一旦你分配了数组,其他部分(几乎)与常规数组完全相同。
英文:
Here's a quick sample to get you started
// allocate 2D array
char** ptr1 = new char*[chartSize];
for (int i = 0; i < chartSize; i++) {
ptr1[i] = new char[chartSize];
// initialise 2D array elements
for (int i = 0; i < chartSize; i++) {
for (int j = 0; j < chartSize; j++) {
ptr1[i][j] = '*';
}
}
Once you have the arrays allocated everything else is (almost) exactly the same as regular arrays.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论