Knight's Tour Generator
Tourneys and the Fast Generation and Obfuscation of Closed Knight's Tours
ConcentricBraid.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 "ConcentricBraid.h"
27 
30 
32 
34  m_pBoard4x4 = new CBoard(m_nMove4x4, 4, 4);
35  m_pBoard6x6 = new CBoard(m_nMove6x6, 6, 6);
36 } //constructor
37 
40 
42  delete m_pBoard4x4;
43  delete m_pBoard6x6;
44 } //destructor
45 
48 
50  const int w = b.GetWidth(); //board width
51  const int h = b.GetHeight(); //board height
52  if((w & 1) || w != h)return; //board must be square with even width
53 
54  const UINT limit = (w%4 == 2)? w/2 - 3: w/2 - 2;
55 
56  for(UINT offset=0; offset<limit; offset+=2)
57  for(UINT k=0; k<4; k++){
58  UINT i = offset;
59  UINT j = offset + k;
60 
61  while(j < w - offset - 2){
62  UINT cur = i*w + j;
63  i = (i == offset)? offset + 1: offset;
64  j += 2;
65  b.InsertUndirectedMove(cur, i*w + j);
66  } //while
67 
68  while(i < w - offset - 2){
69  UINT cur = i*w + j;
70  i += 2;
71  j = (j == w - offset - 1)? w - offset - 2: w - offset - 1;
72  b.InsertUndirectedMove(cur, i*w + j);
73  } //while
74 
75  while(j >= offset + 2){
76  UINT cur = i*w + j;
77  i = (i == w - offset - 1)? w - offset - 2: w - offset - 1;
78  j -= 2;
79  b.InsertUndirectedMove(cur, i*w + j);
80  } //while
81 
82  while(i >= offset + 2){
83  UINT cur = i*w + j;
84  i -= 2;
85  j = (j == offset)? offset + 1: offset;
86  b.InsertUndirectedMove(cur, i*w + j);
87  } //while
88 
89  if(i != offset)
90  b.InsertUndirectedMove(i*w + j, (i - 1)*w + j + 2);
91  } //for
92 
93  //generate center, either 4x4 or 6x6
94 
95  const int m = 4 + w%4; //width of center, either 4 or 6
96  const int offset = (w - m)/2; //offset from top and left
97 
98  if(m == 4)
99  b.CopyToSubBoard(*m_pBoard4x4, offset, offset);
100 
101  else if(m == 6)
102  b.CopyToSubBoard(*m_pBoard6x6, offset, offset);
103 } //GenerateConcentric
Header for the concentric braided tourney generator CConcentricBraid.
int GetHeight()
Get height.
Definition: BaseBoard.cpp:801
void Generate(CBoard &b)
Generate a concentric braided tourney.
CBoard * m_pBoard4x4
Pointer to 4x4 center.
int m_nMove6x6[36]
Undirected move table for 6x6 center.
CConcentricBraid()
Constructor.
int m_nMove4x4[16]
Undirected move table for 4x4 center.
unsigned int UINT
Abbreviation for unsigned integer.
Definition: Defines.h:84
Chessboard.
Definition: Board.h:42
void CopyToSubBoard(CBaseBoard &b, int x, int y)
Copy to sub-board.
Definition: BaseBoard.cpp:443
~CConcentricBraid()
Destructor.
CBoard * m_pBoard6x6
Pointer to 6x6 center.
int GetWidth()
Get width.
Definition: BaseBoard.cpp:794
bool InsertUndirectedMove(int src, int dest)
Insert an undirected move.
Definition: BaseBoard.cpp:482