[Square] Initial implementation
This commit is contained in:
85
Square.cpp
85
Square.cpp
@@ -1,58 +1,65 @@
|
||||
#include "Square.hpp"
|
||||
|
||||
#include <ostream>
|
||||
#include <iostream>
|
||||
|
||||
Square::Square(Index index)
|
||||
{
|
||||
(void)index;
|
||||
Square::Square(Index index) : mIndex(index) {
|
||||
}
|
||||
|
||||
Square::Optional Square::fromCoordinates(Coordinate file, Coordinate rank) {
|
||||
(void)file;
|
||||
(void)rank;
|
||||
return std::nullopt;
|
||||
if (file > 7 || rank > 7) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return Square(rank * 8 + file);
|
||||
}
|
||||
|
||||
Square::Optional Square::fromIndex(Index index) {
|
||||
(void)index;
|
||||
return std::nullopt;
|
||||
if (index >= 64)
|
||||
return std::nullopt;
|
||||
|
||||
return Square(index);
|
||||
}
|
||||
|
||||
Square::Optional Square::fromName(const std::string& name) {
|
||||
(void)name;
|
||||
return std::nullopt;
|
||||
Square::Optional Square::fromName(const std::string &name) {
|
||||
if (name.length() != 2)
|
||||
return std::nullopt;
|
||||
|
||||
auto file = static_cast<int>(name[0]) - 'a';
|
||||
auto rank = static_cast<int>(name[1]) - '1';
|
||||
|
||||
return fromCoordinates(file, rank);
|
||||
}
|
||||
|
||||
Square::Coordinate Square::file() const {
|
||||
return 0;
|
||||
return mIndex % 8;
|
||||
}
|
||||
|
||||
Square::Coordinate Square::rank() const {
|
||||
return 0;
|
||||
return mIndex / 8;
|
||||
}
|
||||
|
||||
Square::Index Square::index() const {
|
||||
return 0;
|
||||
return mIndex;
|
||||
}
|
||||
|
||||
const Square Square::A1 = Square(0 + 0);
|
||||
const Square Square::B1 = Square(0 + 1);
|
||||
const Square Square::C1 = Square(0 + 2);
|
||||
const Square Square::D1 = Square(0 + 3);
|
||||
const Square Square::E1 = Square(0 + 4);
|
||||
const Square Square::F1 = Square(0 + 5);
|
||||
const Square Square::G1 = Square(0 + 6);
|
||||
const Square Square::H1 = Square(0 + 7);
|
||||
|
||||
const Square Square::A1 = Square( 0 + 0);
|
||||
const Square Square::B1 = Square( 0 + 1);
|
||||
const Square Square::C1 = Square( 0 + 2);
|
||||
const Square Square::D1 = Square( 0 + 3);
|
||||
const Square Square::E1 = Square( 0 + 4);
|
||||
const Square Square::F1 = Square( 0 + 5);
|
||||
const Square Square::G1 = Square( 0 + 6);
|
||||
const Square Square::H1 = Square( 0 + 7);
|
||||
|
||||
const Square Square::A2 = Square( 8 + 0);
|
||||
const Square Square::B2 = Square( 8 + 1);
|
||||
const Square Square::C2 = Square( 8 + 2);
|
||||
const Square Square::D2 = Square( 8 + 3);
|
||||
const Square Square::E2 = Square( 8 + 4);
|
||||
const Square Square::F2 = Square( 8 + 5);
|
||||
const Square Square::G2 = Square( 8 + 6);
|
||||
const Square Square::H2 = Square( 8 + 7);
|
||||
const Square Square::A2 = Square(8 + 0);
|
||||
const Square Square::B2 = Square(8 + 1);
|
||||
const Square Square::C2 = Square(8 + 2);
|
||||
const Square Square::D2 = Square(8 + 3);
|
||||
const Square Square::E2 = Square(8 + 4);
|
||||
const Square Square::F2 = Square(8 + 5);
|
||||
const Square Square::G2 = Square(8 + 6);
|
||||
const Square Square::H2 = Square(8 + 7);
|
||||
|
||||
const Square Square::A3 = Square(16 + 0);
|
||||
const Square Square::B3 = Square(16 + 1);
|
||||
@@ -108,19 +115,15 @@ const Square Square::F8 = Square(56 + 5);
|
||||
const Square Square::G8 = Square(56 + 6);
|
||||
const Square Square::H8 = Square(56 + 7);
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const Square& square) {
|
||||
(void)square;
|
||||
std::ostream &operator<<(std::ostream &os, const Square &square) {
|
||||
os << static_cast<char>(square.file() + 'a') << static_cast<char>(square.rank() + '1');
|
||||
return os;
|
||||
}
|
||||
|
||||
bool operator<(const Square& lhs, const Square& rhs) {
|
||||
(void)lhs;
|
||||
(void)rhs;
|
||||
return false;
|
||||
bool operator<(const Square &lhs, const Square &rhs) {
|
||||
return lhs.index() < rhs.index();
|
||||
}
|
||||
|
||||
bool operator==(const Square& lhs, const Square& rhs) {
|
||||
(void)lhs;
|
||||
(void)rhs;
|
||||
return false;
|
||||
bool operator==(const Square &lhs, const Square &rhs) {
|
||||
return lhs.index() == rhs.index();
|
||||
}
|
||||
|
11
Square.hpp
11
Square.hpp
@@ -11,10 +11,9 @@ public:
|
||||
using Coordinate = unsigned;
|
||||
using Index = unsigned;
|
||||
using Optional = std::optional<Square>;
|
||||
|
||||
static Optional fromCoordinates(Coordinate file, Coordinate rank);
|
||||
static Optional fromIndex(Index index);
|
||||
static Optional fromName(const std::string& name);
|
||||
static Optional fromName(const std::string &name);
|
||||
|
||||
Coordinate file() const;
|
||||
Coordinate rank() const;
|
||||
@@ -32,12 +31,14 @@ public:
|
||||
private:
|
||||
|
||||
Square(Index index);
|
||||
|
||||
Index mIndex;
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const Square& square);
|
||||
std::ostream &operator<<(std::ostream &os, const Square &square);
|
||||
|
||||
// Necessary to support Square as the key in std::map.
|
||||
bool operator<(const Square& lhs, const Square& rhs);
|
||||
bool operator==(const Square& lhs, const Square& rhs);
|
||||
bool operator<(const Square &lhs, const Square &rhs);
|
||||
bool operator==(const Square &lhs, const Square &rhs);
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user