Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simplfy?
#1
I have this code and I want to know if there's a way I can make the 1 multiple numbers (ex: a + 1 or 2 or 3...or 8). I think using a function might help but I don't know how to write it. Here's the problem and what I have.

Chess bishop moves diagonally in any number of squares. Given two different squares of the chessboard, determine whether a bishop can go from the first square to the second one in a single move.

The program receives four numbers from 1 to 8 each specifying the column and the row number, first two - for the first square, and the last two - for the second square. The program should output YES if a bishop can go from the first square to the second one in a single move or NO otherwise.

if a + 1 == c and b + 1 == d or a - 1 == c and b - 1 == d:
  print("YES")
elif a - 1 == c and b + 1 == d or a + 1 == c and b - 1 == d:
  print("YES")
else:
  print("NO")
Reply
#2
There is a simpler way to do this. To figure it out, try this exercise:
  • Pick a starting row and column. Any row and column will do, but something near the middle is best.
  • Figure out every ending row and column the bishop could move to from that start.
  • For each ending row and column, subtract the ending row from the starting row and the ending column from the starting column.
  • Look for a pattern in the two numbers you get by subtracting.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020