Knight's Tour Generator
Tourneys and the Fast Generation and Obfuscation of Closed Knight's Tours
Helpers.cpp
Go to the documentation of this file.
1 
4 // MIT License
5 //
6 // Copyright (c) 2019 Ian Parberry
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 
26 #include "Helpers.h"
27 
28 #include "Graph.h"
29 #include "Random.h"
30 #include "Defines.h"
31 #include "Includes.h"
32 #include "Board.h"
33 
34 #if !defined(_MSC_VER)
35 
40  void fopen_s(FILE** stream, const char* name, const char* fmt){
41  *stream = fopen(name, fmt);
42  } //fopen_s
43 
44  #include <sys/time.h>
45 
51 
53  struct timeval now;
54  gettimeofday(&now, NULL);
55  return now.tv_usec/1000;
56  } //timeGetTime
57 #endif
58 
67 
69  MoveDelta( 2, -1), //move 0
70  MoveDelta( 1, -2), //move 1
71  MoveDelta(-1, -2), //move 2
72  MoveDelta(-2, -1), //move 3
73  MoveDelta(-2, 1), //move 4
74  MoveDelta(-1, 2), //move 5
75  MoveDelta( 1, 2), //move 6
76  MoveDelta( 2, 1) //move 7
77 }; //g_vecDeltas
78 
87 
88 std::string MakeFileNameBase(const CTourneyDesc& t, int w){
89  const GeneratorType gentype = t.m_eGenerator;
90  const CycleType cycletype = t.m_eCycle;
91 
92  std::string s = "Error"; //file name string, should never see this default
93 
94  switch(gentype){ //prefix represents the generator type
95  case GeneratorType::Warnsdorff: s = "Warnsd"; break;
96  case GeneratorType::TakefujiLee: s = "Neural"; break;
97  case GeneratorType::DivideAndConquer: s = "Divide"; break;
98  case GeneratorType::ConcentricBraid: s = "Braid"; break;
99  case GeneratorType::FourCover: s = "Cover4"; break;
100  case GeneratorType::Unknown: s = "Unknown"; break;
101  } //switch
102 
103  switch(cycletype){ //suffix represents the cycle type
104  case CycleType::Tour: s += "Tour"; break;
105  case CycleType::TourFromTourney: s += "Join"; break;
106  case CycleType::Tourney: s += "Trny"; break;
107  } //switch
108 
109  if(t.m_bObfuscate)s += "Obf"; //obfuscated
110  if(w > 0)s += std::to_string(w); //board width and height
111 
112  return s;
113 } //MakeFileNameBase
114 
122 
123 std::string NumString(float x){
124  std::string s; //return string
125  if(x < 0)return s;
126 
127  const UINT intpart = (UINT)std::floor(x);
128  const float fracpart = x - intpart;
129 
130  if(fracpart < 0.1f)
131  s = std::to_string(intpart);
132 
133  else{
134  const UINT intfracpart = (UINT)std::round(fracpart*10.0f);
135  s = std::to_string(intpart) + "." + std::to_string(intfracpart);
136  } //else
137 
138  return s;
139 } //NumString
140 
148 
149 void HSVtoRGB(float h, float s, float v, float rgb[3]){
150  const UINT i = UINT(6.0f*h);
151 
152  const float f = 6.0f*h - i;
153  const float p = v*(1.0f - s);
154  const float q = v*(1.0f - s*f);
155  const float t = v*(1.0f - s*(1.0f - f));
156 
157  //set red, green, and blue channels in different ways depending on h
158 
159  switch(i%6){
160  case 0: rgb[0] = v; rgb[1] = t; rgb[2] = p; break;
161  case 1: rgb[0] = q; rgb[1] = v; rgb[2] = p; break;
162  case 2: rgb[0] = p; rgb[1] = v; rgb[2] = t; break;
163  case 3: rgb[0] = p; rgb[1] = q; rgb[2] = v; break;
164  case 4: rgb[0] = t; rgb[1] = p; rgb[2] = v; break;
165  case 5: rgb[0] = v; rgb[1] = p; rgb[2] = q; break;
166  } //switch
167 } //HSVtoRGB
Defines, enumerated types, and typedefs.
Useful includes.
Tourney descriptor.
Definition: Structs.h:40
std::pair< int, int > MoveDelta
Move delta for a knight's move.
Definition: Helpers.h:42
Header for helper functions.
std::string NumString(float x)
Make string from number.
Definition: Helpers.cpp:123
unsigned int UINT
Abbreviation for unsigned integer.
Definition: Defines.h:84
GeneratorType
Generator type.
Definition: Defines.h:47
GeneratorType m_eGenerator
Generator type.
Definition: Structs.h:41
Header for the graph CGraph and its vertices CVertex and edges CEdge.
MoveDeltas g_vecDeltas
Move deltas for all possible knight's moves.
Definition: Helpers.cpp:68
void fopen_s(FILE **stream, const char *name, const char *fmt)
fopen_s for *NIX.
Definition: Helpers.cpp:40
Header for the pseudo-random number generator CRandom.
Header for the chessboard CBoard.
CycleType m_eCycle
Cycle type.
Definition: Structs.h:42
std::vector< MoveDelta > MoveDeltas
Move deltas for knight's moves.
Definition: Helpers.h:43
void HSVtoRGB(float h, float s, float v, float rgb[3])
HSV to RGB color.
Definition: Helpers.cpp:149
std::string MakeFileNameBase(const CTourneyDesc &t, int w)
Make file name base.
Definition: Helpers.cpp:88
CycleType
Cycle type.
Definition: Defines.h:58
UINT timeGetTime()
Something a little bit like timeGetTime for *NIX.
Definition: Helpers.cpp:52
bool m_bObfuscate
Whether to obfuscate.
Definition: Structs.h:43