Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8658617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:48:44+00:00 2026-06-12T15:48:44+00:00

I’m new to Python and trying to write a validate function in the simple

  • 0

I’m new to Python and trying to write a validate function in the simple chest game to achieve:

the a piece should move it’s full extend (which mean in it’s moving
direction like B7:E4, have a another piece F4 blocked, which is a
legal move )

enter image description here

def validate_move_rule(from_position, to_position, position_hash):
    blocker_position = []
    blocker_position.extend(position_hash)
    if(abs((int(to_position[1]) - int(from_position[1]))) == abs(ord(to_position[0]) - ord(from_position[0]))):
        if((int(to_position[1]) - int(from_position[1])) > 0 and (ord(to_position[0]) - ord(from_position[0])) > 0):
            next_position = str_plus(to_position[0],1) + str(int(to_position[1]) + 1)
            if(next_position in blocker_position):
                return True
        elif((int(to_position[1]) - int(from_position[1])) < 0 and (ord(to_position[0]) - ord(from_position[0])) < 0):
            next_position = str_plus(to_position[0],-1) + str(int(to_position[1]) - 1)
            if(next_position in blocker_position):
                return True
            #   left_up
        elif((int(to_position[1]) - int(from_position[1])) > 0 and (ord(to_position[0]) - ord(from_position[0])) < 0):
            next_position = str_plus(to_position[0],1) + str(int(to_position[1]) - 1)
            if(next_position in blocker_position):
                return True
            #left_down
        elif((int(to_position[1]) - int(from_position[1])) < 0 and (ord(to_position[0]) - ord(from_position[0])) > 0):
            next_position = str_plus(to_position[0], -1) + str(int(to_position[1]) + 1)
            if(next_position in blocker_position):
                return True
            #right_up
    elif(from_position[1] == to_position[1]):
        if((ord(to_position[0]) - ord(from_position[0])) > 0):
            next_position = str_plus(to_position[0],1) + to_position[1]
            if(next_position in blocker_position):
                return True
            #right
        elif((ord(to_position[0]) - ord(from_position[0])) < 0):
            next_position = str_plus(to_position[0],-1) + to_position[1]
            if(next_position in blocker_position):
                return True
            #left
    elif(from_position[0] == to_position[0]):
        if((int(to_position[1]) - int(from_position[1])) > 0):
            next_position = to_position[0] + str(int(to_position[1]) + 1)
            if(next_position in blocker_position):
                return True
            #down
        elif((int(to_position[1]) - int(from_position[1])) < 0):
            next_position = to_position[0] + str(int(to_position[1]) - 1)
            if(next_position in blocker_position):
                return True
            #up
    else:
        puts("Error: it's not a legal move(hint: must diagonal horizontal vertical)")
        return False

I found my code is really tedious, I thought Python should have some feature simplify my code, Any suggestion ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:48:45+00:00Added an answer on June 12, 2026 at 3:48 pm
    • use variables for the difference between the positions
    • move the blocker_position check to the bottom
    • you don’t need blocker_position
    • only use int()/str() at the beginning or end of the function
    • in the middle section only figure out how much x and y change, not their value

    Like so:

    def validate_move_rule(from_position, to_position, position_hash):
        to_pos_x = ord(to_position[0])
        to_pos_y = int(to_position[1])
        delta_pos_x = to_pos_x - ord(from_position[0]) # x
        delta_pos_y = to_pos_y - int(from_position[1]) # y
    
        delta_x = 0
        delta_y = 0
    
        if abs(delta_pos_x) == abs(delta_pos_y): 
            # diagonal
            delta_x = 1 if delta_pos_x > 0 else -1
            delta_y = 1 if delta_pos_y > 0 else -1
        elif from_position[1] == to_position[1]: 
            # horizontal     
            delta_x = 1 if delta_pos_x > 0 else -1
        elif from_position[0] == to_position[0]: 
            # vertical     
            delta_y = 1 if delta_pos_y > 0 else -1
        else:
            puts("Error: it's not a legal move(hint: must diagonal horizontal vertical)")
            return False
    
        new_position = chr(to_pos_x + delta_x) + str(to_pos_y + delta_y)
        return new_position in position_hash
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.