Ad

Hello! I have a lot of difficulties to write a good description of this kata. I published it some time ago but I met a lot of critics with that description and received good evaluations of the task. I tried to rewrite it several times but it would not get better. If you like thaŠµ kata and you are interested in helping me it would be great!

image for description

What we have:

  • Input: a table pool (two-dimensional array);

  • "X" is a part of the table or cell;

  • One white cue ball is represented by number >=1. This number means the force of shot: one point of the force is equal to one movement of a ball, so the ball with number 3 can move 3 cells (to move to the next cell you should subtract one point from its force). After collision with a coloured ball it gives the rest points of force to a coloured ball and disappears (the white ball only determines the direction).

  • One coloured ball is represented by "O".

  • Pockets are represented by "U". They can be in any place on the borders of the table;

  • The output is: a boolean (whether the coloured ball rolled into a pocket) and a pair of integers (the final coordinates of the coloured ball).

Rules

The balls can move only to their adjacent cells in the horizontal, vertical ou diagonal direction. The line

You should shot the white ball in the direction of the coloured ball using that way that results in the imaginable straight line between them:

Possible collisions:

1) ["O", "X", "X"]  2) ["X", "X", "X"]  3) ["X", "4", "X"]  
   ["X", "X", "X"]     ["O", "X", "4"]     ["X", "X", "X"]  
   ["X", "X", "4"]     ["X", "X", "X"]     ["X", "O", "X"]  

No collisions:

1) ["X", "4", "X"]  2) ["X", "X", "X", "X"]
   ["X", "X", "X"]     ["X", "X", "X", "O"]
   ["O", "X", "X"]     ["4", "X", "X", "X"]

If the white ball can't reach the coloured ball by that way, a collision won't happen.

In the case of collision, the coloured ball "O" starts to move(in the same direction that the white ball moved) until it stops because its force is over or until it enters a pocket. If it hits the "table wall", the ball changes its angle of movement according to physics.

For example, in this case, after collision the ball "O" goes to the opposite direction:

["X", "O", "X"]  ["X", "O", "X"]  ["X", "X", "X"]  ["X", "X", "X"]  ["X", "X", "X"]
["X", "4", "X"]=>["X", "X", "X"]=>["X", "O", "X"]=>["X", "X", "X"]=>["X", "O", "X"]
["X", "X", "X"]  ["X", "X", "X"]  ["X", "X", "X"]  ["X", "O", "X"]  ["X", "X", "X"]

And in the next case, it "reflects":

 ["X", "O", "X"]  ["X", "O", "X"]  ["X", "X", "X"]  ["X", "X", "X"]  ["X", "X", "X"]
 ["4", "X", "X"]=>["X", "X", "X"]=>["X", "X", "O"]=>["X", "X", "X"]=>["O", "X", "X"]
 ["X", "X", "X"]  ["X", "X", "X"]  ["X", "X", "X"]  ["X", "O", "X"]  ["X", "X", "X"]  

Return true or false if the coloured ball did enter a pocket or not and the coordinates of coloured ball.

Notes

The size of a table can be different but always rectangular.

There are only two balls.

Examples

let table = [["U", "X", "O", "X"], // the collision is possible
             ["X", "X", "X", "X"],
             ["9", "X", "U", "X"]]

 table = [["U", "X", "O", "X"], 
          ["X", "8", "X", "X"], // <-- one movement spends one point of force
          ["X", "X", "U", "X"]]

 table = [["U", "X", "O", "X"], // <-- 7 points of force (the white ball was removed)
          ["X", "X", "X", "X"],
          ["X", "X", "U", "X"]]

 table = [["U", "X", "X", "X"], 
          ["X", "X", "X", "O"],// <--6 points
          ["X", "X", "U", "X"]]
          
 table = [["U", "X", "X", "X"], 
          ["X", "X", "X", "X"],
          ["X", "X", "U", "X"]] // <--the coloured ball entered a pocket

 return [true, [2, 2] ]  
let table = [["U", "X", "X"], // the collision is possible,
             ["1", "O", "U"], // but there is not enough force to reach a pocket
             ["X", "X", "X"]]
return [false, [1, 1] ]
let table =  [["U", "X", "X", "X"], // the collision is not possible
             ["X", "X", "X", "O"],
             ["9", "X", "U", "X"]]
return [false, [1, 3] ]

!The last critic of a Moderator:

  1. would be more readable to have the input as an array of strings, rather than an array of arays of strings, no? (and you could use whitespaces instead of X for "empty" places)

  2. the description really needs a rewrite... ;) General advises:
    a) don't give the examples before the specifications
    b) especially, don't mix explanations and examples
    c) give the general idea of the task right at the beginning before going into details. Like, that's the very first thing that should be in the description, before the picture.

  3. I believe you didn't specify that no pockets will ever be under the starting positions of the balls

  4. the notes are parts of the specs

  5. did you say that the force/speed may be greater than 9 (and is never negative)?

*3 - I'm going t fix it

function poolgame(table) {
         let force;
         let coordBall1 = {};
         let coordBall2 = {};

         for (let i = 0; i < table.length; i++) {
            for (let k = 0; k < table[i].length; k++) {
               if (Object.keys(coordBall1).length && Object.keys(coordBall2).length) break;
               if (!isNaN(table[i][k])) {
                  coordBall1.X = k;
                  coordBall1.Y = i;
                  force = table[i][k];
               } else if (table[i][k] == "O") {
                  coordBall2.X = k;
                  coordBall2.Y = i;
               }
            }
         }

         let dir = { X: 0, Y: 0 };
         let y = Math.abs(coordBall1.Y - coordBall2.Y);
         let x = Math.abs(coordBall1.X - coordBall2.X);

         if (coordBall1.Y === coordBall2.Y) {
            force -= x;
         } else if (coordBall1.X === coordBall2.X) {
            force -= y;
         } else {
            if (y !== x) return [false, [coordBall2.Y, coordBall2.X]]; //check if there is no collision
            force -= x;
         }
         if (force <= 0) return [false, [coordBall2.Y, coordBall2.X]]; //check if there is no collision 

         dir.X = (coordBall1.X > coordBall2.X) ? -1 : (coordBall1.X === coordBall2.X) ? 0 : 1;
         dir.Y = (coordBall1.Y > coordBall2.Y) ? -1 : (coordBall1.Y === coordBall2.Y) ? 0 : 1;

         let tableLimit = {
            X: table[0].length - 1,
            Y: table.length - 1
         }

         while (force--) {
            let possibleX = coordBall2.X + dir.X;
            let possibleY = coordBall2.Y + dir.Y;
            if (possibleX > tableLimit.X || possibleX < 0) dir.X = -dir.X;
            if (possibleY > tableLimit.Y || possibleY < 0) dir.Y = -dir.Y;
            coordBall2.X += dir.X;
            coordBall2.Y += dir.Y;
            if (table[coordBall2.Y][coordBall2.X] === "U") return [true, [coordBall2.Y, coordBall2.X]];
         }
         return [false, [coordBall2.Y, coordBall2.X]];
      }