connect four (game)

nospam.nospam.nospam.alister alister.ware at ntlworld.com
Sat Nov 25 18:24:00 EST 2017


On Sat, 25 Nov 2017 12:26:52 -0800, namenobodywants wrote:

> 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

the documentation should come after the def statement that way is becomes a
"Doc String" & can be accessed using the help function you may also want to
check out the recommended way of structuring a doc string,
 it could help you with your difficulty in writing them (a problem shared by
many)



--
This door is baroquen, please wiggle Handel. (If I wiggle Handel, will it
wiggle Bach?)
                -- Found on a door in the MSU music building




More information about the Python-list mailing list