SEC-S20W6 Practice cycles and Guess the number game

in sec20w6sergeyk •  last month 

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

1000194143.png
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.

1000194128.jpg

1000194133.jpg1000194135.jpg

The breakdown of the creative elements:

  • Quirky Character Introduction:
    A "Mysterious Voice" get to greets the player with a sarcastic tone, setting the scene for a humourous experience.

  • Hint with Personality:
    The game provides sarcastic and playful hints when the guess is too low or high. It is not just about guiding the player but doing so with a mocking sense of humor.

  • Random Sarcastic Comments:
    After every 5 incorrect guesses, a random sarcastic comment appears to keep things light-hearted funny, and enjoyable.

  • Witty Ending:
    Based on how many attempts it takes the players, the game ends with a custom message that congratulates users based on their performance.

  • Immersive Storytelling:
    From the point where player inputs input their name, they are immersed in a slightly whimsical word, with the character responding to their progress with snide occasional praise and remarks.


(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

  • 0 to 1000: If we are to randomly guess, on average, it would take about 500 guesses to hit the correct number, since there is no structured approach. In the worst scenario, it could take up to 1000 guesses.

  • 0 to 10,000: On average, it would take 5000 guesses with the worst case scenario being 10,000 guesses.

Linear Guessing

  • 0 to 1000: If we are to guess sequentially, for example, beginning from 0 and going up, in the worst scenario, it would take 1000 guesses to reach the number. On average, if we find the number after 500 guesses.

  • 0 to 10,000 Similarly, it would take up to 10,000 guesses with an average of 5000 guesses.

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.

  • 0 to 1000: In the worst scenario, we need about 20 guesses. That is because log2(1000)≈9.97 which means rounding up, it would take at most 10 guesses.

  • 0 to 10,000: Similarly, it would take about 14 guesses since log2(10000)≈13.29.

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.

1000194137.jpg

1000194139.jpg

Interpretation of How the Game Works.

Starting the Game:

  • The game begins with 30 balls, and players are prompted to pick between 1 and 5 balls.

Input Validation:

  • If a player enters a number that is outside the range 1 to 5 or tries to take more balls than are available, they are asked to enter a valid number.

Player Turns:

  • The game alternates between Player 1 and Player 2 after each valid move.

End Condition:

  • When the number of remaining balls reaches zero, the current player is declared the winner of the game.

Example

  • Round 1: Player 1 takes 3 balls, leaving 27 balls.
  • Round 2: Player 2 takes 5 balls, leaving 22 balls.

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:

  • Star: This is the starting with scattered creativity.

  • Square: Bringing order and balance.

  • Triangle: Streamlining the structure into something with fewer angles, and sides, symbolizing focus and precision.


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

Cc:-
@sergeyk

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

Upvoted. Thank You for sending some of your rewards to @null. It will make Steem stronger.

Congratulations, your post has been upvoted by @scilwa, which is a curating account for @R2cornell's Discord Community. We can also be found on our hive community & peakd as well as on my Discord Server

Manually curated by @ abiga554
r2cornell_curation_banner.png

Felicitaciones, su publication ha sido votado por @scilwa. También puedo ser encontrado en nuestra comunidad de colmena y Peakd así como en mi servidor de discordia

Omo I am just imagining the amount of time you put in this to make this work. Great work 👏

IMG_20240930_084439.png

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