-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 337b68b
Showing
8 changed files
with
383 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright 2018 RealPolluX | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Conway's 'Game of Life' | ||
Implementation of Conway's 'Game of Life' in PHP and JavaScript. | ||
|
||
## Requirements | ||
- PHP 7.2+ | ||
- JavaScript client with ES6+ support | ||
|
||
## License | ||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
a, a:focus, a:hover { | ||
color: #fff; | ||
} | ||
.btn-secondary, .btn-secondary:hover, .btn-secondary:focus { | ||
color: #333; | ||
text-shadow: none; | ||
background-color: #fff; | ||
border: .05rem solid #fff; | ||
} | ||
html, body { | ||
height: 100%; | ||
background-color: #333; | ||
} | ||
body { | ||
display: -webkit-box; | ||
display: flex; | ||
-webkit-box-pack: center; | ||
justify-content: center; | ||
color: #fff; | ||
text-shadow: 0 .05rem .1rem rgba(0, 0, 0, .5); | ||
box-shadow: inset 0 0 5rem rgba(0, 0, 0, .5); | ||
} | ||
.container { | ||
max-width: 84em; | ||
margin-top: 24px; | ||
} | ||
.masthead { | ||
margin-bottom: 2rem; | ||
} | ||
.masthead-brand { | ||
margin-bottom: 0; | ||
} | ||
.nav-masthead .nav-link { | ||
padding: .25rem 0; | ||
font-weight: 700; | ||
color: rgba(255, 255, 255, .5); | ||
background-color: transparent; | ||
border-bottom: .25rem solid transparent; | ||
} | ||
.nav-masthead .nav-link:hover, .nav-masthead .nav-link:focus { | ||
border-bottom-color: rgba(255, 255, 255, .25); | ||
} | ||
.nav-masthead .nav-link + .nav-link { | ||
margin-left: 1rem; | ||
} | ||
.nav-masthead .active { | ||
color: #fff; | ||
border-bottom-color: #fff; | ||
} | ||
@media (min-width: 48em) { | ||
.masthead-brand { | ||
float: left; | ||
} | ||
.nav-masthead { | ||
float: right; | ||
} | ||
} | ||
.cover { | ||
padding: 0 1.5rem; | ||
} | ||
.cover .btn-lg { | ||
padding: .75rem 1.25rem; | ||
font-weight: 700; | ||
} | ||
.mastfoot { | ||
position: absolute; | ||
color: rgba(255, 255, 255, .5); | ||
bottom: 0; | ||
right: 0; | ||
margin-right: 16px; | ||
} | ||
canvas#board{ | ||
background-color: #01579B; | ||
width: 65%; | ||
box-shadow: 0 0 300px #01579B; | ||
border-radius: 16px; | ||
z-index: 400; | ||
} | ||
#title{ | ||
z-index: 500; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// global vars | ||
const gameSize = 64; | ||
const canvasSize = 512; | ||
|
||
let gameInterval = null; | ||
let paused = false; | ||
|
||
// Ready up the game board | ||
const canvas = document.getElementById('board'); | ||
canvas.width = canvasSize; | ||
canvas.height = canvasSize; | ||
|
||
const context = canvas.getContext('2d'); | ||
|
||
function drawCanvas(jsonObject, context) { | ||
const scale = canvasSize / gameSize; | ||
|
||
// Clear the game board | ||
context.clearRect(0, 0, canvasSize, canvasSize); | ||
|
||
context.fillStyle = '#212121'; | ||
context.globalAlpha = 0.85; | ||
|
||
// Iterate over all rows | ||
for (let row = 0; row < gameSize; row++) { | ||
// Iterate over all columns | ||
for (let column = 0; column < gameSize; column++) { | ||
// Cell is alive | ||
if (jsonObject[row][column] === 1) { | ||
// Draw the cell | ||
context.fillRect(row * scale, column * scale, scale, scale); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Set button actions | ||
// Functions to pause and start/stop the game | ||
document.getElementById('startBtn').onclick = function() { | ||
if (gameInterval === null) { | ||
let gameBoard = []; | ||
|
||
gameInterval = setInterval(async () => { | ||
gameBoard = await getNextState(gameBoard); | ||
drawCanvas(gameBoard, context); | ||
}, 100); | ||
} | ||
}; | ||
|
||
document.getElementById('pauseBtn').onclick = function() { | ||
if (gameInterval !== null) { | ||
paused = !paused; | ||
} | ||
}; | ||
|
||
document.getElementById('stopBtn').onclick = function() { | ||
if (gameInterval !== null) { | ||
clearInterval(gameInterval); | ||
gameInterval = null; | ||
} | ||
}; | ||
|
||
// game 'loop' | ||
async function getNextState(gameBoard) { | ||
if (paused) return; | ||
|
||
// get the next state from the server | ||
const response = await fetch('http://localhost:8080/index.php', { | ||
method: 'POST', | ||
body: JSON.stringify(gameBoard), | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-Game-Of-Life': 1 | ||
} | ||
}); | ||
|
||
return await response.json(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
<?php | ||
/** | ||
* Copyright © 2018 | ||
* "GameOfLife" - Brought to you by: | ||
* ___________ ________ __ | ||
* \__ ___/___ _____ _____ \_____ \ __ _______ _____/ |_ __ __ _____ | ||
* | |_/ __ \\__ \ / \ / / \ \| | \__ \ / \ __\ | \/ \ | ||
* | |\ ___/ / __ \| Y Y \/ \_/. \ | // __ \| | \ | | | / Y Y \ | ||
* |____| \___ >____ /__|_| /\_____\ \_/____/(____ /___| /__| |____/|__|_| / | ||
* \/ \/ \/ \__> \/ \/ \/ | ||
* https://github.com/Team-Quantum | ||
* .PolluX / https://github.com/RealPolluX | ||
* Created @ 2018-12-09 - 11:01 | ||
* | ||
* | ||
* Simple implementation of https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life | ||
* Drawn with canvas and calculations made in php. | ||
* | ||
* RULE #1 | ||
* ------------------------------------------------ | ||
* An alive cell with less then 2 or more than 4 | ||
* neighbors dies. | ||
* | ||
* RULE #2 | ||
* ------------------------------------------------ | ||
* A dead cell with 3 neighbors turns alive. | ||
* | ||
*/ | ||
|
||
// TODO: colors: black = living, white = dead cell | ||
// TODO: drawing in frontend | ||
|
||
// show errors on page | ||
ini_set('display_errors', 0); | ||
error_reporting(0); | ||
|
||
const GAME_SIZE = 64; | ||
|
||
|
||
/** | ||
* Generates a new game board with randomized living cells. | ||
* | ||
* @return array game board | ||
*/ | ||
function generateGameBoard(): array | ||
{ | ||
$grid = []; | ||
|
||
for ($i = 0; $i < GAME_SIZE; $i++) { // x | ||
array_push($grid, []); | ||
|
||
for ($j = 0; $j < GAME_SIZE; $j++){ // y | ||
array_push($grid[$i], rand(0, 1)); | ||
} | ||
} | ||
|
||
return $grid; | ||
} | ||
|
||
/** | ||
* Gets the number of living neighbors for a specific cell | ||
* | ||
* @param array $grid | ||
* @param $l | ||
* @param $m | ||
* | ||
* @return int number of living neighbors | ||
*/ | ||
function getAliveNeighbors(array $grid, $l, $m): int | ||
{ | ||
$aliveNeighbours = 0; | ||
for ($i = -1; $i <= 1; $i++) { | ||
for ($j = -1; $j <= 1; $j++) { | ||
$aliveNeighbours += $grid[$l + $i][$m + $j]; | ||
} | ||
} | ||
|
||
return $aliveNeighbours; | ||
} | ||
|
||
/** | ||
* Generates the next generation which | ||
* will be send to the frontend | ||
* | ||
* @param $grid | ||
* @param $height int which represents the board height (y) | ||
* @param $width int which represents the board width (x) | ||
* | ||
* @return array | ||
*/ | ||
function getNextGeneration($grid, $height, $width): array | ||
{ | ||
$futureGrid = $grid; | ||
|
||
// Loop through all cells | ||
for ($l = 1; $l < $height - 1; $l++) { | ||
for ($m = 1; $m < $width - 1; $m++) { | ||
// get number of nearby cells, that are alive | ||
$aliveNeighbours = getAliveNeighbors($grid, $l, $m); | ||
|
||
// Update the grid (data from last tick) - already counted cell will be replaced | ||
$aliveNeighbours -= $grid[$l][$m]; | ||
|
||
// RULE #1 :: no active members, death follows | ||
if (($grid[$l][$m] == 1) && ($aliveNeighbours < 2)) { | ||
$futureGrid[$l][$m] = 0; | ||
} // RULE #1 :: overpopulation, death follows | ||
elseif (($grid[$l][$m] == 1) && ($aliveNeighbours > 3)) { | ||
$futureGrid[$l][$m] = 0; | ||
} // RULE #2 :: new life | ||
elseif (($grid[$l][$m] == 0) && ($aliveNeighbours == 3)) { | ||
$futureGrid[$l][$m] = 1; | ||
} // no action necessary, copy to new/future array | ||
else { | ||
$futureGrid[$l][$m] = $grid[$l][$m]; | ||
} | ||
} | ||
} | ||
|
||
return $futureGrid; | ||
} | ||
|
||
/** | ||
* Two requests are possible: | ||
* (1) normal get request | ||
* (2) get request with special header | ||
* | ||
* The normal request made against this script will deliver the frontend code (html, css, js) and | ||
* the modified request requires a json payload and the header "X_GAME_OF_LIFE". The response will be a | ||
* new json object with the data for the next tick. | ||
*/ | ||
if (array_key_exists('HTTP_X_GAME_OF_LIFE', $_SERVER)) { | ||
// we got a json/update request, calculate next cycle | ||
header('Content-Type: application/json'); | ||
|
||
// get json body | ||
$body = file_get_contents('php://input'); | ||
|
||
// do calculations | ||
try { | ||
$jsonArray = json_decode($body); | ||
|
||
// on first request, there will be no data, so return the default array | ||
if (count($jsonArray) === 0) { | ||
header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK', true, 200); | ||
exit(json_encode(generateGameBoard())); | ||
} | ||
|
||
$nextGeneration = getNextGeneration($jsonArray, GAME_SIZE, GAME_SIZE); | ||
} catch (Exception $exception) { | ||
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500); | ||
exit('{"type": "error", "message": "' . $exception->getMessage() . '"}'); | ||
} | ||
|
||
// send content to frontend | ||
header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK', true, 200); | ||
exit(json_encode($nextGeneration)); | ||
|
||
} else { | ||
// normal page request, send index html | ||
require 'page.html'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Conway's Game of Life</title> | ||
|
||
<!-- Icon from: https://thenounproject.com/term/conways-game-of-life/14951/ --> | ||
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"> | ||
<link rel="icon" href="/favicon.ico" type="image/x-icon"> | ||
|
||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | ||
<meta name="description" content="Frontend for Conway's Game of Life."> | ||
<meta name="author" content="RealPolluX"> | ||
|
||
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"> | ||
<link href="cover.css" rel="stylesheet"> | ||
</head> | ||
|
||
<body class="text-center"> | ||
<div class="container"> | ||
<h2 id="title"><a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" target="_blank">Conway's Game of Life</a></h2> | ||
<br> | ||
|
||
<canvas height="500" width="500" id="board"></canvas> | ||
<hr> | ||
|
||
<div class="buttons"> | ||
<div class="btn-group" role="group"> | ||
<button type="button" id="startBtn" class="btn btn-lg btn-success">Start</button> | ||
<button type="button" id="pauseBtn" class="btn btn-lg btn-secondary">Pause</button> | ||
<button type="button" id="stopBtn" class="btn btn-lg btn-danger">Stop</button> | ||
</div> | ||
</div> | ||
</div> | ||
<footer class="mastfoot mt-auto"> | ||
<div class="inner"> | ||
<p>Created by <a href="https://github.com/RealPolluX">RealPolluX</a>.</p> | ||
</div> | ||
</footer> | ||
|
||
<script src="game.js"></script> | ||
</body> | ||
|
||
</html> |