I'm looking to design a simple game in which the player plays against the pc by picking a number from 0-9 on his turn.
Once he's picked a number, he can no longer choose it on his next turn.
How can I best represent his choices? As an array or as a string?
The main issue is that I have to remove entries from the array/string as the game progresses and I don't know how to remove part of a string or a box out of an array.
Game example:
player turn 1:
"Please pick a nr: 0 1 2 3 4 5 6 7 8 9 ."
--> 3
pc turn .....
highest number gets a point.
player turn 2:
"You have these numbers left: 0 1 2 4 5 6 7 8 9 . Please pick a nr."
--> 5
and so on.
I'm really enjoying programming little games, but the learning curve is pretty steep.
Thanks for your help.
I would recommend instead of using a raw array that you use a vector. Vectors handle all the memory management of an array and have functions for accessing, editing and removing data inside it. When the player chooses a number it can be simply removed from the vector and looped through on their next turn to see if they can use their next number.
Using a vector:
1 2 3 4 5 6 7 8 9 10 11
// declares a vector of integers
std::vector<int> numbers;
// adds the number 5 onto the end of the vector
numbers.push_back(5);
// retrieves the first element of the vector like an array
std::cout << numbers[0] << std::endl;
// removes an element in the vector
int index = 0;
numbers.erase(numbers.erase + index);
First of all I want to compliment you on thinking about which data structure is appropriate for the problem. Most beginners don't do that. It shows that you have a good head for programming.
The main issue is that I have to remove entries from the array/string
You don't need to remove them. You can store the numbers that have been chosen and then print the ones that have not. This is illustrates a very important idea: the code should represent the data in the way that's most convenient for the code, not in the way that is shown to (or entered by) the user.
Looking closely at the problem, here is what I see:
You don't need to lookup the numbers. All you really care about it whether the number has been chosen already. So my first reaction is to use a std::set.
The values in the set are small integers. This is a perfect match for std::bitset. It stores just one bit for each value. If the bit is set, then the user has chosen the value. If the bit is clear then they have not. Bitset is described here: http://www.cplusplus.com/reference/bitset/bitset/
Here is code that shows it in use. This program just has the user pick numbers, it doesn't actually play the game.
#include <iostream>
#include <bitset>
#include <sstream>
#include <string>
using std::cin;
using std::cout;
using std::bitset;
using std::ostringstream;
using std::string;
constexpr size_t MySize = 10; // one more than the size needed
typedef bitset<MySize> MySet;
// Generate a string with all the numbers that are not set
// in "chosen"
string availableNumbers(const MySet &chosen)
{
ostringstream os;
for (size_t i = 0; i<chosen.size(); ++i) {
if (!chosen.test(i)) {
os << ' ' << i;
}
}
return os.str();
}
int main()
{
MySet chosen; // bits are initialized to zero
while (!chosen.all()) { // while some bits are 0
size_t pick=0;
cout << "Please pick a number: " << availableNumbers(chosen) << '\n';
cin >> pick;
if (pick >= chosen.size() || chosen.test(pick)) {
cout << "Bad number. Pick again\n";
continue;
}
chosen.set(pick);
}
return 0;
}