Problem 53780. Backgammon #5 - Valid Move?

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.
Given a board position and a proposed move, this problem rerquires you to establish if the proposed move is a valid move for white, or not.
The input consists of a vector and a string; the vector describes a backgammon board position using the above notation and the string specifies a proposed move for a single stone based on the throw of a single dice, in a simplified standard notation.
For the purpose of this exercise, the move is expressed simply as 'starting_point/destination_point' using the point numbers as above, and using 'bar' if a stone is moving from the bar, and 'off' if a stone is bearing off the board. Some example proposed moves are thus:
'6/3' '24/18' '13/9' 'bar/22' and '6/off'
You should return true or false, depending on whether the proposed move is valid or not for the given board position. Possible reasons why a move might not be valid include:
  • White does not have a stone on the starting_point
  • Red has more than one stone on the destination_point
  • White has a stone on the bar and the move is not moving that stone off the bar
  • White is trying to bear off a stone, but not all white stones are in white's inner table (points 1 to 6)
There are a few more reasons why a move might not be valid, but I'll leave those for you to work out for yourself!
For this problem, you can assume that the board input vector represents a legal backgammon board position.
Next problem in series: Coming soon!
Regexp cheats and other cheats are not appreciated and will be blocked if you use them.

Solution Stats

2.22% Correct | 97.78% Incorrect
Last Solution submitted on Mar 21, 2022

Problem Comments

Solution Comments

Show comments

Problem Recent Solvers1

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!