How do I generate all of a knight's moves? - python

I am writing a Chess program in Python that needs to generate all the moves of a knight. For those not familiar with chess, a knight moves in an L shape.
So, given a position of (2, 4) a knight could move to (0, 3), (0, 5), (1, 2), (3, 2), etc. for a total of (at most) eight different moves.
I want to write a function called knight_moves that generates these tuples in a list. What is the easiest way to do this in Python?
def knight_moves(position):
''' Returns a list of new positions given a knight's current position. '''
pass

Ok, so thanks to Niall Byrne, I came up with this:
from itertools import product
def knight_moves(position):
x, y = position
moves = list(product([x-1, x+1],[y-2, y+2])) + list(product([x-2,x+2],[y-1,y+1]))
moves = [(x,y) for x,y in moves if x >= 0 and y >= 0 and x < 8 and y < 8]
return moves

Why not store the relative pairs it can move in ? So take your starting point, and add a set of possible moves away from it, you then would just need a sanity check to make sure they are still in bounds, or not on another piece.
ie given your (2, 4) starting point, the options are (-2,-1), (-2,+1), (-1,+2), (+2,+1)
The relative positions would thus always be the same.

Not familiar with chess...
deltas = [(-2, -1), (-2, +1), (+2, -1), (+2, +1), (-1, -2), (-1, +2), (+1, -2), (+1, +2)]
def knight_moves(position):
valid_position = lambda (x, y): x >= 0 and y >= 0 and ???
return filter(valid_position, map(lambda (x, y): (position[0] + x, position[1] + y), deltas))

Instead of using an array, I would suggest you use bitboards. Not only are they very easy to manipulate, they will also reduce the need for boundary checking. With as few as 12 bitboards, you could probably encode the information you need for the whole game.
https://www.chessprogramming.org/Bitboards
The basic idea of bitboards is to use a 64 bit integer and set 1 if a piece is present on the bit. For example, if you had a 64 bit integer to represent white knights, you would set the 2nd and 6th bits at the starting of the game as they are the positions where the white knights are located. Using this notation, it becomes easy to calculate the knight's moves. It will be easy to calculate other pieces' moves too.
With this representation, you could take a look at this link to the chess engine for a ready made algorithm to implement knight's moves.
http://www.mayothi.com/nagaskakichess6.html

This might sound as an overkill if you're not familiar with analytical geometry (or complex numbers geometry) but I came up with a very elegant mathematical solution when
I was implementing a validation for the movement of pieces.
The knight's moves are lying on a circle which can be defined as
(x-x_0)^2+(y-y_0)^2=5 where x_0 and y_0 are the Knight's current coordinates. If you switch to polar coordinates, you can get all possible coordinates with this simple code:
import math
def knight_moves(x,y):
new_positions=[]
r=math.sqrt(5) #radius of the circle
for phi in [math.atan(2),math.atan(1/2)]: #angles in radians
for quadrant in range(4):
angle=phi+quadrant*math.pi/2 # add 0, 90, 180, 270 degrees in radians
new_x=round(x+r*math.cos(angle))
new_y=round(y+r*math.sin(angle))
if max(new_x,new_y,7-new_x,7-new_y)<=7: #validation whether the move is in grid
new_positions.append([new_x,new_y])
return(new_positions)
def validate_knight_move(x,y,x_0,y_0):
return((x-x_0)**2+(y-y_0)**2==5)
x_0=2
y_0=4
moves=knight_moves(x_0,y_0)
print(moves)
validation=[validate_knight_move(move[0],move[1],x_0,y_0) for move in moves]
print(validation)
[[3, 6], [0, 5], [1, 2], [4, 3], [4, 5], [1, 6], [0, 3], [3, 2]]
[True, True, True, True, True, True, True, True]
It's good to point here, that it is much simpler to validate the position than to construct it directly. Therefore, it might be a good idea to just try whether all possible moves lie on the circle or not:
def knight_moves2(x,y):
new_positions=[]
for dx in [-2,-1,1,2]:
for dy in [-2,-1,1,2]:
if(validate_knight_move(x+dx,y+dy,x,y)): #is knight move?
if max(x+dx,y+dy,7-(x+dx),7-(y+dy))<=7: #validation whether the move is in grid
new_positions.append([x+dx,y+dy])
return(new_positions)
new_positions=knight_moves2(x_0,y_0)
print(new_positions)
[[0, 3], [0, 5], [1, 2], [1, 6], [3, 2], [3, 6], [4, 3], [4, 5]]

Here's an easy implementation:
def knights_moves():
a = []
b = (1, 2)
while 1:
a.append(b)
b = (-b[0], b[1])
a.append(b)
b = (b[1], b[0])
if b in a:
return a
[(1, 2), (-1, 2), (2, -1), (-2, -1), (-1, -2), (1, -2), (-2, 1), (2, 1)]
From there you can just simply add the current position to every member of this list, and then double check for validity.

Completing xiaowl's answer,
possible_places = [(-2, -1), (-2, +1), (+2, -1), (+2, +1), (-1, -2), (-1, +2), (+1, -2), (+1, +2)]
def knight_moves(cur_pos):
onboard = lambda (x, y): x >= 0 and y >= 0 and x<8 and y<8
eval_move = lambda(x,y): (cur_pos[0] + x, cur_pos[1] + y)
return filter(onboard, map(eval_move, possible_places))

For the knights moves:
def getAllValidMoves(x0, y0):
deltas = [(-2, -1), (-2, +1), (+2, -1), (+2, +1), (-1, -2), (-1, +2), (+1, -2), (+1, +2)]
validPositions = []
for (x, y) in deltas:
xCandidate = x0 + x
yCandidate = y0 + y
if 0 < xCandidate < 8 and 0 < yCandidate < 8:
validPositions.append([xCandidate, yCandidate])
return validPositions
print getAllValidMoves(3,3)
I just stored all the possible deltas, applied each one of them to the "initial position" and saved the ones that were inside the chessboard

from itertools import product
def moves():
""" The available (relative) moves"""
a = list(product( (1, -1), (2,-2)))
return a + [tuple(reversed(m)) for m in a]
def neighbors(a,b):
# true if x,y belongs in a chess table
in_table = lambda (x, y): all((x < 8, y < 8, x >= 0, y >= 0))
# returns the possible moving positions
return filter(in_table, [(a+x, b+y) for x, y in moves()])
"neighbors" are the available positions that a knight can go from a,b

The below method is implemented in python. It accepts the board (which can be of any m*n & has values 0(available) and 1(occupied) and current position of knight)
def get_knight_moves(board, position):
KNIGHT_STEPS = ((1, 2), (-1, 2), (1, -2), (-1, -2), (2, 1), (-2, 1), (2, -1), (-2, -1))
knight_moves = []
for (i, j) in KNIGHT_STEPS:
try:
x, y = position[0] + i, position[1] + j
if board[x][y] == 0:
knight_moves.append((x, y))
except IndexError:
pass
print(knight_moves)

Related

How to model Bishop movement on a chessboard

I have a board, and I want to model a bishop's possible moves on it. I attempted this code:
for c1, c2 in [(1, -1), (1, 1), (-1, -1), (-1, 1)]:
for x, y in [range(x+c1, board_size), range(y+c2, board_size)]:
moves.append(x, y)
But it doesn't work to find all the moves. Yet, I don't understand why. Doesn't it check all four directions?
Your logic is sound, but your execution is not.
Half of your calculations must go from x or y to 0 (the other half go from x or y to board_size
Ranges don't work from larger to smaller values with the default step, so you'll need to introduce a step of -1 to count from x or y to 0
You should use zip() to create an iterable collection of tuples.
This will work:
right_up = zip(range(x + 1, board_size), range(y - 1, -1, -1))
right_down = zip(range(x + 1, board_size), range(y + 1, board_size))
left_up = zip(range(x - 1, -1, -1), range(y - 1, -1, -1))
left_down = zip(range(x - 1, -1, -1), range(y + 1, board_size))
for r in (right_up, right_down, left_up, left_down):
for new_x, new_y in r: # add coordinates to move list

Pythonic way to calculate pair wise dot product inside a list

I have a list that consists of all combinations of tuples that each elements can only be -1 or 1. The list can be generated as:
N=2
list0 = [p for p in itertools.product([-1, 1], repeat=N)]
For example, if the tuple has N=2 elements:
list0 = [(-1, -1), (-1, 1), (1, -1), (1, 1)]
Thus the total number of tuples is 2^2=4.
If the tuple has N=3 elements:
list0 = [(-1, -1, -1), (-1, -1, 1), (-1, 1, -1), (-1, 1, 1),
(1, -1, -1), (1, -1, 1), (1, 1, -1), (1, 1, 1)]
Here is my concern:
Now I would like to get all the results of dot products between any pair of tuples in the list(including ones a tuple with itself). So for N=2 there will be 6(pairs) + 4(itself) = 10 combinations; for N=3 there will be 28(pairs) + 8(itself) = 36 combinations.
For small N I can do something like:
for x in list0:
for y in list0:
print(np.dot(x,y))
However, assuming I already have list0, what is the optimal way to calculate all the possibilities of dot products, if N is large, like ~50?
You could use the np.dot itself:
import numpy as np
list0 = [(-1, -1, -1), (-1, -1, 1), (-1, 1, -1), (-1, 1, 1), (1, -1, -1), (1, -1, 1), (1, 1, -1), (1, 1, 1)]
# approach using np.dot
a = np.array(list0)
result = np.dot(a, a.T)
# brute force approach
brute = []
for x in list0:
brute.append([np.dot(x, y) for y in list0])
brute = np.array(brute)
print((brute == result).all())
Output
True
What you are asking is the matrix multiplication of a with itself, from the documentation:
if both a and b are 2-D arrays, it is matrix multiplication,
Note that the most pythonic solutio is to use the operator #:
import numpy as np
list0 = [(-1, -1, -1), (-1, -1, 1), (-1, 1, -1), (-1, 1, 1), (1, -1, -1), (1, -1, 1), (1, 1, -1), (1, 1, 1)]
# approach using np.dot
a = np.array(list0)
result = a # a.T
# brute force approach
brute = []
for x in list0:
brute.append([np.dot(x, y) for y in list0])
brute = np.array(brute)
print((brute == result).all())
Output
True
Note: The code was run in Python 3.5
You can stick with numpy
import numpy as np
import random
vals = []
num_vecs = 3
dimension = 4
for n in range(num_vecs):
val = []
for _ in range(dimension):
val.append(random.random())
vals.append(val)
# make into numpy array
vals = np.stack(vals)
print(vals.shape == (num_vecs, dimension))
# multiply every vector with every other using broadcastin
every_with_every_mult = vals[:, None] * vals[None, :]
print(every_with_every_mult.shape == (num_vecs, num_vecs, dimension))
# sum the final dimension
every_with_every_dot = np.sum(every_with_every_mult, axis=every_with_every_mult.ndim - 1)
print(every_with_every_dot.shape == (num_vecs, num_vecs))
# check it works
for i in range(num_vecs):
for j in range(num_vecs):
assert every_with_every_dot[i,j] == np.sum(vals[i]*vals[j])

How can I continue a loop inside exception handling?

I am creating a simple program that operates using Moore's Neighborhood. So given a grid, row, and a column it should return the amount of cells in the vicinity of the position that contain a 1. It works, except when given a position on the edge of the grid. Since it is checking all grids surrounding it, it throws an IndexError when it tries to check a position outside of the grid. What I want it to do is just ignore it without stopping, throwing an error, or manipulating my results, and move onto the next one. But I'm not sure how, I tried doing an exception on the IndexError but it quits out of the loop once it encounters one.
def count_neighbours(grid, row, col):
count = 0
pos = grid[row][col]
try:
for cell in [grid[row+1][col], #(0,-1) All relative to pos
grid[row-1][col], #(0,1)
grid[row+1][col+1], #(1,-1)
grid[row+1][col-1], #(-1,-1)
grid[row][col-1], #(-1,0)
grid[row][col+1], #(1,0)
grid[row-1][col+1], #(1,-1)
grid[row-1][col-1]]: #(-1,1)
if cell == 1:
count += 1
except IndexError:
pass
return count
assert count_neighbours(((1, 1, 1),
(1, 1, 1),
(1, 1, 1),), 0, 2) == 3
The loop is stopping because you are wrapping the entire loop in a try except you want something like this
def count_neighbours(grid, row, col): count = 0
pos = grid[row][col]
for cell in [[row+1,col], #(0,-1) All relative to pos
[row-1,col], #(0,1)
[row+1,col+1], #(1,-1)
[row+1,col-1], #(-1,-1)
[row,col-1], #(-1,0)
[row,col+1], #(1,0)
[row-1,col+1], #(1,-1)
[row-1,col-1]]: #(-1,1)
try:
temp_cell = grid[cell[0]][cell[1]]
if temp_cell == 1:
count += 1
except IndexError:
pass
return count
assert count_neighbours(((1, 1, 1),
(1, 1, 1),
(1, 1, 1),), 0, 2) == 3
Try a different approach, first compute the valid coords for a given point and then check for ones.
For instance you could use this function:
def compute_coords_around(x, y, boundary):
xcoords = [x-1, x, x+1]
ycoords = [y-1, y, y+1]
valid_coords = []
for xc in xcoords:
for yc in ycoords:
if xc <= boundary and yc <= boundary:
valid_coords.append((xc,yc))
return valid_coords
and lets say you want check for adjacent cells of (2, 2) in a matrix of 3x3. You know the maximum value of a column or a row is 2. So you can:
compute_coords_around(2, 2, 2)
That will give you the list:
[(1, 1), (1, 2), (2, 1), (2, 2)]
while:
compute_coords_around(1, 1, 2)
give you:
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
Then your code could be modified to:
def count_neighbours(grid, row, col):
count = 0
pos = grid[row][col]
for (x, y) in compute_coords_around(row, col, len(grid) - 1)
if grid[x][y] == 1:
count += 1
return count
You need finer-grained exception handling (and your algorithm needs to explicitly check for otherwise legal -- in Python -- indices less than zero). Here's one way to achieve both:
OFFSETS = ((-1, -1), (-1, 0), (-1, 1),
( 0, -1), ( 0, 1),
( 1, -1), ( 1, 0), ( 1, 1))
def count_neighbours(grid, row, col):
count = 0
for dr, dc in OFFSETS:
try:
x, y = row+dr, col+dc
if x < 0 or y < 0: # Disallow negative indices.
raise IndexError
if grid[x][y] == 1: # Could also cause an IndexError.
count += 1
except IndexError:
pass
return count
assert count_neighbours(((1, 1, 1),
(1, 1, 1),
(1, 1, 1),), 0, 2) == 3
However having to add an explicit check for negative indices in the inner-most loop is kind of ugly. As I mentioned in a comment, adding an extra row and column to the grid would certainly simplify the processing, as illustrated below:
OFFSETS = ((-1, -1), (-1, 0), (-1, 1),
( 0, -1), ( 0, 1),
( 1, -1), ( 1, 0), ( 1, 1))
def count_neighbours(grid, row, col):
count = 0
for dr, dc in OFFSETS:
try:
if grid[row+dr][col+dc] == 1:
count += 1
except IndexError:
pass
return count
# Note the changed position coordinate arguments.
assert count_neighbours(((0, 0, 0, 0),
(0, 1, 1, 1),
(0, 1, 1, 1),
(0, 1, 1, 1),), 1, 3) == 3

Distance of Robot from Starting Point after a Series of Movements

I am trying to write a program which takes a list of directions and magnitudes and outputs the distance of the robot from its starting position.
I get an error when executing the following code but I cannot identify why I get the error.
import math
position = [0,0]
direction = ['+Y','-X','-Y','+X','-X','-Y','+X']
magnitude = [9,7,4,8,3,6,2]
i = 0
while i < len(direction):
if direction[i] == '+Y': position[0] += magnitude[i]
elif direction[i] == '-Y': position[0] -= magnitude[i]
elif direction[i] == '+X': position[1] += magnitude[i]
elif direction[i] == '-X': position[1] -= magnitude[i]
else: pass
i += 1
print float(math.sqrt(position[1]**2+position[0]**2))
Edit:
I get this error:
IndentationError: unindent does not match any outer indentation level
Most probably you have mixed up your spaces and tabs. In this instance, it might be easier to put the sign within the magnitude and filter with x and y like so:
In [15]: mDr = [ (int(d[0]+m), d[1]) for (d, m) in zip(direction, map(str, magnitude))]
In [16]: mDr
Out[16]: [(9, 'Y'), (-7, 'X'), (-4, 'Y'), (8, 'X'), (-3, 'X'), (-6, 'Y'), (2, 'X')]
In this case, you can get to total x and y distances pretty easily. For example, the y distances:
In [17]: [md[0] for md in mDr if md[1] =='Y']
Out[17]: [9, -4, -6]
And the total y distance in the particular direction:
In [18]: sum( [md[0] for md in mDr if md[1] =='Y'] )
Out[18]: -1
You can do the same for x and then calculate the distance that way.
Here comes my offtopic reaction (your problem was mixing tabs and spaces, my answer is simple rewrite).
import math
xymoves = {"+X": (1, 0), "-X": (-1, 0), "+Y": (0, 1), "-Y": (0, -1)}
position = [0, 0]
directions = ['+Y', '-X', '-Y', '+X', '-X', '-Y', '+X']
assert all(xymove in xymoves for xymove in directions)
magnitudes = [9, 7, 4, 8, 3, 6, 2]
for direction, magnitude in zip(directions, magnitudes):
xmove, ymove = xymoves[direction]
position[0] += magnitude * xmove
position[1] += magnitude * ymove
print math.sqrt(position[1]**2+position[0]**2)
Changes:
looping using for and not while with incrementing index.
the logic "where to move" moved from if elif elif into dictionary xymoves
rejecting to process direction, which is not expected
math.sqrt always returns float, so conversion to float removed
Note, that the dictionary with xymoves could be extended with other directions, e.g. using "N" for North, "NE" for North-East etc.

Add tuple to list of tuples in Python

I am new to python and don't know the best way to do this.
I have a list of tuples which represent points and another list which represents offsets. I need a set of all the combinations that this forms.
Here's some code:
offsets = [( 0, 0),( 0,-1),( 0, 1),( 1, 0),(-1, 0)]
points = [( 1, 5),( 3, 3),( 8, 7)]
So my set of combined points should be
[( 1, 5),( 1, 4),( 1, 6),( 2, 5),( 0, 5),
( 3, 3),( 3, 2),( 3, 4),( 4, 3),( 2, 3),
( 8, 7),( 8, 6),( 8, 8),( 9, 7),( 7, 7)]
I'm not able to use NumPy or any other libraries.
result = [(x+dx, y+dy) for x,y in points for dx,dy in offsets]
For more, see list comprehensions.
Pretty simple:
>>> rslt = []
>>> for x, y in points:
... for dx, dy in offsets:
... rslt.append( (x+dx, y+dy) )
...
>>> rslt
[(1, 5), (1, 4), (1, 6), (2, 5), (0, 5), (3, 3), (3, 2), (3, 4), (4, 3), (2, 3), (8, 7), (8, 6), (8, 8), (9, 7), (7, 7)]
Cycle through the points and the offsets, then build new tuples of adding the offsets to the points.
Personally, I like Alok's answer. However, for fans of itertools, the itertools-based equivalent (in Python 2.6 and later) is:
import itertools as it
ps = [(x+dx, y+dy) for (x, y), (dx, dy) in it.product(points, offsets)]
However, in this case the itertools solution is not faster than the simple one (it's actually a tad slower because it needs to unpack each x, y repeatedly for every offset, while Alok's simple approach unpacks each x, y but once). Still, itertools.product is an excellent alternative to nested loops in other cases, so, it's worth knowing about it!-)
If you don't care about duplicates in the result:
result = []
for ox, oy in offsets:
for px, py in points:
result.append((px + ox, py + oy))
If you do care about duplicates in the result:
result = set()
for ox, oy in offsets:
for px, py in points:
result.add((px + ox, py + oy))

Categories

Resources