Skip to content
Snippets Groups Projects
Commit 4954df2d authored by Eila Aurora Huru Bergene's avatar Eila Aurora Huru Bergene
Browse files
parents 355de913 adbd4269
No related branches found
No related tags found
No related merge requests found
Showing
with 442 additions and 12 deletions
...@@ -15,4 +15,4 @@ endif ...@@ -15,4 +15,4 @@ endif
src = ['main.cpp'] src = ['main.cpp']
exe = executable('program', src, dependencies : [animationwindow_dep, sdl2_dep], cpp_args : compiler_flags) exe = executable('program', src, dependencies : [std_lib_facilities_dep], cpp_args : compiler_flags)
...@@ -32,5 +32,8 @@ namespace TDT4102 { ...@@ -32,5 +32,8 @@ namespace TDT4102 {
void setCallback(std::function<void(void)> callback); void setCallback(std::function<void(void)> callback);
virtual ~Widget() {} virtual ~Widget() {}
void setVisible(bool isVisible); void setVisible(bool isVisible);
unsigned int getWidth() const;
unsigned int getHeight() const;
void setSize(unsigned int newWidth, unsigned int newHeight);
}; };
} }
\ No newline at end of file
...@@ -21,7 +21,8 @@ const TDT4102::Color TDT4102::Color::navy = TDT4102::Color{0x000080}; ...@@ -21,7 +21,8 @@ const TDT4102::Color TDT4102::Color::navy = TDT4102::Color{0x000080};
const TDT4102::Color TDT4102::Color::blue = TDT4102::Color{0x0000ff}; const TDT4102::Color TDT4102::Color::blue = TDT4102::Color{0x0000ff};
const TDT4102::Color TDT4102::Color::teal = TDT4102::Color{0x008080}; const TDT4102::Color TDT4102::Color::teal = TDT4102::Color{0x008080};
const TDT4102::Color TDT4102::Color::aqua = TDT4102::Color{0x00ffff}; const TDT4102::Color TDT4102::Color::aqua = TDT4102::Color{0x00ffff};
const TDT4102::Color TDT4102::Color::orange = TDT4102::Color{0xffa500}; const TDT4102::Color TDT4102::Color::orange = TDT4102::Color{0xffa500};
const TDT4102::Color TDT4102::Color::mid_gray = TDT4102::Color{0x6a6c6e};
const TDT4102::Color TDT4102::Color::alice_blue = TDT4102::Color{0xf0f8ff}; const TDT4102::Color TDT4102::Color::alice_blue = TDT4102::Color{0xf0f8ff};
const TDT4102::Color TDT4102::Color::antique_white = TDT4102::Color{0xfaebd7}; const TDT4102::Color TDT4102::Color::antique_white = TDT4102::Color{0xfaebd7};
const TDT4102::Color TDT4102::Color::aquamarine = TDT4102::Color{0x7fffd4}; const TDT4102::Color TDT4102::Color::aquamarine = TDT4102::Color{0x7fffd4};
......
...@@ -18,4 +18,17 @@ TDT4102::Widget::Widget(TDT4102::Point location, unsigned int widgetWidth, unsig ...@@ -18,4 +18,17 @@ TDT4102::Widget::Widget(TDT4102::Point location, unsigned int widgetWidth, unsig
void TDT4102::Widget::setVisible(bool visible) { void TDT4102::Widget::setVisible(bool visible) {
isVisible = visible; isVisible = visible;
}
unsigned int TDT4102::Widget::getWidth() const {
return width;
}
unsigned int TDT4102::Widget::getHeight() const {
return height;
}
void TDT4102::Widget::setSize(unsigned int newWidth, unsigned int newHeight) {
width = newWidth;
height = newHeight;
} }
\ No newline at end of file
File added
#include "Tile.h" #include "Tile.h"
#include <map>
// For aa sette labelfarge i henhold til hvor mange miner som er rundt
const std::map<int, TDT4102::Color> minesToColor{{1, TDT4102::Color::blue},
{2, TDT4102::Color::red},
{3, TDT4102::Color::dark_green},
{4, TDT4102::Color::dark_magenta},
{5, TDT4102::Color::dark_blue},
{6, TDT4102::Color::dark_cyan},
{7, TDT4102::Color::dark_red},
{8, TDT4102::Color::gold}};
// For aa sette Tilelabel i henhold til state // For aa sette Tilelabel i henhold til state
const std::map<Cell, std::string> cellToSymbol{{Cell::closed, ""}, const std::map<Cell, std::string> cellToSymbol{{Cell::closed, ""},
......
#pragma once #pragma once
#include "AnimationWindow.h" #include "AnimationWindow.h"
#include "widgets/Button.h" #include "widgets/Button.h"
#include <map>
// De forskjellige tilstandene en Tile kan vaere i // De forskjellige tilstandene en Tile kan vaere i
enum class Cell { closed, open, flagged }; enum class Cell { closed, open, flagged };
...@@ -10,6 +11,17 @@ class Tile : public TDT4102::Button ...@@ -10,6 +11,17 @@ class Tile : public TDT4102::Button
Cell state = Cell::closed; Cell state = Cell::closed;
void set_label(std::string s) { this->setLabel(s);} void set_label(std::string s) { this->setLabel(s);}
void set_label_color(TDT4102::Color c) {this->setLabelColor(c);} void set_label_color(TDT4102::Color c) {this->setLabelColor(c);}
// For aa sette labelfarge i henhold til hvor mange miner som er rundt
const std::map<int, TDT4102::Color> minesToColor{{1, TDT4102::Color::blue},
{2, TDT4102::Color::red},
{3, TDT4102::Color::dark_green},
{4, TDT4102::Color::dark_magenta},
{5, TDT4102::Color::dark_blue},
{6, TDT4102::Color::dark_cyan},
{7, TDT4102::Color::dark_red},
{8, TDT4102::Color::gold}};
public: public:
Tile(TDT4102::Point pos, int size); Tile(TDT4102::Point pos, int size);
......
#include "std_lib_facilities.h"
template<typename T>
void print(T a) {
std::cout << a << std::endl;
}
template<typename T>
struct NotAVector {
T a;
};
#include <queue>
#include <deque>
struct Vector2 {
float x;
float y;
Vector2 operator+(Vector2& a) {
Vector2 out;
out.x = x + a.x;
out.y = y + a.y;
return out;
}
};
int main(int argc, char** argv) {
std::vector<std::string> parameters;
for(int i = 0; i < argc; i++) {
parameters.push_back(std::string(argv[i]));
std::cout << "Parameter " << i << ": " << parameters.at(i) << std::endl;
}
std::queue<std::string> koo;
koo.push("Number one");
koo.push("Number two");
koo.push("Number three");
while(!koo.empty()) {
std::cout << koo.front() << std::endl;
koo.pop();
}
std::deque<std::string> d;
std::vector<int> list = {1, 6, 3, 8, 5, 3, 7, 4};
std::vector<Vector2> vecList = {{2, 5}, {2, 7}, {65, 87}};
Vector2 sum = std::accumulate(vecList.begin(), vecList.end(), Vector2{0, 0});
std::random_device device;
std::default_random_engine engine(device());
std::shuffle(list.begin(), list.end(), engine);
int a = 5;
int b = 4;
//std::swap(a, b);
//std::min(a, b);
//std::max(a, b);
std::sort(list.begin(), list.end());
std::fill(list.begin(), list.end(), 55);
std::vector<int> anotherList(100);
std::copy(list.begin(), list.end(), anotherList.begin() + 5);
anotherList.erase(anotherList.begin() + 3, anotherList.begin() + 5);
for(std::vector<int>::reverse_iterator it = list.rbegin(); it != list.rend(); it++) {
std::cout << *it << std::endl;
}
//NotAVector<int> vec;
print<std::string>("a");
print<int>(10);
print(10.0);
std::array<int, 5> array;
std::map<std::string, int> aMap;
std::vector<int> vec(10);
std::unique_ptr<int*> test;
std::map<std::string, int> map;
return 0;
}
// Lecture 13, Examples 3 and 4: STL containers and algorithms, Dragana Laketic
#include "std_lib_facilities.h"
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <cassert>
int main() {
// STL containers
std::queue<int> queue; // Queue is a First-In-First-Out (FIFO) data
// structure. In STL C++ standard library it is
// implemented as a template.
queue.push(1); // We use push() method to enqueue an element in queue
queue.push(2);
queue.push(3);
while(!queue.empty()) { // The value of an element of a queue which is
// first to leave the queue when we call pop() method
// can be accessed by front() method.
cout << queue.front() << endl;
queue.pop();
}
assert(queue.size() == 0); // repetition from last lecture: use of assert
// to make sure all the elements of the queue have left
// the queue (have been popped from the queue).
std::stack<string> stack; // Stack is a Last-In-First-Out data structure. That means
// that the last element placed on stack will be the first to
// leave the stack when we call stack's pop() method.
stack.push("First on Stack");
stack.push("Second on Stack");
stack.push("Third on Stack");
while(!stack.empty()) {
cout << stack.top() << endl; // We use top() method to get the values of
// the element which is placed last on stack.
// It will be the first element to leave the stack
// when we call pop() method.
stack.pop();
}
// Note how we use push() and pop() methods for two different containers -
// a queue and a stack - but how these methods work in accordance to the
// definition of each of them, i.e., the definition of a FIFO and a definition
// of a LIFO data structure.
// Back to vector. Although we know it very well, now we understand
// a "bigger picture" as well: it is yet another template container
// class from STL library.
std::vector<int> vec{50, 70, 20, 40, 80, 10, 90};
// Usually we traverse its elements in a for-loop like this:
for (int i = 0; i < vec.size(); i++) {
cout << vec.at(i) << endl; // Here we use a range-checked method at()
// to access the vector's elements.
}
cout << endl;
// Here is an example of traversing the same vector with the use
// of iterators. Iterators are members of STL containers which contain
// a pointer to an element of the container and thereby enable us to
// access the values stored in the container's elements.
// Note that STL containers also provide begin() and end() methods
// which return an iterator which points to the first element of
// the container and to the location after the last element of the
// container. In other words, an STL container contains the elements in
// the interval [STLcontainer.begin(), STLcontainer.end()) - where
// mathematical notation for open and close limit of the interval applies.
cout << "Vector traversed with iterator: " << endl;
// Pay attention to how the iterator type is declared! It reflects
// the instantiated template of a container!
for(std::vector<int>::iterator it = vec.begin(); it != vec.end(); it++) {
cout << *it << endl;
}
cout << endl;
std::list<int> lst{1, 2, 3}; // Linked lists are containers which use pointers to connect
// the elements into it. There are two types of lists:
// singly linked lists and doubly linked lists. An element
// of a singly linked list beside the data value contains also
// a pointer to the next element in the list, while the doubly
// linked lists elements contain a pointer to a next element in
// the list and a pointer to the previous element in the list.
// With each insersion of a new element, search for an element of
// certain value or erasure of an element, the pointers to some
// memory locations where individual elements are stored must also
// be handled. This all takes much of the computing resources and
// increases the time needed for processing. Therefore, unless really
// necesary within the application at hand (or the task at the exam),
// they are advised to be avoided. Use vectors instead and
// whenever you can!
cout << "List with iterator: " << endl;
// Pay attention to how the type of the iterator is declared in this case!
// Compare it with the iterator type from the vector traversal example!
for (std::list<int>::iterator it = lst.begin(); it != lst.end(); it++) {
cout << *it << endl;
}
cout << endl;
// There exist also a reverse iterator which is analogous to an iterator
// discussed above with the distinction in the direction of the traversal
// which is illustrated in the following example:
cout << "Reverse iterator:" << endl;
for (std::list<int>::reverse_iterator rit = lst.rbegin(); rit != lst.rend(); rit++) {
cout << *rit << endl;
}
cout << endl;
// STL algorithms
// Iterators are especially handy when it comes to using algorithms from
// the STL standard library.
// An example of calling the sort() algorithm for the first 4
// elements of the vector vec defined above:
std::sort(vec.begin(), vec.begin() + 4);
cout << "Partially sorted vector:" << endl;
for(std::vector<int>::iterator it = vec.begin(); it != vec.end(); it++) {
cout << *it << endl;
}
cout << endl;
// An example of using an algorithm from the STL library which
// processes the elements of two different types of containers.
// We provide iterators to the copy algorithm and invoke the copying
// of list lst elements from the first to the last to the first element
// of the vector and the subsequent ones:
std::copy(lst.begin(), lst.end(), vec.begin());
cout << "Vector after copying from the list:" << endl;
for(std::vector<int>::iterator it = vec.begin(); it != vec.end(); it++) {
cout << *it << endl;
}
cout << endl;
// Note that the checks of the container limits are not performed
// so you need to take care of it. Test what would happen if instead
// of a vec.begin() you provided vec.end() / vec.end()-1 / vec.end()-2.
// Am example of fill algorithm:
std::fill(vec.begin(), vec.begin()+5, 500);
cout << "Vector after fill:" << endl;
for(std::vector<int>::iterator it = vec.begin(); it != vec.end(); it++) {
cout << *it << endl;
}
cout << endl;
return 0;
}
// Lecture 13, Example 4: main() function, Dragana Laketic
#include <iostream>
#include <string>
#include <vector>
// Extenden version of main() function has two arguments:
// int argc - total number of program arguments
// char** argv - array of strings which contains the arguments
// (char**-style is inherited from C programming language and
// stands for an array of char* variables (strings) where each
// char* element contains a string. In C, a string was represented
// as an array of char variables with special character '\0'
// at the end to denote the end of string.)
// Run this program from the command line with some arguments listed, like this:
// <path_to_your_project>/builddir/ ./program 1001 textfile.txt binary
// What are the arguments written out in the terminal?
// How can you use these values passed to your program?
int main(int argc, char** argv) {
std::vector<std::string> arguments;
// Each program has at least one argument and that
// is the location - the full path - of the program file within
// the file system where program is run.
std::cout << "I am a program with " << argc << " arguments:" << std::endl;
// We can store arguments from argv array in a more C++-friendly
// manner, like in the example below within a vector of strings.
for (int i = 0; i < argc; i++) {
arguments.push_back(argv[i]); // Note how we use index operator
// on array variable argv.
}
// Her skriver vi ut argumenter fra vektoren:
for (int i = 0; i < arguments.size(); i++) {
std::cout << "Argument " << i << ": " << arguments.at(i) << std::endl;
}
return 0;
}
// Lecture 13, Example 1: templates, Dragana Laketic
#pragma once
#include "std_lib_facilities.h"
// Templates enable the creation of classes and functions which have
// the same interface for different data types. The interface is
// written only once with the data type(s) passed in as parameters.
// Beside one or more data type parameters, there can also be values of
// some other type passed in to a template - most often it is integer
// values.
// When a template class or a template function is used in the program,
// we need to provide parameters - data type(s) and/or values - for the
// concrete class or function. When compiler comes across a templated
// class or function in the prgram file, it instantiates the corresponding
// template with exactly those parameters provided in the program. It is
// worth noting that this type-resolution and template instantiation
// happens at compile time.
// We keep template definitions - template class, template class member functions
// and template functions - within a header file. This is because it is only a template
// and the instantiated classes and functions, those which will be actually used
// in the program, are made by the compiler at compilation time.
// 1) An example of a class template
// With template we use a keyword 'template' followed by
// keyword 'typename' and some name (as of our choice) between
// less than and greater than signs. The name of a typename parameter
// can be whatever you choose but often it is 'T' or some variation
// of it for data types.
template<typename T>
class Point{
// Where ever there is 'T' in the template class definition
// there will be a concrete data type set by the compiler when
// it comes across our invocation of the templated class with
// that concrete parameter passed between '<' and '>'.
T x;
T y;
public:
Point(T xx, T yy) : x{xx}, y{yy} {}
T getX() const {return x;}
T getY() const {return y;}
void printMe() {
cout << "(" << x << ", " << y << ")" << endl;
}
};
// 2) An example of a template class which takes more than one parameter
template<typename T1, typename T2>
class ChoppedPoint {
T1 x;
T2 y;
public:
ChoppedPoint(T1 xx, T2 yy) : x{xx}, y{yy} {}
void printMe() {
cout << "(" << x << ", " << y << ")" << endl;
}
};
// 3) An example of a function template
template<typename TT> // note: here we use TT (our free choice)
//after 'typename'
void PrintValue(TT var) {
cout << "Template function: " << endl;
cout << var << endl;
}
// 4) An example of integer value as a template parameter
// (compare with std::array<> where we also pass in an integer value
// as a template parameter)
template<typename T, int N>
class Collection {
T* collection;
public:
Collection() {
cout << "Constructor of template<typename T, int N> class Collection" << endl;
collection = new T[N];
}
~Collection() {
cout << "Destructor of template<typename T, int N> class Collection" << endl;
delete[] collection;
}
}
// Lecture 13, Example 1: templates, Dragana Laketic
#include "std_lib_facilities.h"
#include "Points.h"
int main() {
// 1) Class template
Point<int> pointI{1, 3}; // Here the compiler instantiates template class Point
// to 'int' data type instead of generic 'T' ...
Point<double> pointD{10.5, 5.5}; // ... and here to 'double'.
cout << "X coordinate int Point: " << pointI.getX() << endl;
pointD.PrintMe();
// 2) Class template with more than one typename parameter
ChoppedPoint<int, double> pointC{4, 7.5};
pointC.PrintMe();
// 3) Function template
cout << "Function template:" << endl;
PrintValue<int>(5); // Providing explicitly data type to instantiate
// a function template with.
PrintValue<string>("ABC"); // Explicit instantiation of the function template
// with data type 'string'.
PrintValue(100); // An example of so-called template argument deduction when
// a compiler deduces which type the function template should
// be instantiated with based on the argument passed to a function.
// Here it was a simple case of a built-in data type int, but
// beware that for more complex types, for example user-defined
// classes, it is recommended providing an explicit data type
// for the function template instantiation like in the two examples
// above.
// 4) Class template with one data type and one integer as parameters
// Note: during lecture we mentioned such case for std::array but here
// is an example of the use of a user-defined template with such parameters
Collection<int, 10> coll; // Note: for exercise, extend class Collection with
// some functions and call those functions ofr coll object.
return 0;
}
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