Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

[task_04] add task_04 #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
63 changes: 63 additions & 0 deletions trunk/210647/task_04/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Лабораторная работа №4 #

## Наследование ##

## Вариант 2 ##

## Реализация/ход работы ##

Создали класс ***GameWindow***:

```c++
//..
class GameWindow : public Window {//1
public:
GameWindow(int id, int height, int width, bool areAdministratorRightsGranted, Settings* settings);//1
virtual void doAction() = 0;//1

protected:
Settings* settings;//1
};
```

Реализовали 2 класса-наследника (***RockPaperScissorsGameWindow*** и ***GuessNumberGameWindow***)
от класса ***GameWindow***:

```c++
\\..
class RockPaperScissorsGameWindow : public GameWindow {//1
public:
RockPaperScissorsGameWindow(int id, int height, int width, bool areAdministratorRightsGranted, Settings* settings);//1
void doAction() override;//1
int calculateMemoryNeeded();//1
};
```

```c++
\\..
class GuessNumberGameWindow : public GameWindow {
public:
GuessNumberGameWindow(int id, int height, int width, bool areAdministratorRightsGranted, Settings* settings);//1
void doAction() override;//1
int calculateMemoryNeeded();//1

private:
int targetNumber;//1
};
```

#### Результат работы программы ####

Вывод меню для выбора игры "Камень-ножницы-бумага":

![img.png](images/img.png)

Вывод меню для выбора игры "Угадай число":

![img_1.png](images/img_1.png)

![img_2.png](images/img_2.png)

#### Вывод ####

Научились создавать простейшие классы-наследники.
Binary file added trunk/210647/task_04/images/img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added trunk/210647/task_04/images/img_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added trunk/210647/task_04/images/img_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions trunk/210647/task_04/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
cmake_minimum_required(VERSION 3.5)

project(task_04)

if(WIN32)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif()

set(CMAKE_CXX_STANDARD 17)

add_executable(po8_210647_task_04
Main.cpp
GameWindow.cpp
GuessNumberGameWindow.cpp
GuessNumberSettings.cpp
GuessNumberSettings.cpp
RockPaperScissorsGameWindow.cpp
RockPaperScissorsSettings.cpp
Settings.cpp
Window.cpp
)
5 changes: 5 additions & 0 deletions trunk/210647/task_04/src/GameWindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "GameWindow.h"

GameWindow::GameWindow(int id, int height, int width, bool areAdministratorRightsGranted, Settings* settings)//210647
: Window(id, height, width, areAdministratorRightsGranted), settings(settings) {//210647
}
25 changes: 25 additions & 0 deletions trunk/210647/task_04/src/GameWindow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef GAME_WINDOW_H
#define GAME_WINDOW_H

#include "Window.h"
#include "Settings.h"

class GameWindow : public Window {//210647
public:
GameWindow(int id, int height, int width, bool areAdministratorRightsGranted, Settings* settings);//210647
virtual void doAction() = 0;//1//210647
virtual ~GameWindow() = default;//1//210647

Settings* getSettings() const {//1//210647
return settings;//1//210647
}//1//210647

void setSettings(Settings* newSettings) {//1//210647
settings = newSettings;//1//210647
}//1//210647

private://1//210647
Settings* settings;//1//210647
};//1//210647

#endif
51 changes: 51 additions & 0 deletions trunk/210647/task_04/src/GuessNumberGameWindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

#include "GuessNumberGameWindow.h"
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <random>

GuessNumberGameWindow::GuessNumberGameWindow(int id, int height, int width, bool areAdministratorRightsGranted, Settings* settings)//1//210647
: GameWindow(id, height, width, areAdministratorRightsGranted, settings) {//1//210647
std::random_device rd;//1//210647
std::uniform_int_distribution distribution(1, 100);//1//210647
targetNumber = distribution(rd);//1//210647
}//210647

void GuessNumberGameWindow::doAction() {//1//210647
doWork();//1//210647
int memoryNeeded = calculateMemoryNeeded();//1//210647
std::cout << "Memory Needed: " << memoryNeeded << " MB" << std::endl;//1//210647

int attempts = 0;//1//210647
bool isGuessed = false;//1//210647

std::cout << "A number between 1 and 100." << std::endl;//1//210647

while (!isGuessed && attempts < 2) {//1//210647
int guess;//1//210647

std::cout << "Enter your guess: ";//1//210647
std::cin >> guess;//1//210647
attempts++;//1//210647

if (guess < targetNumber) {//1//210647
std::cout << "Too low. Try again." << std::endl;//1//210647
}//210647
else if (guess > targetNumber) {//1//210647
std::cout << "Too high. Try again." << std::endl;//1//210647
}//1//210647
else {//1//210647
std::cout << "Congratulations! You guessed the number in " << attempts << " attempts." << std::endl;//1//210647
isGuessed = true;//1//210647
}//1//210647
}//1//210647

if (!isGuessed) {
std::cout << "Sorry, you're out of attempts. The number was " << targetNumber << "." << std::endl;//1//210647
}
}

int GuessNumberGameWindow::calculateMemoryNeeded() const {//1//210647
return getSettings()->getSetting3() * getSettings()->getSetting4();//1//210647
}
17 changes: 17 additions & 0 deletions trunk/210647/task_04/src/GuessNumberGameWindow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef GUESS_NUMBER_GAME_WINDOW_H
#define GUESS_NUMBER_GAME_WINDOW_H

#include "GameWindow.h"
#include "Settings.h"

class GuessNumberGameWindow : public GameWindow {//1//210647
public:
GuessNumberGameWindow(int id, int height, int width, bool areAdministratorRightsGranted, Settings* settings);//1//210647
void doAction() override;//1//210647

private:
int targetNumber;//1//210647
int calculateMemoryNeeded() const;//1//210647
};

#endif
6 changes: 6 additions & 0 deletions trunk/210647/task_04/src/GuessNumberSettings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "GuessNumberSettings.h"

GuessNumberSettings::GuessNumberSettings()//1//210647
: Settings(6, 7, 8, 9, 10) {//1//210647
// �������� �������� ��� ���� "������ �����"//210647
}
12 changes: 12 additions & 0 deletions trunk/210647/task_04/src/GuessNumberSettings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef GUESS_NUMBER_SETTINGS_H
#define GUESS_NUMBER_SETTINGS_H

#include "Settings.h"//1//210647

class GuessNumberSettings : public Settings {//1//210647
public:
GuessNumberSettings();//1//210647
~GuessNumberSettings() override = default;//1//210647
};//1//210647

#endif
68 changes: 68 additions & 0 deletions trunk/210647/task_04/src/Main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "GameWindow.h"
#include "Settings.h"
#include "RockPaperScissorsGameWindow.h"
#include "GuessNumberGameWindow.h"
#include <iostream>

void playRockPaperScissors(Settings& settings) {//1//210647
RockPaperScissorsGameWindow rockPaperScissors(1, 800, 600, true, &settings);//1//210647
while (true) {//1//210647
rockPaperScissors.doAction();//1//210647
int playAgain;//1//210647
std::cout << "Play again? (1 for yes, 0 for no): ";//1//210647
std::cin >> playAgain;//1//210647
if (playAgain == 0) {//1//210647
break;//1//210647
}//1//210647
}//1//210647
}//210647

void playGuessNumber(Settings& settings) {//1//210647
GuessNumberGameWindow guessNumber(2, 800, 600, true, &settings);//1//210647
while (true) {//1//210647
guessNumber.doAction();//1//210647
int playAgain;//1//210647
std::cout << "Play again? (1 for yes, 0 for no): ";//1//210647
std::cin >> playAgain;//1//210647
if (playAgain == 0) {//1//210647
break;//1//210647
}//1//210647
}//210647
}//210647

int main() {//210647
Settings rockPaperScissorsSettings(1, 2, 3, 4, 5);//1//210647
Settings guessNumberSettings(6, 7, 8, 9, 10);//1//210647

int choice;//1//210647

do {
std::cout << "Choose a game:" << std::endl;//1//210647
std::cout << "1. Rock-Paper-Scissors" << std::endl;//1//210647
std::cout << "2. Guess the Number" << std::endl;//1//210647
std::cout << "3. Exit" << std::endl;//1//210647
std::cout << "Enter your choice: ";//1//210647
std::cin >> choice;//1//210647

switch (choice) {//1//210647
case 1://1//210647
playRockPaperScissors(rockPaperScissorsSettings);//1//210647
break;//1//210647

case 2://1//210647
playGuessNumber(guessNumberSettings);//1//210647
break;//1//210647

case 3://210647
std::cout << "Exiting the program." << std::endl;//1//210647
break;//1//210647

default://210647
std::cout << "Invalid choice. Please try again." << std::endl;//1//210647
break;//1//210647
}

} while (choice != 3);//210647

return 0;//210647
}
66 changes: 66 additions & 0 deletions trunk/210647/task_04/src/RockPaperScissorsGameWindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "RockPaperScissorsGameWindow.h"
#include <iostream>
#include <ctime>
#include <random>

using namespace std;

RockPaperScissorsGameWindow::RockPaperScissorsGameWindow(int id, int height, int width, bool areAdministratorRightsGranted, Settings* settings)//1//210647
: GameWindow(id, height, width, areAdministratorRightsGranted, settings) {//1//210647
std::random_device rd;//1//210647
std::uniform_int_distribution distribution(0, 2);//1//210647
randomChoicePlayer2 = distribution(rd);//1//210647
}

void RockPaperScissorsGameWindow::doAction() {//1//210647
doWork();//1//210647
int memoryNeeded = calculateMemoryNeeded();//1//210647
cout << "Memory Needed: " << memoryNeeded << " MB" << endl;//1//210647

int player1Choice;//1//210647
cout << "Player 1, enter your choice (0 - Rock, 1 - Scissors, 2 - Paper): ";//1//210647
cin >> player1Choice;//1//210647


string choicePlayer1;//1//210647
string choicePlayer2;//1//210647

if (player1Choice == 0) {//1//210647
choicePlayer1 = "Rock";//1//210647
}//1//210647
else if (player1Choice == 1) {//1//210647
choicePlayer1 = "Scissors";//1//210647
}//1//210647
else {//1//210647
choicePlayer1 = "Paper";//1//210647
}//1//210647

if (randomChoicePlayer2 == 0) {//1//210647
choicePlayer2 = "Rock";//1//210647
}//1//210647
else if (randomChoicePlayer2 == 1) {//1//210647
choicePlayer2 = "Scissors";//1//210647
}
else {//1//210647
choicePlayer2 = "Paper";//1//210647
}

cout << "Player 1 chose: " << choicePlayer1 << endl;//1//210647
cout << "Player 2 chose: " << choicePlayer2 << endl;//1//210647

if (player1Choice == randomChoicePlayer2) {//1//210647
cout << "It's a tie!" << endl;//1//210647
}
else if ((player1Choice == 0 && randomChoicePlayer2 == 1) ||//1//210647
(player1Choice == 1 && randomChoicePlayer2 == 2) ||//1//210647
(player1Choice == 2 && randomChoicePlayer2 == 0)) {//1//210647
cout << "Player 1 wins!" << endl;//1//210647
}
else {//1//210647
cout << "Player 2 wins!" << endl;//1//210647
}
}

int RockPaperScissorsGameWindow::calculateMemoryNeeded() const {//1//210647
return getSettings()->getSetting1() * getSettings()->getSetting2();//1//210647
}
18 changes: 18 additions & 0 deletions trunk/210647/task_04/src/RockPaperScissorsGameWindow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef ROCK_PAPER_SCISSORS_GAME_WINDOW_H
#define ROCK_PAPER_SCISSORS_GAME_WINDOW_H

#include "GameWindow.h"
#include "Settings.h"

class RockPaperScissorsGameWindow : public GameWindow {//1//210647
public://1
RockPaperScissorsGameWindow(int id, int height, int width, bool areAdministratorRightsGranted, Settings* settings);//1//210647
void doAction() override;//1//210647

private:
int randomChoicePlayer2;//1//210647

int calculateMemoryNeeded() const;//1//210647
};

#endif
6 changes: 6 additions & 0 deletions trunk/210647/task_04/src/RockPaperScissorsSettings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "RockPaperScissorsSettings.h"

RockPaperScissorsSettings::RockPaperScissorsSettings()//1//210647
: Settings(1, 2, 3, 4, 5) {//1//210647
// �������� �������� ��� ���� "������-�������-������"
}
14 changes: 14 additions & 0 deletions trunk/210647/task_04/src/RockPaperScissorsSettings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef ROCK_PAPER_SCISSORS_SETTINGS_H
#define ROCK_PAPER_SCISSORS_SETTINGS_H

#include "Settings.h"//1//210647

class RockPaperScissorsSettings : public Settings {//1//210647
public:
RockPaperScissorsSettings();//1//210647
~RockPaperScissorsSettings() override = default;//1//210647

// �������� �������� ��� ���� "������-�������-������"//1//210647
};

#endif
Loading