Problem 53840. Backgammon #3 - Display a Board Position

A previous problem in this occasional series (Problem 45967) set up a possible representation of a backgammon board:
  • White stones are represented by positive integers Red stones represented by negative integers.
  • The board is a vector of integers: The first 24 integers are the points numbered (from White's perspective) 1 to 24; integers 25 and 26 are white's bar and red's bar respectively; integers 27 and 28 are whites home and red's home respectively
  • Each integer represents the number of stones on that point; e.g. 4 means white has 4 stones on that point; -2 means red has 2 stones on that point.
Please look at Problem 45967 to understand how the board is represented as a 28-element vector.
For the purpose of this problem, the 'visual' representation of the board has been extended to include space for white and red's 'home' - i.e. the place where stones are placed once they are borne off the board. So the visual representation of the empty board is:
Red (X)
+-13-14-15-16-17-18-BB-19-20-21-22-23-24-+HH+
+ BB + +
+ BB + +
+ BB + +
+ BB + +
+ BB + +
+ BB + +
+===================BB===================+==+
+ BB + +
+ BB + +
+ BB + +
+ BB + +
+ BB + +
+ BB + +
+-12-11-10-09-08-07-BB-06-05-04-03-02-01-+HH+
White (O)
The aim of this problem is to write code that will take a board position vector as an input, and output a character array that can be used with fprintf to display a visual representation of the board position. This could be useful when working on other backgammon problems, as it allows a human-readable version of a board position to be quickly displayed.
So, given a board position vector, this problem requires you to output a character array that can be printed to display the board position. Input will only consist of valid board positions - however, you should also be able to send an 'empty' board position vector (i.e. 28 zeros) to display the empty board.
Note that there is only room to display up to 6 stones on each point. If a point has more than six stones, display the first five, and then replace the sixth stone with a number indicating the number of stones. Each point (including bar and home) is represented by two columns of characters; stones should be displayed on the right-hand column, so that two-digit numbers can be correctly displayed (see examples below).
Some example inputs and outputs are:
Starting board:
Board=[-2,0,0,0,0,5,0,3,0,0,0,-5,5,0,0,0,-3,0,-5,0,0,0,0,2,0,0,0,0];
displays as:
Red (X)
+-13-14-15-16-17-18-BB-19-20-21-22-23-24-+HH+
+ O X BB X O + +
+ O X BB X O + +
+ O X BB X + +
+ O BB X + +
+ O BB X + +
+ BB + +
+===================BB===================+==+
+ BB + +
+ X BB O + +
+ X BB O + +
+ X O BB O + +
+ X O BB O X + +
+ X O BB O X + +
+-12-11-10-09-08-07-BB-06-05-04-03-02-01-+HH+
White (O)
Board with stones on bar and home:
Board=[0,0,1,0,0,3,0,1,0,0,2,-2,1,0,0,0,-1,0,-5,0,0,0,-1,0,0,-1,7,-5];
displays as:
Red (X)
+-13-14-15-16-17-18-BB-19-20-21-22-23-24-+HH+
+ O X BX X X + X+
+ BB X + X+
+ BB X + X+
+ BB X + X+
+ BB X + X+
+ BB + +
+===================BB===================+==+
+ BB + 7+
+ BB + O+
+ BB + O+
+ BB O + O+
+ X O BB O + O+
+ X O O BB O O + O+
+-12-11-10-09-08-07-BB-06-05-04-03-02-01-+HH+
White (O)
Board with most stones borne off:
BoardIn=[0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,-2,0,0,0,13,-12];
displays as:
Red (X)
+-13-14-15-16-17-18-BB-19-20-21-22-23-24-+HH+
+ BB X X + X+
+ BB X + X+
+ BB + X+
+ BB + X+
+ BB + X+
+ BB +12+
+===================BB===================+==+
+ BB +13+
+ BB + O+
+ BB + O+
+ BB + O+
+ BB O + O+
+ BB O + O+
+-12-11-10-09-08-07-BB-06-05-04-03-02-01-+HH+
White (O)
To make it slightly easier for you, the function template below contains the character array for the empty board. Have fun, and good luck!
Note that the test suite will compare your chartacter array against the correct array, but will also fprintf your array so that you can check visually that it looks correct.
Previous problem in series: Problem 46006. Backgammon #2 - Your turn!
Regexp cheats and other cheats are not appreciated and will be blocked if you use them. This means that you can't use regexp to process the board character array - please comment if you think that this represents an unreasonable restriction! The aim is to produce code that will generate boards programmatically from generalised input. If I see hard-coded solutions I will try to add more test cases to block them.

Solution Stats

3.37% Correct | 96.63% Incorrect
Last Solution submitted on Mar 19, 2022

Problem Comments

Solution Comments

Show comments

Problem Recent Solvers2

Suggested Problems

More from this Author17

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!