Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <iostream>
#include <random>

int16_t GenerateRandomNumber() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А почему для хранения чисел от 0 до 9 вы выбрали именно 2 байтное знаковое? Если эконоить на размере, то можно же выбрать более подходящий тип

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Потому что int8_t начинал парситься в char, что заставляло
либо использовать inp - '0' в качестве числа, либо принимать int и делать static_cast

Оба варианта показались не очень красивыми, решил оставить этот (хотя, конечно, в таком виде неочевидно, почему 16)

std::random_device random;

std::default_random_engine engine(random());
std::uniform_int_distribution<int16_t> uniform_dist(0, 9);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Отлично, что используете более современный подход. srand в задании я предложил использовать для простоты, но вообще в настоящем коде правильнее было бы использовать типы из <random>


return uniform_dist(engine);
}

int16_t ReceiveUserInput() {
int16_t inp{};

std::cout << "Guess a number from 0 to 9: ";
std::cin >> inp;

return inp;
}

int main() {
auto random_number = GenerateRandomNumber();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно сделать константой


while (true) {
auto user_input = ReceiveUserInput();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В рамках одной итерации тоже может быть константой)


if (user_input == random_number) {
std::cout << "Correct!" << std::endl;
break;
}

std::cout << "Wrong!" << std::endl;
}
}