2016-12-06 6 views
0

Ich bin ein Anfänger in Python und mache ein Brettspiel, bei dem der Spieler eingibt, wie viele Plätze er nach links/rechts bewegen möchte usw. Manchmal bewegt er sich jedoch richtig und manchmal nicht. Ich denke, es hat etwas mit meinem Grid-Array zu tun, aber ich bin mir nicht sicher ... Wäre toll, wenn mir jemand helfen könnte, danke!Warum funktioniert meine Array-Bearbeitung nicht korrekt?

choice=0 
b=0 
oldP=0 
newP=0 
player_location='X' 
x=8 
y=0 
xi=0 
yi=0 
up=0 
down=0 
left=0 
right=0 
new_board=[xi][yi] 
gold_coins=0 
bandits=5 
treasure_chests=10 
a=1 
xi2=0 
yi2=0 

import random 
def menu(): 
    print('If you would like to play the Treasure Hunt , press 1') 
    choice=input('If not, press any key to exit') 
    if choice=='1': 
     print('Great! You have made the right choice :)') 
    else: 
     print('Goodbye.') 
     quit() 
menu() 
def grid(): 
    new_board = [ ] 

def board(): 
    new_board = [ ] 
    top_row = [' 1 ',' 2 ',' 3 ',' 4 ',' 5 ',' 6 ',' 7 ',' 8 '] 

    new_board.append(top_row) 

    for x in range(0,8): 
    row = [' 0 ']*8 
    new_board.append(row) 
    return new_board 

def print_board(b): 
    row_letters = [' ','A','B','C','D','E','F','G','H'] 
    i = 0 
    for row in b: 
    print (row_letters[i],''.join(row)) 
    i=i+1 
new_board = board() 
xi=int(8) 
yi=int(0) 
new_board[xi][yi] = player_location 
print_board(new_board) 
while a==1: 
    upordown=input('Would you like to move up or down? Enter \'u\' for up or \'d\' for down.').lower() 
    upordown=upordown.lower() 
    while not (upordown== 'u' or upordown== 'd'): 
     print('Invalid input. Please try again') 
     upordown= input().lower() 
    while upordown=='u': 
     try: 
      up=int(input('How many spaces would you like to move up?')) 
      b=0 
     except: 
      print('This is not a valid input, please enter a number.') 
      b=1 
     if b==0: 
      break 
    while upordown=='d': 
     try: 
      down=int(input('How many spaces would you like to move down?')) 
      b=0 
     except: 
      print('This is not a valid input, please enter a number.') 
      b=1 
     if b==0: 
      break 
    leftorright=input('Would you like to move left or right Enter \'l\' for left or \'r\' for right.').lower() 
    leftorright.lower() 
    while not (leftorright== 'l' or leftorright== 'r'): 
     print('Invalid input. Please try again') 
     leftorright= input().lower() 
    while leftorright=='l': 
     try: 
      left=int(input('How many spaces would you like to move left?')) 
      b=0 
     except: 
      print('This is not a valid input, please enter a number.') 
      b=1 
     if b==0: 
      break  
    while leftorright=='r': 
     try: 
      right=int(input('How many spaces would you like to move right?')) 
      b=0 
     except: 
      print('This is not a valid input, please enter a number.') 
      b=1 
     if b==0: 
      break 
    print('Okay...') 

    grid() 
    while True: 
     board() 
     player_location=' X ' 
     if upordown=='d' and leftorright=='l': 
      new_board[y-down][x-left] = player_location 
      new_board = board() 
      xi2=int(xi2-left) 
      yi2=int(yi2-down) 
      print ("The current x location is",xi) 
      print ("The current y location is",yi) 
      print ("The amount you chose to go down was",down) 
      print ("The amount you chose to go right was",left) 
      xi = int(xi+left) 
      print("The new x location is",xi2) 
      yi = int(yi+down) 
      print("The new y location is",yi2) 
      print(' ') 
      while 0>xi2 or xi2>8 or 0>yi2 or yi2>8: 
       print('Your move was illegal. Please enter a move that will position you within the grid') 
       left=int(input('Please enter the number of moves you would like to move right')) 
       down=int(input('Please enter the number of moves you would like to move up.')) 
       xi=int(xi+left) 
       yi=int(yi+down) 
      new_board[xi][yi] = player_location 
      print_board(new_board) 
      break 
     elif upordown=='d' and leftorright=='r': 
      new_board = board() 
      xi2=int(xi2+right) 
      yi2=int(yi2-down) 
      print ("The current x location is",xi) 
      print ("The current y location is",yi) 
      print ("The amount you chose to go down was",down) 
      print ("The amount you chose to go right was",right) 
      xi = int(xi+right) 
      print("The new x location is",xi2) 
      yi = int(yi+down) 
      print("The new y location is",yi2) 
      print(' ') 
      while 0>xi2 or xi2>8 or 0>yi2 or yi2>8: 
       print('Your move was illegal. Please enter a move that will position you within the grid') 
       right=int(input('Please enter the number of moves you would like to move right')) 
       down=int(input('Please enter the number of moves you would like to move down.')) 
       xi=int(xi+right) 
       yi=int(yi+down) 
      new_board[xi][yi] = player_location 
      print_board(new_board) 
      break 
     elif upordown=='u' and leftorright=='l': 
      new_board = board() 
      xi2=int(xi2+left) 
      yi2=int(yi2-up) 
      print ("The current x location is",xi) 
      print ("The current y location is",yi) 
      print ("The amount you chose to go up was",up) 
      print ("The amount you chose to go left was",left) 
      xi = int(xi+left) 
      print("The new x location is",xi2) 
      yi = int(yi-up) 
      print("The new y location is",yi2) 
      print(' ') 
      while 0>xi2 or xi2>8 or 0>yi2 or yi2>8: 
       print('Your move was illegal. Please enter a move that will position you within the grid') 
       left=int(input('Please enter the number of moves you would like to move right')) 
       up=int(input('Please enter the number of moves you would like to move up.')) 
       xi=int(xi-left) 
       yi=int(yi-up) 
      new_board[xi][yi] = player_location 
      print_board(new_board) 
      break 
     elif upordown=='u' and leftorright=='r': 
      new_board = board() 
      xi2=int(xi2+right) 
      yi2=int(yi2+up) 
      print ("The current x location is",xi) 
      print ("The current y location is",yi) 
      print ("The amount you chose to go up was",up) 
      print ("The amount you chose to go right was",right) 
      xi = int(xi-right) 
      print("The new x location is",xi2) 
      yi = int(yi+up) 
      print("The new y location is",yi2) 
      print(' ') 
      while 0>xi2 or xi2>8 or 0>yi2 or yi2>8: 
       print('Your move was illegal. Please enter a move that will position you within the grid') 
       right=int(input('Please enter the number of moves you would like to move right')) 
       up=int(input('Please enter the number of moves you would like to move up.')) 
       xi=int(xi-right) 
       yi=int(yi-up) 
      new_board[xi][yi] = player_location 
      print_board(new_board) 
      break 

ich angebracht habe alle meine Code einschließlich der Überprüfung, so dass der Benutzer das Gitter nicht entkommen, da ich nicht sicher bin, welche Teile enthalten, um mein Problem und Programm klar zu machen.

+1

_ "Ich bin ein Anfänger in Python ..." _ - Stop! Sie müssen ein paar Schritte zurückgehen. Ein Projekt wie dieses ist nicht etwas, was ein Anfänger versuchen sollte. Versuchen Sie etwas wie Tic-Tac-Toe zu machen und dann nach oben zu gehen. –

+0

@leaf Definitiv wahr, ein bisschen abenteuerlich! Jedoch habe ich dies als eine Anfänger-Herausforderung auf einer Website gesehen und hoffe wirklich, es zu vervollständigen – user5095215

+0

Definieren Sie "Anfänger". Bedeutet Anfänger wie in _Just download Python_, oder wie in _Ich habe Python seit ein paar Monaten jetzt gemacht _ Anfänger. Gerade jetzt, das größte Problem, das ich mit Ihrem Code sehe, ist, dass es sehr ausführlich ist. –

Antwort

2

Praxis Verbesserungen:

Es ist eine wunderbare Einführung here

ich empfehlen, inkrementeller Programmierung zu debuggen: Sie ein paar Zeilen zu schreiben, stellen Sie sicher, dass sie arbeiten, und nicht weiter, bis Sie das haben bestehender Code korrekt Auf diese Weise, wenn etwas nicht funktioniert, weißt du, dass es in den letzten Zeilen ist, die du hinzugefügt hast. Andernfalls erhalten Sie 200 Zeilen Code und nicht viel Ahnung, wo der Fehler liegt.

Programmänderungen

Dieser Code ist für die Fehlersuche und Wartung nicht entwickelt; Sie sollten diese Probleme beheben, bevor Sie fortfahren. Vor allem haben Sie zwei Sätze von Eingaben erstellt, die eine Aufgabe ausführen (die Bewegung des Benutzers abrufen) und dann vier Codeblöcke, die dieselbe Aufgabe ausführen (den Benutzer verschieben).

Stattdessen finden Sie einige brauchbare Formulierung, die der Spieler die Bewegung auf eine Zeile eingeben muss. Sie wandeln das in Bewegungskoordinaten um (wie [1, -2] für "eins nach oben und links zwei") und verwenden dann einen einzelnen Codeblock, um sich durch das Gitter zu bewegen. Anstatt vier Codeblöcke zu testen, testen Sie einen; Wenn Sie eine Änderung vornehmen müssen, machen Sie eine Änderung, nicht vier Kopien unter Rotation und Reflexion.

Ergebnis Der Code, den Sie mit leben soll wie folgt aussehen:

def get_move(): 
    advice = "Please enter your move in two integers:\n" + 
       "vertical, then horizontal. Use positive numbers\n" + 
       "for up and right, negative for down and left.\n" 
    example = "For instance, the line \n\t2 -1\n" + 
       " is 2 spaces up, one space left." 
    move = input(advice + example).split 
    x_move = int(move[0]) 
    y_move = int(move[1]) 
    # Here, you check move legality; repeat until you get a legal move 

    return x_move, y_move 

while True: 
    print_board() 
    x_move, y_move = get_move() # get_move contains the input interaction and data checking 
    move(x_move, y_move) # perform the move; update the board. 

Beachten Sie auch, dass Sie wahrscheinlich das bestehende Board aktualisieren sollen, anstatt (scheinbar) für jede Bewegung einen neuen erstellt. Bleiben Sie in dieser Schleife, bis das Spiel vorbei ist; brich nicht bei jeder Bewegung aus.

Ist das eine Lösung?

+0

Vielen Dank für die Ratschläge und Tipps. Ich werde definitiv diese Einführung in das Debugging lesen, und das hat mich sicherlich zu einer Lösung bewegt! – user5095215

+0

Großartig. Wenn Sie zu einer Lösung kommen, denken Sie bitte daran, hierher zu kommen, die hilfreichen Dinge zu bewerten und eine Antwort zu akzeptieren - selbst wenn Sie selbst eine schreiben müssen. Dadurch kann Stack Overflow die betreffende Aufgabe ordnungsgemäß beenden und archivieren. – Prune

Verwandte Themen