Re: First attempt at a Python prog (Chess)

Discussion in 'Python' started by Oscar Benjamin, Feb 15, 2013.

  1. On 15 February 2013 11:36, Tim Golden <> wrote:
    > On 15/02/2013 11:22, Oscar Benjamin wrote:
    >> Why not make board a list of lists. Then you can do:
    >> for row in board:
    >> for piece in row:
    >> rather than using range().
    >> Or perhaps you could have a dict that maps position tuples to pieces,
    >> e.g.: {(1, 2): 'k', ...}

    > I'm laughing slightly here because, at the monthly London Python
    > Dojo, we often find ourselves implementing board-game mechanics
    > of one sort or another: Boggle, Battleships, Sliding block,
    > Connect 4, Noughts-and-Crosses, even things like Game of Life
    > (which has a board of sorts).
    > And the "how shall we represent the board?" question is pretty
    > much the first thing any team asks themselves. And you always
    > get someone in favour of lists of lists, someone for one long
    > list,

    I always get confused when doing this about which of my coordinates
    needs to be multiplied (i.e. whether I am in Fortran or C order).

    > someone who likes a string, someone (me) who likes a sparse
    > dict keyed on coords,

    Clearly better than the others.

    > someone else likes nested defaultdicts,
    > and occasionally more outlandish schemes.
    > We even went to the extent of having a Dojo a few months back
    > which was solely about implementing the ideal board for varying
    > characteristics, but we didn't come to any conclusions :)

    In this case the innermost loop of the program is over the pieces on
    the board. Clearly you want a data structure that allows you to
    iterate directly over them. (Actually since that loop is to calculate
    the score I would replace it with a function that computes the change
    in score as a result of each move).

    > (Also I seem to remember that the OP was advised earlier precisely
    > to abandon lists of lists in favour of something else).

    Actually the suggestion was for the list of lists (instead of a flat list).

    Oscar Benjamin, Feb 15, 2013
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oscar Benjamin

    Re: First attempt at a Python prog (Chess)

    Oscar Benjamin, Feb 13, 2013, in forum: Python
    Oscar Benjamin
    Feb 13, 2013
  2. Ian Kelly
    Ian Kelly
    Feb 14, 2013
  3. jkn
    Dennis Lee Bieber
    Feb 15, 2013
  4. Rick Johnson
    Robert Kern
    Apr 30, 2014
  5. Oscar Benjamin

    Re: First attempt at a Python prog (Chess)

    Oscar Benjamin, Feb 15, 2013, in forum: Python
    Neil Cerutti
    Feb 19, 2013

Share This Page