2013-10-24 5 views
0

파이어 엠블렘과 같은 파이어 게임을 파이 게임으로 프로그래밍하고 있지만 플레이어의 움직임에 문제가 있습니다. 작동 방식은 플레이어를 선택하면 파란색으로 강조 표시된 모든 허용 된 동작이 표시되지만 내 문제는 가능한 모든 동작에 대한 목록이 표시되는 데 문제가 있다는 것입니다. 난 당신이 마음에 무엇을 모르기 때문에회전 기반 이동 알고리즘

def showPerson(tilex, tiley, personAtTile): 
global ALLOWEDMOVES 
ALLOWEDMOVES = [] 
prepare = {k:v for v,k in PLAYERSPOSITION.items()} 
z = PLAYERDISTANCE[personAtTile] 
#get all coords for the possible moves 
currentNewSpots = [] 
oldSpots = [] 
a = PLAYERSPOSITION[personAtTile][0] 
b = PLAYERSPOSITION[personAtTile][1] 
c = a + 1 
test = findTileLetter(c, b) 
if test: 
    currentNewSpots.append((c, b)) 
    ALLOWEDMOVES.append((c, b)) 
c = a -1 
test = findTileLetter(c, b) 
if test: 
    currentNewSpots.append((c, b)) 
    ALLOWEDMOVES.append((c, b)) 
c = b + 1 
test = findTileLetter(a, c) 
if test: 
    currentNewSpots.append((a, c)) 
    ALLOWEDMOVES.append((a, c)) 
c = b - 1 
test = findTileLetter(a, c) 
if test: 
    currentNewSpots.append((a, c)) 
    ALLOWEDMOVES.append((a, c)) 

for x in range(PLAYERDISTANCE[prepare[(tilex, tiley)]]): 
    for y in range(len(currentNewSpots) - 1): 
     a = currentNewSpots[y][0] 
     b = currentNewSpots[y][1] 
     c = a + 1 
     test = findTileLetter(c, b) 
     if test and ((c, b)) not in ALLOWEDMOVES: 
      currentNewSpots.append((c, b)) 
      ALLOWEDMOVES.append((c, b)) 
     c = a -1 
     test = findTileLetter(c, b) 
     if test and ((c, b)) not in ALLOWEDMOVES: 
      currentNewSpots.append((c, b)) 
      ALLOWEDMOVES.append((c, b)) 
     c = b + 1 
     test = findTileLetter(a, c) 
     if test and ((c, b)) not in ALLOWEDMOVES: 
      currentNewSpots.append((a, c)) 
      ALLOWEDMOVES.append((a, c)) 
     c = b - 1 
     test = findTileLetter(a, c) 
     if test and ((c, b)) not in ALLOWEDMOVES: 
      currentNewSpots.append((a, c)) 
      ALLOWEDMOVES.append((a, c)) 
+1

"허용 된"동작에 대한 기준을 이해하지 못합니다. –

+0

Welcome to Stack Overflow! 파이썬 질문을했지만 코드가 제대로 들여 쓰기가되지 않았습니다. 공백은 파이썬에서 구문 적으로 중요하며, 들여 쓰기가 적절하지 않으면 코드가하는 일을 알 수 없으므로 도움을 줄 수 없습니다. 질문을 수정하고 들여 쓰기를 수정하십시오. 감사! –

+0

나는이 게임이 길 찾기를 사용한다고 믿는다. 길 찾기에 대한 훌륭한 소개 : http://theory.stanford.edu/~amitp/GameProgramming/ – ninMonkey

답변

1

, 나는 다음과 같은 가정하에 진행됩니다 : (x, y)의 광장이 무난 경우

  • findTileLetter(x, y)가 나에게 말한다. 즉, 유닛이 그 사각형을 통과하거나 끝낼 수 있는지를 알려줍니다.
  • 단위는 한 턴에 PLAYERDISTANCE[unit] + 1 번까지 올라갈 수 있습니다. 각 단계를 위, 아래, 왼쪽 또는 오른쪽으로 가져갈 수 있습니다. 대각선 단계는 허용되지 않지만 대신 단계별로 수행해야합니다. 왼쪽과 위로. 그걸 염두에두고

, 우리는주의 당신이 이렇게 적은 개 이상의 currentNewSpots의 요소 당신이해야보다 당신이있는 요소에서 스텝을 생략 할 모든 x 루프를 반복

for y in range(len(currentNewSpots) - 1): 

라인에 currentNewSpots이 마지막으로의 앞에 x 루프가 추가되었습니다. 따라서 잠재적 인 목적지 사각형을 남겨 두십시오.

for y in range(len(currentNewSpots)) 

수정이 문제에 라인을 변경.

또한, y 루프에 델타 Y 시험은 맞지 않아요 : 테스트 코드를 작업하는 다음과

c = b + 1 
    test = findTileLetter(a, c) 
    if test and ((c, b)) not in ALLOWEDMOVES: ### <--- should be (a, c) 
     currentNewSpots.append((a, c)) 
     ALLOWEDMOVES.append((a, c)) 

물방울의. grid 타일의 세계를 정의합니다 : 0 타일은 통과 할 수 있지만 1 타일은 통과 할 수 있습니다. MY_XMY_Y은 검색 할 위치를 정의합니다. 매 단계마다 맵을 표준 출력으로 출력하여 지금까지 발견 한 사각형을 보여줍니다.

import sys 

MY_X = 3 
MY_Y = 4 
MY_RNG = 2 

grid = [ 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], 
    [0, 0, 1, 1, 1, 1, 0, 0, 1, 0], 
    [0, 1, 1, 1, 1, 1, 1, 1, 1, 1], 
    [0, 0, 1, 1, 0, 1, 1, 0, 0, 1], 
    [0, 0, 1, 1, 1, 1, 1, 0, 0, 0], 
    [0, 0, 0, 0, 1, 1, 1, 1, 1, 0], 
    [0, 0, 0, 1, 1, 1, 1, 0, 0, 0], 
    [0, 1, 1, 1, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
] 

def findTileLetter(x, y): 
    return grid[y][x] 

class Person: 
    pass 

def showMap(): 
    for y in range(len(grid)): 
     for x in range(len(grid[y])): 
      if grid[y][x] == 0: 
       sys.stdout.write(' ') 
      elif x == MY_X and y == MY_Y: 
       sys.stdout.write('x') 
      elif (x, y) in ALLOWEDMOVES: 
       sys.stdout.write('o') 
      else: 
       sys.stdout.write('-') 
     sys.stdout.write('\n') 

me = Person() 

ALLOWEDMOVES = [] 

PLAYERDISTANCE = {} 
PLAYERDISTANCE[me] = MY_RNG 

PLAYERSPOSITION = {} 
PLAYERSPOSITION[me] = (MY_X, MY_Y) 

def showPerson(tilex, tiley, personAtTile): 
    global ALLOWEDMOVES 
    ALLOWEDMOVES = [] 
    prepare = {k:v for v,k in PLAYERSPOSITION.items()} 
    z = PLAYERDISTANCE[personAtTile] 
    #get all coords for the possible moves 
    currentNewSpots = [] 
    oldSpots = [] 
    a = PLAYERSPOSITION[personAtTile][0] 
    b = PLAYERSPOSITION[personAtTile][1] 
    c = a + 1 
    test = findTileLetter(c, b) 
    if test: 
     currentNewSpots.append((c, b)) 
     ALLOWEDMOVES.append((c, b)) 
    c = a -1 
    test = findTileLetter(c, b) 
    if test: 
     currentNewSpots.append((c, b)) 
     ALLOWEDMOVES.append((c, b)) 
    c = b + 1 
    test = findTileLetter(a, c) 
    if test: 
     currentNewSpots.append((a, c)) 
     ALLOWEDMOVES.append((a, c)) 
    c = b - 1 
    test = findTileLetter(a, c) 
    if test: 
     currentNewSpots.append((a, c)) 
     ALLOWEDMOVES.append((a, c)) 

    showMap() 

    for x in range(PLAYERDISTANCE[prepare[(tilex, tiley)]]): 
     for y in range(len(currentNewSpots)): 
      a = currentNewSpots[y][0] 
      b = currentNewSpots[y][1] 
      c = a + 1 
      test = findTileLetter(c, b) 
      if test and ((c, b)) not in ALLOWEDMOVES: 
       currentNewSpots.append((c, b)) 
       ALLOWEDMOVES.append((c, b)) 
      c = a - 1 
      test = findTileLetter(c, b) 
      if test and ((c, b)) not in ALLOWEDMOVES: 
       currentNewSpots.append((c, b)) 
       ALLOWEDMOVES.append((c, b)) 
      c = b + 1 
      test = findTileLetter(a, c) 
      if test and ((a, c)) not in ALLOWEDMOVES: 
       currentNewSpots.append((a, c)) 
       ALLOWEDMOVES.append((a, c)) 
      c = b - 1 
      test = findTileLetter(a, c) 
      if test and ((a, c)) not in ALLOWEDMOVES: 
       currentNewSpots.append((a, c)) 
       ALLOWEDMOVES.append((a, c)) 
     showMap() 

showPerson(MY_X, MY_Y, me) 
print ALLOWEDMOVES 
+0

와우 그 간단한 작은 실수는 내 모든 문제를 고쳤습니다, 감사합니다!:) 나는 거의 일주일 동안이 작업을하고 있었고 코드를 여러 번 검토 한 후에 매우 혼란 스러웠습니다. 온라인에서 새로운 솔루션을 찾으려고 노력한 결과, 꽤 피곤해졌습니다. 다시 한 번 감사드립니다. – KodyVanRy