Knight's Tour Generator
Tourneys and the Fast Generation and Obfuscation of Closed Knight's Tours
Task.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 "Includes.h"
27 #include "Defines.h"
28 #include "Input.h"
29 #include "Generator.h"
30 
31 std::atomic_bool g_bFinished(false);
32 
37 
38 bool StartGenerateTask(const CTourneyDesc& t, int nNumThreads){
39  UINT n = 0;
40  printf("Enter board width.\n");
41  const bool bRestart = ReadBoardSize(n, t);
42 
43  if(!bRestart)
44  CGenerator(n, n).Generate(t, nNumThreads); //perform the task
45 
46  return bRestart;
47 } //StartGenerateTask
48 
54 
55 bool StartMeasureTask(const CTourneyDesc& t, int nNumThreads){
56  UINT n = 0;
57  printf("Enter board width.\n");
58  bool bRestart = ReadBoardSize(n, t);
59 
60  if(!bRestart){
61  UINT nSamples = 0;
62  printf("Enter number of samples.\n");
63  bRestart = ReadUnsigned(nSamples, Parity::DontCare, 1);
64 
65  if(!bRestart)
66  CGenerator(n, n).Measure(t, nNumThreads, nSamples); //perform the task
67  } //if
68 
69  return bRestart;
70 } //StartGenerateTask
71 
77 
78 bool StartTimeTask(const CTourneyDesc& t, int nNumThreads){
79  UINT nSamples = 0;
80  printf("Enter number of samples.\n");
81  bool bRestart = ReadUnsigned(nSamples, Parity::DontCare, 1);
82 
83  if(!bRestart){
84  UINT lo = 0; //lower end of the range of sizes to be timed
85  printf("Enter lowest board size in range.\n");
86  bRestart = ReadBoardSize(lo, t);
87 
88  if(!bRestart){
89  UINT hi = 0; //upper end of the range of sizes to be timed
90  printf("Enter highest board size in range.\n");
91  bRestart = ReadBoardSize(hi, t);
92 
93  if(hi < lo) //in case of smart-aleck
94  std::swap(lo, hi);
95 
96  if(!bRestart){ //perform task
97  printf("This may take a while");
98 
99  for(UINT n=lo; n<=hi; n+=2){
100  CGenerator(n, n).Time(t, nNumThreads, nSamples); //generate
101  putchar('.');
102  } //for
103 
104  putchar('\n');
105  } //if
106  } //if
107  } //if
108 
109  return bRestart;
110 } //StartTimeTask
111 
117 
118 bool StartTask(Task task, const CTourneyDesc& t, int nNumThreads){
119  bool bRestart = false;
120  g_bFinished = false;
121 
122  switch(task){
123  case Task::Generate:
124  bRestart = StartGenerateTask(t, nNumThreads);
125  break;
126 
127  case Task::Measure:
128  bRestart = StartMeasureTask(t, nNumThreads);
129  break;
130 
131  case Task::Time:
132  bRestart = StartTimeTask(t, nNumThreads);
133  break;
134  } //switch
135 
136  return bRestart;
137 } //StartTask
bool ReadUnsigned(UINT &n, Parity parity, UINT lo)
Read a UINT from stdin.
Definition: Input.cpp:36
Defines, enumerated types, and typedefs.
void Time(const CTourneyDesc &t, int nThreads, int n)
Time.
Definition: Generator.cpp:305
Useful includes.
Tourney descriptor.
Definition: Structs.h:40
bool StartGenerateTask(const CTourneyDesc &t, int nNumThreads)
Definition: Task.cpp:38
bool ReadBoardSize(UINT &n, const CTourneyDesc &t)
Read board size from stdin.
Definition: Input.cpp:91
void Generate(const CTourneyDesc &t, int nThreads)
Generate.
Definition: Generator.cpp:66
Header for the tourney and knight's tour generator CGenerator.
std::atomic_bool g_bFinished(false)
Search termination flag.
bool StartTimeTask(const CTourneyDesc &t, int nNumThreads)
Definition: Task.cpp:78
unsigned int UINT
Abbreviation for unsigned integer.
Definition: Defines.h:84
Task
Task type.
Definition: Defines.h:68
bool StartMeasureTask(const CTourneyDesc &t, int nNumThreads)
Definition: Task.cpp:55
Knight's tour and tourney generator.
Definition: Generator.h:47
bool StartTask(Task task, const CTourneyDesc &t, int nNumThreads)
Start task.
Definition: Task.cpp:118
void Measure(const CTourneyDesc &t, int nThreads, int n)
Measure.
Definition: Generator.cpp:165
Header for input functions.