Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Уткин О.В. - prep12 #44

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
87af4a8
Merge remote-tracking branch 'jirfag/master'
oleggator Nov 8, 2016
fe7d294
Merge remote-tracking branch 'jirfag/master'
oleggator Nov 9, 2016
1d5691c
Merge remote-tracking branch 'origin/master'
oleggator Nov 10, 2016
9c9c312
Test commit
oleggator Nov 10, 2016
1114dfc
Add .gitignore
oleggator Nov 10, 2016
f318ebc
First commit
oleggator Nov 13, 2016
05f0c35
Remove const
oleggator Nov 13, 2016
d6909dc
Include vector and algorithms
oleggator Nov 13, 2016
b698e55
Remove &
oleggator Nov 13, 2016
cef1f89
Enable debug mode
oleggator Nov 13, 2016
d47e1e9
Add new test level
oleggator Nov 14, 2016
f7f8fbc
Add maze visualizer
oleggator Nov 14, 2016
c41dca7
Merge remote-tracking branch 'origin/algorithm1' into algorithm1
oleggator Nov 14, 2016
8063926
Delete step execution for debug mode
oleggator Nov 14, 2016
0f89c27
Add 5th test level
oleggator Nov 14, 2016
289d283
Fix dead end loop
oleggator Nov 14, 2016
8761370
Update maze-visualizer
oleggator Nov 14, 2016
d6e2935
Change vector to C-array
oleggator Nov 15, 2016
3d41526
Change vector to C-array #2
oleggator Nov 15, 2016
41d280c
Change map to unordered map
oleggator Nov 15, 2016
6ff8026
Update Runner.cpp
oleggator Nov 15, 2016
9695746
Change array sorting to finding minimum element
oleggator Nov 16, 2016
39e3869
Fix function description
oleggator Nov 16, 2016
ba0b205
Change comparison condition
oleggator Nov 16, 2016
962e013
Delete console output from debug mode
oleggator Nov 16, 2016
f4bba8b
Add huge test level
oleggator Nov 16, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
.idea
CMakeCache.txt
CMakeFiles
CMakeScripts
Makefile
cmake_install.cmake
install_manifest.txt
CTestTestfile.cmake
.idea
*/bin/*
*/lib/*
labyrinth
node_modules
steps.txt
131 changes: 123 additions & 8 deletions Runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,129 @@
//

#include "Runner.hpp"
#include <vector>
#include <cstdlib>
#include "utils.hpp"
#include <algorithm>

Direction Runner::step()
{
// TODO: you need to replace the code below with your implementation of labyrinth solver.
// Now here is the stupid implementation with random choicing of direction.
const std::vector<Direction> directions = {Direction::UP, Direction::DOWN, Direction::LEFT, Direction::RIGHT};
return directions[std::rand() % directions.size()];
bool Point::operator==(const Point& point) const {
return (point.x == this->x) && (point.y == this->y);
}

size_t PointHash::operator()(const Point &point) const {
// Формула Кантора
return (point.x + point.y) * (point.x + point.y + 1) / 2 + point.y;
}

Runner::Runner(): directions {
Direction::UP,
Direction::DOWN,
Direction::LEFT,
Direction::RIGHT
} {
#if DEBUG
steps.open("steps.txt", std::ofstream::out);
#endif
}

#if DEBUG

Runner::~Runner() {
steps.close();
}
#endif

Direction Runner::step() {

incrementCurrentPositionPassagesCount();
Direction nextDirection = getNextDirection();

#if DEBUG
steps << currentPosition.x << ' ' << currentPosition.y << ' ' << labyrinthMap[Point({currentPosition.x, currentPosition.y})] << std::endl;
#endif
previousPosition = currentPosition;
currentPosition = getDirectionPoint(nextDirection);
return nextDirection;
}

// Возвращает направление следующего шага
Direction Runner::getNextDirection() {
Direction possibleDirections[4];
unsigned short size = 0;

const BlockType blocks[] = {
Runner::current_status.up,
Runner::current_status.down,
Runner::current_status.left,
Runner::current_status.right
};


for (int i = 0; i < 4; ++i) {
switch (blocks[i]) {
case BlockType::EXIT:
return directions[i];

case BlockType::ENTER:
case BlockType::FREE:
possibleDirections[size] = directions[i];
++size;
break;

case BlockType::WALL:
break;
}
}

return *std::min_element(possibleDirections, possibleDirections + size,
[this](Direction lhs, Direction rhs) -> bool {
if (getDirectionPoint(rhs) == previousPosition) {
return 1;
}

unsigned int lhsPassagesCount = 0;
if (labyrinthMap.count(getDirectionPoint(lhs)) > 0) {
lhsPassagesCount = labyrinthMap[getDirectionPoint(lhs)];
}

unsigned int rhsPassagesCount = 0;
if (labyrinthMap.count(getDirectionPoint(rhs)) > 0) {
rhsPassagesCount = labyrinthMap[getDirectionPoint(rhs)];
}

return lhsPassagesCount <= rhsPassagesCount;
}
);
}

// Вычисляет координаты точки по направлению
Point Runner::getDirectionPoint(Direction direction) {
Point point = currentPosition;

switch (direction) {
case Direction::UP:
++point.y;
break;

case Direction::DOWN:
--point.y;
break;

case Direction::LEFT:
--point.x;
break;

case Direction::RIGHT:
++point.x;
break;
}

return point;
}

// Инкрементируем количество посещений текущей ячейки
void Runner::incrementCurrentPositionPassagesCount() {
if (labyrinthMap.count(currentPosition) > 0) {
++labyrinthMap[currentPosition];
} else {
labyrinthMap.insert(std::pair<Point, unsigned short>(currentPosition, 1));
}
}
37 changes: 35 additions & 2 deletions Runner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,44 @@
#ifndef LABYRINTH_RUNNER_HPP
#define LABYRINTH_RUNNER_HPP

#define DEBUG 0

#if DEBUG
#include <fstream>
#endif

#include <unordered_map>

#include "RunnerBase.hpp"

struct Point {
int x, y;
bool operator==(const Point& point) const;
};

struct PointHash {
size_t operator()(const Point& point) const;
};

class Runner: public RunnerBase {
public:
Direction step();
public:
#if DEBUG
std::ofstream steps;
~Runner();
#endif

Runner();
Direction step();

private:
void incrementCurrentPositionPassagesCount();
Direction getNextDirection();
Point getDirectionPoint(Direction direction);

const Direction directions[4];
Point currentPosition = {0, 0};
Point previousPosition = {0, 0};
std::unordered_map<Point, unsigned short, PointHash> labyrinthMap;
};


Expand Down
106 changes: 106 additions & 0 deletions maze-visualizer/frontend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
var steps = [];
var startPoint = [];

window.onload = function() {

handleMazeFile();
handleStepsFile();
};

function handleMazeFile() {
var xhr = new XMLHttpRequest();

xhr.open('GET', 'mazeFile', true);
xhr.send();

xhr.onreadystatechange = function() {
if (xhr.readyState != 4) return;

if (xhr.status != 200) {
alert(xhr.status + ': ' + xhr.statusText);
} else {
var maze = JSON.parse(xhr.responseText);
maze.forEach(function(row, i, arr) {
var rowElement = document.createElement("TR");

row.forEach(function(item, j, arr2) {
if (item == 2) {
startPoint[1] = maze.length - 1 - i;
startPoint[0] = j;
}
var cellElement = document.createElement("TD");
cellElement.className = "i" + item;
cellElement.id = j + ":" + (maze.length - 1 - i);

rowElement.appendChild(cellElement);
});

document.getElementsByTagName("table")[0].appendChild(rowElement);
});
}
}

};

function handleStepsFile() {
var xhr = new XMLHttpRequest();

xhr.open('GET', 'steps', true);
xhr.send();

xhr.onreadystatechange = function() {
if (xhr.readyState != 4) return;

if (xhr.status != 200) {
alert(xhr.status + ': ' + xhr.statusText);
} else {
steps = JSON.parse(xhr.responseText);
}

}
}

var j = 0;

function solve() {
var i = j;
var timeId = setInterval(function() {
var id = (parseInt(startPoint[0]) + parseInt(steps[i][0])) + ":" + (parseInt(startPoint[1]) + parseInt(steps[i][1]));

document.getElementById(id).innerHTML = steps[i][2];

if (steps[i][2] == 1) {
document.getElementById(id).style.backgroundColor = "green";
document.getElementById(id).style.color = "white";
} else if (steps[i][2] == 2) {
document.getElementById(id).style.backgroundColor = "yellow";
document.getElementById(id).style.color = "black";
} else {
document.getElementById(id).style.backgroundColor = "red";
document.getElementById(id).style.color = "white";
}

++i;
if (i == steps.length)
clearInterval(timeId);

}, 100);
}

function next() {
var id = (parseInt(startPoint[0]) + parseInt(steps[j][0])) + ":" + (parseInt(startPoint[1]) + parseInt(steps[j][1]));
document.getElementById(id).innerHTML = steps[j][2];
if (steps[j][2] == 1) {
document.getElementById(id).style.backgroundColor = "green";
document.getElementById(id).style.color = "white";
} else if (steps[j][2] == 2) {
document.getElementById(id).style.backgroundColor = "yellow";
document.getElementById(id).style.color = "black";
} else {
document.getElementById(id).style.backgroundColor = "red";
document.getElementById(id).style.color = "white";
}

++j;
}

55 changes: 55 additions & 0 deletions maze-visualizer/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="frontend.js"></script>
<style type="text/css">
* {
font-family: Menlo, Monaco, monospace;
margin: 0;
}

table {
margin: auto;
text-align: center;
border-collapse: collapse;
}

td {
height: 20px;
width: 20px;
}

#buttons {
width: 100%;
font-size: xx-large;
text-align: center;
}

.i0, .i2 {
background-color: white;
}
.i2 {
background-color: blue !important;
color: white !important;
}
.i1 {
background-color: black;
border: 1px solid black;
}
.i3 {
background-color: purple !important;
}
.i4 {
background-color: grey;
}
</style>
</head>
<body>
<div id="buttons">
<button id="solve" onclick="solve()">Solve</button>
<button id="next" onclick="next();">Next</button>
</div>
<table></table>
</body>
</html>
Loading