from_to_table.cc
Go to the documentation of this file.
1 // cwchessboard -- A C++ chessboard tool set for gtkmm
2 //
3 //! @file from_to_table.cc Program used to generate cwchess::from_to_table[].
4 //
5 // Copyright (C) 2008, by
6 //
7 // Carlo Wood, Run on IRC <carlo@alinoe.com>
8 // RSA-1024 0x624ACAD5 1997-01-26 Sign & Encrypt
9 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6 F6 F6 55 DD 1C DC FF 61
10 //
11 // This program is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 2 of the License, or
14 // (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 
24 #include "sys.h"
25 #include <iostream>
26 #include <iomanip>
27 #include <cstdlib>
28 #include <stdint.h>
29 
30 typedef uint64_t mask_t;
31 
32 void output(int from, int to, mask_t mask)
33 {
34  static int count = 0;
35  if (count == 0)
36  std::cout << " ";
37  std::cout << " {CW_MASK_T_CONST(0x" << std::hex << std::setfill('0') << std::setw(16) << mask << std::dec << ")},";
38  if (++count == 4)
39  {
40  std::cout << '\n';
41  count = 0;
42  }
43 }
44 
45 int main()
46 {
47  for (int from = 0; from < 64; ++from)
48  {
49  int from_col = from & 7;
50  int from_row = from >> 3;
51  for (int to = 0; to < 64; ++to)
52  {
53  if (from == to)
54  {
55  output(from, to, CW_MASK_T_CONST(0));
56  continue;
57  }
58  int to_col = to & 7;
59  int to_row = to >> 3;
60  int col_diff = abs(from_col - to_col);
61  int row_diff = abs(from_row - to_row);
62  if (col_diff != 0 && row_diff != 0 && col_diff != row_diff)
63  {
64  output(from, to, CW_MASK_T_CONST(0));
65  continue;
66  }
67  int dcol = (to_col - from_col);
68  if (dcol)
69  dcol /= col_diff;
70  int drow = (to_row - from_row);
71  if (drow)
72  drow /= row_diff;
73  mask_t mask = 0;
74  int c = from_col;
75  int r = from_row;
76  do
77  {
78  mask |= CW_MASK_T_CONST(1) << ((r << 3) | c);
79  c += dcol;
80  r += drow;
81  }
82  while (c != to_col || r != to_row);
83  output(from, to, mask);
84  }
85  }
86 }

Copyright © 2006 - 2010 Carlo Wood.  All rights reserved.