| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
Pointing to multi-Dimensional arrays
My question is not how to point to the actual array but it is how can I make a statement to let my user choose the array that he wants. Such as my example below. If the user wanted to use the array "a1" what would be some options for him to do so without writing every single pointer out so the user could choose between them. Is it possible to declare one variable and have it choose the right array depending on what the user types?
Code:
int main(void)
{
//num[0]stands for 'a' on the board in ascending order
//example num[1] == 'b' or num[2] == 'c'
//and num[][0] is for the numbers and so on.
string num[9][9];
num[0][0]="a1"; string a1 = num[0][0];//pointers//
string* p1 = &num[0][0];
num[0][1]="a2"; string a2 = num[0][1];
string* p2 = &num[0][1];
num[0][2]="a3"; string a3 = num[0][2];
string* p3 = &num[0][2];
num[0][3]="a4";//and you get the point im sure.
num[0][4]="a5";
num[0][5]="a6";
num[0][6]="a7";
num[0][7]="a8";
cout<<" Enter a place \n";
cin>>?<=something to input to reference num[][];
system("PAUSE");
return 0;
}
if someone has any suggestions I would appreciate them very much |
|
#2
|
|||
|
|||
|
Sounds to me this is a job for a class object.
You can make a class containing the stuff you need of the places. It can contain (multi-dim) arrays, strings etc. Then you make a one-dom array containing the objects. E.g. ob[0] = CPlace("a1"); ob[1] = CPlace("a2"); ... ob[10] = CPlace("b1"); ... etc. Now when the user enters 'the place to be', for example "c5" you just walk through the array and find the correct object. Then return that object so it can be used. Just my two cents |
|
#3
|
||||
|
||||
|
Well,
Heres the thing Michael soft, I have created a class for this and here it is in a nutshell, what I am trying to accomplish. I am making a game board that is 8 by 8, one row is numbered 'a1 - a8' then 'b1 -b8' and so on. The numbers co-incide with each individual block of the game board. so I put that multi-dimensional array that I showed you, inside the class of the gameboard, well actually that might be were I went wrong, come to think of it, because I put it as a method and not the constructor. so basically I want to make it simple for the user by asking him were do you want to move the piece from and were too. then the user enters 'a1' then 'b2' just for an example. So I will try what you said as far as the one-dome array first but if you have any more suggestions I don't mind em =Þ I would have replied and tried your suggestion earlier but I just got two class assignments due today and I haven't finished them because I get rapped up with trying to learn C++ that it consumes all my time and I forget that I have a life, lol. BY THE WAY thx for the help |
|
#4
|
||||
|
||||
|
MichaelSoft?
Ok michael soft I have started to test your method of doing this but I have one simple question, were would the one dom array go would it go in the class of the multi dimensional or would it go in the (main)?
|
|
#5
|
|||
|
|||
|
Hi there Geo,
Okay .. a gameboard. Why didn't you say so before ;-) There can be a simple solution and a difficult one ... and some in between. Just a quick scrabble: Code:
void main()
{
// Create the board
CBoard board(8,8);
char cFrom[10], cTo[10];
// Ask the user the move
// e.g. cFrom => "a7" and cTo => "c1"
// Calculate to integers
int x1, y1, x2, y2;
board.Convert(cFrom, x1, y1);
board.Convert(cTo , x2, y2);
board.Switch(x1, y1, x2, y2);
}
class CPiece
{
CPiece(int Type=0)
{
m_Type=Type;
}
int m_Type;
}
class CBoard
{
CBoard(int xS, int yS)
{
for(m_y=0; m_y< yS; m_y++)
{
m_board[m_y] = new CPiece[];
for(m_x=0; m_x< xS; m_x++)
{
m_board[m_y][m_x] = new CPiece(x); // Set a 'piece' on the board
}
}
}
~CBoard()
{
int x, y;
for(y=0; y< m_y; y++)
{
for(x=0; x< m_x; x++)
{
delete m_board[y][x];
}
delete [] m_board[y];
}
}
void Convert(const char *cPlace, int &x1 int &y)
{
x = cPlace[0]-'a'+1;
y = cPlace[1]-'1'+1;
}
void Switch(int x1, int y1, int x2, int y2)
{
CPiece *p = m_board[y1][x1];
m_board[y1][x1] = m_board[y2][x2];
m_board[y2][x2] = p;
}
CPiece ***m_board;
int m_x, m_y;
};
I'm sure that the array is not generated correctly, it is always a struggle to find the correct order of * and []. But this is the base... Don't complain about compile errors as this forum-editor is no compiler The above allows you to extent the CPiece class to different sub-classes: for each type of piece a seperate sub-class. And then you can add a member function to check if the proposed 'move' is a valid one. Good luck, MichaelSoft |
|
#6
|
||||
|
||||
|
Thx Michael Soft and dont worry about complaints here, I dont complain when some one is taking the time to help me out. =)> The people that complain are ones that really dont want to do the work for themselves.
This will take me a little bit to try this suggestion. Mostly because Im gonna have to start over on what I have been writing. I think as a semi-begginer maybe I picked a game to make that was to difficult, but I just cant give up now that I got this far. The game is Chess by the way and Its going to be a win32 app. thx again. -:GeO:- |
|
#7
|
|||
|
|||
|
'I knew it'! That's why I mentioned "to check if the proposed 'move' is a valid one". ;-)
Kinda got me thinking on my first program tryings: Basic on a Commodore 64 (brings up -sweet- memories). It was also a chess-game. I never got very far, but the best thing to learn programming is to have something that is keeping you going. I guess that it is going to be a two-player game? Because the AI in a chess game is a whole dimension on its own. Not too bad to have it a go though ![]() |
|
#8
|
||||
|
||||
|
*MichaelSoft*
Ya its definitely going to be a two player game at least for right now =Þ, I mean I semi-understand ai actions, but that's kinda out of my scope at the moment.
Glad I could refresh some old memories for you. =) |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Pointing to multi-Dimensional arrays |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|