Hi everyone I am a student who is totally new with programming. I am unable to create an array which has to content elements(1E, 2E, 3E, 4E). I tried using char however I am unable to display as it only shows the letter E. I need help please.
For reference the question was
Elimination is a one-player game. The board consists of a set of 12 tiles, number 1 through 12. The player rolls a pair of dice and removes tile based on the numbers shown on the dice. For each roll, the player can remove either the two tiles corresponding to the numbers shown on the dice or a single tile corresponding to the sum of the numbers on the dice. If the same number appears on both dice, the player can remove only the tile corresponding to the sum of the dice. Play continues until the player cannot make a legal move or all the tiles have been removed. The player score is the sum of the remaining tiles. See sample output where the letter āEā denotes that it is not being removed. When the tile is being removed, it is denoted as āXā. A random number generator is used to generate the dice number.
based on the instruction I have to create the 12 tiles in this order (1E, 2E, 3E,4E,5E,....12E)
THe thing is a character is well..a character not 2 characters. You must use a string of some sort I would suggest using std::string this will be able to store the letter and number. You could also use a std::pair<char, char> I suppose. Or make yourself a simple struct for a pair like:
1 2 3 4 5 6
template <class T, class U>
struct pair
{
T first;
T second;
};
I am able to understand the first two line of command however the third one is confusing.
cout << i+1 << ( tiles[i] ? 'X' : 'E' ) << ' ';
As it got both X and E but it only display X with the numbers.Can you explain the function ?
I have another question I tried using the function it helps to change all the 12 numbers to have the same alphabet with it but how can I change only one number to have 'X' and all the rest to have 'E' like (1E, 2E,3E,4E,5E,6X,7E,8E,9E,10E,11E,12E) this.
What keskiverto has done is pretty smart, so I'm sure you missed most of it... (at your level).
The first thing to understand is that what the user sees ("7E", "X", etc) is not exactly how you have to keep things in your program.
There are only twelve tiles, and the only information you have to track about each tile is whether it is there or not. Hence the array of 12 bools. The tile number is its index into the array, and its state is a Boolean value ('is tile present?').
When he prints the tile for the user, he uses that information to display the correct thing to him (or her):
- print the tile number
- print its status (E or X)
- print a space
For the original question/post, create an object to store the individual numbers and associated characters and then store these objects in a std::vector.
You could do an array as well.
Here is a code example, you should be able to paste the below code in an empty project file and run:
I was afraid that the ternary operator is a bit too much. It is syntactic sugar that keeps code compact and "simple". C++ offers a lot of syntactic sugar and -- as you can guess -- is therefore quite complex.
Lets do two fundamental changes to my previous snippet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
constint Begin = 1;
constint End = 13;
bool tiles [End];
for ( int i=Begin ; i < End ; ++i )
{
tiles[i] = true;
}
int dice1 = (rand() % 6) + 1;
tiles[dice1] = false;
for( int i=Begin ; i < End ; ++i )
{
cout << i << ( tiles[i] ? 'E' : 'X' ) << ' ';
}
cout << '\n';
What were the changes?
1. We reserve more memory (13) than we need (12) and then never use the element tiles[0]. Now we can use the tile numbers directly as indices, rather than always calculating the +1. We name the limits Begin and End so that we don't have to write raw 1 or 13 into the various loops. Unused space for one bool is a small price for convenience and efficiency.
2. I did change the convention. The true now means 'E' (present) and false is 'X' (removed). Loop on lines 4-7 sets each tile to be present. This change will simplify certain piece of code that your homework will need.
Back to the array access. Lines 6 and 14 do access individual element, but being in loop that happens for all 12 elements. Line 10 now explicitly changes one array element.
@keskiverto
If I want to have more than 1 number with a 'X' and the rest with 'E' how do I have to change the code or rather which part of the code need to be changed?
You set the value of desired array elements to false. The lines 9-10 did change one element (but we don't know which, apart from it being one of the first 6).
Your program needs a loop, where in each iteration you do remove (i.e. set false) one or two tiles. It seems that removing an already removed tile is an invalid move that ends the loop.
For example, if first throw is 1 and 1, you have to remove tile 2. If you do throw 1 and 1 again, the game is over.
When the dice are not equal, the player has a choice to remove either those two values or their sum. At that point the player has to know what tiles are left after the previous round, because he should not make invalid move uninformed.
In other words, every iteration should
1. show tiles
2. throw dice
3. let player choose
4. break or remove tiles
When the loop has ended, you need one more loop that computes the score.
Help!
I have manage to complete my codes however I have a major issue as wherever it repeats it doesn't show the tiles it have cancelled earlier it only cancel the tile that choose lastly
my current code:
//Display the 12 tiles
const int Begin = 1;
const int End = 13;
bool tiles [End];
for ( int i=Begin ; i < End ; ++i )
{
tiles[i] = true;
}
int diceroll = (rand() % 6) + 1;
for( int i=Begin ; i < End ; ++i )
{
cout << i << ( tiles[i] ? 'E' : 'X' ) << ' ';
}
cout << '\n';
while(true)
{
//Rolling the pair of dice
dice1 = (rand() % 6) + 1;
dice2 = (rand() % 6) + 1;
//Results
cout<<"Roll dice:"<<dice1<<" "<<dice2<<endl;
//Player choose the option 'S' or 'D'
cout<<"Enter D to remove tiles as dice roll,S to remove sum of dice roll:";
cin>>choice;
//choice D
if (choice == 'D' || choice == 'd')
{
if (dice1 == dice2)
cout<<"Wrong move";
else
const int Begin = 1;
const int End = 13;
bool tiles [End];
for ( int i=Begin ; i < End ; ++i )
{
tiles[i] = true;
}
int diceroll = (rand() % 6) + 1;
tiles[dice1]=false;
tiles[dice2]=false;
for( int i=Begin ; i < End ; ++i )
{
cout << i << ( tiles[i] ? 'E' : 'X' ) << ' ';
}
cout << '\n';
}
else if (choice == 'S' || choice == 's')
{
sum = dice1 +dice2;
const int Begin = 1;
const int End = 13;
bool tiles [End];
for ( int i=Begin ; i < End ; ++i )
{
tiles[i] = true;
}
int diceroll = (rand() % 6) + 1;
tiles[sum]=false;
for( int i=Begin ; i < End ; ++i )
{
cout << i << ( tiles[i] ? 'E' : 'X' ) << ' ';
}
cout << '\n';
}
else
{
cout<<"Invalid option!";
}
Next, consider how indentation makes reading easier.
Move lines 23-27 to line 30.
Remove lines 21, 44-51, 55-59, 65-72, and 75-79.
You need to enclose in braces the statements that should execute with the else of line 43.
You need something that will end/break the while loop.
You need the "count and show the score" at the end.
If I need to create a function such that if the number is already with an X and if I try to re-create it have to end the game . I tried but my codes look wrong and I don't know how to improve.
1 2 3 4 5 6 7 8 9
elseif (choice == 'S' || choice == 's')
{
sum = dice1 +dice2;
tiles[sum]=false;
while (tiles[sum]=false == tiles[sum]=false)
}