Wednesday, July 16, 2025

Where Again is Arkansas? A Geography Location Game, Part 2: Determining Which State the Player Has Clicked

 

I ended my previous post having developed a mileage matrix of distances between states. In this post, I focus on how to integrate that mileage matrix into the game. When the player clicks on the wrong state, the distance in miles between the center point of both states will be used as a type of accuracy score. For example, it is one thing to confuse Georgia and Alabama, given that they share a border, but quite another to confuse Georgia with Oregon. This would clearly show that the player really has no clue where Georgia is located. 

In order to use mileage matrix, I obviously had to know which two states were being compared. Although I already know the target state given that it was chosen at random by the computer, I needed a way to figure out which state the player clicked on. How to do this was not immediately clear to me. I mentioned in my previous post about I could have created individual images for each of the lower 48 states, which would have allowed me to use LiveCode's intersect function. Although I may revert to that strategy at some point, I wanted to find another way based on the general x and y coordinates of each state. After trying one approach that was good, but not great, it occurred to me to compare of the sum of the distances on the x and y axes between the clicked location and the pair of coordinates for each state. The state that had the lowest sum would be the state clicked on by the player.

I created a new card to test out the approach. Here's a screen shot:


So, why use the sum of the x and y coordinates. Notice how some of the states line up nicely on the vertical axis (e.g., North Dakota, South Dakota, Nebraska, Kansas, Oklahoma, and Texas) and others line up on the horizontal axis (e.g., Tennessee and North Carolina). By summing up the x and y coordinates of the player's click, then comparing this to the absolute distance between all of the states, the state with the lowest difference has the highest probability of being the correct state. Error only creeps in when the player clicks on a spot that is far from the state's middle. The error is less when the state is relatively symmetrical, like Colorado, and more when the state has a non-symmetrical shape, such as Louisiana, Florida, or Michigan (geez, what a weird geography for a state having an upper and lower peninsula configuration). Similarly, the error rate is higher for the smallest states, like Rhode Island and Connecticut. 

Here's the code for the button "What state am I clicking" that does the work:

1:  on mouseup  
2:      
3:    wait until the mouseclick  
4:    put the location of image "USA with labels.png" into varLocationUSA  
5:    put the mouseloc into field "clicked spot"  
6:    put (item 1 of the mouseloc - item 1 of varLocationUSA) into varStateLocationx  
7:    put (item 2 of the mouseloc - item 2 of varLocationUSA) into varStateLocationy  
8:      
9:    //Compute the absolute x and y distance from the mouse click and each of the lower 48 states  
10:    put the number of lines in field "coordinates" into L  
11:    put empty into field "state xy differences"  
12:    repeat with i = 1 to L  
13:     put item 1 of line i of field "coordinates" into varX  
14:     put item 2 of line i of field "coordinates" into varY  
15:     put abs(varStateLocationx - varX) into varTemp1  
16:     put abs(varStateLocationy - varY) into varTemp2  
17:     put item 3 of line i of field "coordinates" into varTemp3  
18:     put varTemp1&comma&varTemp2&comma&varTemp3 into varTemp4  
19:     put varTemp4 after field "state xy differences"  
20:     put return after field "state xy differences"  
21:     sort field "state xy differences" ascending numeric by first item of each  
22:    end repeat  
23:      
24:    //Compare the sum of the absolute values of the x and y and use lowest value to determine the state  
25:    put empty into field "xy sum"  
26:    put the number of lines in field "state xy differences" into L  
27:    repeat with i = 1 to L  
28:     put item 1 of line i of field "state xy differences" into varX  
29:     put item 2 of line i of field "state xy differences" into varY  
30:     put varX + varY into varZ  
31:     put varz&comma&item 3 of line i of field "state xy differences"&return after field "xy sum"  
32:    end repeat  
33:    sort field "xy sum" ascending numeric by item 1 of each  
34:    put item 2 of line 1 of field "xy sum" into varYouFoundTheState  
35:    put varYouFoundTheState into field "target state"  
36:    
37:  end mouseup  

Lines 4-7 note the location of the USA map and then record the x and y of the player's click relative to the USA map's center. (This was explained more fully in my previous post.) The x and y location of the player's click are stored in the variables varStateLocationx and varStateLocationy.

Lines 9-22 compare the x and y location of the player's click to each of the 48 states. Since we are interested in the raw distance between the two points, lines 15 and 16 compute the absolute value of this distance. The results of these comparisons are neatly put into the field "state xy differences." Lines 24-35 take the data in the field "state xy differences" and sums the first two items in line 30, which are the x and y distances from the player's click to each of the states. The results of this simple arithmetic is put into another field - "xy sum" (line 31) - which is then sorted from low to high (line 33). The "winning" state is therefore the one that is found at the top of this field. Line 34 puts the name of this state into the field "target state," thus revealing the identity of the clicked state on the screen.

The reason why I need to sum both the absolute values of x and y is clearly shown if I click on Oklahoma, but a little left of center (just left of the "OK"). This is exactly where I clicked when I captured the screen shot above. The x coordinate of this click could be identifying a bunch of states. In fact, Oklahoma is the sixth state in the list. The margin of error is just too great when using just the x axis. However, when you add the x and y coordinates together, Oklahoma is the clear winner with a sum of 24. Kansas comes in second with a sum of 71, which is not even close.

As always, I use fields to place all of these values so that I can see what is going on with the algorithm even though I know doing so dramatically slows down the processing speed. When I go to integrate this approach into the game's engine, I'll convert the fields into variables just by changing the names. For example, I'll change field "state xy differences" to something like variable "varStateXYDifferences."

No comments:

Post a Comment