# Build a working chessboard

## The challenge

Create a program that mimics a chessboard.

## The datails

The chessboard must be surrounded by the labels of each row and column, being the columns labeled from 'a' to 'h' and the rows labeled from '1' to '8'. Also, place the default position for each chess piece (blacks are on the top). Each piece is represented by the first letter of his name:

King = K
Queen = Q
Rook = R
Bishop = B
Knight = N
Pawn = P


Also by his colour

W = White
B = Black


So any given piece is recognized by his name and colour. E.G: NB = Black Knight. Empty squares are represented by a hyphen (-).

The program must show the chess board with the pieces. Then it should ask the user to make a move with the message "Move". The program must read user input by console or equivalent. We don't care if the move is legal or not. The program must reprint the chess board with the piece moved, deleting any piece it was on that square. The move is represented origin square destination square. E.g:

2c 5d = Moves the piece in square 2c to square 5d


If the program detects there is not piece in the origin square or an invalid input, it must show the message "Invalid move" and ask again for another move. If the program detects the final square is occuped by another piece it must show the message "[Name of the moving piece] x [Name of the captured piece]". Eg:

PB x KW


The program is intended to run in a loop, so after each user input the program will ask again for another move. There is no exit clause for the loop.

Example of a initial chessboard and first user's input and program's output:

-   a   b   c   d   e   f   g   h   -

8   RB  NB  BB  QB  KB  BB  NB  RB  8

7   PB  PB  PB  PB  PB  PB  PB  PB  7

6   -   -   -   -   -   -   -   -   6

5   -   -   -   -   -   -   -   -   5

4   -   -   -   -   -   -   -   -   4

3   -   -   -   -   -   -   -   -   3

2   PW  PW  PW  PW  PW  PW  PW  PW  2

1   RW  NW  BW  QW  KW  BW  NW  RW  1

-   a   b   c   d   e   f   g   h   -

Move
2a 4a

-   a   b   c   d   e   f   g   h   -

8   RB  NB  BB  QB  KB  BB  NB  RB  8

7   PB  PB  PB  PB  PB  PB  PB  PB  7

6   -   -   -   -   -   -   -   -   6

5   -   -   -   -   -   -   -   -   5

4   PW  -   -   -   -   -   -   -   4

3   -   -   -   -   -   -   -   -   3

2   -   PW  PW  PW  PW  PW  PW  PW  2

1   RW  NW  BW  QW  KW  BW  NW  RW  1

-   a   b   c   d   e   f   g   h   -

Move

• 1. Conventionally white's pieces start on rows 1 and 2, not black's. 2. Is this intended to run in a loop? You haven't described how it takes input of the chessboard position. 3. If it is in a loop, how does it exit? 4. Is promotion impossible? – Peter Taylor Dec 9 '11 at 12:14
• My bad. I had a internet problem when posting the question and it seems that the one I finally posted was the first version. I have added info and fixed the numbers in the chessboard. Promotion is impossible. The program only have to move the pieces reading user's input. I'm not sure I get what do you ask in 2. If you need more info, feel free to ask. – Averroes Dec 9 '11 at 12:28
• So after checkmate the user has to kill the program with Ctrl-C or equivalent? Meh. Looks like a pretty boring problem anyway if it doesn't have to check legality of moves, to be honest. – Peter Taylor Dec 9 '11 at 13:11
• haha No prob :P When doing the question I was thinking checking the moves was adding a extra layer of difficult that people may consider excesive for making a program just for fun. Anyways, if the people wants to do it, I can change the question. – Averroes Dec 9 '11 at 13:44
• I'll just leave this here, not as a submission, but as a reminder of history: en.wikipedia.org/wiki/1K_ZX_Chess – nitro2k01 Jan 14 '14 at 16:45

## Perl, 211 characters

y/W/B/for@8=@1=map$_.W,split//,RNBQKBNR;@b{$_.1..$_.9}=(@$_,/2/?PW:/7/?PB:"- ")x8for@r=reverse 1..8;$c=join" ","",a..h;do{y/a-h\n/1-8/d;/ /;@b{$',$}=@b{$,19};say$c;say"$_ @b{$_.1..$_.8} $_"for@r;say$c}while<>


Nothing very unusual about this first attempt, except maybe the use of a do..while loop in golf. Uses say, so you need to run this with perl -M5.01. The input parsing is really klugy and fragile; in particular, excess whitespace will kill it. On the other hand, you're not constrained to use 1-8 for rows and a-h for columns, either will do. End the program with Ctrl-C or by ending the input (Ctrl-D in Unix).

I assumed I don't need to match the whitespace in the sample output exactly — I prefer a more compact board anyway. (I did keep the board orientation, even though flipping it upside down would save me eight chars.) A sample game session might look something like this:

  a  b  c  d  e  f  g  h
8 RB NB BB QB KB BB NB RB 8
7 PB PB PB PB PB PB PB PB 7
6 -  -  -  -  -  -  -  -  6
5 -  -  -  -  -  -  -  -  5
4 -  -  -  -  -  -  -  -  4
3 -  -  -  -  -  -  -  -  3
2 PW PW PW PW PW PW PW PW 2
1 RW NW BW QW KW BW NW RW 1
a  b  c  d  e  f  g  h
2f 3f
a  b  c  d  e  f  g  h
8 RB NB BB QB KB BB NB RB 8
7 PB PB PB PB PB PB PB PB 7
6 -  -  -  -  -  -  -  -  6
5 -  -  -  -  -  -  -  -  5
4 -  -  -  -  -  -  -  -  4
3 -  -  -  -  -  PW -  -  3
2 PW PW PW PW PW -  PW PW 2
1 RW NW BW QW KW BW NW RW 1
a  b  c  d  e  f  g  h
7e 5e
a  b  c  d  e  f  g  h
8 RB NB BB QB KB BB NB RB 8
7 PB PB PB PB -  PB PB PB 7
6 -  -  -  -  -  -  -  -  6
5 -  -  -  -  PB -  -  -  5
4 -  -  -  -  -  -  -  -  4
3 -  -  -  -  -  PW -  -  3
2 PW PW PW PW PW -  PW PW 2
1 RW NW BW QW KW BW NW RW 1
a  b  c  d  e  f  g  h
2g 4g
a  b  c  d  e  f  g  h
8 RB NB BB QB KB BB NB RB 8
7 PB PB PB PB -  PB PB PB 7
6 -  -  -  -  -  -  -  -  6
5 -  -  -  -  PB -  -  -  5
4 -  -  -  -  -  -  PW -  4
3 -  -  -  -  -  PW -  -  3
2 PW PW PW PW PW -  -  PW 2
1 RW NW BW QW KW BW NW RW 1
a  b  c  d  e  f  g  h
8d 4h
a  b  c  d  e  f  g  h
8 RB NB BB -  KB BB NB RB 8
7 PB PB PB PB -  PB PB PB 7
6 -  -  -  -  -  -  -  -  6
5 -  -  -  -  PB -  -  -  5
4 -  -  -  -  -  -  PW QB 4
3 -  -  -  -  -  PW -  -  3
2 PW PW PW PW PW -  -  PW 2
1 RW NW BW QW KW BW NW RW 1
a  b  c  d  e  f  g  h
^C

• You don't print out "Move" to prompt for a move. Do you print out the PB x KW for a capture either? I did not see anything in your code that looks like it would do that. – Kevin Cathcart Dec 9 '11 at 18:28
• @Kevin: You're right, I missed those requirements. I'm going to have to rethink the design a bit. – Ilmari Karonen Dec 9 '11 at 20:33