Dear Monks,

I am trying to write a program to solve a puzzle I came across.

There is one horse on a chessboard, and it can move according to the rules of chess. It needs to cover all the 64 squares of the board in 63 moves. So, it can not go back to a square which it has already been in.

I am attaching what I have so far, and it works for the first 42 moves, but it fails for the 43rd move.

Can someone please tell me what is the best way to debug such a program?

Also, is this the kind of solution you would have thought of or is there a better solution?
Should I rather have created objects and used them?

Thanks,
Nitish

#! /usr/bin/perl use strict; use warnings; use Data::Dumper; my $puzzle_board = create_board(); solve ($puzzle_board); print_board ($puzzle_board); sub solve { my $board = shift @_; my $move = 1; my ($x, $y) = (0, 1); # Change this number to solve more or less of the puzzle while ( $move < 44 ) { my ($newx, $newy) = calculate_move($board, $x, $y); print "$move: $x,$y->$newx,$newy\t"; if ( verify_move($board, $newx, $newy) == 1 ) { forward($board, $x, $y, $newx, $newy); } elsif ( $board->[$x][$y]{trial} < 7 ) { $board->[$x][$y]{trial}++; next; } else { back($board, $x, $y, $newx, $newy); $x = $board->[$newx][$newy]{oldx}; $y = $board->[$newx][$newy]{oldy}; $move--; next; } ($x, $y) = ($newx, $newy); $move++; } } sub forward { my ($board, $x, $y, $newx, $newy) = @_; $board->[$newx][$newy]->{visited} = $board->[$x][$y]->{visited} + + 1; $board->[$newx][$newy]->{oldx} = $x; $board->[$newx][$newy]->{oldy} = $y; } sub back { my ($board, $x, $y, $newx, $newy) = @_; $board->[$newx][$newy]{trial} = 0; $board->[$newx][$newy]{visited} = 0; } # Assumes that it is passed valid x and y values in the board. sub calculate_move { my ($board, $x, $y) = @_; my ($newx, $newy) = 0; if ($board->[$x][$y]{trial} == 0) { $newx = $x + 1; $newy = $y + 2; } if ($board->[$x][$y]{trial} == 1) { $newx = $x - 1; $newy = $y + 2; } if ($board->[$x][$y]{trial} == 2) { $newx = $x + 1; $newy = $y - 2; } if ($board->[$x][$y]{trial} == 3) { $newx = $x - 1; $newy = $y - 2; } if ($board->[$x][$y]{trial} == 4) { $newx = $x + 2; $newy = $y + 1; } if ($board->[$x][$y]{trial} == 5) { $newx = $x - 2; $newy = $y + 1; } if ($board->[$x][$y]{trial} == 6) { $newx = $x + 2; $newy = $y - 1; } if ($board->[$x][$y]{trial} == 7) { $newx = $x - 2; $newy = $y - 1; } return ($newx, $newy); } sub verify_move { my ($board, $x, $y) = @_; if ($x > 7 || $x < 0 || $y > 7 || $y < 0) { return 0; } if ($board->[$x][$y]{visited} > 0) { return 0; } return 1; } sub create_board { my @board; my ($i, $j); for ( $i = 0; $i < 8; $i++ ) { for ( $j = 0; $j < 8; $j++ ) { $board[$i][$j] = { x => $i, y => $j, visited => 0, trial => 0, oldx => 0, oldy => 0 } } } $board[0][1] = { x => 0, y => 1, visited => 1, trial => 0 }; return \@board; } sub print_board { my $board_ref = shift @_; my ($i, $j); print Dumper $board_ref; print "\nThe Chess Board\n"; for ($i = 7; $i >= 0; $i--) { for ($j = 0; $j < 8; $j++) { printf ("%02d ", $board_ref->[$i][$j]{visited} ); } print "\n\n"; } }

In reply to How to debug a long while loop by nbezzala

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.