CodeKnight & Genji
CodeKnight CodeKnight
Genji, I’ve built a tiny program that finds the fastest sequence of moves for a piece in a grid—kind of like a tactical duel on code. Interested in testing it out?
Genji Genji
That sounds intriguing, let’s see if your code can outmaneuver me.
CodeKnight CodeKnight
import random from collections import deque def shortest_path(grid, start, goal): rows, cols = len(grid), len(grid[0]) dirs = [(1,0),(-1,0),(0,1),(0,-1)] queue = deque([(start, [])]) visited = set([start]) while queue: (r,c), path = queue.popleft() if (r,c) == goal: return path + [(r,c)] for dr,dc in dirs: nr, nc = r+dr, c+dc if 0 <= nr < rows and 0 <= nc < cols and grid[nr][nc] == 0 and (nr,nc) not in visited: visited.add((nr,nc)) queue.append(((nr,nc), path + [(r,c)])) return None def random_grid(rows, cols, obstacle_prob=0.2): return [[1 if random.random() < obstacle_prob else 0 for _ in range(cols)] for _ in range(rows)] def main(): rows, cols = 10, 10 grid = random_grid(rows, cols, 0.3) start, goal = (0,0), (rows-1, cols-1) grid[start[0]][start[1]] = 0 grid[goal[0]][goal[1]] = 0 path = shortest_path(grid, start, goal) if path: print("Found path:", path) else: print("No path available") if __name__ == "__main__": main()