Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication6
- {
- class Program
- {
- //Array for queen x and y locations, it's here because I'm really bad at class scope.
- static int[,] queenLoc = new int[8, 2];
- //Counter for queens that increments each loop, supposedly...
- static int queenNumber = 0;
- static void Main(string[] args)
- {
- Console.WriteLine("Ready to begin, press any Key");
- Console.ReadLine();
- Console.Clear();
- Console.SetCursorPosition(0, 0);
- //Generate Board
- for (int i = 0; i < 8; i++)
- {
- Console.WriteLine("00000000");
- }
- Console.ReadLine();
- bool queenCheckSuccess = true;
- //Begin board run, rows then columns
- for (int y = 0; y < 8; y++)
- {
- for (int x = 0; x < 8; x++)
- {
- //the "+ 1"s here is for human readableness onscreen and could possibly be cleaned up
- queenLoc[queenNumber, 0] = x + 1 ;
- queenLoc[queenNumber, 1] = y + 1 ;
- Console.SetCursorPosition(0, 10);
- Console.Write("Checking Queen {0} at location {1}, {2}... ", queenNumber + 1, x + 1, y + 1);
- queenCheckSuccess = queenCheck(queenNumber, x + 1, y + 1);
- if (queenCheckSuccess)
- {
- ;
- Console.SetCursorPosition(0, 10);
- Console.Write("Check ok, placed Queen at {0}, {1} ", x + 1, y + 1);
- Console.SetCursorPosition(queenLoc[queenNumber, 0] - 1, queenLoc[queenNumber, 1] - 1);
- Console.Write("X");
- //escape this loop and increment queen index;
- x = 8;
- queenNumber++;
- }
- else
- {
- Console.SetCursorPosition(0, 10);
- Console.Write("Check Failed ");
- if (x == 7)
- {
- //clear this slot and move to previous
- queenLoc[queenNumber, 0] = 0;
- queenLoc[queenNumber, 1] = 0;
- queenNumber--;
- y--;
- x = queenLoc[queenNumber, 0] - 1;
- Console.SetCursorPosition(queenLoc[queenNumber, 0] - 1, queenLoc[queenNumber, 1] - 1);
- Console.Write("0");
- }
- }
- }
- }
- Console.SetCursorPosition(0, 10);
- Console.Write("COMPLETE SUCCESS!!! "); //this is always a lie...
- Console.ReadLine();
- }
- public static bool queenCheck(int _queenNumber, int _x, int _y)
- {
- if (_queenNumber == 0)
- return true;
- for (int i = 0; i < _queenNumber; i++)
- {
- //get x difference for check against y
- double yDiff = Math.Abs(queenLoc[i, 0] - _x);
- if (queenLoc[i,0] != 0)
- {
- //check column
- if (_x == queenLoc[i,0])
- return false;
- //check diagonal
- else if (_y - yDiff == queenLoc[i,1])
- return false;
- }
- }
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement