Semi-random starting position

I wrote a script that one of the players can run and then post the results to the chat:

function allowedCoord([newRow, newCol], placements) {
  return placements.every(([oldRow, oldCol]) => {
    if (oldRow === newRow) {
      return newCol < oldCol - 1 || oldCol + 1 < newCol;
    }

    if (oldCol === newCol) {
      return newRow < oldRow - 1 || oldRow + 1 < newRow;
    }

    return true;
  });
}

function randomPos() {
  return 3 + Math.floor(Math.random() * 15);
}

function newCoord(placements) {
  const coord = [randomPos(), randomPos()];

  if (allowedCoord(coord, placements)) {
    return coord;
  }

  return newCoord(placements);
}

function randomPlacements(n = 0, placements = []) {
  if (n <= 0) {
    return placements;
  }

  placements.push(newCoord(placements));

  return randomPlacements(n - 1, placements);
}

function transformRowCoord(pos) {
  const row = String.fromCodePoint(pos + 0x41);

  if (row === 'I') {
    // I is omitted from the board
    return 'R';
  }

  return row;
}

function getStones(n) {
  const placements = randomPlacements(n * 2);

  placements.forEach(([row, col], i) => {
    const player = i % 2 === 0 ? 'B' : 'W';
    const turn = i + 1;

    console.log(`${turn}: ${player} ${transformRowCoord(row)}${col}`);
  });
}

Simply open up your browser’s console, paste the above script in there, and type:

getStones(3);

if you want each players to get 3 stones.

After the random stones have been posted to chat, each player simply places their stones in turn.

Note There is a slight change from the above requirements. If an adjacent stone is encountered we will redraw (as opposed to pushing). This is to prevent a slightly increased chance of seeing one point jumps or caps. As well as preventing the awkward situation of drawing a stone in the center of a ponnuki.

8 Likes