<commit_before>/*
LightCycles

v0.1 K Ellis 19/12/2014

Inspired by the Sinclair ZX Spectrum classic by PSS from 1983 of the same name.
This is an experiment in HTML5's Canvas and Javascript.
*/

(function () {
  "use strict";

  //Initialise the 'global' variables
  var lc = {},
    canvas = document.getElementById('canvas'),
    context = canvas.getContext('2d');

  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;


  function initialiseGame(numberOfPlayers) {
    var iy, ix;

    lc.pixelSize = (Math.floor(canvas.width / 320) < Math.floor(canvas.height / 200)) ? Math.floor(canvas.width / 320) : Math.floor(canvas.height / 200);

    //Set player start position and directions.
    lc.player1 = {
      xPos: 289,
      yPos: 100,
      dir: 0,
      crashed: false
    };
    lc.player2 = {
      xPos: 30,
      yPos: 100,
      dir: 1,
      crashed: false
    };

    lc.inGame = true;
    lc.players = (numberOfPlayers);
    lc.endMessage = 'Welcome to LightCycles';

    //Clear the drawing area
    context.clearRect(0, 0, canvas.width, canvas.height);

    //(re)set the game array ready for the start of the game.
    lc.gameGrid = []; //320 x 200 grid
    lc.gameDrawn = []; //320 x 200 grid
    for (ix = 0; ix < 320; ix += 1) {
      for (iy = 0; iy < 200; iy += 1) {
        lc.gameGrid[ix * 320 + iy] = 0;
        lc.gameDrawn[ix * 320 + iy] = 0;
        if (iy === 0 || iy === 199) {
          //Replace top/bottom edge with a wall
          lc.gameGrid[ix * 320 + iy] = 1;
        }
        if (ix === 0 || ix === 319) {
          //Replace left/right edge with a wall
          lc.gameGrid[ix * 320 + iy] = 1;
        }
      }
    }
  }

  function drawGameGrid() {
    var iy, ix,
      xOffset, yOffset,
      block, drawn;


    xOffset = (canvas.width - (320 * lc.pixelSize)) / 2;
    yOffset = (canvas.height - (200 * lc.pixelSize)) / 2;

    for (ix = 0; ix < 320; ix += 1) {
      for (iy = 0; iy < 200; iy += 1) {

        //Check gameGrid against drawnGrid, so we only draw the deltas and don't
        //waste cyclers drawing stuff that is already on screen.
        block = lc.gameGrid[ix * 320 + iy];
        drawn = lc.gameDrawn[ix * 320 + iy];
        if (block !== drawn) {
          //Draw pixel grid

          switch (block) {
          case 1: //borders
            context.fillStyle = '#00e2ff';
            break;
          case 2: //Player 1 (red)
            context.fillStyle = '#ff0000';
            break;
          case 3: //player 2 (green)
            context.fillStyle = '#00ff00';
            break;
          default:
            context.fillStyle = '#000000';
            break;
          }

          //Draw a pre-defined wall
          //context.fillStyle = '#'+Math.floor(Math.random()*16777215).toString(16);
          context.fillRect((ix * lc.pixelSize) + xOffset, (iy * lc.pixelSize) + yOffset, lc.pixelSize, lc.pixelSize);
          lc.gameDrawn[ix * 320 + iy] = block;
        } //end draw pixel grid
      }
    }
  }

  function updatePlayerPositions() {

    if (lc.players === 1) {
      //if single player game, we'll update player 2 here.
      //The computer logic is simple, in that it aways turns the same way, unless
      //that way is blocked. I should add some randomness here!
      switch (lc.player2.dir) {
      case 0:
        if (lc.gameGrid[((lc.player2.xPos - 1) * 320) + lc.player2.yPos] !== 0) {
          if (lc.gameGrid[(lc.player2.xPos * 320) + (lc.player2.yPos - 1)] === 0) {
            lc.player2.dir = 2;
          } else {
            lc.player2.dir = 3;
          }
        }
        break;
      case 1:
        if (lc.gameGrid[((lc.player2.xPos + 1) * 320) + lc.player2.yPos] !== 0) {
          if (lc.gameGrid[(lc.player2.xPos * 320) + (lc.player2.yPos + 1)] === 0) {
            lc.player2.dir = 3;
          } else {
            lc.player2.dir = 2;
          }
        }
        break;
      case 2:
        if (lc.gameGrid[(lc.player2.xPos * 320) + (lc.player2.yPos - 1)] !== 0) {
          if (lc.gameGrid[((lc.player2.xPos - 1) * 320) + lc.player2.yPos] !== 0) {
            lc.player2.dir = 1;
          } else {
            lc.player2.dir = 0;
          }
        }
        break;
      case 3:
        if (lc.gameGrid[(lc.player2.xPos * 320) + (lc.player2.yPos + 1)] !== 0) {
          if (lc.gameGrid[((lc.player2.xPos + 1) * 320) + lc.player2.yPos] !== 0) {
            lc.player2.dir = 0;
          } else {
            lc.player2.dir = 1;
          }
        }
        break;
      }
    }

    //Player 1
    switch (lc.player1.dir) {
    case 0: //left
      lc.player1.xPos -= 1;
      break;
    case 1: //left
      lc.player1.xPos += 1;
      break;
    case 2: //up
      lc.player1.yPos -= 1;
      break;
    case 3: //down
      lc.player1.yPos += 1;
      break;
    }
    if (lc.gameGrid[(lc.player1.xPos * 320) + lc.player1.yPos] === 0) {
      lc.gameGrid[(lc.player1.xPos * 320) + lc.player1.yPos] = 2;
    } else {
      lc.player1.crashed = true;
    }

    //Player 2
    switch (lc.player2.dir) {
    case 0: //left
      lc.player2.xPos -= 1;
      break;
    case 1: //left
      lc.player2.xPos += 1;
      break;
    case 2: //up
      lc.player2.yPos -= 1;
      break;
    case 3: //down
      lc.player2.yPos += 1;
      break;
    }
    if (lc.gameGrid[(lc.player2.xPos * 320) + lc.player2.yPos] === 0) {
      lc.gameGrid[(lc.player2.xPos * 320) + lc.player2.yPos] = 3;
    } else {
      lc.player2.crashed = true;
    }
  }

  function checkForPlayerCrash() {
    //Someone has crashed, so what's the score alfy?    
    if ((lc.player1.crashed === true && lc.player2.crashed === true) || ((lc.player1.xPos === lc.player2.xPos) && (lc.player1.yPos === lc.player2.yPos))) {
      //it's a draw!
      lc.inGame = false;
      return "Both players have crashed! It's a draw!";
    }

    if (lc.player1.crashed === true) {
      //Player 1<commit_msg>Add some randomness to the computer controlled player<commit_after>/*
LightCycles

v0.1 K Ellis 19/12/2014

Inspired by the Sinclair ZX Spectrum classic by PSS from 1983 of the same name.
This is an experiment in HTML5's Canvas and Javascript.
*/

(function () {
  "use strict";

  //Initialise the 'global' variables
  var lc = {},
    canvas = document.getElementById('canvas'),
    context = canvas.getContext('2d');

  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;


  function initialiseGame(numberOfPlayers) {
    var iy, ix;

    lc.pixelSize = (Math.floor(canvas.width / 320) < Math.floor(canvas.height / 200)) ? Math.floor(canvas.width / 320) : Math.floor(canvas.height / 200);

    //Set player start position and directions.
    lc.player1 = {
      xPos: 289,
      yPos: 100,
      dir: 0,
      crashed: false
    };
    lc.player2 = {
      xPos: 30,
      yPos: 100,
      dir: 1,
      crashed: false
    };

    lc.inGame = true;
    lc.players = (numberOfPlayers);
    lc.endMessage = 'Welcome to LightCycles';

    //Clear the drawing area
    context.clearRect(0, 0, canvas.width, canvas.height);

    //(re)set the game array ready for the start of the game.
    lc.gameGrid = []; //320 x 200 grid
    lc.gameDrawn = []; //320 x 200 grid
    for (ix = 0; ix < 320; ix += 1) {
      for (iy = 0; iy < 200; iy += 1) {
        lc.gameGrid[ix * 320 + iy] = 0;
        lc.gameDrawn[ix * 320 + iy] = 0;
        if (iy === 0 || iy === 199) {
          //Replace top/bottom edge with a wall
          lc.gameGrid[ix * 320 + iy] = 1;
        }
        if (ix === 0 || ix === 319) {
          //Replace left/right edge with a wall
          lc.gameGrid[ix * 320 + iy] = 1;
        }
      }
    }
  }

  function drawGameGrid() {
    var iy, ix,
      xOffset, yOffset,
      block, drawn;


    xOffset = (canvas.width - (320 * lc.pixelSize)) / 2;
    yOffset = (canvas.height - (200 * lc.pixelSize)) / 2;

    for (ix = 0; ix < 320; ix += 1) {
      for (iy = 0; iy < 200; iy += 1) {

        //Check gameGrid against drawnGrid, so we only draw the deltas and don't
        //waste cyclers drawing stuff that is already on screen.
        block = lc.gameGrid[ix * 320 + iy];
        drawn = lc.gameDrawn[ix * 320 + iy];
        if (block !== drawn) {
          //Draw pixel grid

          switch (block) {
          case 1: //borders
            context.fillStyle = '#00e2ff';
            break;
          case 2: //Player 1 (red)
            context.fillStyle = '#ff0000';
            break;
          case 3: //player 2 (green)
            context.fillStyle = '#00ff00';
            break;
          default:
            context.fillStyle = '#000000';
            break;
          }

          //Draw a pre-defined wall
          context.fillRect((ix * lc.pixelSize) + xOffset, (iy * lc.pixelSize) + yOffset, lc.pixelSize, lc.pixelSize);
          lc.gameDrawn[ix * 320 + iy] = block;
        } //end draw pixel grid
      }
    }
  }

  function updatePlayerPositions() {
    var choice;
    if (lc.players === 1) {
      choice = Math.floor(Math.random() * 2); //Random turn direction if computer is 1 pixel from crashing.
      //if single player game, we'll update player 2 here.
      switch (lc.player2.dir) {
      case 0: //Left
        if (lc.gameGrid[((lc.player2.xPos - 1) * 320) + lc.player2.yPos] !== 0) {
          if (choice === 0) {
            if (lc.gameGrid[(lc.player2.xPos * 320) + (lc.player2.yPos - 1)] === 0) {
              lc.player2.dir = 2;
            } else {
              lc.player2.dir = 3;
            }
          } else {
            if (lc.gameGrid[(lc.player2.xPos * 320) + (lc.player2.yPos + 1)] === 0) {
              lc.player2.dir = 3;
            } else {
              lc.player2.dir = 2;
            }
          }
        }
        break;

      case 1: //Right
        if (lc.gameGrid[((lc.player2.xPos + 1) * 320) + lc.player2.yPos] !== 0) {
          if (choice === 0) {
            if (lc.gameGrid[(lc.player2.xPos * 320) + (lc.player2.yPos + 1)] === 0) {
              lc.player2.dir = 3;
            } else {
              lc.player2.dir = 2;
            }
          } else {
            if (lc.gameGrid[(lc.player2.xPos * 320) + (lc.player2.yPos - 1)] === 0) {
              lc.player2.dir = 2;
            } else {
              lc.player2.dir = 3;
            }
          }
        }
        break;

      case 2: //Up
        if (lc.gameGrid[(lc.player2.xPos * 320) + (lc.player2.yPos - 1)] !== 0) {
          if (choice === 0) {
            if (lc.gameGrid[((lc.player2.xPos - 1) * 320) + lc.player2.yPos] !== 0) {
              lc.player2.dir = 1;
            } else {
              lc.player2.dir = 0;
            }
          } else {
            if (lc.gameGrid[((lc.player2.xPos + 1) * 320) + lc.player2.yPos] !== 0) {
              lc.player2.dir = 0;
            } else {
              lc.player2.dir = 1;
            }
          }
        }
        break;

      case 3: //Down
        if (lc.gameGrid[(lc.player2.xPos * 320) + (lc.player2.yPos + 1)] !== 0) {
          if (choice === 0) {
            if (lc.gameGrid[((lc.player2.xPos + 1) * 320) + lc.player2.yPos] !== 0) {
              lc.player2.dir = 0;
            } else {
              lc.player2.dir = 1;
            }
          } else {
            if (lc.gameGrid[((lc.player2.xPos - 1) * 320) + lc.player2.yPos] !== 0) {
              lc.player2.dir = 1;
            } else {
              lc.player2.dir = 0;
            }
          }
        }
        break;

      }
    } //end computer controlled player.

    //Player 1
    switch (lc.player1.dir) {
    case 0: //left
      lc.player1.xPos -= 1;
      break;
    case 1: //right
      lc.player1.xPos += 1;
      break;
    case 2: //up
      lc.player1.yPos -= 1;
      break;
    case 3: //down
      lc.player1.yPos += 1;
      break;
    }
    if (lc.gameGrid[(lc.player1.xPos * 320) + lc.player1.yPos] === 0) {
      lc.gameGrid[(lc.player1.xPos * 320) + lc.player1.yPos] = 2;
    } else {
      lc.player1.crashed = true;
    }

    //Player 2
    switch (lc.player2.dir) {
    case 0: //left
      lc.player2.xPos -= 1;
      break;
    case 1: //right
      lc.player2.xPos += 1;
      break;
    case 2: //up
      lc.player2.yPos -= 1;
      break;
    case 3: //down
      lc.player2.yPos += 1;
      break;
    }
    if (lc.gameGrid[(lc.player2.xPos * 320) + lc.player2.yPos] === 0) {
      lc.gameGrid[(lc.player2.xPos * 320) + lc.player2.yPos] = 3;
    } else {
      lc.player2.crashed = true;
    }
  }

  function checkForPlayerCrash() {
    //Someone has crashed, so what's the score alfy?    
    if ((lc.player1.crashed === true && lc.player2.crashed === true) || ((lc.player1.xPos === lc.player2.xPos) && (lc.player1.yPos === lc.player2.yPos))) {
      //it's a draw!
      lc.inGame = false;
      return "Both players have crashed! It's a draw!";
    }

    if (lc.player1.crashed === true) {
      //Player 1
{'fraction_non_alphanumeric': 0.15075728155339807, 'fraction_numerical': 0.04831067961165048, 'mean_word_length': 1.5813953488372092, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4960210', 'n_tokens_mistral': 5090, 'n_tokens_neox': 4482, 'n_words': 1121}