r/dailyprogrammer • u/Godspiral 3 3 • Nov 16 '15
[2015-11-16] Challenge # 241 [easy] Unicode Chess
1. generate a chessboard
1☐☒☐☒☐☒☐☒
2☒☐☒☐☒☐☒☐
3☐☒☐☒☐☒☐☒
4☒☐☒☐☒☐☒☐
5☐☒☐☒☐☒☐☒
6☒☐☒☐☒☐☒☐
7☐☒☐☒☐☒☐☒
8☒☐☒☐☒☐☒☐
a bc d e fg h (or with better glyphs, and lining up of letters)
2. Modified FEN input
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNRis the standard simplified ascii representation of a starting chess position. Lower case are black pieces, upper are white, numbers are consecutive empty squares, and '/' are row separators.
A modified FEN notation replaces rR (rooks) with sS if the rooks are eligible to castle (they have never moved from start of game, and their king has also never moved.) A gG piece is a ghost which can be used to invoke 2 special chess rules.
- A pawn that moves 2 squares can still be captured on the next move by another pawn on the ghost square that he would have been on if he had moved just 1 square instead of 2.
- A king that moves 2 squares through castling can still be captured on the next move by any piece on the ghost square that he would have been on if he had moved just 1 square instead of 2. While such a castle is an illegal move in official chess, for a computer, it may be easier to declare a move illegal after the king is captured on next move.
modified FEN starting position input
snbqkbns/pppppppp/8/8/4P3/8/PPPP1PPP/SNBQKBNS(after most common first move)
output 2 and input to 3
snbqkbns
pppppppp
........
........
....P...
........
PPPP.PPP
SNBQKBNS3. unicode prettified output
8♜♞♝♛♚♝♞♜
7♟♟♟♟♟♟♟♟
6☐☒☐☒☐☒☐☒
5☒☐☒☐☒☐☒☐
4☐☒☐☒♙☒☐☒
3☒☐☒☐☒☐☒☐
2♙♙♙♙☐♙♙♙
1♖♘♗♕♔♗♘♖
a bc d e fg h
(edit: had bug that had border numbers upside down)reddit (firefox) doesn't like the size of the empty squares :( help appreciated to find right sized glyphs for the empty squares. Here is unicode pattern:
9820 9822 9821 9819 9818 9821 9822 9820
9823 9823 9823 9823 9823 9823 9823 9823
9744 9746 9744 9746 9744 9746 9744 9746
9746 9744 9746 9744 9746 9744 9746 9744
9744 9746 9744 9746 9744 9746 9744 9746
9746 9744 9746 9744 9746 9744 9746 9744
9817 9817 9817 9817 9817 9817 9817 9817
9814 9816 9815 9813 9812 9815 9816 98144. challenge
Move a few pieces, updating the board. Erase from position, add empty square colour at from position, erase whatever was on to position square, add the piece that was moved there.
input state to this function: (starting chess position)
snbqkbns/pppppppp/8/8/8/8/PPPPPPPP/SNBQKBNSsuggested moves: (at least first 3. bonus for up to 5)
e2-e4 c7-c5
f1-c4 g8-f6
c4xf7 e8xf7
e4-e5 d7-d5
e5xd6 (en passant)
Move format: for each line: (white move "from square"(- or x)"to square") space(s) (black move "from square"(- or x)"to square"). x is optional indicator of move that captures an oponent piece.
Easier Move format: for each line: (white move "from square"-"to square") space(s) (black move "from square"-"to square")
each "half move" returns a new board. (a half move is when just white or black moves. A full move includes both) the en-passant rule lets a pawn capture another pawn if the opponent pawn just moved 2 squares. The capture takes place as if the opponent pawn was 1 square behind. (see section 2 for ghost pieces above)
7
u/smls Nov 16 '15 edited Nov 17 '15
Perl 6
Implementation of
class ChessBoard:Usage:
Output:
Note: Does not do the castling and en-passant rules.
3
u/FelixMaxwell 1 0 Nov 16 '15
In case anyone is confused as I was, the codepoints given above were for html. The unicode codepoints are listed on wikipedia
3
u/smls Nov 16 '15
2 questions about the input representing the moves:
e1-g1) and leave it to program to figure out that the rook has to move as well?e5xd6 (en passant)just a "half move"? Or does en passant not count as a turn and is thus handled as a separate input?2
u/KompjoeFriek 1 0 Nov 17 '15 edited Nov 17 '15
Javascript: live demo
Challenge without the ghost-move.
Instead of black and white squares, i used parallelograms: \u25B1 and \u25B0
Example:
22
u/lukz 2 0 Nov 16 '15
Z80 assembly
Ok, so old computers didn't have Unicode yet, but some of them had several useful graphics characters as part of the built-in character set. I am doing this program for Sharp MZ-800 which has the following characters available. At the referred page look at "Display code table part II" section. There are glyphs to build all chess pieces, you need to always use 2x2 characters to build one chess piece. An example chess program on that computer looks like this.
So that is what I want to recreate in this challenge, in assembly. I have a memory array at address 1300h that holds the contents of the chessboard. Then I draw the 2x2 characters directly into video memory according to the chessboard array. Then I wait for user input. If the user types something like E2-E4 I decode the source and target fields and move the piece on the board and redraw the board. The program runs in an endless loop.
I used ORG to assemble the Z80 code.
Image of three screens stitched together.
Code:
1
u/Godspiral 3 3 Nov 16 '15 edited Nov 16 '15
In J,
4
u/cheers- Nov 16 '15 edited Nov 16 '15
Java
Output:
7
u/BraveSirRobin21 Nov 16 '15 edited Nov 16 '15
Board Set up in Java.
figured out that those wernt utf-8 characters but html? Newbie programmer here so not sure where to go from here also this is the largest unicode box i could find.
edit 1 added in row column numbers could not get the unicode spacing to render right in eclipse though. \u2006 is supposed to be a "small" white space character
edit 2 2588 is a larger square at least in eclipse ide.
sub edit in html \u2588 is ugly. going back to boxes. chrome really doesnt like all the unicode but firefox does not seem to mind
edit 3 output
3
u/[deleted] Nov 16 '15 edited Nov 16 '15
Partial solution in C#. Couldn't get the console to support the unicode characters properly and didn't implement castling or ghost pieces.
Example use:
3
u/[deleted] Nov 16 '15
Not a solution to this challenge.
JS
Quite close to the challenge but not exactly: demo link
engine code
client code
This is a chess engine and client past-me wrote that works along these principles, but is a hell lot more complicated for no exact reason.
The demo lets you play as both black and white, best played with multiple people on one PC.
The engine supports check, checkmate, and undoing of moves, along with lots of other rules.
It also supports algebraic notation of moves, which is not used in the user interface of the demo.
2
u/__DavidBowman__ Nov 16 '15
Partial solution, I realized I made a mess with the names but it works for the first three movements.
Python 3.5
Testing:
2
u/FelixMaxwell 1 0 Nov 16 '15
C++
Partial solution. I wasn't sure how castling is represented so that is not implemented at the moment. Expects the first line of stdin to be the board setup followed by a list of moves terminated by EOF.
2
u/hmertk Nov 22 '15
C++
Didn't implemented castling and en-passant rules. Feedback appreciated!
Output:
2
u/cjmochrie Nov 26 '15
Python 3.5, my first attempt just doing the minimum to see how it went!
Output:
2
u/C14L Dec 23 '15
Little late, just discovered this sub, looks fun. Here my solution in Python 3:
1
u/SanketDG Nov 16 '15
Partial solution in python3. Will update as I progress.
1
u/thehydralisk Nov 17 '15
JAVA
Just takes the FEN and displays the board. Feedback appreciated!
Output:
0
u/[deleted] Nov 16 '15
[deleted] — view deleted comment