Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | None | 0 0
  1. // knightstour2.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <cstdio>
  6. #include <iostream>
  7. #include <vector>
  8. #include <sstream>
  9. #include <list>
  10. #include <iterator>
  11. #include <cmath>
  12. using std::vector;
  13. using std::string;
  14. using std::list;
  15. using std::cout;
  16. using std::cin;
  17. using std::endl;
  18. #define N 5
  19.  
  20. using namespace std;
  21.  
  22. // Devining boardsize (maybe changeable by the user later)
  23. const int boardSize = 5;
  24.  
  25. // Total moves needed to complete the knights tour = (boardSize * boardSize) - 1
  26. const int totalNumMoves = (boardSize*boardSize) - 1;
  27. // current move
  28. int moveCount = 0;
  29.  
  30. // 2D array for the chessboard
  31. // the value of the arrayposition defines if it has been visited yet
  32. // 0 = not visited and > 0 means it has been visited
  33. // visited places will also be chronologically numbered
  34. // for instance if a place in the array has a value of 4 it means it is the forth place the knight has been
  35. int chessboard[boardSize][boardSize];
  36.  
  37. //current position of the knight
  38. int curPosX;
  39. int curPosY;
  40.  
  41. //starting position is always the same (maybe changeable by user later)
  42. int startPosX = 0;
  43. int startPosY = 0;
  44.  
  45. // define a structure for a square with an x and a y coordinate
  46. typedef struct chessMove {
  47.     int x, y;
  48. } chessMove;
  49.  
  50.  
  51.  
  52.  
  53. void PrepBoard()
  54. {
  55.     int x = startPosX;
  56.     int y = startPosY;
  57.  
  58.     for (int i = 0; i < boardSize; i++)
  59.     {
  60.         for (int j = 0; j < boardSize; j++)
  61.         {
  62.             chessboard[i][j] = 0;
  63.         }
  64.     }
  65.     chessboard[x][y] = 1;
  66. }
  67.  
  68. bool IsMovePossible(chessMove nextMove)
  69. {
  70.     int i = nextMove.x;
  71.     int j = nextMove.y;
  72.     if ((i >= 0 && i < N) && (j >= 0 && j < N) && (chessboard[i][j] == 0))
  73.     {
  74.         return true;
  75.     }
  76.     return false;
  77. }
  78.  
  79. bool CalculateTour(chessMove possibleMove[], chessMove currMove, int moveCount)
  80. {
  81.     if (moveCount == totalNumMoves)
  82.     {
  83.         return true;
  84.     }
  85.     chessMove nextMove;
  86.  
  87.     cout << "N = " << N << endl;
  88.     for (int i = 0; i < N; i++)
  89.     {
  90.         nextMove.x = currMove.x + possibleMove[i].x;
  91.         nextMove.y = currMove.y + possibleMove[i].y;
  92.         if (IsMovePossible(nextMove))
  93.         {
  94.             cout << "nextMove is possible!, nextMove = " << nextMove.x << ", " << nextMove.y << endl;
  95.             chessboard[nextMove.x][nextMove.y] = moveCount + 1;
  96.             if (CalculateTour(possibleMove, nextMove, moveCount) == true)
  97.             {
  98.                 return true;
  99.             }
  100.             else
  101.             {
  102.                 cout << "setting next move to 0?";
  103.                 chessboard[nextMove.x][nextMove.y] = 0;
  104.             }
  105.         }
  106.     }
  107.     return false;
  108.  
  109. }
  110.  
  111.  
  112.  
  113. void PrintBoard()
  114. {
  115.     for (int i = 0; i < N; i++)
  116.     {
  117.         cout << "| ";
  118.         for (int j = 0; j < N; j++)
  119.         {
  120.             string extra_space = " ";
  121.             if (chessboard[i][j] >= 10) extra_space = "";
  122.             cout << chessboard[i][j] << extra_space << " | ";
  123.         }
  124.         cout << "\n";
  125.     }
  126. }
  127.  
  128. int main(int argc, char ** argv)
  129. {
  130.     chessMove currentMove = { 0,0 };
  131.     // all possible moves the the knight could make
  132.     chessMove possibleMove[8] =
  133.     {
  134.         { 2,1 },{ 1,2 },{ -1,2 },{ -2,1 },{ -2,-1 },{ -1,-2 },{ 1,-2 },{ 2,-1 }
  135.     };
  136.     PrepBoard();
  137.     if (CalculateTour(possibleMove, currentMove, moveCount) == false)
  138.     {
  139.         cout << "\nKnightsTour does not exist";
  140.     }
  141.     else
  142.     {
  143.         cout << "\nKnightsTour does exist \n";
  144.         PrintBoard();
  145.     }
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement