Chess Checker Project

Chess Checker Project
0

#1

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.


#2

Try creating a 2 dimensional array like:

var chessBoard = [
  ['p1','p1','p1','p1','p1', 'p1','p1','p1'],
  ['p1','p1','p1','p1','p1', 'p1','p1','p1'],
  [null, null, null, null, null, null,null,null],
  [null, null, null, null, null, null,null,null],
  [null, null, null, null, null, null,null,null],
  [null, null, null, null, null, null,null,null],  
  ['p2','p2','p2','p2','p2', 'p2','p2','p2'],
  ['p2','p2','p2','p2','p2', 'p2','p2','p2']
];

This just represents the starting board. Since you stated you only wanting to be able to see if the pieces were being attached horizontally, vertically, or diagonally, then I assumed you do not care about what type of pieces they are (i.e. King, Queen, Knight, etc…). That is why I represented player 1 as ‘p1’ and player 2 as ‘p2’ and used null for blank squares.

Now that you have your board, you need to write some code which will check left, right, up, and down and diagonal of which ever player (p1 or p2) you want to consider your pieces.

The array I show above only shows the starting positions of the pieces, so if already have a specified board configuration, you will need to adjust the array above to fit your given scenario before searching for attacks.

To get you started, I will show you how you could check for horizontal attacks. I will assume that I am player 1 and I want to check left and right of all my current pieces for a p2.

function horizontalCheck() {
  for (var row=0; row < chessBoard.length; row++) {
    for (var col=0; col < chessBoard[row].length; col++) {
      if (chessBoard[row][col] === 'p1') {
        if (col>0)
          var left = chessBoard[row][col-1];
        if (col < chessBoard.length -1)
          var right = chessBoard[row][col+1];
        if (left === 'p2' || right === 'p2')
          console.log('player 1 at position ' + (row + 1) + ',' + (col + 1) + ' can be attacked.');
      }
    }
  }
}

horizontalCheck();

If you are not familiar with how to work with arrays, you should review the challenges at FreeCodeCamp.org using the Map button at the top of the FCC site and search for “array” in the challenges.


#3

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.


#4

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?


#5

You should be able to take what I have given you above for horizontal moves and modify it for a specific position on the board. You would need to modify the function to have two arguments rowArg and colArg which would constitute the position in the 2-dimensional array and then use similar logic to check just that one position. My solution checks the entire board and not one specific position. The logic for checking horizontal would be almost identical.

See if you can modify my posted solution above to make it work for one position and post your code here. Then, if you are still unable to figure out the vertical and diagonal, let us know. We want you to learn on your own how to accomplish this task and not write out a complete solution for you. I strongly suggest you complete the Basic JavaScript, Basic/Intermediate/Advanced Algorithm sections of Free Code Camp to give you a stronger understanding of manipulating and referencing arrays. That is all this chess board attack problem (as you have described it) is about.

When you reply next time, please post any code or written algorithm you have for this project.


#6

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


#7

You can post your Python code here also. There are many here on the forum (including myself) who know Python as well as they do JavaScript.


#8
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]

#9

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

When you call the function diagonal, what does the list board_in look like? Also, can you explain exactly what you want the above function to do?


#10


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]


#11

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.


#12

try the link below. It will answer your question


#13
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]

#14

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.


#15

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.


#16

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


#17

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)?


#18

I combined your two Chess Checker topics, because they are related to the same topic. Please do not create duplicate topics. I have unlisted the most recent creation.

Thank you.