Skip to content

Commit

Permalink
starting mancala game, board drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
slior committed Aug 31, 2020
1 parent 44be231 commit 0f71c1e
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 0 deletions.
47 changes: 47 additions & 0 deletions mancala/dist/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!doctype html>
<html>
<head>
<title>Mancala</title>
<style type="text/css">
canvas {
align : center;
}

textarea {
border: 1px solid black;
}

body {
font-family : Arial;
}

#gameOverMsg {
font-size: xx-large;
color: red;
border : 2px solid red;
}

</style>
<script src="main.js"></script>
<script>

function $(id) { return document.getElementById(id); }

var cnvs = null;

function setup()
{
//cnvs = main.initCanvas('cnvsMain')
main.initGame('cnvsMain');
}
</script>
</head>
<body onload="setup();">

<div width="100%" align="center">
<canvas id="cnvsMain" width="850" height="500">

</canvas>
</div>
</body>
</html>
20 changes: 20 additions & 0 deletions mancala/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "mancala",
"version": "1.0.0",
"description": "",
"private": "true",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"fabric": "^3.6.3"
},
"devDependencies": {
"webpack": "^4.44.1",
"webpack-cli": "^3.3.12"
}
}
70 changes: 70 additions & 0 deletions mancala/src/drawing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const {fabric} = require("fabric")
const {range} = require("./util.js")

function initCanvas(canvasEl)
{
let ret = canvasEl && (new fabric.Canvas(canvasEl));
if (ret)
{
ret.line = (x1,y1,x2,y2,c) => drawLineOn(ret,x1,y1,x2,y2,c);
}
return ret;
}

function drawLineOn(cnvs,x1,y1,x2,y2,color)
{
let l = new fabric.Line([x1,y1,x2,y2], {
stroke: color || 'black',
strokeWidth: 1,
selectable: false
});
cnvs.add(l);

}

function drawBoard(cnvs)
{
let CELL_SIZE = 50;
let CELL_COUNT = 14;

let TOP_LEFT = { x : 50, y : 50};

let playerCellCount = CELL_COUNT/2-1;
let boardWidthInCells = playerCellCount+2 //for the side cells (player homes)
let boardHeightInCells = 3;

//frame
horizLine(TOP_LEFT.x,TOP_LEFT.y,boardWidthInCells * CELL_SIZE); //top left to top right
verticalLine(TOP_LEFT.x,TOP_LEFT.y,CELL_SIZE*boardHeightInCells); //top left to bottom left
horizLine(TOP_LEFT.x,TOP_LEFT.y + CELL_SIZE*boardHeightInCells,boardWidthInCells * CELL_SIZE); // bottom left to bottom right
verticalLine(TOP_LEFT.x + boardWidthInCells * CELL_SIZE,TOP_LEFT.y,CELL_SIZE * boardHeightInCells); //top right to bottom right

//home cells
verticalLine(TOP_LEFT.x + CELL_SIZE,TOP_LEFT.y,CELL_SIZE*boardHeightInCells)
verticalLine(TOP_LEFT.x + CELL_SIZE*(boardWidthInCells-1),TOP_LEFT.y,CELL_SIZE*boardHeightInCells)

//cell horizontal lines
let upperCellY = TOP_LEFT.y + CELL_SIZE;
let lowerCellY = TOP_LEFT.y + CELL_SIZE*2;
let lineLen = (boardWidthInCells-2)*CELL_SIZE;

horizLine(TOP_LEFT.x + CELL_SIZE,upperCellY,lineLen)
horizLine(TOP_LEFT.x + CELL_SIZE,lowerCellY,lineLen)

//cell borders
range(2,CELL_COUNT/2).map(cellNum => {
verticalLine(TOP_LEFT.x + cellNum*CELL_SIZE,TOP_LEFT.y,CELL_SIZE)
verticalLine(TOP_LEFT.x + cellNum*CELL_SIZE,TOP_LEFT.y+CELL_SIZE*(boardHeightInCells-1),CELL_SIZE)
} )

function verticalLine(x,y,len) { cnvs.line(x,y,x,y+len); }

function horizLine(x,y,len) { cnvs.line(x,y,x+len,y); }

}

module.exports = {
drawLineOn : drawLineOn
, drawBoard : drawBoard
, initCanvas : initCanvas
}
14 changes: 14 additions & 0 deletions mancala/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@


const {initCanvas,drawBoard} = require("./drawing.js")


function initGame(cnvsELID)
{

drawBoard(initCanvas(cnvsELID));
}

module.exports = {
initGame : initGame
}
11 changes: 11 additions & 0 deletions mancala/src/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

function range(start,finish)
{ // TODO: validate 'target'
let ret = [start]
for (var i = start+1; i <= finish; i++) ret.push(i)
return ret;
}

module.exports = {
range : range
}
11 changes: 11 additions & 0 deletions mancala/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const path = require('path');

module.exports = {
entry: './src/index.js',
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'dist'),
library : 'main',
libraryTarget : 'var'
},
};

0 comments on commit 0f71c1e

Please sign in to comment.