Chess Checker Project

I want to write a program which takes my chess board as input and scans the entire board to see whether any of the pieces are being attacked horizontally, vertically or diagonally. How can I do this? I don’t know how to go about doing this. Please help.

Yeah, not clear to me what you mean by “being attacked horizontally, vertically or diagonally”. Do you mean as opposed to the standard chess moves? There are of course other chess attacks, like the knight and en passant. Even promotion or castling can be an attack. It sounds like you want everyone to attack like a queen.

Assuming we’re talking about standard chess, if I were approaching this, I would start with an array as Randell suggests, but I would have information about what each piece is. Then I would step through each of player 1’s pieces, checking each of its potential moves. For line attacking pieces (queens, rooks, bishops) you can step through a line and stop checking once you hit a chessman or an edge. If the chessmen is yours or an edge, you stop. If it’s an enemy piece, that is an attack. Of course, you need to make sure that it doesn’t leave player 1 in check or it would not be a valid attack. (I would write a subroutine backwards from the king, working backwards for any potential attack vector, with the new hypothetical board.) Similar checks would need to be done for knights, promotion, castling, and en passant.

Right now I’m just trying to work on the horizontal,vertical and diagonal move check. Suppose I have a piece- any piece doesn’t matter which one-at position 0. I want the check to check the length of the piece’s row and the length of the piece’s column which basically means the piece’s horizontal and vertical rows and columns from its position on the chess board, for any attacker. The same applies diagonally. I want to check whether there are any attackers in any of the piece’s diagonal position.
Can you help me?

I’m designing this program in Python, not Javascript. I’ve written a program myself but I’m getting a few errors for it. For instance, for the diagonal program, I am getting the error ‘NoneType’ object has no attribute ‘getitem

def diagonal(board_in):
  for row in range(n-1):
    for col in range(row + 1, n):
      if abs(board_in[row] - board_in[col])== abs(row-col):
        return False
      else:
        print board_in[row][col]


def diagonal(board_in):
for row in range(n-1):
for col in range(row + 1, n):
if abs(board_in[row] - board_in[col])== abs(row-col):
return False
else:
print board_in[row][col]

Hi there. Your post got my attention as I’m a chess enthusiast.
A few months ago I saved the following links in my favourites for future reference:

  1. https://github.com/jhlywa/chess.js
  2. https://github.com/oakmac/chessboardjs/

Check them out as they may offer some insight.

try the link below. It will answer your question

type or paste code here
def diagonal(board_in):
  for row in range(n-1):
    for col in range(row + 1, n):
      if abs(board_in[row] - board_in[col])== abs(row-col):
        return False
      else:
        print board_in[row][col]

I’ve just posted my diagonal program and if anyone can spot any problems with it, I’m open to suggestions.

One of the suggestion from @Soupedenuit is that I utilize the chess.js file. Since I’m writing this program in Python I can’t use the chess.js file but I’ve found a file called Python-chess which I think also describes chess pieces,locations etc. What I don’t understand about this file is whether it describes attacks (i.e:the horizontal,vertical and diagonal ones which I want), and if it does which of the functions of the file are used for it. So if anyone knows anything about this file or any file which describes functions for these attacks, please tell me.

I’m designing a chess game in Python and to make it a little more interesting a sort of Chess board checker which checks whether any of the pieces are being attacked by any other pieces. For this purpose I tried using the Python-Chess library but I don’t know the functions in it so well which is why I’m having trouble with the horizontal.vertical and diagonal attacks, which is to say the Rook, Bishop and Queen’s attacks. I don’t know whether these or any such functions are even defined in the Python-Chess library. So you can help tell me about this library, or any other library which defines and which will help me with my program, please tell me. I’d be very grateful.

Python-Chess has pretty good documentation and you can find the required methods defined here:
https://python-chess.readthedocs.io/en/latest/core.html#chess.Board.is_attacked_by

Sorry for the late reply. I tried using the link you suggested. Can you please explain in detail how to use the .attacks class (at least I think it’s a class)?