first thingy nya
commit
19adee5552
|
@ -0,0 +1,19 @@
|
||||||
|
# This is a good default: files that are auto-detected by git to be text are
|
||||||
|
# converted to the platform-native line ending (LF on Unix, CRLF on Windows)
|
||||||
|
# in the working tree and to LF in the repository.
|
||||||
|
#
|
||||||
|
* text=auto
|
||||||
|
|
||||||
|
# Use `eol=crlf` for files that should have the CRLF line ending both in the
|
||||||
|
# working tree (even on Unix) and in the repository.
|
||||||
|
#
|
||||||
|
#*.bat text eol=crlf
|
||||||
|
|
||||||
|
# Use `eol=lf` for files that should have the LF line ending both in the
|
||||||
|
# working tree (even on Windows) and in the repository.
|
||||||
|
#
|
||||||
|
#*.sh text eol=lf
|
||||||
|
|
||||||
|
# Use `binary` to make sure certain files are never auto-detected as text.
|
||||||
|
#
|
||||||
|
#*.png binary
|
|
@ -0,0 +1,30 @@
|
||||||
|
.bdep/
|
||||||
|
|
||||||
|
# Local default options files.
|
||||||
|
#
|
||||||
|
.build2/local/
|
||||||
|
|
||||||
|
# Compiler/linker output.
|
||||||
|
#
|
||||||
|
*.d
|
||||||
|
*.t
|
||||||
|
*.i
|
||||||
|
*.i.*
|
||||||
|
*.ii
|
||||||
|
*.ii.*
|
||||||
|
*.o
|
||||||
|
*.obj
|
||||||
|
*.gcm
|
||||||
|
*.pcm
|
||||||
|
*.ifc
|
||||||
|
*.so
|
||||||
|
*.dll
|
||||||
|
*.a
|
||||||
|
*.lib
|
||||||
|
*.exp
|
||||||
|
*.pdb
|
||||||
|
*.ilk
|
||||||
|
*.exe
|
||||||
|
*.exe.dlls/
|
||||||
|
*.exe.manifest
|
||||||
|
*.pc
|
|
@ -0,0 +1,4 @@
|
||||||
|
/config.build
|
||||||
|
/root/
|
||||||
|
/bootstrap/
|
||||||
|
build/
|
|
@ -0,0 +1,7 @@
|
||||||
|
project = knights-tour
|
||||||
|
|
||||||
|
using version
|
||||||
|
using config
|
||||||
|
using test
|
||||||
|
using install
|
||||||
|
using dist
|
|
@ -0,0 +1,22 @@
|
||||||
|
# Uncomment to suppress warnings coming from external libraries.
|
||||||
|
#
|
||||||
|
#cxx.internal.scope = current
|
||||||
|
|
||||||
|
cxx.std = experimental
|
||||||
|
cxx.features.modules = true
|
||||||
|
|
||||||
|
using cxx
|
||||||
|
|
||||||
|
hxx{*}: extension = hxx
|
||||||
|
ixx{*}: extension = ixx
|
||||||
|
txx{*}: extension = txx
|
||||||
|
cxx{*}: extension = cxx
|
||||||
|
mxx{*}: extension = mxx
|
||||||
|
|
||||||
|
# Assume headers are importable unless stated otherwise.
|
||||||
|
#
|
||||||
|
hxx{*}: cxx.importable = true
|
||||||
|
|
||||||
|
# The test target for cross-testing (running tests under Wine, etc).
|
||||||
|
#
|
||||||
|
test.target = $cxx.target
|
|
@ -0,0 +1,5 @@
|
||||||
|
knights-tour
|
||||||
|
|
||||||
|
# Testscript output directory (can be symlink).
|
||||||
|
#
|
||||||
|
test-knights-tour
|
|
@ -0,0 +1,49 @@
|
||||||
|
module board;
|
||||||
|
|
||||||
|
import <cstdint>;
|
||||||
|
import <iostream>;
|
||||||
|
import <vector>;
|
||||||
|
import <tuple>;
|
||||||
|
|
||||||
|
board::board() {
|
||||||
|
this->cells[0][0] = 1;
|
||||||
|
this->x = 0;
|
||||||
|
this->y = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void board::move(uint8_t x, uint8_t y) {
|
||||||
|
this->x = x;
|
||||||
|
this->y = y;
|
||||||
|
this->cells[x][y] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::tuple<uint8_t, uint8_t>> board::cell_moves(uint8_t x, uint8_t y) {
|
||||||
|
std::vector<std::tuple<uint8_t, uint8_t>> moves = {};
|
||||||
|
|
||||||
|
if (x >= 1 && y >= 2 && !this->cells[x-1][y-2]) moves.push_back({ x - 1, y - 2 });
|
||||||
|
if (x >= 1 && y <= 5 && !this->cells[x-1][y+2]) moves.push_back({ x - 1, y + 2 });
|
||||||
|
if (x >= 2 && y >= 1 && !this->cells[x-2][y-1]) moves.push_back({ x - 2, y - 1 });
|
||||||
|
if (x >= 2 && y <= 6 && !this->cells[x-2][y+1]) moves.push_back({ x - 2, y + 1 });
|
||||||
|
if (x <= 5 && y >= 1 && !this->cells[x+2][y-1]) moves.push_back({ x + 2, y - 1 });
|
||||||
|
if (x <= 5 && y <= 6 && !this->cells[x+2][y+1]) moves.push_back({ x + 2, y + 1 });
|
||||||
|
if (x <= 6 && y >= 2 && !this->cells[x+1][y-2]) moves.push_back({ x + 1, y - 2 });
|
||||||
|
if (x <= 6 && y <= 5 && !this->cells[x+1][y+2]) moves.push_back({ x + 1, y + 2 });
|
||||||
|
|
||||||
|
return moves;
|
||||||
|
}
|
||||||
|
|
||||||
|
void board::print_board() {
|
||||||
|
for (uint8_t i = 0; i < 8; i++) {
|
||||||
|
for (uint8_t j = 0; j < 8; j++) {
|
||||||
|
if (this->x == i && this->y == j) {
|
||||||
|
std::cout << 'o';
|
||||||
|
} else if (this->cells[i][j]) {
|
||||||
|
std::cout << 'x';
|
||||||
|
} else {
|
||||||
|
std::cout << ' ';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << '\n';
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
export module board;
|
||||||
|
|
||||||
|
import <cstdint>;
|
||||||
|
export import <vector>;
|
||||||
|
export import <tuple>;
|
||||||
|
|
||||||
|
export struct board {
|
||||||
|
bool cells[8][8] = {};
|
||||||
|
|
||||||
|
uint8_t x;
|
||||||
|
uint8_t y;
|
||||||
|
|
||||||
|
board();
|
||||||
|
|
||||||
|
void move(uint8_t x, uint8_t y);
|
||||||
|
|
||||||
|
std::vector<std::tuple<uint8_t, uint8_t>> cell_moves(uint8_t x, uint8_t y);
|
||||||
|
|
||||||
|
void print_board();
|
||||||
|
};
|
|
@ -0,0 +1,6 @@
|
||||||
|
libs =
|
||||||
|
#import libs += libhello%lib{hello}
|
||||||
|
|
||||||
|
exe{knights-tour}: {mxx hxx ixx txx cxx}{**} $libs testscript
|
||||||
|
|
||||||
|
cxx.poptions =+ "-I$out_root" "-I$src_root"
|
|
@ -0,0 +1,51 @@
|
||||||
|
import <iostream>;
|
||||||
|
import <vector>;
|
||||||
|
import <tuple>;
|
||||||
|
|
||||||
|
import board;
|
||||||
|
|
||||||
|
int main (int argc, char* argv[]) {
|
||||||
|
std::vector<std::tuple<uint8_t, uint8_t>> history = {{ 0, 0 }};
|
||||||
|
|
||||||
|
board b;
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
auto moves = b.cell_moves(b.x, b.y);
|
||||||
|
|
||||||
|
if (moves.size() == 0) break;
|
||||||
|
|
||||||
|
uint8_t tm = 255;
|
||||||
|
uint8_t tx = 0;
|
||||||
|
uint8_t ty = 0;
|
||||||
|
|
||||||
|
for (auto [nx, ny] : moves) {
|
||||||
|
uint8_t nm = b.cell_moves(nx, ny).size();
|
||||||
|
if (nm > tm) continue;
|
||||||
|
tm = nm; tx = nx; ty = ny;
|
||||||
|
}
|
||||||
|
|
||||||
|
b.move(tx, ty);
|
||||||
|
history.push_back({ tx, ty });
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "<!--\n";
|
||||||
|
b.print_board();
|
||||||
|
std::cout << "-->\n";
|
||||||
|
|
||||||
|
std::cout << "<svg viewBox=\"0 0 80 80\" xmlns=\"http://www.w3.org/2000/svg\">";
|
||||||
|
for (uint8_t i = 0; i < 8; i++) {
|
||||||
|
for (uint8_t j = 0; j < 8; j++) {
|
||||||
|
std::cout << "<rect x=\"" << i * 10 + 1 << "\" y=\""
|
||||||
|
<< j * 10 + 1 << "\" width=\"8\" height=\"8\" "
|
||||||
|
<< "fill=\"gray\"/>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
uint8_t px = 0;
|
||||||
|
uint8_t py = 0;
|
||||||
|
for (auto [x, y] : history) {
|
||||||
|
std::cout << "<line x1=\"" << px * 10 + 5 << ".5\" y1=\"" << py * 10 + 5 << ".5\" x2=\"" << x * 10 + 5 << ".5\" y2=\"" << y * 10 + 5 << ".5\" stroke=\"black\"/>";
|
||||||
|
px = x; py = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "</svg>";
|
||||||
|
}
|
File diff suppressed because one or more lines are too long
After Width: | Height: | Size: 7.3 KiB |
|
@ -0,0 +1,9 @@
|
||||||
|
: basics
|
||||||
|
:
|
||||||
|
$* 'World' >'Hello, World!'
|
||||||
|
|
||||||
|
: missing-name
|
||||||
|
:
|
||||||
|
$* 2>>EOE != 0
|
||||||
|
error: missing name
|
||||||
|
EOE
|
|
@ -0,0 +1,5 @@
|
||||||
|
: 1
|
||||||
|
name: knights-tour
|
||||||
|
version: 0.1.0
|
||||||
|
depends: * build2 >= 0.15.0
|
||||||
|
depends: * bpkg >= 0.15.0
|
|
@ -0,0 +1,11 @@
|
||||||
|
: 1
|
||||||
|
summary: knights-tour project repository
|
||||||
|
|
||||||
|
#:
|
||||||
|
#role: prerequisite
|
||||||
|
#location: https://pkg.cppget.org/1/stable
|
||||||
|
#trust: ...
|
||||||
|
|
||||||
|
#:
|
||||||
|
#role: prerequisite
|
||||||
|
#location: https://git.build2.org/hello/libhello.git
|
Loading…
Reference in New Issue