From 8cd46542efbe1e66ec5e85ab5eaad5001923e101 Mon Sep 17 00:00:00 2001 From: Tait Hoyem <44244401+TTWNO@users.noreply.github.com> Date: Fri, 5 Apr 2019 22:16:23 +0000 Subject: [PATCH] Fix board type. Add pos_to_pair, pair_to_pos functions --- src/functions.cpp | 35 +++++++++++++++++++++++++++++------ src/functions.h | 8 ++++++-- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/functions.cpp b/src/functions.cpp index 8c1e641..154efbb 100644 --- a/src/functions.cpp +++ b/src/functions.cpp @@ -1,23 +1,46 @@ #include "functions.h" +#include +#include +#include +#include // TODO implement functions.h functions. // NOTE tests will NOT run unless you implement these functions. std::pair pos_to_pair(Position pn){ - // TODO replace with real code. Just implementing for running the tests. - return std::make_pair(-1, -1); + int x,y = 0; + for (x = pn; x >= 8; x = x-8){ + ++y; + } + return std::make_pair(x, std::abs(y-7)); } +// TODO find way to make function arbitary to board size as to allow wide game chess variants. Do much later. Not important now. Position pair_to_pos(std::pair pr){ - return Position::A1; + if (pr.first > 7 || pr.first < 0 + || pr.second > 7 || pr.second < 0) { + throw std::invalid_argument("Cannot use any pairs with values > 7 or < 0."); + } + int int_val = std::abs(pr.second - 7)*8 + pr.first; + if (int_val > 0 && int_val < 64) { + return static_cast(int_val); + } else { + throw std::invalid_argument("Something went terribly wrong. x and y < 8 && x and y > 0 but abs(y-7)*8 + x < 0 or >= 64"); + } } - -std::vector get_possible_movers(Position pn, std::vector board){ +std::vector get_possible_movers(Position pn, std::array board){ std::vector pns = {Position::A1}; return pns; } -std::vector get_possible_moves(Position pn, std::vector board){ +std::vector get_possible_moves(Position pn, std::array board){ std::vector pns = {Position::A1}; return pns; } + +std::array dumb_move(Position from, Position to, std::array board){ + PieceType piece = board[from]; + board[to] = piece; + board[from] = PieceType::NONE; + return board; +} diff --git a/src/functions.h b/src/functions.h index 337fe18..16b4744 100644 --- a/src/functions.h +++ b/src/functions.h @@ -12,8 +12,12 @@ std::pair pos_to_pair(Position pn); // Get all positions of pieces which can move to this square // This may require helper functions for each individual peice. // TODO rename to something less stupid. -std::vector get_possible_movers(Position pn, std::vector pt); +std::vector get_possible_movers(Position pn, std::array pt); // Get all possible moved for piece in Position pn. // This may require helper functions for each individual piece. -std::vector get_possible_moves(Position pn, std::vector pt); +std::vector get_possible_moves(Position pn, std::array pt); + +// Dumb function to do board moves. +// Does NOT check for valid moves. Only moves PieceType of Pos1 to Pos2, then replaces Pos1 with Piecetype::NONE +std::array dumb_move(Position from, Position to, std::array board);