Skip to content
Snippets Groups Projects
Commit 410e62af authored by Bianca Mazzetti's avatar Bianca Mazzetti
Browse files

Publish exercise 4

parent a7bebb39
No related branches found
No related tags found
No related merge requests found
File added
#include "std_lib_facilities.h"
#include "test.h"
int main()
{
// Her kan du teste koden og funksjonene dine,
// Ingenting som skrives her blir automatisk rettet, du tester her for din egen del
return 0;
}
#include "masterVisual.h"
// i denne løses 5 a, d, g og litt av f
// BEGIN: 5
//void playMasterMindVisual(){}
// END: 5
void addGuess(MastermindWindow &mwin, const string code, const char startLetter)
{
// BEGIN: 5b
// END: 5b
}
void addFeedback(MastermindWindow &mwin, const int correctPosition, const int correctCharacter)
{
// BEGIN: 5e
// END: 5e
}
void MastermindWindow::drawCodeHider()
{
if (code_hidden) {
draw_rectangle(Point{padX, 3 * padY}, winW - size * padX, padY, Color::black);
}
}
MastermindWindow::MastermindWindow(int x, int y, int w, int h, int size, const string &title)
: AnimationWindow(x, y, w, h, title),
guessBtn{{upperLeftCornerBtn.x, upperLeftCornerBtn.y}, btnW, btnH, "Add"},
guess{{upperLeftCornerInBox.x, upperLeftCornerInBox.y}, inBoxW, inBoxH, ""},
size(size)
{
add(guess);
add(guessBtn);
guessBtn.setCallback(std::bind(&MastermindWindow::cb_guess, this));
};
void MastermindWindow::drawGuessesAndFeedbacks()
{
std::map<int, Color> colorConverter{
{1, Color::red},
{2, Color::green},
{3, Color::yellow},
{4, Color::blue},
{5, Color::blue_violet},
{6, Color::dark_cyan}};
for (int guessIndex = 0; guessIndex < static_cast<int>(guesses.size()); guessIndex++)
{
/*********************************************************************/
// BEGIN: 5c
// Implementer funksjonalitet slik at det vises fargede rektangler i grafikkvinduet
// Legg merke til at vi er i et for-løkke som går gjennom alle gjettene som er gjort.
// Tegn gjettet som ligger på plassen guessIndex i vektoren guesses.
for (int i = 0; i < size; i++)
{
// Denne for-løkken går gjennom alle firkantene som skal tegnes i et gjett.
// Tegn rektangler ved bruk av draw_rectangle(). Bruk: colorConverter.at() for å få riktig farge
}
// END: 5c
}
for (int feedbackIndex = 0; feedbackIndex < static_cast<int>(feedbacks.size()); feedbackIndex++)
{
/*********************************************************************/
// BEGIN: 5f
// Implementer feedback
// Her skal mye likt gjøres som i 5c
for (int i = 0; i < size; i++)
{
// Tegn sirkler ved hjelp av draw_circle().
}
// END: 5f
}
}
string MastermindWindow::wait_for_guess()
{
while (!button_pressed && !should_close())
{
drawGuessesAndFeedbacks();
// Burde tegnes sist siden den skal ligge på toppen
drawCodeHider();
next_frame();
}
button_pressed = false;
string newGuess = guess.getText();
guess.setText("");
return newGuess;
}
string MastermindWindow::getInput(unsigned int n, char lower, char upper)
{
bool validInput = false;
string guess;
while (!validInput && !should_close())
{
guess.clear();
string input = wait_for_guess();
if (input.size() == n)
{
for (unsigned int i = 0; i < n; i++)
{
char ch = input.at(i);
if (isalpha(ch) && toupper(ch) <= upper && lower <= toupper(ch))
{
guess += toupper(ch);
}
else
break;
}
}
if (guess.size() == n)
{
validInput = true;
}
else
{
cout << "Invalid input guess again" << endl;
}
}
return guess;
}
void MastermindWindow::setCodeHidden(bool hidden) {
code_hidden = hidden;
}
#pragma once
#include "widgets/Button.h"
#include "widgets/TextInput.h"
#include "AnimationWindow.h"
#include "std_lib_facilities.h"
using namespace TDT4102;
// BEGIN: 5a
constexpr int winW = 0; // velg vindu bredde
constexpr int winH = 0; // velg vindu hoyde
constexpr int padX = 0; // velg x skalering
constexpr int padY = 0; // velg y skalering
constexpr int radCircle = 0; // velg sirkel radius
// END: 5a
// BEGIN 5
// deklarer playMasterMindVisual her
// END: 5
constexpr int btnW = padX * 2;
constexpr int btnH = padY;
constexpr Point upperLeftCornerBtn = Point{winW - padX - btnW, padY};
constexpr int inBoxW = winW - 3 * padX - btnW;
constexpr int inBoxH = padY;
constexpr Point upperLeftCornerInBox = Point{padX, padY};
struct Guess {
const string code;
char startLetter = 'a';
};
struct Feedback {
const int correctPosition = 0;
const int correctCharacter = 0;
};
// Her defineres klassen MastermindWindow, som arver fra klassen AnimationWindow.
// Det betyr at alle medlemsvariable og medlemsfunksjoner i AnimationWindow, også er en
// del av MastermindWindow. Du kan lese mer om klasser i kapittel 9.4 og om arv
// i kapittel 14.3 i læreboka. Dette skal du lære mer om senere i faget.
class MastermindWindow : public AnimationWindow
{
public:
MastermindWindow(int x, int y, int w, int h, int size, const std::string &title);
void cb_guess(){ newGuess(); }
vector<Guess> guesses;
vector<Feedback> feedbacks;
void drawGuessesAndFeedbacks();
void setCodeHidden(bool hidden);
string getInput(unsigned int n, char lower, char upper);
private:
string wait_for_guess();
void newGuess() { button_pressed = true; }
void drawCodeHider();
bool button_pressed = false;
bool code_hidden = true;
Button guessBtn;
TextInput guess;
int size = 0;
};
void addGuess(MastermindWindow &mwin, const string code, const char startLetter);
void addFeedback(MastermindWindow &mwin, const int correctPosition, const int correctCharacter);
#include "mastermind.h"
// BEGIN: 4
//Oppgave 4a til 4j løses her forutenom 4e og 4f
//playMastermind(){}
// END: 4
// BEGIN: 4e
///*returverdi*/ checkCharactersAndPosition(/*param 1: code, param 2: guess*/) {}
// END: 4e
// BEGIN: 4f
///*returverdi*/ checkCharacters(/*param 1: code, param 2: guess*/) {}
// END: 4f
#pragma once
#include "std_lib_facilities.h"
#include "utilities.h"
// BEGIN: 4
//deklarer playMastermind her
// END: 4
// BEGIN: 4e
//deklarer checkCharactersAndPosition her
// END: 4e
// BEGIN: 4f
//deklarer checkCharacters her
// END: 4f
#include "test.h"
void testCallByValue()
{
int v0 = 5;
int increment = 2;
int iterations = 10;
int result = incrementByValueNumTimes(v0, increment, iterations);
cout << "v0: " << v0 << " increment: " << increment
<< " iterations: " << iterations << " result: " << result << '\n';
}
void testCallByReference()
{
// BEGIN: 1d
// END: 1d
}
void testString(){
// 3b OG 3e gjøres inne i her
// BEGIN: 3e
// END: 3e
}
\ No newline at end of file
#pragma once
#include "utilities.h"
#include "std_lib_facilities.h"
void testCallByValue();
void testCallByReference();
void testString();
\ No newline at end of file
#include "utilities.h"
int incrementByValueNumTimes(int startValue, int increment, int numTimes)
{
for (int i = 0; i < numTimes; i++)
startValue += increment;
return startValue;
}
// BEGIN: 1d
///*returverdi*/ incrementByValueNumTimesRef(/*param 1: startValue(ref), param 2: increment, param 3: numtimes*/){}
// END: 1d
// BEGIN: 1e
///*returverdi*/ swapNumbers(/*parametre*/){}
// END: 1e
// BEGIN: 2b
///*returverdi*/ printStudent(/*input Student*/) {}
// END: 2b
// BEGIN: 2c
///*returverdi*/ isInProgram(/*param 1: Student, param 2: string*/){}
// END: 2c
// BEGIN: 3a
///*returverdi*/ randomizeString(/*param 1: antall tegn, param 2: øvre grense, param 3: nedre grense */)
// END: 3a
// BEGIN: 3c
///*returverdi*/ readInputToString(/*param 1: lengde n, param 2: øvre grense, param 3: nedre grense*/)
// END: 3c
// BEGIN: 3d
///*returverdi*/ countChar(/*param 1: string, param 2: char*/)
// END: 3d
#pragma once
#include "std_lib_facilities.h"
// BEGIN: 1b
// deklarerer funksjonen incrementByValueNumTimes her
// END: 1b
// BEGIN: 1d
// deklarerer funksjonen incrementByValueNumTimesRef her
// END: 1d
// BEGIN: 1e
// deklarerer funksjonen swapNumbers her
// END: 1e
// BEGIN: 2a
// lag struct Student her
// END: 2a
// BEGIN: 2b
// deklarerer funksjonen printStudent her
// END: 2b
// BEGIN: 2c
// deklarerer funksjonen isInProgram her
// END: 2c
// BEGIN: 3a
// deklarerer funksjonen randomizeString her
// END: 3a
// BEGIN: 3c
// deklarerer funksjonen readInputToString her
// END: 3c
// BEGIN: 3d
// deklarerer funksjonen countChar her
// END: 3d
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment