Iam attempting to program a chess game in Java that will display entirely in the console. I am using Unicode characters for this such as White Chess Queen U+2655. My problem is that I cannot find any whitespace characters that match the width of the chess pieces so I can't display the board correctly. Take a look at my output below, I moved the pawn from h2 to h4:
Here on this website, the chess pieces have slightly more than the width of a standard character. In my console in Eclipse, the chess pieces have a width that is slightly less than 2 standard characters.
Edit: turns out it was the consolas font that was causing me problems. Consolas, displays chess pieces with a very strange width, about 1.8 characters wide. I switched the font of my console to a few different monospaced fonts until I found one that correctly sized the chess pieces. Is there any way to ensure specific widths, regardless of the font?
If you're not using a monospaced font the width of the pieces will vary with the font. They might match one of the unicode spaces, perhaps the en-space (\u2002) or the em-space (\u2003). Any solution would be specific to your environment. Ref: unicode spaces.
Steve Eddins retired from MathWorks in 2024 after 30 years of service. He can now be found at MATLAB Central and at Matrix Values, where he continues to write about MATLAB and related topics. His MathWorks career included image processing, toolbox development, MATLAB development and design, development team management, and MATLAB design standards. He wrote the Steve on Image Processing blog for 18 years and is a co-author of Digital Image Processing Using MATLAB.
An amateur musician and French horn enthusiast, Steve is a member of Concord Orchestra and Melrose Symphony Orchestra, as well as a member of the board of directors for Cormont Music and the Kendall Betts Horn Camp. He blogs about music and French horn at Horn Journey.
Today I want to show you how I made that diagram in MATLAB. First, let's talk about the board. Back in 2011, I wrote about a variety of ways to make a checkerboard (or chessboard) pattern. In that post, I played games with repmat, powers of -1, floor, and round. It got a little crazy.
Next, I needed to replicate each element of f to make an image with larger squares. Do you know how to replicate elements of a matrix? Some experienced MATLAB users would do it using the kron function. Now, however, you can just use the new repelem function. The reference page tells you when this function was introduced.
Now I want to show you a coordinate system trick. If you turn on the axes display, you can see the image pixel coordinates. (In Image Processing Toolbox terminology, these are called intrinsic coordinates.)
When I get to the part about displaying the queens, however, I would like to be able to place them based on the coordinates of each chessboard square, independent of the number of pixels per square. I can do that by manipulating the XData and YData properties of the image. These properties assign spatial coordinates to the left/right and top/bottom edges of the image.
Now let's put some queens on the board. This turns out to be pretty easy because you can just do it with a text object. Since R2014b, you've been able to draw text in MATLAB graphics using Unicode characters. And the Unicode character set includes chess symbols! The black queen symbol is at code point 9819 (decimal).
I have one more little coding trick to show you. When I wrote my eight queens animation code, I wanted to have an easy to show and remove a queen at any square on the board. So, I made an matrix of text objects, one at each square. Then I could just index into the matrix of text objects and turn the Visible property on and off. Here's how. (First, let me delete the text object I just made.)
I want to put chess symbols inline in my document. I know that, using skak, I can produce nice looking inline symbols with commands like \symking, etc. However, these are only white, and I need white and black pieces. The skak package also provides commands like \WhiteKingOnWhite and \BlackKingOnWhite, which look nice, but are way too big. I cannot find a way to resize them (I tried using the built-in \small command, as well as packages like smaller and relzise). I also looked at using commands like \Pisymbolfselchx, where x is an integer, from a package I found called bartel-chess-fonts, but it looks like this isn't included with TeXLive, and I don't want to mess with installing additional packages. Lastly, I tried \char"xxxx, where xxxx is the unicode specifier corresponding to the desired piece, but I get an error ! Bad character code (9812) (not sure if this is relevant, but 9812 is the HTML code for the white king, which is the piece whose unicode representation I was trying to use when I got the error). What else can I try?
As an alternative you could create pictures of the various figurines (e.g. by using lualatex, standalone and one of the open type fonts which has the symbols), and then use them with a faked font encoding:
There is also the skaknew font that provides black pieces. It is included in TeX Live/on CTAN. The pieces are encoded as latin letters after a font switch. The white pieces are uppercase and the corresponding black piece is the same letter lower case.
(Commentary by the author: this program suffers similarly of slowness, in eliminating rotational equivalents, as does its Scheme ancestor. Some reasons: it uses backtracking and that is slow; it uses essentially the same inefficient storage format for solutions [one could for instance use integers], and it does not precompute rotational equivalents. However, it does satisfy the task requirements, and might be regarded as a good start. And it can solve the m=5, n=6 case in practical time on a fast machine. m=7, n=7 is a more annoying case.)
There are two Fortran programs and a driver script. One program generates a Fortran module for basic operations; the other program (which must be linked with the generated module) does the actual work. The driver script is for Unix shell.
For speed, armies are represented by 64-bit or 128-bit integers, depending on the value of n. A 1-bit represets a queen. Rotations and reflections of the board are elemental integer operations on an army. Checking for any attacks is an elemental integer-to-boolean operation on the two armies (though the program detects rook-like attacks by a different mechanism). Equivalence under interchange of the colors can be tested by reversing which army gets which integer value.
Textual rather than HTML output. Whilst the unicode symbols for the black and white queens are recognized by the Ubuntu 16.04 terminal, I found it hard to visually distinguish between them so I've used 'B' and 'W' instead.
This means you could look for a font which includes the chess symbol unicode block and has a license which fits your purpose. Most fonts are in TrueType format which is already vector-based and can be converted to SVG with Apache SVG Font Converter. This would mean you wouldn't even need to redraw them manually.
The topics that I wanted to cover turned out to be too long for a single blog post, so the final program does not provide a payable game, I plan to write another blog post covering some more topics related to this.
The first thing we need in a chess game are the chess pieces, and to keep the application simple, we will use the Unicode characters for the chess pieces, so each piece will actually be displayed as text. There are 12 chess pieces in total listed below with their name, mnemonic letter and Unicode code point. The Unicode glyphs, as displayed below, are too small to be used in the game, but they can be rendered using a larger font to make them bigger.
We will use a single snip class for all chess pieces, since the only difference between them is how they are displayed. To have a minimal working snip, three things need to be present in the derived class:
Our chess-piece% snip class receives three arguments in the constructor: a glyph, which is a string representing the Unicode character for the piece, a font used to render the glyph and a size which is the size in pixels of the chess piece (since the piece is a square it will have the same width and height). The font object could already determine the size of the piece, but a separate size parameter allows us to define chess pieces that are larger than the glyph that they display (as an exercise, you can experiment with different variations of font and size in the make-chess-piece defined later).
The draw method is used to paint the snip contents onto the canvas. It receives the device context, dc and the snip position on the canvas. The method also receives other parameters which would allow re-drawing only the parts of the snip that actually need updating, but our code always draws the entire snip contents. Our implementation simply draws the glyph in the middle of the snip area, but nothing else, so the actual snip appears transparent, with only the chess piece being drawn.
Creating chess-piece% instances is inconvenient, as we have to remember the Unicode glyph for each chess piece, as well as the font and size to use for them (and preferably to create all pieces of the same font and size). To simplify their, we can write a function, make-chess-piece which receives the piece mnemonic (K for king, Q for queen, etc) and creates the piece. Thus to create a white king piece, we can just call (make-chess-piece "K"):
You can find the entire program in this GitHub Gist and if you run it you will get the result as shown below. With only about 65 lines of code we already have an application which can display chess pieces and move them around a board using the mouse. The board however does not look like a chess board at all, so we need to work on that next.
You can find the updated program in this GitHub Gist and if you run it you will get the result as shown below. We now have a chess board on which to move the pieces, but the pieces can still freely move on the board. We need to restrict the valid location of each chess piece to one of the squares and we will address this in the next section.
3a8082e126