SEC-S20W6 Practice cycles and Guess the number game

josepha -

Hello Steemians, welcome to my post, and stay safe as you read through this post.

canvas

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

Yes, there are more than one colors and styles that we can use beyond the ones we've used. The ANSI escape codes, which are best used to control the appearance of text in the terminal, offer more options including additional colors, bold, blinking text, background colors, and underlined. Below is how we can check them:

Expand the Color Range:

The basic 8 colors we've defined can be extended by using the bold or bright version of these colors. The below colors are referred to as **bright versions:

  1. Bright Black: \e[90m
  2. Bright Red: \e[91m
  3. Bright Green: \e[92m
  4. Bright Yellow: \e[93m
  5. Bright Blue: \e[94m
  6. Bright Magenta: \e[95m
  7. Bright Cyan: \e[96m
  8. Bright White: \e[97m

Background Colors:
We can also set our background colors using similar escape codes as given in the below example.

  1. \e[40m for Black background

  2. \e[41m for Red background

  3. Similarly, \e[42m to \e[47m for other colors.

    Also, we can use the bright background versions if we wish.

    • \e[100m to \e[107m for bright backgrounds.

Text Styles:

  1. Bold: \e[1m
  2. Underline: \e[4m
  3. Blinking: \e[5m

Resetting Styles:
\e[0m resets all text styling back to default.

For us to further experiment, we can adjust our #define statements with these codes. For example, we could define BTIGHT_RED as "\e]91m" and add new lines to display a bright version of our colors.

Below is an example for us to add more colors and background to the existing code:

#define BRIGHT_RED  "\e[91m"
#define BRIGHT_GREEN "\e[92m"
#define BRIGHT_BLUE  "\e[94m"
#define BACK_RED     "\e[41m" // Red background

cout << BRIGHT_RED << "This text is bright red!" << RESET << endl;
cout << BRIGHT_GREEN << "This text is bright green!" << RESET << endl;
cout << BACK_RED << "This text has a red background!" << RESET << endl;

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

Here I have written a creative version of "Guess the Number", blending humor, storytelling, and a quirky character guide to make the game fun. In my version, the user interacts with a character that provides hints and sarcastic commentary, which makes the game more fun and enjoyable even if it takes long to guess the number.

The breakdown of the creative elements:


(1 point) 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.

Let's say that the range is 0 to 1000 or from 0 to 10,000, the time it would take to guess a number would be depends on the strategy used. Below I have made an estimate based on different approaches:

Random Guessing

Linear Guessing

Binary Search

With binary search, we can have the range of possible numbers after each guess, which is the most efficient method (approach). The time to find the number grows logarithmically.

Summary of our estimates:

Based on Random Guessing:

  1. 0 to 1000: ~500 guesses (average)
  2. 0 to 10,000: ~5000 guesses (average)

Based on Linear Guessing:

  1. 0 to 1000: ~500 guesses (average)
  2. 0 to 10,000: ~5000 guesses (average)

Based on Binary Search:

  1. 0 to 1000: 10 guesses (worst case)
  2. 0 to 10,000: 14 guesses (worst case)

The most efficient is the binary search which we could guess a number in a range as large as 10,000 within just 14 attempts.


(3 points) 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 I have written a simple two-player game where players take turns picking between 1 and 5 balls from a total of 30. In the game, the winner is the one who takes the last ball.

Interpretation of How the Game Works.

Starting the Game:

Input Validation:

Player Turns:

End Condition:

Example

The game continues until the last ball is taken, and the player who takes it wins the game.


(3 points) 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 I will be writing a code that transitions from a star pattern to a square pattern which allows me to look for a further transformation into a triangle.

Star Pattern (Start Point)

The first image shows a pyramid-like star pattern. This can be interpreted as the initial step, symbolizing the creative or scattered starting point.

   #include <iostream>

using namespace std;

int rows = 5;
for (int r = 1; r <= rows; r++) {
    for (int s = rows; s > r; s--) {
        cout << " ";
    }
    for (int c = 1; c <= 2 * r - 1; c++) {
        cout << "*";
    }
    cout << "\n";
}

Square Pattern

This is the next step which shows a square pattern, which is more structured and symmetrical compared to the star. This can be a transformation from the initial scattered pattern to a more structured one.

int rows = 4, cols = 9;
for (int r = 1; r <= rows; r++) {
    for (int c = 1; c <= cols; c++) {
        cout << "*";
    }
    cout << "\n";
}

Triangle Pattern

Now is the time for us to transform the square into a triangle. Here we can pick any type of triangle, for example, isosceles, right, or equilateral. Here we have an example of a right-angled triangle which can be a step up from the basic square pattern.

int rows = 5;
for (int r = 1; r <= rows; r++) {
    for (int c = 1; c <= r; c++) {
        cout << "*";
    }
    cout << "\n";
}

The more challenging triangle: Isosceles Triangle

For this experience, we can create an Isosceles triangle which requires aligning stars symmetrically with spaces, similar to the initial pyramid pattern but flipped.

int rows = 5;
for (int r = 1; r <= rows; r++) {
    for (int s = rows; s > r; s--) {
        cout << " ";  // Print spaces to center the triangle
    }
    for (int c = 1; c <= 2 * r - 1; c++) {
        cout << "*";
    }
    cout << "\n";
}

The summary of Transformations:


I am inviting: @kuoba01, @simonnwigwe, and @ruthjoe

Cc:-
@sergeyk