Git Product home page Git Product logo

fend19-js1-five-in-a-row's Introduction

FEND19 - JavaScript 1 - Five In a Row

preview

Description

Five In a Row is an abstract strategy board game. Players alternate turns placing a marker of their color on an empty board cell. The goal of the game is to order unbroken row of five markers horizontally, vertically, or diagonally.

Win condition check

When a cell is clicked it's added to the list of cells owned by the active player.

// all cell element IDs follow "cell-x-y" format
const coordinates = thisCell.getAttribute("id").match(/\d+/g);
const clickedCell = { x: Number(coordinates[0]), y: Number(coordinates[1]) };

PLAYER_INFO[activePlayer]["cells"].push(clickedCell);

After each successful click on a cell the game checks if the active player has won.

if (hasPlayerWon(clickedCell, activePlayer) === true) {
  alert(PLAYER_INFO[activePlayer]["name"] + " wins !!!");
  clearBoard();
} else if (countAllClickedCells() === BOARD_WIDTH * BOARD_HEIGHT) {
  alert("Draw !!!");
  clearBoard();
}

Owned cells are used to determine if one of the winning conditions was triggered.

function hasPlayerWon(clickedCell, activePlayer) {
  const ownedCells = PLAYER_INFO[activePlayer]["cells"];
  if (countHorizontalNeighbors(clickedCell) >= WINNING_LINE_LENGTH - 1) return true;
  else if (countVerticalNeighbors(clickedCell) >= WINNING_LINE_LENGTH - 1) return true;
  // and so on

Win conditions are determined by counting consecutive cells in various directions.

function countHorizontalNeighbors(clickedCell) {
  return consecutiveCells(clickedCell, getNeighbourW) + consecutiveCells(clickedCell, getNeighbourE);
}

The number of consecutive cells is calculated by looking for a neigbour in a specific direction. If a neighbour is found the process is repeated with the new-found cell as the new starting point.

function consecutiveCells(clickedCell, neighbourDirectionCheck) {
  let neighbours = 0;
  const pointerCell = { x: clickedCell.x, y: clickedCell.y };
  const maxDistance = Math.max(BOARD_WIDTH, BOARD_HEIGHT);
  while (neighbours < maxDistance) {
    const neighbourCell = neighbourDirectionCheck(pointerCell);
    if (neighbourCell) {
      neighbours++;
      pointerCell.x = neighbourCell.x;
      pointerCell.y = neighbourCell.y;
    } else {
      break;
    }
  }
  return neighbours;
}

Direction checks search ownedCells array for a cell with coordinates that a neigbour cell in that direction would have to have.

function getNeighbourN(cell) {
  return ownedCells.find(neighbor => neighbor.y - 1 === cell.y && neighbor.x === cell.x);
}

fend19-js1-five-in-a-row's People

Contributors

dmitrijv avatar

Watchers

James Cloos avatar  avatar

Forkers

mahmudalhakim

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.