Advertisement
Guest User

Untitled

a guest
May 24th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication6
  8. {
  9. class Program
  10. {
  11. //Array for queen x and y locations, it's here because I'm really bad at class scope.
  12. static int[,] queenLoc = new int[8, 2];
  13. //Counter for queens that increments each loop, supposedly...
  14. static int queenNumber = 0;
  15.  
  16. static void Main(string[] args)
  17. {
  18. Console.WriteLine("Ready to begin, press any Key");
  19. Console.ReadLine();
  20. Console.Clear();
  21. Console.SetCursorPosition(0, 0);
  22. //Generate Board
  23. for (int i = 0; i < 8; i++)
  24. {
  25. Console.WriteLine("00000000");
  26. }
  27. Console.ReadLine();
  28. bool queenCheckSuccess = true;
  29.  
  30. //Begin board run, rows then columns
  31. for (int y = 0; y < 8; y++)
  32. {
  33.  
  34. for (int x = 0; x < 8; x++)
  35. {
  36.  
  37. //the "+ 1"s here is for human readableness onscreen and could possibly be cleaned up
  38. queenLoc[queenNumber, 0] = x + 1 ;
  39. queenLoc[queenNumber, 1] = y + 1 ;
  40.  
  41. Console.SetCursorPosition(0, 10);
  42. Console.Write("Checking Queen {0} at location {1}, {2}... ", queenNumber + 1, x + 1, y + 1);
  43. queenCheckSuccess = queenCheck(queenNumber, x + 1, y + 1);
  44.  
  45. if (queenCheckSuccess)
  46. {
  47. ;
  48. Console.SetCursorPosition(0, 10);
  49. Console.Write("Check ok, placed Queen at {0}, {1} ", x + 1, y + 1);
  50. Console.SetCursorPosition(queenLoc[queenNumber, 0] - 1, queenLoc[queenNumber, 1] - 1);
  51. Console.Write("X");
  52. //escape this loop and increment queen index;
  53. x = 8;
  54. queenNumber++;
  55. }
  56. else
  57. {
  58. Console.SetCursorPosition(0, 10);
  59. Console.Write("Check Failed ");
  60. if (x == 7)
  61. {
  62. //clear this slot and move to previous
  63. queenLoc[queenNumber, 0] = 0;
  64. queenLoc[queenNumber, 1] = 0;
  65. queenNumber--;
  66. y--;
  67. x = queenLoc[queenNumber, 0] - 1;
  68. Console.SetCursorPosition(queenLoc[queenNumber, 0] - 1, queenLoc[queenNumber, 1] - 1);
  69. Console.Write("0");
  70. }
  71. }
  72.  
  73. }
  74.  
  75. }
  76. Console.SetCursorPosition(0, 10);
  77. Console.Write("COMPLETE SUCCESS!!! "); //this is always a lie...
  78. Console.ReadLine();
  79.  
  80. }
  81. public static bool queenCheck(int _queenNumber, int _x, int _y)
  82. {
  83.  
  84. if (_queenNumber == 0)
  85. return true;
  86. for (int i = 0; i < _queenNumber; i++)
  87. {
  88. //get x difference for check against y
  89. double yDiff = Math.Abs(queenLoc[i, 0] - _x);
  90. if (queenLoc[i,0] != 0)
  91. {
  92. //check column
  93. if (_x == queenLoc[i,0])
  94. return false;
  95. //check diagonal
  96. else if (_y - yDiff == queenLoc[i,1])
  97. return false;
  98.  
  99. }
  100.  
  101. }
  102. return true;
  103. }
  104.  
  105. }
  106.  
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement