I am trying to create a fruit machine game and it was going very well until today.
I am trying to get the program to pick a symbol (called class Symbol) from a collection of symbols (called class Reel) and to display THAT symbol together with the one before it and the one after it - so the display would show something like this:
Symbol before middle line symbol
Middle line symbol (chosen randomly)
Symbol after middle line symbol
I have created a function that chooses a random symbol for the middle line and stores that in position 1 of an array called symbol_display. Position 0 of symbol_display is the symbol before and position 2 is the symbol after. After much testing this function works pretty well.
The problem is I now need to return a pointer to this array so my another function can then show the output onto the screen. Obviously the pointer will point to position 0 symbol_display and I can cout this onto the screen no problem. The problem comes when I try to cout symbol_display[1] and symbol_display[2]. Instead of getting the expected symbols cout is showing other symbols from the reel!
Here is a copy of the code I have so far (this is a function in class Reel):
Symbol* show_random()
{
srand(time(NULL));
int i, j, k;
Symbol* symbol_display[3];
j = rand() % MAX_SYMBOL;
//looks at the symbol before. If the symbol chosen is first in the queue then stores the address of the last symbol
//in the reel
i = j - 1;
if (i < 0)
{
i = (MAX_SYMBOL-1);
}
//if the symbol chosen is last in the queue then stores the address of the first symbol in the reel
k = j + 1;
if (k > (MAX_SYMBOL-1))
{
k = 0;
}
//builds the display showing the symbols chosen - stores them in an array
symbol_display[0] = m_p_symbol[i];
symbol_display[1] = m_p_symbol[j];
symbol_display[2] = m_p_symbol[k];
//These lines tested the function. It works correctly here
cout << i << " " << j << " " << k << endl;
cout << symbol_display[0]->symbol_name() << " " << symbol_display[1]->symbol_name() << " "<< symbol_display[2]->symbol_name() << endl;
return *symbol_display; // returns a pointer to the array
} |
I have tested this function independently and it gives the expected results. The problem comes when I return a pointer to symbol_display which return to a third class (called class Machine) and then try to output the contents:
void show_winlines()
{
//this line prints what I expected
cout << m_machine_contents[0].show_random()->symbol_name() << endl;
//These two lines do not print what is expected
cout << ((m_machine_contents[0].show_random())+1)->symbol_name() << endl;
cout << ((m_machine_contents[0].show_random())+2)->symbol_name() << endl;
}
}; |
I suspect that the problem lies in the lines
cout << ((m_machine_contents[0].show_random())+1)->symbol_name() << endl;
cout << ((m_machine_contents[0].show_random())+2)->symbol_name() << endl; |
So if I have returned a pointer to an array from a function I know how to point to the first element of that array but how do I access the 2nd and 3rd.
I dont explain myself very well. If you have any questions please ask - it probably doesnt help that I am trying to use 3 different classes!