From e82491b4c5331dcd854a446df01a9c76c0e50369 Mon Sep 17 00:00:00 2001 From: Angeld55 Date: Sat, 21 Sep 2019 12:08:32 +0300 Subject: Add files via upload --- DiamondProblemAndPolymorphism/Chess/Knight.hpp | 37 ++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 DiamondProblemAndPolymorphism/Chess/Knight.hpp (limited to 'DiamondProblemAndPolymorphism/Chess/Knight.hpp') diff --git a/DiamondProblemAndPolymorphism/Chess/Knight.hpp b/DiamondProblemAndPolymorphism/Chess/Knight.hpp new file mode 100644 index 0000000..eb2ee9f --- /dev/null +++ b/DiamondProblemAndPolymorphism/Chess/Knight.hpp @@ -0,0 +1,37 @@ +#include "Figure.hpp" + +class Knight : public Figure +{ +public: + Knight(int xCoordinate, int yCoordinate, bool isWhite) :Figure(xCoordinate, yCoordinate, isWhite) + {} + + bool Move(int xNew, int yNew); + + void Print(); +}; +bool Knight::Move(int xNew, int yNew) +{ + if (!isValidCoord(xNew, yNew)) + return false; + int possibleX[] = { 1, 1, -1, -1, 2, 2, -2, -2 }; + int possibleY[] = { 2, -2, 2, -2, 1, -1, 1, -1 }; + for (int i = 0; i < 8; ++i) + { + if (xNew == xCoord + possibleX[i] + && yNew == yCoord + possibleY[i]) + { + xCoord = xNew; + yCoord = yNew; + return true; + } + + } + return false; + +} + +void Knight::Print() +{ + cout << 'K'; +} \ No newline at end of file -- cgit v1.3.1-23-gd82d