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:
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:
Board with stones on bar and home:
Board with most stones borne off:
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!
Next problem in series: Problem 53850. Backgammon #4 - Dice Probabilities
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 CommentsShow comments
Problem Recent Solvers2