0

Title pretty much sums it up. I'm trying to display the chess unicode characters in the win10 command prompt and it displays the replacement character (�) instead (copy pasting that gives me the correct character again)

Enforcing UTF-8 encoding. Setting the CMD Charpage to UTF-8 encoding. Using the unicode characters instead of codes in my strings.

$icons = {
  "Pawn" => "\u2659", # ♙
  "Rook" => "\u2656", # ♖
  "Knight" => "\u2658", # ♘
  "Bishop" => "\u2657", # ♗
  "Queen" => "\u2655", # ♕
  "King" => "\u2654", # ♔

  "Black" => "\u25A0", # ■
  "White" => " " #
}

class Graphics
  def display_board(b);
  system ("cls")
  #system ("chcp 65001")
  board = b.get_map();
    board.each_with_index do |subarr,x|
      str = "";

      subarr.each_with_index do |value,y|
        str += "[" + $icons[value] + "]";
      end

      puts(str);
    end
  end
end

Expected result is this :

[♖][♘][♗][♔][♕][♗][♘][♖]
[♙][♙][♙][♙][♙][♙][♙][♙]
[ ][■][ ][■][ ][■][ ][■]
[■][ ][■][ ][■][ ][■][ ]
[ ][■][ ][■][ ][■][ ][■]
[■][ ][■][ ][■][ ][■][ ]
[♙][♙][♙][♙][♙][♙][♙][♙]
[♖][♘][♗][♔][♕][♗][♘][♖]

Actual result : � for every chess piece.

lacostenycoder
  • 10,623
  • 4
  • 31
  • 48
Julien
  • 35
  • 4
  • What codeset is your terminal using? A mismatch in the terminal will result in characters like that in the output. I get reasonable output in a Mac OS terminal set for UTF-8: `=> {"Pawn"=>"♙", "Rook"=>"♖", "Knight"=>"♘", "Bishop"=>"♗", "Queen"=>"♕", "King"=>"♔", "Black"=>"■", "White"=>" "}` – the Tin Man Jul 28 '19 at 21:41
  • See my updated answer. You need a font on windows which supports a broader set of UTF-8. – lacostenycoder Jul 29 '19 at 20:58

1 Answers1

1

This doesn't appear to be a ruby problem but rather the default encoding setting for windows and which fonts are used by the command prompt cmd.exe.

Here's how you should be able to get this to work.

  1. Download and install this free monospaced font.
  2. right click windows start button and select Run then type regedit and enter
  3. type in the following path HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Console\TrueTypeFont
  4. Right click on the white area under the key entries and select new > string value
  5. in the value data box type Droid Sans Mono
  6. Click OK and then restart Windows.
  7. Right click windows button > run > cmd
  8. Right click the top of the window and select DejaVue Sans Mono font
  9. Test it by typing this ruby -e 'puts "\u2658"'

You should see a font that outputs a knight ♘

For more help see https://www.thewindowsclub.com/add-custom-fonts-to-command-prompt

For other possible fonts which can be used see suggestions on this answer

lacostenycoder
  • 10,623
  • 4
  • 31
  • 48