Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51276 > unrolled thread

How would I do this?

Started byJohn Doe <h4ck3rpr0n3@gmail.com>
First post2013-07-25 20:16 -0700
Last post2013-07-25 22:02 -0600
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  How would I do this? John Doe <h4ck3rpr0n3@gmail.com> - 2013-07-25 20:16 -0700
    Re: How would I do this? Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-25 21:31 -0600
      Re: How would I do this? h4ck3rpr0n3@gmail.com - 2013-07-25 20:40 -0700
        Re: How would I do this? Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-25 22:02 -0600

#51276 — How would I do this?

FromJohn Doe <h4ck3rpr0n3@gmail.com>
Date2013-07-25 20:16 -0700
SubjectHow would I do this?
Message-ID<a68a52f8-a9f2-438c-a728-402dadf822f6@googlegroups.com>
Hey guys,

I;m working on making a chess program and I hit a wall. I have no idea how to get the position of the chess pieces. I have the chess board 1-8 and A-H for black as well as 1-8 and a-h for white. i just have to figure out how to declare those positions for the actual pieces so like the row of pawns for black would be B1-B8 as well as be able to replace the other coordinates with the piece when they move there like if I moved a black pawn from B1 to C1 B1 would be empty and then C1 would have it. 

I'm sorry if that's confusing but I can't seem to figure out how to do it... Any help is greatly appreciated and thank you in advance!

[toc] | [next] | [standalone]


#51278

FromIan Kelly <ian.g.kelly@gmail.com>
Date2013-07-25 21:31 -0600
Message-ID<mailman.5130.1374809568.3114.python-list@python.org>
In reply to#51276
On Thu, Jul 25, 2013 at 9:16 PM, John Doe <h4ck3rpr0n3@gmail.com> wrote:
> Hey guys,
>
> I;m working on making a chess program and I hit a wall. I have no idea how to get the position of the chess pieces. I have the chess board 1-8 and A-H for black as well as 1-8 and a-h for white. i just have to figure out how to declare those positions for the actual pieces so like the row of pawns for black would be B1-B8 as well as be able to replace the other coordinates with the piece when they move there like if I moved a black pawn from B1 to C1 B1 would be empty and then C1 would have it.
>
> I'm sorry if that's confusing but I can't seem to figure out how to do it... Any help is greatly appreciated and thank you in advance!

The usual way to do this would be to represent the board as an 8x8
list of lists, with each item of the inner lists representing one
space on the board and denoting its contents.  For example, you might
have board[1][4] = 'P', indicating that E2 (1, 4 in zero-based
row-column coordinates) holds a white pawn.

[toc] | [prev] | [next] | [standalone]


#51279

Fromh4ck3rpr0n3@gmail.com
Date2013-07-25 20:40 -0700
Message-ID<a8abab99-374f-422a-8e3a-5f0cb21f7a8a@googlegroups.com>
In reply to#51278
Thank you for the quick reply. Unfortunately I'm still a little confused... How might I implement that into my current code?

def Make_Board():
    Col = "+--+"
    Row = "|  |"
    Col2 = "--+"
    Row2 = "  |"
    print(" ", Col + Col2 + Col2 + Col2 + Col2 + Col2 + Col2 + Col2)
    print("1", Row + Row2 + Row2 + Row2 + Row2 + Row2 + Row2 + Row2, "1")
    print(" ", Col + Col2 + Col2 + Col2 + Col2 + Col2 + Col2 + Col2)
    print("2", Row + Row2 + Row2 + Row2 + Row2 + Row2 + Row2 + Row2, "2")
    print(" ", Col + Col2 + Col2 + Col2 + Col2 + Col2 + Col2 + Col2)
    print("3", Row + Row2 + Row2 + Row2 + Row2 + Row2 + Row2 + Row2, "3")
    print(" ", Col + Col2 + Col2 + Col2 + Col2 + Col2 + Col2 + Col2)
    print("4", Row + Row2 + Row2 + Row2 + Row2 + Row2 + Row2 + Row2, "4")
    print(" ", Col + Col2 + Col2 + Col2 + Col2 + Col2 + Col2 + Col2)
    print("5", Row + Row2 + Row2 + Row2 + Row2 + Row2 + Row2 + Row2, "5")
    print(" ", Col + Col2 + Col2 + Col2 + Col2 + Col2 + Col2 + Col2)
    print("6", Row + Row2 + Row2 + Row2 + Row2 + Row2 + Row2 + Row2, "6")
    print(" ", Col + Col2 + Col2 + Col2 + Col2 + Col2 + Col2 + Col2)
    print("7", Row + Row2 + Row2 + Row2 + Row2 + Row2 + Row2 + Row2, "7")
    print(" ", Col + Col2 + Col2 + Col2 + Col2 + Col2 + Col2 + Col2)
    print("8", Row + Row2 + Row2 + Row2 + Row2 + Row2 + Row2 + Row2, "8")
    print(" ", Col + Col2 + Col2 + Col2 + Col2 + Col2 + Col2 + Col2)

def Col_Label():
    Col_Label_Black = "    A  B  C  D  E  F  G  H"
    print(Col_Label_Black)

def Col_Label2():
    Col_Label_White = "    A  B  C  D  E  F  G  H"
    Col_Label_White = Col_Label_White.lower()
    print(Col_Label_White)

def Board():
    Col_Label()
    Make_Board()
    Col_Label2()

Board()



Thanks again for your help!

[toc] | [prev] | [next] | [standalone]


#51280

FromIan Kelly <ian.g.kelly@gmail.com>
Date2013-07-25 22:02 -0600
Message-ID<mailman.5131.1374811387.3114.python-list@python.org>
In reply to#51279
On Thu, Jul 25, 2013 at 9:40 PM,  <h4ck3rpr0n3@gmail.com> wrote:
> Thank you for the quick reply. Unfortunately I'm still a little confused... How might I implement that into my current code?

Where you print spaces indicating an empty space, you need to look up
the current square in the board data structure to see what is
contained there (whether it's an empty space or not) and print it
accordingly.

I strongly recommend that you work out how to print the board using
nested for loops instead of all those individual print statements.
The result will be more compact, easier to work with, and less
error-prone.  The basic skeleton should look something like this:

for row in range(8):
    for column in range(8):
        print(board[row][column])

Which you'll need to embellish with the row and column dividers and labels.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web