For fun: rushhour game

Summer is coming, but it’s too hot outside, so let’s have fun inside. I googled around to see how the rush hour game is implemented in Prolog, and I wonder how a 6x6 rectangular game should be represented.

like this?

[ [-, -, -, -, -, -],
  [-, -, -, -, b, -],
  [-, -, r, r, b, -],
  [-, -, -, -, b, -],
  [-, -, -, -, -, -],
  [-, -, -, -, -, -] ]

I would need to define moves by moving the list elements, which is easy horizontally, see docs for nth0/4 (I guess this works both ways, left and right):

edge(List, Index, OldElem, NewElem, NewList) :-
    nth0(Index, List, OldElem, Transfer),
    OldElem = [-, R2, R3, R4, R5, R6],
    NewElem = [R2, R3, R4, R5, R6, -],
    nth0(Index, NewList, NewElem, Transfer).

How do I do this vertically? Does it make sense to structure the array in matrix form at all (and use some of the matrix packs to transpose the matrix), or should I use a flat list and implement the moves with indices?

Efficiency is not needed. I found some solutions in the internet, but they are not pretty (and I want to solve it myself).

Ops, it’s not that trivial… We don’t want to break the blue bus :slight_smile:

[ [-, -, -, -, -, -],
  [-, -, -, -, b, -],
  [-, r, r, b, -, -],
  [-, -, -, -, b, -],
  [-, -, -, -, -, -],
  [-, -, -, -, -, -] ]

I solved the problem by using a list of car/3 compounds, with atoms for Color, Orientation and a list for the positions occupied.

  • move a horizontal car left: maplist sum(1) for all positions (or -1, or +/- 10 for vertical cars)
  • check if a border is reached by constraining positions to be between 11…16 or 21…26 etc.
  • check if cars collide by appending their positions, length N, sorting them and testing if length is still N

The rest is breadth search (and I will add a heuristic to prefer the red car close to 35/36 to simulate my nephew‘s strategy).

  • The check for the border could be removed by adding a big accordion bus at 10…17, 20, 30, …70…77, 67, 57, …, 27. That bus doesn’t move.
  • The check for collision could be improved by keeping track the empty spaces (then we don’t need the accordion bus either).
  • Todo: render the game on SWISH using Unicode car signs :sport_utility_vehicle::oncoming_automobile::taxi::oncoming_taxi::automobile::oncoming_police_car::fire_engine::minibus: