2014-03-26 5 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 인수, 따라서 오류 메시지를 제공하지 않습니다.

관련 문제