Showing posts with label Board game. Show all posts
Showing posts with label Board game. Show all posts

Sunday, June 19, 2016

Artificial Intelligence using python

The below code is to reach a goal in a board game with robot at position with value 'r' and goal with value 'g' in the row* column board.

Below are the conventions for the code to solve the problem.


m - size of grid
grid - position of the board(in row * col)
g - goal position
r - robot current postion in the board


#!/bin/python
def find_m_position(grid):
for row in range(m):
for col in range(m):
if grid[row][col] == 'r':
return (row,col)

def find_p_postion(grid):
for row in range(m):
for col in range(m):
if grid[row][col] == 'g':
return (row, col)


def PathtoGoal(n,grid):
#print all the moves here
#pass
row_m, col_m = find_m_position(grid)
row_p, col_p = find_p_postion(grid)

while((row_m != row_p) and (col_m != col_p)):
diff_row = row_m - row_p
diff_col = col_m - col_p
if(diff_row < 0):
print "DOWN"
row_m = row_m + 1


else:
print "UP"
row_m = row_m - 1

if(diff_col > 0):
print "LEFT"
col_m = col_m - 1
else:
print "RIGHT"
col_m = col_m + 1


return None

m = input()

grid = []
for i in xrange(0, m):
grid.append(raw_input().strip())

#print grid

PathtoGoal(m,grid)