简体   繁体   English

对角主教在棋盘上移动 python

[英]Diagonal bishop moves on chess board with python

I have the following problem我有以下问题

In chess, the bishop moves diagonally, any number of squares.在国际象棋中,主教沿对角线移动任意数量的方格。 Given two different squares of the chessboard, determine whether a bishop can go from the first to the second in one move.给定棋盘的两个不同方格,确定一个象是否可以一步从第一个到第二个 go。

The program receives as input four numbers from 1 to 8, specifying the column and row numbers of the starting square and the column and row numbers of the ending square.该程序接收从 1 到 8 的四个数字作为输入,指定起始方块的列号和行号以及结束方块的列号和行号。 The program should output YES if a Bishop can go from the first square to the second in one move, or NO otherwise.如果主教可以 go 从第一个方格移动到第二个方格,程序应该 output YES,否则为 NO。

For example:例如:

Input: 2 3 5 6输入:2 3 5 6

Output: Output:

YES是的

It is assumed that the cells are numbered from left to right and from bottom to top, ie, the bottom left cell has column number 1 and row number 1 while the bottom-right cell has column number 8 and row number 1.假设单元格是从左到右和从下到上编号的,即左下角的单元格的列号为 1,行号为 1,而右下角的单元格的列号为 8,行号为 1。

How far did I get?我走了多远?

I have managed to get to check if the bishop moved diagonally, but it can move any diagonal, so it is not correct.我已经设法检查主教是否沿对角线移动,但它可以移动任何对角线,所以这是不正确的。 Can someone give me some hints?有人可以给我一些提示吗?

my code我的代码


initial_coord_x=int (input('enter the initial x'))
initial_coord_y=int (input('enter the initial y'))
final_coord_x=int (input('enter the final x'))
final_coord_y=int (input('enter the final y'))
if final_coord_x<=8 and final_coord_y<=8:
  if final_coord_x < initial_coord_x and final_coord_y > initial_coord_y:
    print ('you moved legally')
  elif final_coord_x < initial_coord_x and final_coord_y < initial_coord_y:
    print ('you moved legally')
  elif final_coord_x > initial_coord_x and final_coord_y > initial_coord_y:
    print ('you moved legally')
  elif final_coord_x > initial_coord_x and final_coord_y < initial_coord_y:
    print ('you moved legally')
  else:
    print ('no!')


else:
  print ('illegal move, you moved outside the chessboard')

To check the possibility of bishop moving (at existing cells) it is enough to check whether the absolute value of horizontal displacement is equal to the absolute value of vertical displacement (so both positions lie in the same diagonal)要检查主教移动(在现有单元格处)的可能性,只需检查水平位移的绝对值是否等于垂直位移的绝对值(因此两个位置位于同一条对角线上)

dx = abs(final_coord_x - initial_coord_x)
dy = abs(final_coord_y - initial_coord_y)
if (dx == dy) and (dx > 0):
     legal move 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM