SEC-S20W6 Practice cycles and Guess the number game

in sec20w6sergeyk •  2 months ago 

Practice cycles and Guess the number game.png

Edited by Canva

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

CodeColor
30Black
31Red
32Green
33Yellow
34Blue
35Magenta
36Cyan
37White

Background Colors

CodeColor
40Black Background
41Red Background
42Green Background
43Yellow Background
44Blue Background
45Magenta Background
46Cyan Background
47White Background

High Brightness Text Colors

CodeColor
90Bright Black
91Bright Red
92Bright Green
93Bright Yellow
94Bright Blue
95Bright Magenta
96Bright Cyan
97Bright White

High Brightness Background Colors

CodeColor
100Bright Black Background
101Bright Red Background
102Bright Green Background
103Bright Yellow Background
104Bright Blue Background
105Bright Magenta Background
106Bright Cyan Background
107Bright 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

0.JPG

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

image.png

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

3.JPG

4.JPG

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.

image.png

image.png

image.png

How the game works:

  1. 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.

  2. 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.

  3. 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.

  4. Turn Alternation: After each valid turn, the game automatically switches to the other player, ensuring that players alternate taking turns.

  5. 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:

image.png

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

image.png

Print a number of stars (e.g. 9 stars)

To print 9 stars using a loop:

image.png

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.

image.png

Symmetric triangle (pyramid):

image.png


Thank you very much for reading, it's time to invite my friends @cruzamilcar63, @pelon53, @adeljose to participate in this contest.

Best Regards,
@kouba01

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  
Loading...

@tipu curate

You've got a free upvote from witness fuli.
Peace & Love!

IMG_20240930_084439.png

Congratulations!!🎉🎉 Your post has been upvoted by TEAM 03 (content seekers) using steemcurator05. Continue making creative and quality content on the blog. By @damithudaya

Loading...