title: Logic game, 9 square cards and one big square
body: <p>I'm not sure this is the right place to go, maybe some other stackexchange, tell me I would post somewhere else.</p>
  
  <p>Here is my problem, I found an old game at my friends place, it's supposed to be a mind game : 9 small square cards, and you have to place them so that they all fit together, here is a picture :</p>
  
  <p><img src="https://i.stack.imgur.com/wKr1Y.jpg" alt="the game to solve"></p>
  
  <p>After a few hours in front of the game i gathered that there was no real easy fair way to finish the game, and so I went the programmatical way.</p>
  
  <p>This is where I'm having a hard time, I though I could just use some random functions, a big loop, and get it over with. But there is something like (4*9)^9 solutions, so it seems not that easy.</p>
  
  <p>Here is the code I wrote, which is pretty useless for now :
  Every time I go into the loop, I shuffle my array, rotate my cards by a random value and check if the puzzle is right, a lot of wasted cycles, but I don't know where to start to make it more efficient.</p>
  
  <p>EDIT : 
  Fixed code, i get a few deck with 8 cards, but no 9 cards deck, if anyone has a fix to my code, or maybe there is no solution ?</p>
  
  <pre><code>require 'json'
  
  class Array
      def rotate n
          a =dup
          n.times do a &lt;&lt; a.shift end
          a
      end
  end
  
  
  @grid = [[{"type"=&gt;"p", "head" =&gt; 1},{"type"=&gt;"c", "head" =&gt; 1},{"type"=&gt;"a", "head" =&gt; 2},{"type"=&gt;"o", "head" =&gt; 2}],
  [{"type"=&gt;"o", "head" =&gt; 1},{"type"=&gt;"a", "head" =&gt; 2},{"type"=&gt;"c", "head" =&gt; 2},{"type"=&gt;"p", "head" =&gt; 1}],
  [{"type"=&gt;"c", "head" =&gt; 1},{"type"=&gt;"p", "head" =&gt; 2},{"type"=&gt;"o", "head" =&gt; 2},{"type"=&gt;"a", "head" =&gt; 1}],
  [{"type"=&gt;"p", "head" =&gt; 1},{"type"=&gt;"c", "head" =&gt; 2},{"type"=&gt;"o", "head" =&gt; 2},{"type"=&gt;"a", "head" =&gt; 1}],
  [{"type"=&gt;"p", "head" =&gt; 2},{"type"=&gt;"c", "head" =&gt; 2},{"type"=&gt;"a", "head" =&gt; 1},{"type"=&gt;"c", "head" =&gt; 1}],
  [{"type"=&gt;"a", "head" =&gt; 1},{"type"=&gt;"p", "head" =&gt; 2},{"type"=&gt;"o", "head" =&gt; 2},{"type"=&gt;"p", "head" =&gt; 1}],
  [{"type"=&gt;"a", "head" =&gt; 1},{"type"=&gt;"o", "head" =&gt; 1},{"type"=&gt;"a", "head" =&gt; 2},{"type"=&gt;"c", "head" =&gt; 2}],
  [{"type"=&gt;"o", "head" =&gt; 1},{"type"=&gt;"a", "head" =&gt; 2},{"type"=&gt;"c", "head" =&gt; 2},{"type"=&gt;"p", "head" =&gt; 1}],
  [{"type"=&gt;"p", "head" =&gt; 1},{"type"=&gt;"c", "head" =&gt; 2},{"type"=&gt;"o", "head" =&gt; 2},{"type"=&gt;"a", "head" =&gt; 1}]]
  @new_grid = [nil, nil, nil,nil, nil, nil,nil, nil, nil]
  @used = [false, false, false,false, false, false,false, false, false]
  
  def check_validity(card, position, orientation)
      # since I'm adding from top left to bottom, I only need to check top and left           
      try_card = @grid[card].rotate orientation     
      valid = true
      # top
      if (@new_grid[position-3])
          if (try_card[0]["type"] != @new_grid[position-3][2]["type"] || try_card[0]["head"] == @new_grid[position-3][2]["head"])
              valid = false
          end
      end
      # left
      if (@new_grid[position-1] &amp;&amp; (position % 3) != 0)
          if (try_card[3]["type"] != @new_grid[position-1][1]["type"] || try_card[3]["head"] == @new_grid[position-1][1]["head"])
              valid = false       
          end
      end
      return valid
  end
  
  
  def solve_puzzle(position)
      (0..8).each do |card|
          unless (@used[card])
              (0..3).each do |orientation|
                  if (check_validity(card, position, orientation))
                      @used[card] = true
                      @new_grid[position] = @grid[card].rotate orientation
                                          if position == 7 
                                              puts @new_grid.to_json                                      
                                          end
                      if (position &lt; 8)
                          solve_puzzle(position + 1)
                      else
                          puts "I WON"
                          puts @new_grid.to_json
                      end
                      @new_grid[position] = nil
                      @used[card] = false
                  end
              end
          end
      end
  end
  
  
  solve_puzzle(0)
  </code></pre>