变量设置不正确?

时间:2016-06-16 00:17:26

标签: python python-2.7

任何人都知道我的代码有什么问题?当我运行它时,我一直收到一个空的source变量(AKA充满空格):(如果有帮助,我在Linux上运行2.7.11,尝试运行Befunge-93程序接收一次一个字符)

import readchar

RIGHT = 0
DOWN = 1
LEFT = 2
UP = 3

direction = RIGHT
string_mode = False
position = [0,0]
source = [[" " for j in range(80)] for i in range(25)]
def delta(direct):
  if direct == RIGHT:
    return [1,0]
  elif direct == LEFT:
    return [-1,0]
  elif direct == UP:
    return [0,1]
  elif direct == DOWN:
    return [0,-1]
  else:
    raise "WTF, GO AND FIX THIS!"
def get_input():
  try:
    return raw_input()
  except:
    return input()

def get_chr(i):
  try:
    return unichr(i)
  except:
    return chr(i)
class Stack:
  def __init__(self):
    self.value = []
  def pop(self):
    if len(self.value) == 0:
      return 0
    return self.value.pop()
  def push(self,value):
    self.value.append(value)

class Program(Stack):
  def PushZero(self): self.push(0)
  def PushOne(self): self.push(1)
  def PushTwo(self): self.push(2)
  def PushThree(self): self.push(3)
  def PushFour(self): self.push(4)
  def PushFive(self): self.push(5)
  def PushSix(self): self.push(6)
  def PushSeven(self): self.push(7)
  def PushEight(self): self.push(8)
  def PushNine(self): self.push(9)
  def Add(self):
    a = self.pop()
    b = self.pop()
    self.push(b+a)
  def Subtract(self):
    a = self.pop()
    b = self.pop()
    self.push(b-a)
  def Multiply(self):
    a = self.pop()
    b = self.pop()
    self.push(b*a)
  def Divide(self):
    a = self.pop()
    b = self.pop()
    while a == 0:
      a = int(get_input())
    self.push(int(b/a))
  def Modulo(self):
    a = self.pop()
    b = self.pop()
    while a == 0:
      a = int(get_input())
    self.push(b%a)
  def Not(self):
    a = self.pop()
    if a == 0:
      self.push(1)
    else:
      self.push(0)
  def Greater(self):
    a = self.pop()
    b = self.pop()
    if b > a:
      self.push(1)
    else:
      self.push(0)
  def MoveLeft(self):
    global direction
    direction = LEFT
  def MoveRight(self):
    global direction
    direction = RIGHT
  def MoveUp(self):
    global direction
    direction = UP
  def MoveDown(self):
    global direction
    direction = DOWN
  def MoveRandom(self):
    global direction
    import random
    direction = random.choice([LEFT,RIGHT,UP,DOWN])
  def HorizontalIf(self):
    a = self.pop()
    if a == 0:
      self.MoveRight()
    else:
      self.MoveLeft()
  def VerticalIf(self):
    a = self.pop()
    if a == 0:
      self.MoveDown()
    else:
      self.MoveUp()
  def StringMode(self):
    global string_mode
    string_mode = 2
  def Duplicate(self):
    a = self.pop()
    self.push(a)
    self.push(a)
  def Swap(self):
    a = self.pop()
    b = self.pop()
    self.push(a)
    self.push(b)
  def Pop(self):
    self.pop()
  def OutInt(self):
    import sys
    a = self.pop()
    sys.stdout.write(a)
  def OutAscii(self):
    import sys
    a = self.pop()
    sys.stdout.write(get_chr(a))
  def Bridge(self):
    global position
    x = (position[0]+delta(direction)[0]) % 80
    y = (position[1]+delta(direction)[0]) % 25
    position = [x,y]
  def Get(self):
    y = self.pop()
    x = self.pop()
    if not ((0 <= x < 80) and (0 <= y < 25)):
      self.push(0)
    else:
      self.push(ord(source[x][y]))
  def Put(self):
    y = self.pop()
    x = self.pop()
    v = self.pop()
    if not ((0 <= x < 80) and (0 <= y < 25)):
      raise "FIX THIS YOU IDIOT!"
    source[x][y] = get_chr(v)
  def AskInt(self):
    s = int(get_input())
    self.push(s)
  def AskAscii(self):
    s = readchar.readchar()
    self.push(ord(s))
  def Terminate(self):
    import sys
    sys.exit()

bf = Program()
def make_fit(s):
  if len(s) > 80:
    return list(s[0:80])
  elif len(s) < 80:
    return make_fit(s+" ")
  else:
    return make_fit(s)
def remove_newline(s):
  return s[:-1]
def read_file(name):
  global source
  s = []
  f = open(name,"r")
  for dummy_i in range(25):
    try:
      s.append(make_fit(remove_newline(f.readline())))
    except:
      s.append([" "]*80)
  f.close()
  source = s

def current_char():
  return source[position[0]][position[1]]

def move():
  position[0]+=delta(direction)[0]
  position[0] %= 80
  position[1]+=delta(direction)[1]
  position[1] % 25

def step():
  global string_mode
  if not string_mode:
    if current_char()=="0":
      bf.PushZero()
    elif current_char()=="1":
      bf.PushOne()
    elif current_char()=="2":
      bf.PushTwo()
    elif current_char()=="3":
      bf.PushThree()
    elif current_char()=="4":
      bf.PushFour()
    elif current_char()=="5":
      bf.PushFive()
    elif current_char()=="6":
      bf.PushSix()
    elif current_char()=="7":
      bf.PushSeven()
    elif current_char()=="8":
      bf.PushEight()
    elif current_char()=="9":
      bf.PushNine()
    elif current_char()=="+":
      bf.Add()
    elif current_char()=="-":
      bf.Minus()
    elif current_char()=="*":
      bf.Multiply()
    elif current_char()=="/":
      bf.Divide()
    elif current_char()=="%":
      bf.Modulo()
    elif current_char()=="!":
      bf.Not()
    elif current_char()=="`":
      bf.Greater()
    elif current_char()==">":
      bf.MoveRight()
    elif current_char()=="<":
      bf.MoveLeft()
    elif current_char()=="v":
      bf.MoveDown()
    elif current_char()=="^":
      bf.MoveUp()
    elif current_char()=="?":
      bf.Random()
    elif current_char()=="_":
      bf.HorizontalIf()
    elif current_char()=="|":
      bf.VerticalIf()
    elif current_char()=='"':
      string_mode=True
    elif current_char()==":":
      bf.Duplicate()
    elif current_char()=="\\":
      bf.Swap()
    elif current_char()=="$":
      bf.Pop()
    elif current_char()==".":
      bf.OutInt()
    elif current_char()==",":
      bf.OutAscii()
    elif current_char()=="#":
      bf.Bridge()
    elif current_char()=="g":
      bf.Get()
    elif current_char()=="p":
      bf.Put()
    elif current_char()=="@":
      bf.Terminate()
    elif current_char()=="&":
      bf.AskInt()
    elif current_char()=="~":
      bf.AskAscii()
    else:
      1 + 1 == 2
  else:
    if current_char()=='"':
      string_mode = False
    bf.push(ord(current_char()))
  bf.Bridge()
def main_loop(program_name):
  global source
  read_file(program_name)
  while True:
    step()
main_loop("test.txt")

这个位置似乎在对角线向下和向右移动,我试过看,但无济于事。

2 个答案:

答案 0 :(得分:0)

问题在第194行:

def current_char():
  return source[position[0]][position[1]]

你正在对待这个,因为你确定的位置不是None,位置[0]和位置[1]不是None,但是python并不知道这个,并且那里有&#39;没有价值,所以你必须在返回前检查位置,位置[0]和位置[1]是否为无

答案 1 :(得分:0)

我认为你的问题出在move函数中,你可以在你的位置列表中添加一些值,但是你没有更正它仍然在范围内

在原始功能中

def move():
    position[0]+=delta(direction)[0] % 80
    position[1]+=delta(direction)[1] % 25

您只需更正增量并将该值添加到前一个值,最终将为您提供超出范围的值。由于delta函数只生成1,0,-1的值,前两个在模数下保持不变,最后一个是n-1(n = 80或25),并且结果被添加到您的位置列表中而没有任何纠正是导致问题的原因。

例如说你的位置是[0,24],你的delta是[0,-1],你当前的move下你会得到

->position[1]+= -1 % 25
->position[1]+= 24
==> position == [0,48]

解决方案是确保位置中的值始终保持在范围

def move():
    dx,dy = delta(direction) # to just call the function once
    position[0]= (position[0] + dx) % 80
    position[1]= (position[1] + dy) % 25