MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
knight::Tour Class Reference

Public Member Functions

void drawBoard (mxvk::VK_Sprite &whiteCell, mxvk::VK_Sprite &redCell, mxvk::VK_Sprite &visitedCell, float scaleX, float scaleY) const
void drawKnight (mxvk::VK_Sprite &texture, float scaleX, float scaleY) const
int getMoves () const
bool isTourOver () const
void nextMove ()
void resetTour ()
void resetTour (int startRow, int startCol)
void resetTourFromPoint (float x, float y)
 Tour ()

Detailed Description

Definition at line 22 of file knight.cpp.

Constructor & Destructor Documentation

◆ Tour()

knight::Tour::Tour ( )

Definition at line 265 of file knight.cpp.

265 : moves(1), tourOver(false) {
266 std::srand(static_cast<unsigned int>(std::time(nullptr)));
267 initializeBoard();
268 resetTour();
269 }
void resetTour()
Definition knight.cpp:331

Member Function Documentation

◆ drawBoard()

void knight::Tour::drawBoard ( mxvk::VK_Sprite & whiteCell,
mxvk::VK_Sprite & redCell,
mxvk::VK_Sprite & visitedCell,
float scaleX,
float scaleY ) const

Definition at line 380 of file knight.cpp.

380 {
381 for (int row = 0; row < BOARD_SIZE; ++row) {
382 for (int col = 0; col < BOARD_SIZE; ++col) {
383 mxvk::VK_Sprite *cell = nullptr;
384 if (board[row][col] == -1) {
385 cell = &visitedCell;
386 } else if ((row + col) % 2 == 0) {
387 cell = &whiteCell;
388 } else {
389 cell = &redCell;
390 }
391
392 cell->drawSpriteRect(
393 static_cast<int>(std::lround((START_X + col * CELL_SIZE) * scaleX)),
394 static_cast<int>(std::lround((START_Y + row * CELL_SIZE) * scaleY)),
395 static_cast<int>(std::lround(CELL_DRAW_SIZE * scaleX)),
396 static_cast<int>(std::lround(CELL_DRAW_SIZE * scaleY)));
397 }
398 }
399 }
void drawSpriteRect(int x, int y, int w, int h)
Queue a draw into an explicit destination rectangle.

◆ drawKnight()

void knight::Tour::drawKnight ( mxvk::VK_Sprite & texture,
float scaleX,
float scaleY ) const

Definition at line 401 of file knight.cpp.

401 {
402 texture.drawSpriteRect(
403 static_cast<int>(std::lround((START_X + knightPos.col * CELL_SIZE + 5) * scaleX)),
404 static_cast<int>(std::lround((START_Y + knightPos.row * CELL_SIZE + 5) * scaleY)),
405 static_cast<int>(std::lround(KNIGHT_SIZE * scaleX)),
406 static_cast<int>(std::lround(KNIGHT_SIZE * scaleY)));
407 }

◆ getMoves()

int knight::Tour::getMoves ( ) const
inlinenodiscard

Definition at line 33 of file knight.cpp.

33{ return moves; }

◆ isTourOver()

bool knight::Tour::isTourOver ( ) const
inlinenodiscard

Definition at line 34 of file knight.cpp.

34{ return tourOver; }

◆ nextMove()

void knight::Tour::nextMove ( )

Definition at line 367 of file knight.cpp.

367 {
368 if (tourOver || static_cast<std::size_t>(moves) >= moveSequence.size()) {
369 return;
370 }
371
372 const Position nextPosition = moveSequence[static_cast<std::size_t>(moves)];
373 board[knightPos.row][knightPos.col] = -1;
374 knightPos = nextPosition;
375 ++moves;
376 board[knightPos.row][knightPos.col] = moves;
377 tourOver = static_cast<std::size_t>(moves) == moveSequence.size();
378 }

◆ resetTour() [1/2]

void knight::Tour::resetTour ( )

Definition at line 331 of file knight.cpp.

331 {
332 resetTour(std::rand() % BOARD_SIZE, std::rand() % BOARD_SIZE);
333 }

◆ resetTour() [2/2]

void knight::Tour::resetTour ( int startRow,
int startCol )

Definition at line 335 of file knight.cpp.

335 {
336 if (startRow < 0 || startRow >= BOARD_SIZE || startCol < 0 || startCol >= BOARD_SIZE) {
337 return;
338 }
339
340 clearBoard();
341 knightPos = Position(startRow, startCol);
342 board[knightPos.row][knightPos.col] = 1;
343 moveSequence.clear();
344 moveSequence.push_back(knightPos);
345 solveKnightsTour(knightPos, 2);
346 moves = 1;
347 tourOver = false;
348 }

◆ resetTourFromPoint()

void knight::Tour::resetTourFromPoint ( float x,
float y )

Definition at line 350 of file knight.cpp.

350 {
351 const int localX = static_cast<int>(std::floor(x)) - START_X;
352 const int localY = static_cast<int>(std::floor(y)) - START_Y;
353 if (localX < 0 || localY < 0) {
354 return;
355 }
356
357 const int col = localX / CELL_SIZE;
358 const int row = localY / CELL_SIZE;
359 if (row >= BOARD_SIZE || col >= BOARD_SIZE ||
360 localX % CELL_SIZE >= CELL_DRAW_SIZE || localY % CELL_SIZE >= CELL_DRAW_SIZE) {
361 return;
362 }
363
364 resetTour(row, col);
365 }

The documentation for this class was generated from the following file: