connect four (game)

namenobodywants nospam.nospam.namenobodywants at gmail.com
Fri Nov 24 19:26:00 EST 2017


On Friday, November 24, 2017 at 8:07:07 AM UTC-8, Chris Angelico wrote:

> This is the kind of function that needs a docstring and some comments.
> What exactly is this doing? What are the "lines" of the board? What's
> the difference between "linear" and "lines"? What exactly is it
> returning?

producing documentation is an extremely difficult task for me, but i've come up
 with the following:

"""
makelines(length,numrows,numcolumns) IS THE LIST OF ALL LISTS L, WITH LENGTH
length, OF COORDINATES
FROM A numrows x numcolumns MATRIX, SUCH THAT THE ENTRIES OF L ALL LIE IN A
LINE:

LET horizontal BE ALL THE APPROPRIATE-LENGTH LISTS WHOSE ENTRIES LIE IN A
HORIZONTAL LINE
LET vertical BE ALL THE APPROPRIATE-LENGTH LISTS WHOSE ENTRIES LIE IN A
VERTICAL LINE
LET downward BE ALL THE APPROPRIATE-LENGTH LISTS WHOSE ENTRIES LIE IN A
DOWNWARD-SLOPING DIAGONAL LINE
LET upward BE ALL THE APPROPRIATE-LENGTH LISTS WHOSE ENTRIES LIE IN AN
UPWARD-SLOPING DIAGONAL LINE
THEN makelines(length,numrows,numcolumns) IS THE UNION OF ALL THE
AFOREMENTIONED SETS
"""

def makelines(length,numrows,numcolumns):
    horizontal = [[(i, j+k) for k in range(length)] for i in range(numrows) for
j in range(numcolumns)]
    vertical = [[(i+k, j) for k in range(length)] for i in range(numrows) for j
in range(numcolumns)]
    downward = [[(i+k, j+k) for k in range(length)] for i in range(numrows) for
j in range(numcolumns)]
    upward = [[(i+k, j-k) for k in range(length)] for i in range(numrows) for j
in range(numcolumns)]
    linear = horizontal + vertical + downward + upward
    return [line for line in linear if all(i in range(6) and j in range(7) for
(i,j) in line)]

def getlines(board):
    coordlines = makelines(4,6,7) ## GLOBAL
    return [[board[square] for square in line] for line in coordlines


i tried to remove all the superfluous spaces from that, but lining up code
vertically is very helpful to me, so i don't think i can really dispense with
the practice

peace
stm




More information about the Python-list mailing list