Array as closed "loop" for direction move Topic is solved

Ask gaming related questions (AHK v1.1 and older)
tabr3
Posts: 34
Joined: 25 Feb 2024, 04:06

Array as closed "loop" for direction move

Post by tabr3 » 26 May 2024, 03:09

Code: Select all

    positions := { "apple": 0, "boy": 1, "cat":2 } 
	
    startIndex := positions[startPos]
    endIndex := positions[endPos]

    moves := abs(endIndex - startIndex) 
    direction := endIndex > startIndex ? "right" : "left"
This script return the direction and number of move by compare two value different.
If I am at the cat position (2) and want to go to the apple, moving left 2 is correct. But in a closed "loop", going from the cat position to the right by 1 will also lead to the apple. Both right 1 and left 2 are valid moves, but right 1 requires fewer steps. If we have a longer array, such as the cat being at position 100 and the apple at position 3, going left for 97 moves is awful. Instead, if we can move right directly to the apple in just 3 moves(or a case from apple to cat also 3 move). How to install this close loop, fewer moves logic into the existing script?

User avatar
Chunjee
Posts: 1502
Joined: 18 Apr 2014, 19:05
Contact:

Re: Array as closed "loop" for direction move

Post by Chunjee » 26 May 2024, 03:46

Wouldn't the distance be more necessary for a linked list?

Seeing as these are keys and not array positions, the distance is imagined.

User avatar
Chunjee
Posts: 1502
Joined: 18 Apr 2014, 19:05
Contact:

Re: Array as closed "loop" for direction move  Topic is solved

Post by Chunjee » 26 May 2024, 08:21

Although I do not understand the point of the question, it intrigued me. I came up with this:

Code: Select all

myArr := [1, 2, 3, 4, 5, 6, 7]
currentPosition = 5 ; Index of the current position, which is 5
desiredPosition = 1 ; Index of the desired position, which is 1

msgbox, % fn_fastestDirection(myArr, currentPosition, desiredPosition)
; => "Right (3 steps)"

; functions
fn_fastestDirection(arr, currentPosition, desiredPosition) {
	n := arr.count()
	
	; Calculate the right distance
	rightDistance := mod(desiredPosition - currentPosition + n, n)
	
	; Calculate the left distance
	leftDistance := mod(currentPosition - desiredPosition + n, n)
	
	; Compare the distances and return the faster direction
	if (rightDistance <= leftDistance) {
		return "Right (" rightDistance " steps)"
	} else {
		return "Left (" leftDistance " steps)"
	}
}
Last edited by Chunjee on 26 May 2024, 14:04, edited 1 time in total.

tabr3
Posts: 34
Joined: 25 Feb 2024, 04:06

Re: Array as closed "loop" for direction move

Post by tabr3 » 26 May 2024, 10:22

Thank you, you are doing it correctly, that is what I want.

The aim of this script is to select/switch the items from a "slot" list kind of system in the game. All the items are stored in the "slot" list in a closed "loop" style.

Code: Select all

positions := { "apple": 0, "ball": 1, "cat": 2, "dog": 3, "egg": 4, "fish": 5}
currentposition:="ball" ; The current equipped item is "ball".
newPosition:="fish"     ; The new target item to equip is "fish".
The function will output/send the "left" key (can be "up" or any other key that switching the items slot direction) two times so that the current equipped item will be switched to "fish".

[Mod edit: Added [code][/code] tags.]


Post Reply

Return to “Gaming Help (v1)”