I am working on a board game and have a 2d char array for board in my main:
我正在制作一个棋盘游戏并且在我的主要游戏中有一个2d char数组用于登录:
char board[*size][*size];
for(int i = 0; i < *size; i++) {
for(int j = 0; j < *size; j++) {
board[i][j] = ".";
}
}
I want to use this in my function named playerOneMove(?), change some of its elements and than bring back to main again to use it in playerTwoMove(?)
我想在我的名为playerOneMove(?)的函数中使用它,更改它的一些元素,然后再次返回main以在playerTwoMove(?)中使用它
I can do this with 1D integer arrays but i couldn't make this work. I just want to learn the method, not full code.
我可以使用1D整数数组执行此操作,但我无法使其工作。我只是想学习方法,而不是完整的代码。
2 个解决方案
#1
0
The best way to learn is by looking at code.
最好的学习方法是查看代码。
The below code passes a 2D array. Study it.
以下代码传递2D数组。研究它。
#include <iostream>
#include <cstdio>
using namespace std;
// Returns a pointer to a newly created 2d array the array2D has size [height x width]
int** create2DArray(unsigned height, unsigned width){
int** array2D = 0;
array2D = new int*[height];
for (int h = 0; h < height; h++){
array2D[h] = new int[width];
for (int w = 0; w < width; w++){
// fill in some initial values
// (filling in zeros would be more logic, but this is just for the example)
array2D[h][w] = w + width * h;
}
}
return array2D;
}
int main(){
printf("Creating a 2D array2D\n");
printf("\n");
int height = 15;
int width = 10;
int** my2DArray = create2DArray(height, width);
printf("Array sized [%i,%i] created.\n\n", height, width);
// print contents of the array2D
printf("Array contents: \n");
for (int h = 0; h < height; h++) {
for (int w = 0; w < width; w++)
{
printf("%i,", my2DArray[h][w]);
}
printf("\n");
}
// important: clean up memory
printf("\n");
printf("Cleaning up memory...\n");
for ( h = 0; h < height; h++){
delete [] my2DArray[h];
}
delete [] my2DArray;
my2DArray = 0;
printf("Ready.\n");
return 0;
}
#2
0
Here's just math formulas for converting any kind of 2d array (width = height OR width != height) where x, y - indexes of 2d array; index - index of 1d array. That's for base 1 - first 2d element has index 11 (x=1, y=1). Guess you may implement it wherever you wish.
这里只是用于转换任何类型的2d数组(宽度=高度或宽度!=高度)的数学公式,其中x,y - 2d数组的索引; index - 1d数组的索引。那是基数1 - 第一个2d元素有索引11(x = 1,y = 1)。猜猜你可以在任何你想要的地方实现它。
2D to 1D
2D到1D
index = width * (x-1) + y
index = width *(x-1)+ y
1D to 2D
1D到2D
x = (index / width) + 1
x =(索引/宽度)+ 1
y = ((index - 1) % width) + 1
y =((index - 1)%width)+ 1
For base 0 - 1st element indexes x=0, y=0
对于基数0 - 第一个元素索引x = 0,y = 0
2D to 1D
2D到1D
index = width * x + y
index = width * x + y
1D to 2D
1D到2D
x = index / width
x =索引/宽度
y = (index - 1) % width
y =(index - 1)%width
#1
0
The best way to learn is by looking at code.
最好的学习方法是查看代码。
The below code passes a 2D array. Study it.
以下代码传递2D数组。研究它。
#include <iostream>
#include <cstdio>
using namespace std;
// Returns a pointer to a newly created 2d array the array2D has size [height x width]
int** create2DArray(unsigned height, unsigned width){
int** array2D = 0;
array2D = new int*[height];
for (int h = 0; h < height; h++){
array2D[h] = new int[width];
for (int w = 0; w < width; w++){
// fill in some initial values
// (filling in zeros would be more logic, but this is just for the example)
array2D[h][w] = w + width * h;
}
}
return array2D;
}
int main(){
printf("Creating a 2D array2D\n");
printf("\n");
int height = 15;
int width = 10;
int** my2DArray = create2DArray(height, width);
printf("Array sized [%i,%i] created.\n\n", height, width);
// print contents of the array2D
printf("Array contents: \n");
for (int h = 0; h < height; h++) {
for (int w = 0; w < width; w++)
{
printf("%i,", my2DArray[h][w]);
}
printf("\n");
}
// important: clean up memory
printf("\n");
printf("Cleaning up memory...\n");
for ( h = 0; h < height; h++){
delete [] my2DArray[h];
}
delete [] my2DArray;
my2DArray = 0;
printf("Ready.\n");
return 0;
}
#2
0
Here's just math formulas for converting any kind of 2d array (width = height OR width != height) where x, y - indexes of 2d array; index - index of 1d array. That's for base 1 - first 2d element has index 11 (x=1, y=1). Guess you may implement it wherever you wish.
这里只是用于转换任何类型的2d数组(宽度=高度或宽度!=高度)的数学公式,其中x,y - 2d数组的索引; index - 1d数组的索引。那是基数1 - 第一个2d元素有索引11(x = 1,y = 1)。猜猜你可以在任何你想要的地方实现它。
2D to 1D
2D到1D
index = width * (x-1) + y
index = width *(x-1)+ y
1D to 2D
1D到2D
x = (index / width) + 1
x =(索引/宽度)+ 1
y = ((index - 1) % width) + 1
y =((index - 1)%width)+ 1
For base 0 - 1st element indexes x=0, y=0
对于基数0 - 第一个元素索引x = 0,y = 0
2D to 1D
2D到1D
index = width * x + y
index = width * x + y
1D to 2D
1D到2D
x = index / width
x =索引/宽度
y = (index - 1) % width
y =(index - 1)%width