Problem 46006. Backgammon #2 - Your turn!
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.
So initial board set-up is (from White's perspective):
Red (X)
+-13-14-15-16-17-18-BB-19-20-21-22-23-24-+
+ 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-+
White (O)
Which is represented as:
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];
White moves towards point 1, red towards point 24.
A possible approach, when developing algorithms to determine valid moves, strategies, etc. is only to code them so that they operate from white's perspective. This avoids having to have algorithms work 'in both directions' and thus simplifies their development.
What this will mean, however, is that when it is red's turn, the board has to be 'flipped' so that red 'becomes' white and vice versa. The move/strategy algorithms can then be applied to this flipped board, and then the board 'flipped' back for white's turn.
The input is a vector describing a backgammon board position using the above notation. You should return a vector which is the flipped board in the same notation.
Before attempting to flip the board, you should check that the input represents a legal backgammon board position. If it doesn't then you should return NaN instead.
Previous problem in series: Problem 45967. Backgammon #1- Pip Count
Next problem in series: Problem 53840. Backgammon #3 - Display a Board Position
Regexp cheats and other cheats are not appreciated and will be blocked if you use them.
Solution Stats
Solution Comments
Show commentsProblem Recent Solvers2
Suggested Problems
-
272 Solvers
-
Sum of odd numbers in a matrix
575 Solvers
-
4675 Solvers
-
4255 Solvers
-
85 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!