Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8467887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:46:33+00:00 2026-06-10T15:46:33+00:00

I’ve spent a long time trying to figure out this issue, I need a

  • 0

I’ve spent a long time trying to figure out this issue, I need a fresh pair of eyes and someone who knows python a little better.

I started implementing a chess game to help me learn python, and I’ve basically only implemented moving pawns and displaying the board. Here’s the issue:

-when I move any of the top players’ pawns either 1 or 2 spaces forward, everything is fine.

-when I move any of the bottom players’ pawns 2 spaces forward, everything is fine.

-when I move any of the bottom players’ pawns 1 space forward, everything goes awry. On the display, the whole row of the new position turns into pawns, but after printing results, all of the open spots now “belong” to the bottom player (they should belong to no one, 0).

I’ve tried testing different columns with the same results.

Here’s an illustration of the issue:

SETUP

1 [R][k][B][K][Q][B][k][R]
2 [P][P][P][P][P][P][P][P]
3 [ ][ ][ ][ ][ ][ ][ ][ ]
4 [ ][ ][ ][ ][ ][ ][ ][ ]
5 [ ][ ][ ][ ][ ][ ][ ][ ]
6 [ ][ ][ ][ ][ ][ ][ ][ ]
7 [P][P][P][P][P][P][P][P]
8 [R][k][B][K][Q][B][k][R]
   a  b  c  d  e  f  g  h

OK

Enter a move [r0,c0,r1,s1]: [2,a,3,a]

1 [R][k][B][K][Q][B][k][R]
2 [ ][P][P][P][P][P][P][P]
3 [P][ ][ ][ ][ ][ ][ ][ ]
4 [ ][ ][ ][ ][ ][ ][ ][ ]
5 [ ][ ][ ][ ][ ][ ][ ][ ]
6 [ ][ ][ ][ ][ ][ ][ ][ ]
7 [P][P][P][P][P][P][P][P]
8 [R][k][B][K][Q][B][k][R]
   a  b  c  d  e  f  g  h

OK

Enter a move [r0,c0,r1,s1]: [2,a,4,a]

1 [R][k][B][K][Q][B][k][R]
2 [ ][P][P][P][P][P][P][P]
3 [ ][ ][ ][ ][ ][ ][ ][ ]
4 [P][ ][ ][ ][ ][ ][ ][ ]
5 [ ][ ][ ][ ][ ][ ][ ][ ]
6 [ ][ ][ ][ ][ ][ ][ ][ ]
7 [P][P][P][P][P][P][P][P]
8 [R][k][B][K][Q][B][k][R]
   a  b  c  d  e  f  g  h

OK

Enter a move [r0,c0,r1,s1]: [7,a,5,a]

1 [R][k][B][K][Q][B][k][R]
2 [P][P][P][P][P][P][P][P]
3 [ ][ ][ ][ ][ ][ ][ ][ ]
4 [ ][ ][ ][ ][ ][ ][ ][ ]
5 [P][ ][ ][ ][ ][ ][ ][ ]
6 [ ][ ][ ][ ][ ][ ][ ][ ]
7 [ ][P][P][P][P][P][P][P]
8 [R][k][B][K][Q][B][k][R]
   a  b  c  d  e  f  g  h

??NOT OK?? – (after moving, all ‘blank’ spaces belong to bottom player, and all of row 6 (as shown below) have the type variable set to pawn)

Enter a move [r0,c0,r1,s1]: [7,a,6,a]

1 [R][k][B][K][Q][B][k][R]
2 [P][P][P][P][P][P][P][P]
3 [ ][ ][ ][ ][ ][ ][ ][ ]
4 [ ][ ][ ][ ][ ][ ][ ][ ]
5 [ ][ ][ ][ ][ ][ ][ ][ ]
6 [P][P][P][P][P][P][P][P]
7 [ ][P][P][P][P][P][P][P]
8 [R][k][B][K][Q][B][k][R]
   a  b  c  d  e  f  g  h

And the code:

#!/usr/bin/python

import sys

# This is easy to solve with a simple tiny wrapper:
class Callable:
    def __init__(self, anycallable):
        self.__call__ = anycallable

class Piece:
    Arr_Slot = 0
    Row = 0
    Col = 0
    Sym = "[ ]"
    Id = ""
    Player = 0
    def __init__(self, row, col, player):
        self.Row = row
        self.Col = col
        self.Player = player
        self.Arr_Slot = self.Arr_Slot + 1

class Blank(Piece):
    Sym = "[ ]"
    Id  = "Blank"
    Row = 0
    Col = 0
    def __init__(self,row,col):
        self.Row = row
        self.Col = col

class King(Piece):
    Sym = "[K]"
    Id = "King"

class Queen(Piece):
    Sym = "[Q]"
    Id = "Queen"

class Rook(Piece):
    Sym = "[R]"
    Id = "Queen"

class Bishop(Piece):
    Sym = "[B]"
    Id = "Bishop"

class Knight(Piece):
    Sym = "[k]"
    Id = "Knight"

class Pawn(Piece):
    Sym = "[P]"
    Id = "Pawn"
    def TryMove(board,row0,col0,row1,col1):
        type = board.Board[row0][col0].Id
        player = board.Board[row0][col0].Player

    if player == 1:
        if (row1 == (row0+1) or ((row1 == (row0+2)) and row0 == 1)) and (col0 == col1):
            if board.Board[row1][col1].Player != player:
                board.Board[row1][col1].Sym = board.Board[row0][col0].Sym
                board.Board[row1][col1].Id  = board.Board[row0][col0].Id
                board.Board[row1][col1].Player = board.Board[row0][col0].Player
                board.MakeBlank(row0,col0)
                return 1
            else:
                print "Error: Spot taken by same player"
                return 0
        else:
            print "Error: Illegal move"
            return 0
    elif player == 2:
        if (row1 == (row0-1) or ((row1 == (row0-2)) and row0 == 6)) and (col0 == col1):
            if board.Board[row1][col1].Player != player:
                board.Board[row1][col1].Sym = board.Board[row0][col0].Sym
                board.Board[row1][col1].Id  = board.Board[row0][col0].Id
                board.Board[row1][col1].Player = board.Board[row0][col0].Player
                board.MakeBlank(row0,col0)
                return 1
            else:
                print "Error: Spot taken by same player"
                return 0
        else:
            print "Error: Illegal move"
            return 0
    else:
        print "Error: Don't own that spot"
    TryMove = Callable(TryMove)

class Board:
    """The game board"""
    Board_Dims = 8
    Arr_Slot = 0
    Board = []
    def __init__(self):
        self.Board = [[Piece for i in range(self.Board_Dims)] for j in range(self.Board_Dims)]

    def AddPiece(self, Piece):
        self.Board[Piece.Row][Piece.Col] = Piece
        self.Board[Piece.Row][Piece.Col].Arr_Slot = self.Arr_Slot
        self.Arr_Slot = self.Arr_Slot + 1
    def MakeBlank(self,row,col):
        self.Board[row][col].Sym = "[ ]"
        self.Board[row][col].Id = "Blank"
        self.Board[row][col].Player = 0
    def PrintBoard(self):
        sys.stdout.write("\n")
        for i in range(self.Board_Dims):
            sys.stdout.write(str(i+1) + " ")
            for j in range(self.Board_Dims):
                sys.stdout.write(self.Board[i][j].Sym)
            sys.stdout.write("\n")
        sys.stdout.write("   a  b  c  d  e  f  g  h\n")
    def TryMove(self, row0, col0, row1, col1):
        type = self.Board[row0][col0].Id
        player = self.Board[row0][col0].Player
        if type == "Blank":
            print "Error: No piece there"
            return 0
        if type == "Pawn":
            return Pawn.TryMove(self,row0,col0,row1,col1)


GameBoard = Board()

GameBoard.AddPiece(King(0,3,1))
GameBoard.AddPiece(Queen(0,4,1))
GameBoard.AddPiece(Rook(0,0,1))
GameBoard.AddPiece(Rook(0,7,1))
GameBoard.AddPiece(Knight(0,1,1))
GameBoard.AddPiece(Knight(0,6,1))
GameBoard.AddPiece(Bishop(0,2,1))
GameBoard.AddPiece(Bishop(0,5,1))
for i in range(GameBoard.Board_Dims):
GameBoard.AddPiece(Pawn(1,i,1))

GameBoard.AddPiece(King(7,3,2))
GameBoard.AddPiece(Queen(7,4,2))
GameBoard.AddPiece(Rook(7,0,2))
GameBoard.AddPiece(Rook(7,7,2))
GameBoard.AddPiece(Knight(7,1,2))
GameBoard.AddPiece(Knight(7,6,2))
GameBoard.AddPiece(Bishop(7,2,2))
GameBoard.AddPiece(Bishop(7,5,2))

for j in range(GameBoard.Board_Dims):
GameBoard.AddPiece(Pawn(6,j,2))

for i in range(2,5):
for j in range(GameBoard.Board_Dims):
    GameBoard.AddPiece(Blank(i,j))

GameBoard.PrintBoard()
result = 1
while 1:
inp = raw_input("Enter a move [r0,c0,r1,s1]: ")
r1 = int(inp[1])-1
c1 = int(ord(inp[3]))-97
r2 = int(inp[5])-1
c2 = int(ord(inp[7]))-97
result = GameBoard.TryMove(r1,c1,r2,c2)
if result == 0:
    print "ERROR: TRY AGAIN"
print GameBoard.Board[3][3].Player
print GameBoard.Board[3][3].Sym
GameBoard.PrintBoard()    

Commenting these lines out:

board.Board[row1][col1].Sym = board.Board[row0][col0].Sym
board.Board[row1][col1].Id  = board.Board[row0][col0].Id
board.Board[row1][col1].Player = board.Board[row0][col0].Player

Makes it basically do nothing, so I guess you could say this is where the problem is, but it works for the opposite player, and it’s the same code, so that’s why I’m uncertain as to whether the problem is actually in these lines.

Any help is much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:46:34+00:00Added an answer on June 10, 2026 at 3:46 pm

    I would take a completely different approach, instead of using a board to store positions and blank pieces, I would store the position information in the 32 Pieces generated at setup and only use Board class to store the list of pieces on it and to do Board.draw()

    e.g. something along the lines:

    class Piece(object):
        symbol = " "
        name = None
        def __init__(self, side, col, row):
            self.side = side
            self.col = col
            self.row = row
    
    class Pawn(Piece):
        symbol = "P"
        name = "Pawn"
        def __init__(self, side, col):
            if side == "white":
                row = 2
            else:
                row = 7
            super(Pawn, self).__init__(self, side, col, row) 
        def move(col, row):
            if self.check_if_move_is_valid(col, row):
                self.col = col
                self.row = row
    
    ... same for other pieces...
    
    class Board(object):
        def __init__(self):
            self.pieces = list()
            for col in range(1,9):
                self.pieces.append(Pawn("white", col))
                self.pieces.append(Pawn("black", col))
            self.pieces.append(Rook("white"), 1)
            self.pieces.append(Rook("black"), 8)
            ... etc ...
    
        def draw():
            grid = create_empty_grid(8)
            for piece in self.pieces:
                 grid[piece.row][piece.col] = piece.symbol
            for row in grid:
                 print ''.join(row)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.