Reputation:
cards[] = {2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11};
I have this array for my game, and when I have used a number in my array, it will be replaced with 0. After the game I need to reset the array, otherwise 0's will stay in array.
How can I reset that array?
Upvotes: 0
Views: 2997
Reputation: 5241
Make an array which has the original values stored. When you want to reset it, copy it to the original string:
int temp_cards[] = {2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11};
int cards[] = {2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11};
...
memcpy( cards, init_cards, sizeof cards ); // copying temp_card to card and resetting card
memcpy
is recommended, but if you want to keep it simple you can also copy one string to another using =
.
Another method which @Dandorid suggested is good, just I will provide you how to do it :
void reset (int *cards) {
int temp_cards[] = {2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11};
memcpy (cards, temp_cards, sizeof (cards));
}
An when you call it, pass the cards
array.
Upvotes: 2
Reputation: 6563
Define one array for init values and other for working values:
const int init_cards[] = {2,2,2,2,3,3...};
int cards[sizeof init_cards / sizeof init_cards[0]];
...
memcpy( cards, init_cards, sizeof cards ); // reset values
To reset - copy init_cards
to cards
.
Upvotes: 1
Reputation: 205
Create a function that initializes your array; then call it at the start of each game.
Upvotes: 1
Reputation: 5697
You can't. You need to make an array you won't change and copy it to the working array. Repeat the copy as required.
Upvotes: 0
Reputation: 17605
You could use a function, say Initialize
, that uses memcopy
(or a manually coded loop) to fill the array with some predifined values; there is no way to restore some initial state to the array.
Upvotes: 0
Reputation: 409166
Have two arrays: One with the default settings and one which you modify. When "resetting" copy from the default array to the array you modify.
Upvotes: 0