-1
import math

EMPTY = '-'

def is_between(value, min_value, max_value):
    """ (number, number, number) -> bool

    Precondition: min_value <= max_value

    Return True if and only if value is between min_value and max_value,
    or equal to one or both of them.

    >>> is_between(1.0, 0.0, 2)
    True
    >>> is_between(0, 1, 2)
    False
    """
    return value >= min_value and value <= max_value

    # Students are to complete the body of this function, and then put their
    # solutions for the other required functions below this function.

def game_board_full(cells):
    """ (str) -> bool

    Return True if no EMPTY in cells and else False

    >>> game_board_full ("xxox")
    True
    >>> game_board_full ("xx-o")
    False
    """

    return "-" not in cells



def get_board_size (cells):
    """ (str) -> int

    Return the square root of the length of the cells

    >>>get_board_size ("xxox")
    2
    >>>get_board_size ("xoxoxoxox")
    3
    """
    sqrt_cell= len(cells) ** 0.5

    return  int(sqrt_cell)


def make_empty_board (size):
    """ (int) -> str
    Precondition: size>=1 and size<=9

    Return a string for storing information with the size
    >>>make_empty_board (2)
    "----"
    >>>make_empty_board (3)
    "---------"
    """

    return "-" *size ** 2 



def get_position (row_index,col_index,size):
    """ (int,int,int) -> int

    Precondition:size >=col_index and size >= row_index

    Return the str_index of the cell with row_index,col_index and size
    >>>get_position (2,2,4)
    5
    >>>get_position (3,4,5)
    13
    """
    str_index = (row_index - 1) * size + col_index - 1  
    return str_index


def make_move( symbol,row_index,col_index,game_board):
    """(str,int,int,str) -> str

    Return the resultant game board with symbol,row_index,col_index and game_board
    >>>make_move("o",1,1,"----")
    "o---"
    >>>make_move("x"2,3,"---------")
    "-----x---"
    """
    length=len(game_board)
    size=len(cells) ** 0.5 
    str_index = (row_index - 1) * size + col_index - 1
    return "-"*(str_index-1)+symbol+"-"*(length-str_index)




def extract_line (cells,direction,cells_num):
    """ (str,str,int) -> str

    Return the characters of a specified row with cells, direction and cells_num

    >>>extract_line ("xoxoxoxox","across",2)
    "oxo"
    >>>extract_line ("xoxo","up_diagonal","-")
    "xo"
    """
    num=cells_num
    s=cells
    size= get_board_size (cells)
    if direction=="across":
        return  s[(num-1)* size : num*size]
    elif direction=="down":
        return  s[num-1:size **2:size]
    elif direction=="up_diagonal":
        return  s[(size-1)*size:size-2:1-size]
    elif direction=="down_diagonal":
        return  s[0:size*2:size+1]

NameError: name 'cells' is not defined

I don't know how to define cells because it is a parameter.

Hayley Guillou
  • 3,953
  • 4
  • 24
  • 34

2 Answers2

1

You have NO cells parameter in

def make_move( symbol,row_index,col_index,game_board):

Next time read the error message carefully so you know in which code line you have a problem.

Psytho
  • 3,313
  • 2
  • 19
  • 27
0

I find that you have referred to the variable cells in your make_move function without declaration, maybe you should define it in the arguments list.

By the way, you MUST put up all your traceback or it's difficult for all of us to find out the problem.

Matt
  • 74,352
  • 26
  • 153
  • 180
hsfzxjy
  • 1,242
  • 4
  • 14
  • 22
  • Traceback (most recent call last): File "C:\Users\Administrator\Desktop\新建文件夹\a1_simple_check.py", line 53, in result = tictactoe_functions.make_move('X', 1, 1, '-') File "C:\Users\Administrator\Desktop\新建文件夹\tictactoe_functions.py", line 93, in make_move size=len(cells) ** 0.5 builtins.NameError: name 'cells' is not defined – Lingxiao Zhang Oct 05 '15 at 00:31
  • You should declare the cells variable in the make_move's arguments list. – hsfzxjy Oct 05 '15 at 00:36
  • no, I don't know how to define " cells" because it is a parameter, it can be anything I want – Lingxiao Zhang Oct 05 '15 at 01:27
  • so why did you refer to the cells, in the function make_move? you may exactly know what the argument means if the code was written by yourself. – hsfzxjy Oct 05 '15 at 01:32