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.
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!
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 Stats
Problem Comments
4 Comments
Solution Comments
Show comments
Loading...
Problem Recent Solvers2
Suggested Problems
-
4560 Solvers
-
Back to basics 4 - Search Path
380 Solvers
-
438 Solvers
-
201 Solvers
-
112 Solvers
More from this Author17
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
There are a couple problems with the tests. The "correct" result listed in test #4 has an error in it. One of the B's down the centerline in an 'X'. Also, the importdata() command used in test #5 seems to fail.
Hi @William, many thanks for attempting this problem. The result in the test suite for test 4 is correct. The Bs down the centreline represent the Bar, and that test case has one of red's stones on the bar (-1 in the Board vector's 26th element), so the 'X' on the Bar is correct and represents that stone. This case is shown in the second example of the problem description, where you also see the 'X' on the Bar. As for the importdata() function, I'm not sure why that failed for you, but it must have been a Cody glitch, because it seems to work OK for me now (I submitted a dummy solution to test that case, which passed). Maybe you can try again? Regards, Mark
@William, just to follow up on the last point; I have investigated further and indeed you are correct that the importdata() function fails for your code (but not my reference solution!). I can't immediately see why this is. Test 5 was intended as an 'anti-cheating' test but is perhaps not needed for this problem, so I've removed test 5 for now. If others try to game the problem by cheating I'll have another think about how to block those solutions! If you can fix your code to pass test 4 then I think you'll now get a working solution. Regards, Mark
Mark, Thanks for the explanation! I have never played Backgammon, so this is a good learning opportunity for me. My modified code now passes all the tests, including an uncommented version of test #5.