SEC-S20W6 Practice cycles and Guess the number game
Hello steemians,
Experiment with colors—are there more than the ones we’ve used? How can you check?
Yes, we can use ESC sequences to change the appearance of text in the console, and we can customize more colors or styles that were not used in the previous examples. In fact, besides the basic colors mentioned (red, green, blue, etc.), there are other colors that can be applied, such as colored backgrounds or text styles such as bold or underlined text.
Escape sequences are instructions that are executed at the console level to change the visual appearance of text. In C++, an escape sequence is done using \033
or \e
which represents the ASCII code for the escape symbol, followed by a custom sequence to specify certain effects on the text such as colors or styles (such as bold or underlined).
Escape sequence syntax:
\033[<effect code>m
The number that comes after the square bracket
[ ]
is responsible for specifying the desired effect or color.You can combine more than one effect or color using the semicolon
;
like:\033[1;31m
which means Bold text in red.
Standard Text Colors
Code | Color |
---|---|
30 | Black |
31 | Red |
32 | Green |
33 | Yellow |
34 | Blue |
35 | Magenta |
36 | Cyan |
37 | White |
Background Colors
Code | Color |
---|---|
40 | Black Background |
41 | Red Background |
42 | Green Background |
43 | Yellow Background |
44 | Blue Background |
45 | Magenta Background |
46 | Cyan Background |
47 | White Background |
High Brightness Text Colors
Code | Color |
---|---|
90 | Bright Black |
91 | Bright Red |
92 | Bright Green |
93 | Bright Yellow |
94 | Bright Blue |
95 | Bright Magenta |
96 | Bright Cyan |
97 | Bright White |
High Brightness Background Colors
Code | Color |
---|---|
100 | Bright Black Background |
101 | Bright Red Background |
102 | Bright Green Background |
103 | Bright Yellow Background |
104 | Bright Blue Background |
105 | Bright Magenta Background |
106 | Bright Cyan Background |
107 | Bright White Background |
Also, You can control text styles such as bold, underline, invert colors, or even strikethrough. Here are some important codes:
- 1m: Bold Text.
- 4m: Underline text.
- 7m: Inverted colors.
- 9m: Strikethrough text.
- 0m: Reset to default.
You can combine multiple effects to apply to text. For example:
\033[1;31;47m
: Makes text bold in red color with white background.
Using bright colors:
cout << "\033[91m" << "Bright red text!" << "\033[0m" << endl;
cout << "\033[102m" << "Bright green background!" << "\033[0m" << endl;
This method allows you to customize the text in the console interface with custom colors and fonts, which is useful when creating interactive programs that rely on colored output to make the output more visible and attractive.
Write your own version of the game "Guess the Number"—this time, creativity is more important than efficiency.
In this code the player interacts with an entity called "The Seer" who offers a challenge by choosing a secret number between 0 and 99 The player must then guess the number with a limited number of attempts determined by the difficulty level they choose at the beginning of the game
The game begins with an introduction by the Seer who asks for the player's name and offers three difficulty levels: easy medium and hard This choice defines the number of attempts the player will have to guess the secret number The player then makes guesses and the Seer provides hints based on how close each guess is to the correct number
If the player successfully guesses the number the Seer gives customized congratulations based on the number of attempts it took For example if the player guesses the number quickly they receive praise for their skill If it takes longer they are commended for their perseverance
You don’t need to write any code for this question—just estimate how long it would take to guess a number if the range is from 0 to 1000 or from 0 to 10,000.
To estimate the time it would take you to guess a number in a given range we can use the dichotomous search approach. This method divides the range in half for each attempt, thus rapidly reducing the size of the problem.
The maximum number of attempts needed to guess a number in a given range can be estimated using the following formula.
Number of attempts equals logarithm to base 2 of the size of the range.
For a range of 0 to 1000, log base 2 of 1000 is approximately equal to 9.97.
So it would take approximately 10 attempts at most to guess the number in a range of 0 to 1000.
For a range of 0 to 10000, log base 2 of 10000 is approximately equal to 13.29.
So it would take approximately 14 attempts at most to guess the number in a range of 0 to 10000.
Assuming that one attempt takes approximately 5 seconds which includes thinking time and typing the answer we can calculate the approximate total time for each range
For a range of 0 to 1000 it would take 10 attempts times 5 seconds which is about 50 seconds
For a range of 0 to 10000 it would take 14 attempts times 5 seconds which is about 70 seconds or 1 minutes and 10 seconds
Write a two-player game. There are 30 balls on the table, and players take turns picking between 1 and 5 balls. The winner is the one who takes the last ball. There's no need to develop a winning strategy. Just write code to enforce the rules, make the players take turns, and announce the winner.
How the game works:
Setup: There are 30 balls on the table at the start of the game. Two players ("Player 1" and "Player 2") take turns picking between 1 and 5 balls from the table.
Player Turns: On each player's turn, the game will prompt them to enter how many balls they want to take. The player can only pick between 1 and 5 balls, and they cannot take more balls than are currently left on the table.
Input Validation: If the player enters an invalid number (e.g., a number outside the range of 1 to 5 or more balls than are available), the game will prompt them again until they enter a valid number.
Turn Alternation: After each valid turn, the game automatically switches to the other player, ensuring that players alternate taking turns.
Game End: The game continues until all the balls are taken. The player who takes the last ball wins the game. Once the last ball is taken, the game announces the winner and exits.
Example of gameplay:
Follow the same path from a single star to a square, then transform the square into a triangle. Pick any triangle. Experienced students should choose one with a higher number, as it's more challenging. Triangle number 1 is for those who scored lower in previous lessons.
Here's how to create a simple star, then a square or rectangle using stars, and finally different types of triangles in C++. We'll start with a single star and work our way up to more complex shapes, as suggested in the question.
Print a single star
Print a number of stars (e.g. 9 stars)
To print 9 stars using a loop:
Create a rectangle (e.g. 4x9 stars)
Now we will print a 4-row rectangle with 9 stars in each line. We need to use two nested loops: one for the rows and one for the columns.
Symmetric triangle (pyramid):
Thank you very much for reading, it's time to invite my friends @cruzamilcar63, @pelon53, @adeljose to participate in this contest.
Best Regards,
@kouba01
@tipu curate
Upvoted 👌 (Mana: 0/6) Get profit votes with @tipU :)
You've got a free upvote from witness fuli.
Peace & Love!