Skip to content

Commit

Permalink
wrote do homework and integrated all the other scripts to play nice w…
Browse files Browse the repository at this point in the history
…ith argv/
  • Loading branch information
uberj committed Dec 2, 2011
1 parent bcb3c51 commit e4759b8
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 8 deletions.
16 changes: 15 additions & 1 deletion board_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,19 @@ def pb( board, n ):
print "======================="
for i in range(n):
for j in range(n):
print board[i][j],
if board[i][j] < 10:
print str(board[i][j])+" ",
else:
print board[i][j],
print
"""
Take a board of size n and a solution array (with moves in
accending order).
@return A board displaying the move order and where the move was
on the input board.
"""
def solution_to_board( solution, board, n):
i = 1
for move in solution:
board[move[0]][move[1]] = i
i += 1
Binary file removed board_utils.pyc
Binary file not shown.
38 changes: 38 additions & 0 deletions do_homework.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

do_problem2 () {
solutions=0
for ((i=0; i<$2; i++))
do
for ((j=0; j<$3; j++))
do
echo "Knights tour with initial move ($i, $j)"
python knights_tour.py $1 $i $j
if [ $? -eq 0 ]
then
solutions=$((solutions+1))
fi
echo
done
done
echo "There were $solutions successful tours out of $1 attempts."
echo
}

echo "===================================================="
echo "===================================================="
echo "Problem 2 and 3 . Test your program by using as starting square each of the 25 squares on a 5 by 5 gameboard. How many tours did it find?"
echo "===================================================="
do_problem2 5 5 5

echo "===================================================="
echo "===================================================="
echo "Problem 4. Run your program from 4 different initial squares on a 6 by 6 gameboard. Does your program always find a tour? Are the tours you found open or closed? How many different tours does your program?"
echo "===================================================="
do_problem2 6 2 2

echo "===================================================="
echo "===================================================="
echo "Problem 4. Run your program from 4 different initial squares on a 8 by 8 gameboard. Does your program always find a tour? Are the tours you found open or closed? How many different tours does your program?"
echo "===================================================="
do_problem2 8 2 2
25 changes: 18 additions & 7 deletions knights_tour.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from tour_utils import *
from board_utils import *
import sys
import pdb
"""
Do a knights tour with an NxN board.
Expand All @@ -25,7 +26,6 @@ def do_knights_tour( solution, index, board, n ):
return
# Move there.
make_move( best, board, n )
pb(board, n)
# Remember where we are.
solution.append( best )
# Recurse.
Expand All @@ -34,12 +34,23 @@ def do_knights_tour( solution, index, board, n ):


if __name__ == "__main__":
n = 5
initial_move = (0,0)
if len(sys.argv) == 1:
print "Usage: python knights_tour.py n i j"
print "n = board size (n by n)"
print "i j = Initial move in the ith jth index"
sys.exit(0)
n = int(sys.argv[1])
initial_move = (int(sys.argv[2]),int(sys.argv[3]))
board = [ [0]*n for i in range(n) ]
pb(init_board( board, n ),n)
#print best_move( (1,2), board, n)
sol = knights_tour(n,initial_move )
print sol
print len(sol)
sol = [initial_move]+knights_tour(n,initial_move )
#pdb.set_trace()
print "Board solution"
solution_to_board(sol, board, n)
mark_last_move( board, n, sol[-1])
pb(board , n)
if len(sol) == (n*n - 1):
sys.exit(0)
else:
sys.exit(1)

35 changes: 35 additions & 0 deletions tour_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,48 @@ def best_move( index, board, n ):
continue
elif best is None:
best = possible
continue
elif val(possible, board) == val(best, board):
# Tie breaker
if abs(possible[0] - possible[1]) < abs(best[0] - best[1]):
best = possible
continue
elif val(possible, board) < val(best, board):
best = possible
else:
continue

return best

"""
HACK HACK HACK.
If there is only one sqaure left that is still a zero.
1) Find it
2) Get the last known move
3) See if you can move into the last square from the last known move.
4 a) If you can mark it.
b) If you can't don't mark it.
"""
def mark_last_move( board, n, last_known ):
zeros = 0
zero_location = None
for i in range(n):
for j in range(n):
if board[i][j] == 0:
zeros += 1
zero_location = (i,j)
if zeros != 1:
return
else:
if abs( zero_location[0] - last_known[0] ) == 1 and abs( zero_location[1] - last_known[1] ) == 2:
board[zero_location[0]][zero_location[1]] = n*n
elif abs( zero_location[0] - last_known[0] ) == 2 and abs( zero_location[1] - last_known[1] ) == 1:
board[zero_location[0]][zero_location[1]] = n*n
else:
return





"""
Expand Down

0 comments on commit e4759b8

Please sign in to comment.