|
Alright well since we don't get any challenges really around here, I would like to offer one just for the fun of the community.
If any of you are familiar with the Knight's Tour, it's a problem in which you must move the Knight piece around the whole board and touch every square once.
You can find more information on it here: Knight's tour - Wikipedia, the free encyclopedia
What you need to do:
- Create an application that will take the chess board and move the knight around the squares.
- Language can be any possible, C#, Java, anything!
- Note: Some kind of visual frame is not required but I think it would be cool.
I don't mind if you make it print the current square in which the knight is travelling to, or if you make a 2d frame and make the piece move itself.
I'm interested to see what people come up with, good luck! I will be submitting my code too after I start
NOTE: You must label your tiles, here's the current labeling.
"A knight's tour is a sequence of moves of a knight on a chessboard such that the knight visits every square exactly once"
??
Might try this one. Bruteforcing is not gonna work. that's for sure :/
the wiki page practically tells you how to solve it lol
ty wiki for the algorithm
https://gist.github.com/wktr/7368338Code:;; Knight's tour using Warnsdorff's rule. ;; https://en.wikipedia.org/wiki/Knight%27s_tour (defconstant +board-sz+ 8) (defparameter *board* (make-array `(,+board-sz+ ,+board-sz+) :initial-element 0)) (defconstant +knight-moves+ '(#(1 -2) #(2 -1) #(2 1) #(1 2) #(-1 2) #(-2 1) #(-2 -1) #(-1 -2))) (defun board-at (x y) (aref *board* y x)) (defun board-set (x y val) (setf (aref *board* y x) val)) (defun print-board () (dotimes (y +board-sz+) (dotimes (x +board-sz+) (format t "[~d] " (board-at x y))) (terpri))) (defun is-valid-sq (x y) (and (>= x 0) (>= y 0) (< x +board-sz+) (< y +board-sz+) (zerop (board-at x y)))) (defun get-moves (x y) (remove-if-not (lambda (lst) (apply #'is-valid-sq lst)) (mapcar (lambda (mvmt) (let ((new-x (+ x (aref mvmt 0))) (new-y (+ y (aref mvmt 1)))) (list new-x new-y))) +knight-moves+))) (defun get-min-move (moves) (if (null moves) nil (apply #'min (mapcar (lambda (move) (length (get-moves (car move) (second move)))) moves)))) (defun next-sq (x y) (let* ((moves (get-moves x y)) (min-move (get-min-move moves))) (when (null moves) (return-from next-sq nil)) (dolist (move moves) (when (= min-move (length (get-moves (car move) (second move)))) (return-from next-sq move))))) (defun sq-name (x y) (format nil "~C~D" (code-char (+ (char-int #\a) x)) (abs (- y +board-sz+)))) (defun begin-tour (x y) (format t "Moved to ~A~%" (sq-name x y)) (let ((next (next-sq x y))) (board-set x y 1) (print-board) (when (null next) (format t "No more moves!~%") (quit)) (apply #'begin-tour next))) (begin-tour 0 0)
« Previous Thread | Next Thread » |
Thread Information |
Users Browsing this ThreadThere are currently 1 users browsing this thread. (0 members and 1 guests) |