Commit 5ef2571a authored by Jan Pokorný's avatar Jan Pokorný 💬
Browse files

generating games randomly

parent a59c1f99
......@@ -69,6 +69,18 @@ GameState ConnectFour3D::getState(const ConnectFour3D &parent) const {
}
}
bool ConnectFour3D::isValid() const{
CF3DBoard stones = (firstPlayerStones | secondPlayerStones);
if ((stones | (stones >> 16)) != stones){
return false;
}
return true;
}
int ConnectFour3D:: stonesPlayed() const{
return countOnes(firstPlayerStones | secondPlayerStones);
}
ConnectFour3D ConnectFour3D::loadGame(istream &is) {
ConnectFour3D game;
for (int i = 0; i < 64; ++i) {
......@@ -92,6 +104,9 @@ ConnectFour3D ConnectFour3D::loadGame(istream &is) {
throw ParseException("Unexpected character");
}
}
if(!game.isValid()){
throw ParseException("Invalid board");
}
return game;
}
......
......@@ -37,6 +37,8 @@ struct ConnectFour3D {
int getChildrenCount() const;
vector<ConnectFour3D> generateChildren() const;
bool firstPlayerPlays() const;
int stonesPlayed() const;
bool isValid() const;
/* board mapped to bits (index)
......
#pragma once
#include <random>
#include "GameState.hpp"
#include <cassert>
struct GameGenerator{
GameGenerator() = default;
template <typename Game>
Game generate(const Game & game, int moves){
for(int i = 0; i < 10; ++i){
Game copy = game;
int movesDone= 0;
while(movesDone < moves && game.getState() == GameState::Open){
vector<Game> children = copy.generateChildren();
std::uniform_int_distribution<> distrib(0, children.size()-1);
copy = children[distrib(gen)];
++movesDone;
}
if(movesDone == moves){
return copy;
}
}
assert(0 && "not able to generatate game");
}
private:
std::mt19937 gen;
};
\ No newline at end of file
#include "ConnectFour3D.hpp"
#include "Qubic.hpp"
#include "utils.hpp"
#include "GameGenerator.hpp"
#include <assert.h>
#include <iomanip>
int main(void) {
GameGenerator gg;
ConnectFour3D cfempty;
assert(gg.generate(cfempty, 0) == cfempty);
assert(gg.generate(cfempty, 1) != cfempty);
for(int i = 0; i < 7;++i){
assert(gg.generate(cfempty, i).stonesPlayed() == i);
}
assert(gg.generate(cfempty, 5) != gg.generate(cfempty, 5));
return 0;
}
\ No newline at end of file
#pragma once
#include <exception>
class ParseException : public std::exception {
......
Supports Markdown
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