SEC-S20W6 Practice cycles and Guess the number game

Hello Everyone
I'm AhsanSharif From Pakistan
Greetings you all, hope you all are well and enjoying a happy moment of life with steem. I'm also good Alhamdulillah.


image.png
Canva Work

Experiment with colors—are there more than the ones we’ve used? How can you check?

Escape Codes

In programming, we use ESC to set the colors of our text and to change its background. First of all, let us know what ANSI escape codes are. These are codes that are interpreted by terminal emulators to control format colors or various output options. All these codes are widely used in command-line interfaces in programming.

Structure of ESC:

\e or \033: These indicate our escape character. Followed by:
[ This is to start by controlling our sequence. And then:
;: We use it to separate a sequence of numbers. Finally:
m: Finally we use it to override our color or style control sequence.

For example

\e[31m: Sets the text color to red.
\e[0m: Resets all formatting back to default.

Here are some basic escape color codes.

Foreground / Standard Text Color

CodeColor
\e[30mBlack
\e[31mRed
\e[32mGreen
\e[33mYellow
\e[34mBlue
\e[35mMagenta (purple)
\e[36mCyan (light blue)
\e[37mWhite

BG Colors

CodeColor
\e[40mBlack background
\e[41mRed background
\e[42mGreen background
\e[43mYellow background
\e[44mBlue background
\e[45mMagenta background
\e[46mCyan background
\e[47mWhite background

Bright Bold Version Colors

CodeColor
\e[90mBright Black (Gray)
\e[91mBright Red
\e[92mBright Green
\e[93mBright Yellow
\e[94mBright Blue
\e[95mBright Magenta
\e[96mBright Cyan
\e[97mBright White

In addition to colors, ANSI escape codes allow you to modify text styles

Text Style

CodeColor
\e[1mBold
\e[4mUnderline
\e[5mBlink (on some terminals)
\e[7mInverted colors (swap foreground and background)
\e[0mReset all styles and colors to default

You can combine multiple ANSI codes in a single escape sequence by separating them with semicolons (;)

Combine Code

CodeColor
\e[1;31mBold red text
\e[4;32mUnderlined green text
\e[7;34mInverted blue text (swaps foreground and background)

Here is an example of a C++ program where I put different colors and styles.

#include <iostream>
using namespace std;

#define RESET   "\e[0m"    // Reset all attributes
#define BOLD    "\e[1m"    // Bold
#define UNDERLINE "\e[4m"  // Underline

// Foreground colors
#define BLACK   "\e[30m"   // Black
#define RED     "\e[31m"   // Red
#define GREEN   "\e[32m"   // Green
#define YELLOW  "\e[33m"   // Yellow
#define BLUE    "\e[34m"   // Blue
#define MAGENTA "\e[35m"   // Magenta
#define CYAN    "\e[36m"   // Cyan
#define WHITE   "\e[37m"   // White

// Background colors
#define BGBLACK   "\e[40m"   // Black background
#define BGRED     "\e[41m"   // Red background
#define BGGREEN   "\e[42m"   // Green background
#define BGYELLOW  "\e[43m"   // Yellow background
#define BGBLUE    "\e[44m"   // Blue background
#define BGMAGENTA "\e[45m"   // Magenta background
#define BGCYAN    "\e[46m"   // Cyan background
#define BGWHITE   "\e[47m"   // White background

int main()
{
    cout << BOLD << RED << "This is bold red text!" << RESET << endl;
    cout << UNDERLINE << GREEN << "This is underlined green text!" << RESET << endl;
    cout << BLUE << BGBLUE << "This is blue text with a blue background!" << RESET << endl;
    cout << YELLOW << BGMAGENTA << "This is yellow text with a magenta background!" << RESET << endl;
    cout << BOLD << CYAN << BGWHITE << "This is bold cyan text with a white background!" << RESET << endl;

    return 0;
}

Here is the output of this code.

Bold Red text.
Underlined Green text.
Blue text on a Blue background.
Yellow text on a Magenta background.
Bold Cyan text on a White background.


Write your version of the game "Guess the Number"—this time, creativity is more important than efficiency.

Here is the C++ Code For Guess The Number.

image.png

image.png

image.png

Explanation:

  • This is a game that is set in an enchanted forest that gives a mystical sound to the player to choose any number. And challenges him to choose the correct number.

  • In this game, the player will have seven chances to choose one of the correct numbers.

  • This is a game that offers feedback with every guess.

  • As soon as the player selects any number, a message will be shown on his victory. Even if he fails, he will get a message to encourage him.

  • It is a game that gives us a different kind of experience and completes this adventure using language.

Here is the output:


image.png

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.

If we try to guess the number from zero to thousand or from zero to 10 thousand, it will take us a lot of time. We have two types of strategies for how to solve this.

Our first strategy will be one in which we have to make a random guess. Random guess means that we have to search for random numbers from zero to 1000 and from zero to 10,000.

This procedure is very time taking procedure. According to an average which is from zero to a thousand we have to make about 500 guesses to guess our number then we will be able to guess our correct number.

Similarly, which is from zero to 10 thousand, according to an average, we have to guess about five thousand. Then we will be able to find our correct number.

The second method we have is that we will find it according to the binary search which we can also call the optimal strategy.

If we were to use a binary search strategy where you systematically cut the range of possibilities in half with each guess. This has the advantage of reducing the number of guesses required.

Here we can calculate the number of estimates using the log formula. A log formula that has two logarithmic ranges.

Here is the log formula that we use.

Log2(n)

0 to 1000: log2(1000)
Similarly, if we take out our range from zero to thousand, then approximately our guesses are 10.

0 to 10000: log2(10,000)
Same, if we use the log formula, our range from zero to 10,000 will have approximately 14 guesses.

Finally, we agree that if we do a random guess it will take us a lot of time and it will be a very long procedure. But if we use binary search, we will find our number very quickly. From zero to thousand there are only 10 guesses and from zero to 10 thousand there are only 14 guesses. In this range, we can find our number.

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.

Here is the C++ Program Code

image.png

image.png

image.png

Explanation:

  • It is designed as a game in which two players will participate. First, the first player will start and when any player chooses his number, it is a condition for him to select the number from one to five. If the player selects another number, that input will be invalidated.

  • The game has a rule whereby no player can take more than the ball on the table.

  • When one player's turn is complete, he/she will be switched and the other player's turn will be played.

  • In this game, the player who picks up the last ball wins.

  • The game ends when there are no balls left on the table.

Here is the Output On Playing With Rules:


image.png

Here is the Output On Entering Wrong Key:


image.png

Follow the same path from a single star to a square, then transform the square into a triangle.

Here is the C++ Program Code:


image.png


image.png

Explanation:

I have chosen the last triangle given in the post in this question and will construct it.

To complete this task, I will start with a star, first I will print a star, then I will print squares of stars, and then in the final form, I will print my triangle.

Our first step in the code above is to print a star in the center. That's all it does.

In the next second step, we will print a square of stars. In which we will have columns and rows and in this case they are five-cross-five.

The next step is our final step in which we will turn our stars square into a triangle.

Here is the Output


image.png

Thank you so much for staying here. I would like to invite @kouba01, @bossj23, and @ruthjoe to join this task.

Cc:
@sergeyk

Dated: 19-10-2024 About Me
Sort:  
Loading...

Thank you so much dear @ruthjoe for your kind support and love. I wish you more success.

Coin Marketplace

STEEM 0.23
TRX 0.22
JST 0.036
BTC 98537.12
ETH 3353.21
USDT 1.00
SBD 3.15