Commit a57f6120 authored by Emil's avatar Emil

Added unit tests for nextmove

parent 2924b300
......@@ -98,7 +98,7 @@ namespace std
namespace gameengine // resume namespace gameengine
{
using BitPieces = std::bitset<detail::computeBoardSize()>;
using BitBoard = std::array<BitPieces, 4>;
using BitBoard = std::array<BitPieces, 2>;
using BitPosSet = std::unordered_set<BitPos>;
......
......@@ -45,7 +45,7 @@ namespace gameengine::utility
* placed
* \return Returns true if the move is legal */
bool isLegalMove(const BitBoard& board, const PlayerId& player_id,
const BitPos& board_pos);
const BitPos& board_pos, const BitPos& board_new_pos);
/*! Perform a move on a given board for current player id and valid board
* position
......@@ -53,7 +53,7 @@ namespace gameengine::utility
* \param[in] player_id Current player
* \param[in board_pos Valid piece placement */
void move(BitBoard& board, const PlayerId& player_id,
const BitPos& board_pos);
const BitPos& board_pos, const BitPos& board_new_pos);
} // namespace gameengine::utility
......
......@@ -9,6 +9,11 @@
namespace gameengine::utility
{
BitPieces occupiedPosition(const BitBoard& board, const PlayerId& player_id)
{
return board[size_t(player_id)];
}
BitPieces occupiedPositions(const BitBoard& board)
{
......@@ -40,7 +45,7 @@ namespace gameengine::utility
throw eagde_case::MyException();
}
else {
switch (dir) {
case MoveDirection::N: //new position when moving North
return gameengine::BitPos(board_pos.value()+8);
......@@ -59,32 +64,48 @@ namespace gameengine::utility
case MoveDirection::NW: //new position when moving North-West
return gameengine::BitPos(board_pos.value()+7);
}
}
//Should not get here
throw eagde_case::MyException();
}
BitPosSet legalMoves(const BitBoard& board, const PlayerId& player_id)
{
return {};
//BitPieces board_player = occupiedPosition(board,player_id);
BitPosSet Legal_pos;
for (int Nstep = 0; Nstep < 63; Nstep++)
{
if (occupied(occupiedPosition(board,player_id),BitPos(Nstep)) == true){
if (isLegalMove(board,player_id,BitPos(Nstep),nextPosition(BitPos(Nstep),MoveDirection::NE)) == true)
{
Legal_pos.insert(nextPosition(BitPos(Nstep),MoveDirection::NE));
}
if (isLegalMove(board,player_id,BitPos(Nstep),nextPosition(BitPos(Nstep),MoveDirection::NW)) == true)
{
Legal_pos.insert(nextPosition(BitPos(Nstep),MoveDirection::NW));
}
}
}
return {Legal_pos};
}
bool isLegalMove(const BitBoard& board, const PlayerId& player_id,
const BitPos& board_pos)
const BitPos& /*board_pos*/, const BitPos& board_new_pos)
{
if (){
if (occupied(occupiedPosition(board,player_id),board_new_pos)==1){
return false;
}
else {
return true;
}
else{
return false;
}
}
void move(BitBoard& board, const PlayerId& player_id,
const BitPos& board_pos)
void move(BitBoard& /*board*/, const PlayerId& /*player_id*/,
const BitPos& /*board_pos*/, const BitPos& /*board_new_pos*/)
{
}
} // namespace gameengine::utility
......@@ -28,7 +28,6 @@ int main(int argc, char** argv) try {
return app.exec();
}
catch ()
catch (std::string exception) {
std::cout << "Runtime exception: " << exception << std::endl;
}
......
......@@ -9,8 +9,7 @@ find_package(Threads REQUIRED)
# Test sources
set(TESTS
predefined_game_engine_unittests
predefined_utility_unittests
utility_unittests
)
......
// gtest
#include <gtest/gtest.h> // googletest header file
// engine
#include <engine/gameengine.h>
#include <engine/monkeyai.h>
// stl
#include <variant>
using namespace gameengine;
/////////////////
/// Next Position
// Testing fixture
struct NextPositionParameters {
BitPos pos_in;
MoveDirection dir_in;
BitPos next_pos_expected;
};
struct NextPositionFixture : testing::Test,
testing::WithParamInterface<NextPositionParameters> {
};
// Tests
TEST_P(NextPositionFixture, EQ_Test)
{
auto params = GetParam();
auto next_pos
= gameengine::utility::nextPosition(params.pos_in, params.dir_in);
EXPECT_EQ(next_pos, params.next_pos_expected);
}
// Test set instantiations
INSTANTIATE_TEST_CASE_P(
NextPosition,
NextPositionFixture,
testing::Values(NextPositionParameters{
BitPos(0), MoveDirection::N, BitPos(8)},
NextPositionParameters{
BitPos(9), MoveDirection::S, BitPos(1)}
) );
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment