Skip to content

Instantly share code, notes, and snippets.

@Wunkolo
Created September 14, 2015 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Wunkolo/9d1a2650b0de9f3297cc to your computer and use it in GitHub Desktop.
Save Wunkolo/9d1a2650b0de9f3297cc to your computer and use it in GitHub Desktop.
Implementation of the Drunken Biship
#pragma once
#include <stdint.h>
#include <algorithm>
template<size_t Width, size_t Height, typename T>
class Field
{
public:
Field(size_t StartX = Width / 2, size_t StartY = Height / 2)
{
StartPos.X = StartX;
StartPos.Y = StartY;
CurPos = StartPos;
try
{
Data = new T[Width * Height]{};
}
catch( std::bad_alloc& e )
{
Data = nullptr;
e;
return;
}
}
~Field()
{
if( Data )
{
delete[] Data;
}
}
inline size_t GetWidth() const
{
return Width;
}
inline size_t GetHeight() const
{
return Height;
}
// Reset Field into its default state
void Reset(size_t StartX = Width >> 1, size_t StartY = Height >> 1)
{
StartPos.X = StartX;
StartPos.Y = StartY;
CurPos = StartPos;
Set()
std::copy_n(
std::begin(StartPoint),
2,
std::begin(Position)
);
std::fill_n(
std::begin(Data),
Width * Height,
T(0))
}
inline T& GetCell(size_t X, size_t Y) const
{
X = X > Width ? Width : X;
Y = Y > Height ? Height : Y;
return Data[Width * Y + X];
}
inline T& GetCell(size_t Index) const
{
return Data[Index];
}
inline T& Visit(size_t X, size_t Y) const
{
X = X > Width ? Width : X;
Y = Y > Height ? Height : Y;
Data[Width * Y + X]++;
return Data[Width * Y + X];
}
inline T& Visit(size_t X, size_t Y, T Influence) const
{
X = X > Width ? Width : X;
Y = Y > Height ? Height : Y;
return Data[Width * Y + X] += value;
}
inline T& Set(size_t Index, T Value)
{
Index = Index > Width*Height ? Width*Height : Index;
return Data[Index] = Value;
}
inline T& Set(size_t X, size_t Y, T value) const
{
X = X > Width ? Width : X;
Y = Y > Height ? Height : Y;
return Data[Width * Y + X] = value;
}
inline T Get(size_t X, size_t Y) const
{
X = X > Width ? Width : X;
Y = Y > Height ? Height : Y;
return Data[Width * Y + X];
}
inline T Get(size_t Index) const
{
return Data[Index];
}
inline const T* GetData() const
{
return Data;
}
inline void Move(bool Right, bool Down)
{
CurPos.X += Right ? 1 : (CurPos.X == 0 ? 0 : -1);
CurPos.X = CurPos.X > Width - 1 ? Width - 1 : CurPos.X;
CurPos.Y += Down ? 1 : (CurPos.Y == 0 ? 0 : -1);
CurPos.Y = CurPos.Y > Height - 1 ? Height - 1 : CurPos.Y;
Visit(CurPos.X, CurPos.Y);
}
inline void Move(bool Right, bool Down, T Influence)
{
CurPos.X += Right ? 1 : (CurPos.X == 0 ? 0 : -1);
CurPos.X = CurPos.X > Width - 1 ? Width - 1 : CurPos.X;
CurPos.Y += Down ? 1 : (CurPos.Y == 0 ? 0 : -1);
CurPos.Y = CurPos.Y > Height - 1 ? Height - 1 : CurPos.Y;
Visit(CurPos.X, CurPos.Y, Influence);
}
inline size_t GetStartX() const
{
return StartPos.X;
}
inline size_t GetStartY() const
{
return StartPos.Y;
}
inline size_t GetCurX() const
{
return CurPos.X;
}
inline size_t GetCurY() const
{
return CurPos.Y;
}
private:
struct
{
size_t X, Y;
} StartPos, CurPos;
T* Data;
};
#include <iostream>
#include <iomanip>
#include <stdint.h>
#include <string>
#include "Field.hpp"
#define Width 31
#define Height 23
int main()
{
const uint8_t Symbols[] = " .o+=*B0X@%&#/^SE";
Field<Width, Height, uint8_t> Heatmap;
const uint8_t Data[] =
//"abcdefghijklmnop";
//{ 0x69, 0xfc, 0x4c, 0xdc, 0xba, 0xf0, 0x35, 0x70, 0x99, 0xb8, 0xbb, 0x40, 0x41, 0x6b, 0xfe, 0xf0 };
{ 0x16, 0x27, 0xac, 0xa5, 0x76, 0x28, 0x2d, 0x36, 0x63, 0x1b, 0x56, 0x4d, 0xeb, 0xdf, 0xa6, 0x48 };
for( size_t i = 0; i < sizeof(Data); i++ )
{
uint8_t CurByte = Data[i];
for( size_t j = 0; j < 4; j++ )
{
Heatmap.Move(
(CurByte & 1) ? true : false,
(CurByte & 2) ? true : false
);
CurByte >>= 2;
}
}
Heatmap.Set(Heatmap.GetStartX(), Heatmap.GetStartY(), 15);
Heatmap.Set(Heatmap.GetCurX(), Heatmap.GetCurY(), 16);
std::cout << '\xDA' << std::string(Width, '\xC4') << '\xBF' << std::endl;
for( size_t Y = 0; Y < Heatmap.GetHeight(); Y++ )
{
std::cout << '\xB3';
for( size_t X = 0; X < Heatmap.GetWidth(); X++ )
{
std::cout << Symbols[Heatmap.Get(X, Y) % sizeof(Symbols)];
}
std::cout << '\xB3' << std::endl;
}
std::cout << '\xC0' << std::string(Width, '\xC4') << '\xD9' << std::endl;
std::cout.flush();
std::cin.get();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment