Knight's Tour Generator
Tourneys and the Fast Generation and Obfuscation of Closed Knight's Tours
Functions | Variables
Helpers.cpp File Reference

Code for helper functions.. More...

#include "Helpers.h"
#include "Graph.h"
#include "Random.h"
#include "Defines.h"
#include "Includes.h"
#include "Board.h"
#include <sys/time.h>

Go to the source code of this file.

Functions

void fopen_s (FILE **stream, const char *name, const char *fmt)
 fopen_s for *NIX. More...
 
UINT timeGetTime ()
 Something a little bit like timeGetTime for *NIX. More...
 
std::string MakeFileNameBase (const CTourneyDesc &t, int w)
 Make file name base. More...
 
std::string NumString (float x)
 Make string from number. More...
 
void HSVtoRGB (float h, float s, float v, float rgb[3])
 HSV to RGB color. More...
 

Variables

MoveDeltas g_vecDeltas
 Move deltas for all possible knight's moves. More...
 

Function Documentation

◆ fopen_s()

void fopen_s ( FILE **  stream,
const char *  name,
const char *  fmt 
)

Implementation of fopen_s when we're not under Windows.

Parameters
streamOutput stream.
nameFile name.
fmtFile format.

Definition at line 40 of file Helpers.cpp.

◆ HSVtoRGB()

void HSVtoRGB ( float  h,
float  s,
float  v,
float  rgb[3] 
)

Convert color in HSV format to RGB format. This is a helper function for generating a pseudorandom color. All parameters are floating point values in \([0,1]\).

Parameters
h[in] Hue.
s[in] Saturation.
v[in] Value.
rgb[out] Array of three RGB values.

Definition at line 149 of file Helpers.cpp.

◆ MakeFileNameBase()

std::string MakeFileNameBase ( const CTourneyDesc t,
int  w 
)

Make the base of a file name (without the extension) based on a tourney. dwscription. If the generator type is GeneratorType::Unknown, then the file name base returned will be "Unknown". If the generator type or cycle type is not one of the ones hard-coded in the switch statements in this function, then the file name base returned will be "Error".

Parameters
tTourney descriptor.
wBoard width (defaults to -1, which means "none".
Returns
String that describes the tourney.

Definition at line 88 of file Helpers.cpp.

◆ NumString()

std::string NumString ( float  x)

Make a compact string representation of a floating point number, that is, if it's close enough to an integer, then don't use decimal point notation, otherwise use 1 digit after the decimal point. This saves space when rendering massive knight's tours to an SVG file. The floating point number is assumed to be positive, otherwise the null string is returned.

Parameters
xThe number to be processed.
Returns
Compact string form of x.

Definition at line 123 of file Helpers.cpp.

◆ timeGetTime()

UINT timeGetTime ( )

Well, it kinda-sorta is like the Windows version of timeGetTime(), but not really. Since it will be used to seed a PRNG, all that really matters is that it returns something different each time is is called. It can be slow but hopefully we'll only used it a few times during initialization.

Returns
Number of milliseconds since the start of the current epoch.

Definition at line 52 of file Helpers.cpp.

Variable Documentation

◆ g_vecDeltas

MoveDeltas g_vecDeltas
Initial value:
= {
MoveDelta( 2, -1),
MoveDelta( 1, -2),
MoveDelta(-1, -2),
MoveDelta(-2, -1),
MoveDelta(-2, 1),
MoveDelta(-1, 2),
MoveDelta( 1, 2),
MoveDelta( 2, 1)
}
std::pair< int, int > MoveDelta
Move delta for a knight's move.
Definition: Helpers.h:42

Move deltas (horizontal and vertical displacements) for the 8 knight's moves indexed counterclockwise from \((2, -1)\) (the origin is at top left of the board, positive \(x\) is rightwards, and positive \(y\) is downwards.

movedeltas.png

Definition at line 68 of file Helpers.cpp.