- 1

Chess

I try to solve this task: Chess rook moves horizontally or vertically. Given two different cells of the chessboard, determine whether a rook can go from the first cell to the second in one move. The program receives the input of four numbers from 1 to 8, each specifying the column and row number, first two - for the first cell, and then the last two - for the second cell. The program should output YES if a rook can go from the first cell to the second in one move, or NO otherwise here you can see the original task: https://snakify.org/de/lessons/if_then_else_conditions/problems/rook_move/ I should use the if, elif, else function for it. I think now for many days and can't find a solution. Can anybody help?

30th Apr 2020, 1:23 PM
Sur Tur
Sur Tur - avatar
20 Answers
+ 6
if (a-c)*(b-d)==0:print("Yes") else:print("No")
2nd May 2020, 11:59 AM
Oma Falk
Oma Falk - avatar
+ 3
Since a rook can only move horizontally or vertically, for it to be able to reach another square within one move, both squares must be either on the same row or the same column. Therefore, it is sufficient to check if either the two rows or the two columns are equal. If either of the pairs is equal, the rook can perform the move, otherwise it would take two moves.
30th Apr 2020, 1:39 PM
Shadow
Shadow - avatar
+ 3
HonFu Alexander Thiem i think we got it
2nd May 2020, 2:50 PM
Oma Falk
Oma Falk - avatar
+ 2
Ah, I see, so blocking own or eneny stones in the path and cases like this don't even come in.
30th Apr 2020, 1:49 PM
HonFu
HonFu - avatar
+ 1
You take a two-dimensional array and put something in there that signifies the pieces, for example 0 for nothing, 1 for rook. Then you have to calculate, which fields your rook can access, by looping. And if the target coordinates from the input are in the collection of available fields, you get a True, otherwise False. There are a lot of chess codes in Code Playground that you can check for these kind of things. Otherwise please *try* to solve this task, writing the code yourself, even if it's hard, even if you fail first. Honestly try to figure it out - applying your own hands. If you get stuck, show us your code.
30th Apr 2020, 1:37 PM
HonFu
HonFu - avatar
+ 1
this array thing help me. i try on my own I just wanted to have some tipps :D
30th Apr 2020, 2:06 PM
Sur Tur
Sur Tur - avatar
+ 1
Are you doing this in Python? Use a list, that contains eight lists with eight zeros in it. These are your empty fields. But as Shadow said, for the very strongly simplified task that was given, you won't really need it.
30th Apr 2020, 2:11 PM
HonFu
HonFu - avatar
+ 1
Yeah you can create a list of 64 variables!! As you can see my code also shows that your use of or was correct https://code.sololearn.com/chGWresqnGeK/?ref=app
30th Apr 2020, 4:04 PM
Alexander Thiem
Alexander Thiem - avatar
+ 1
if (a-c)*(b-d):print("No") else:print("Yes") is shorter isnt it
2nd May 2020, 1:00 PM
Alexander Thiem
Alexander Thiem - avatar
+ 1
Hmm Yes😉
2nd May 2020, 1:07 PM
Alexander Thiem
Alexander Thiem - avatar
+ 1
Awright, Oma Falk, then I'll stop googling for golfing technique now. 😏
2nd May 2020, 3:02 PM
HonFu
HonFu - avatar
0
I use snakify to learn python because I can learn very well with this. The only problem is I can't ask questions. So I do it here. Lists are in a later chapter so I also think there should be a solution with out.
30th Apr 2020, 2:16 PM
Sur Tur
Sur Tur - avatar
0
if I have a = int(input()) b = int(input()) c = int(input()) d = int(input()) Feld11 = a == 1 and b == 1 or c == 1 and d == 1 Feld12 = a == 1 and b == 2 or c == 1 and d == 2 Feld13 = a == 1 and b == 3 or c == 1 and d == 3 can i create a list with 64 new variables? to have all fields defined? I am not so sure about my use of "or"
30th Apr 2020, 2:19 PM
Sur Tur
Sur Tur - avatar
0
Wow, Oma, that simple, hm? 😁👍
2nd May 2020, 12:16 PM
HonFu
HonFu - avatar
0
print("No" if (a-c)*(b-d) else "Yes") is shorter yet. 😉
2nd May 2020, 1:06 PM
HonFu
HonFu - avatar
15th May 2020, 12:36 PM
Sur Tur
Sur Tur - avatar
0
Good
15th May 2020, 12:47 PM
Alexander Thiem
Alexander Thiem - avatar
21st Jul 2020, 3:36 AM
shubham kumar
shubham kumar - avatar
0
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner scan= new Scanner(System.in); int a=scan.nextInt(); //first rook line; int b=scan.nextInt(); //first rook column int x=scan.nextInt(); //second rook line int y=scan.nextInt(); //second rook column if (a==x || b==y){ System.out.println("YES"); }else{ System.out.println("NO"); } } }
24th Sep 2021, 4:53 PM
Symbat Mekembayeva
Symbat Mekembayeva - avatar