2014-03-26 53 views
0

我正在研究一个较大的项目来编写代码,以便用户可以在计算机上播放Connect 4。现在,用户可以选择是否先走,然后绘制板。为了确保用户只能输入合法的动作,我遇到了一个问题,我的函数legal_moves()需要1个位置参数,并且给出了0,但我不明白我需要做什么, 。位置参数未定义

#connect 4 
#using my own formating 

import random 

#define global variables 
X = "X" 
O = "O" 
EMPTY = "_" 
TIE = "TIE" 
NUM_ROWS = 6 
NUM_COLS = 8 

def display_instruct(): 
    """Display game instructions.""" 
    print(
    """ 
    Welcome to the second greatest intellectual challenge of all time: Connect4. 
    This will be a showdown between your human brain and my silicon processor. 

    You will make your move known by entering a column number, 1 - 7. Your move 
    (if that column isn't already filled) will move to the lowest available position. 

    Prepare yourself, human. May the Schwartz be with you! \n 
    """ 
    ) 

def ask_yes_no(question): 
    """Ask a yes or no question.""" 
    response = None 
    while response not in ("y", "n"): 
     response = input(question).lower() 
    return response 

def ask_number(question,low,high): 
    """Ask for a number within range.""" 
    #using range in Python sense-i.e., to ask for 
    #a number between 1 and 7, call ask_number with low=1, high=8 
    low=1 
    high=NUM_COLS 
    response = None 
    while response not in range (low,high): 
     response=int(input(question)) 
    return response 

def pieces(): 
    """Determine if player or computer goes first.""" 
    go_first = ask_yes_no("Do you require the first move? (y/n): ") 
    if go_first == "y": 
     print("\nThen take the first move. You will need it.") 
     human = X 
     computer = O 
    else: 
     print("\nYour bravery will be your undoing... I will go first.") 
     computer = X 
     human = O 
    return computer, human 

def new_board(): 
    board = [] 
    for x in range (NUM_COLS): 
     board.append([" "]*NUM_ROWS) 
    return board 

def display_board(board): 
    """Display game board on screen.""" 
    for r in range(NUM_ROWS): 
     print_row(board,r) 
    print("\n") 

def print_row(board, num): 
    """Print specified row from current board""" 
    this_row = board[num] 
    print("\n\t| ", this_row[num], "|", this_row[num], "|", this_row[num], "|", this_row[num], "|", this_row[num], "|", this_row[num], "|", this_row[num],"|") 
    print("\t", "|---|---|---|---|---|---|---|") 

# everything works up to here! 

def legal_moves(board): 
    """Create list of column numbers where a player can drop piece""" 
    legals = [] 
    if move < NUM_COLS: # make sure this is a legal column 
     for r in range(NUM_ROWS): 
      legals.append(board[move]) 
    return legals #returns a list of legal columns 
    #in human_move function, move input must be in legal_moves list 
    print (legals) 

def human_move(board,human): 
    """Get human move""" 
    legals = legal_moves(board) 
    print("LEGALS:", legals) 
    move = None 
    while move not in legals: 
     move = ask_number("Which column will you move to? (1-7):", 1, NUM_COLS) 
     if move not in legals: 
      print("\nThat column is already full, nerdling. Choose another.\n") 
    print("Human moving to column", move) 
    return move #return the column number chosen by user 

def get_move_row(turn,move): 
    move=ask_number("Which column would you like to drop a piece?") 
    for m in range (NUM_COLS): 
     place_piece(turn,move) 
    display_board() 

def place_piece(turn,move): 
    if this_row[m[move]]==" ": 
     this_row.append[m[move]]=turn 


display_instruct() 
computer,human=pieces() 
board=new_board() 
display_board(board) 
move= int(input("Move?")) 
legal_moves() 

print ("Human:", human, "\nComputer:", computer) 

回答

2

正确的倒在脚本的底部,你叫:

move= int(input("Move?")) 
legal_moves() 
      #^no arguments 

这并不提供必要的board说法,因此错误消息。