changed triggers, cutted createTable, modified content.handlebars

master
puskin94 2015-04-19 18:25:34 +02:00
parent 17caa96080
commit 4a70863822
5 changed files with 62 additions and 74 deletions

View File

@ -17,48 +17,35 @@ attribution github => ["https://github.com/puskin94", "puskin"];
triggers any => "game", "play";
handle remainder => sub {
(my $inputNum, my $dimension) = split/ /;
(my $inputNum, my $dimension) = split/ /;
return unless $inputNum;
# Play 128, 256, 512, 1024, 2048, 4096, 8192
return unless $inputNum % 128 == 0 && $inputNum <= 8192;
my $base = 128;
if (!$dimension || $dimension > 10 || $dimension < 3) {
$dimension = 4;
}
return unless (($inputNum == $base) || # play 128
($inputNum == $base*2) || # play 256
($inputNum == $base*4) || # play 512
($inputNum == $base*8) || # play 1024
($inputNum == $base*16) || # play 2048
($inputNum == $base*32) || # play 4096
($inputNum == $base*64)); # play 8192
my $text = 'Play '.$inputNum;
if (!$dimension || $dimension > 10 || $dimension < 3) {
$dimension = 4;
}
my $text = 'Play '.$inputNum;
return $text,
structured_answer => {
id => 'game2048',
name => '2048',
data => {
inputNum => $inputNum,
dimension => $dimension
},
templates => {
group => 'base',
item => 0,
options => {
content => 'DDH.game2048.content'
}
}
};
return $text,
structured_answer => {
id => 'game2048',
name => '2048',
data => {
inputNum => $inputNum,
dimension => $dimension
},
templates => {
group => 'text',
item => 0,
options => {
content => 'DDH.game2048.content'
}
}
};
};

View File

@ -1,7 +1,15 @@
<i><b>Play <span id="game">{{inputNum}}</span>
<div id="dimContainer"><span id="dimension">{{dimension}}</span> x {{dimension}}</b></i></div>
<div class="game2048__container">
<span class="counter"><span class="points">0</span> Points</span>
<div id="2048-area">
Play <span id="game">{{inputNum}}</span>
<div id="dimContainer"><span id="dimension">{{dimension}}</span> x {{dimension}}</div>
<span class="game2048__counter">
<span class="game2048__points">0</span> Points
</span>
<table id="game2048__area" class ="game2048__area">
{{#loop dimension}}
<tr>{{#loop dimension}}<td></td>{{/loop}}</tr>
{{/loop}}
</table>
</div>

View File

@ -1,14 +1,13 @@
.zci--game2048 .area {
.zci--game2048 .game2048__area {
table-layout: fixed;
border: 3px solid #776E65;
border-collapse: collapse;
margin-left: auto;
margin-right: auto;
box-shadow: 2px 2px 2px #999;
font-style: italic;
}
.zci--game2048 .area td, tr {
.zci--game2048 .game2048__area td, tr {
height: 50px;
width: 50px;
border: 3px solid #776E65;
@ -16,12 +15,11 @@
font-weight: bold;
}
.zci--game2048 .points {
.zci--game2048 .game2048__points {
font-weight: bold;
font-style: italic;
}
.zci--game2048 .counter {
.zci--game2048 .game2048__counter {
margin:auto;
display:table;
}

View File

@ -4,8 +4,11 @@ DDH.game2048.build = function(ops) {
// Global Variables Declaration
var $tempArea, $container, $WINNUM, $SIZE, $spanPoints,
goOn = true, started = false, area = [];
var $tempArea, $container, $WINNUM, $SIZE, $spanPoints;
var continueGame = true,
started = false,
area = [];
/* This function ( using 'transpose' and 'swapRows' )
@ -33,17 +36,24 @@ DDH.game2048.build = function(ops) {
} else {
// if a move can be made
if (moves !== 0) {
area[row-moves][col] = area[row][col];
area[row][col] = '';
flag=true;
}
i = row+1;
while(i < $SIZE && exit === false) {
// if numbers can be summed
if(area[row-moves][col] === area[i][col]) {
// sum numbers
area[row-moves][col] *= 2;
// delete the old number
area[i][col] = '';
// add points
points = area[row-moves][col];
flag = true; exit = true;
} else {
// else quit the while loop
@ -70,7 +80,7 @@ DDH.game2048.build = function(ops) {
increasePoints(points);
if (checkWin() || checkLose()) {
goOn = false;
continueGame = false;
flag = false;
}
/* This check is mandatory in order to avoid the appearance of a new
@ -196,7 +206,7 @@ recall the transpose() function (final state)
function swapRows() {
var nArea = [];
for(i = 0; i < $SIZE; i++) {
for(var i = 0; i < $SIZE; i++) {
nArea[$SIZE-1-i] = area[i];
}
return nArea;
@ -208,7 +218,7 @@ recall the transpose() function (final state)
function getRand() {
var rand=Math.floor(Math.random() * 11);
var rand = Math.floor(Math.random() * 11);
var posX, posY;
rand = (rand < 10) ? 2 : 4;
@ -274,20 +284,6 @@ recall the transpose() function (final state)
// This function creates and prints on page the gaming table
function createTable() {
var nCell = '';
var table = $('<table/>').attr("id","area").attr("class","area");
for (var i = 0; i < $SIZE; i++) {
nCell += '<td></td>';
}
for(var r = 0; r < $SIZE; r++){
table.append('<tr>' + nCell + '</tr>');
}
$($container).append(table);
}
function start() {
getArea();
getRand();
@ -305,13 +301,12 @@ recall the transpose() function (final state)
started = true;
$container = $('#2048-area');
$WINNUM = $('#game').html();
$SIZE = parseInt($("#dimension").html(), 10);
$spanPoints = $('.points');
$container = $('#game2048__container');
$WINNUM = ops.data[0].inputNum;
$SIZE = ops.data[0].dimension;
$spanPoints = $('.game2048__points');
createTable($container);
$tempArea = $('#area');
$tempArea = $('#game2048__area');
start();
$('html').keydown(function(event){
@ -321,7 +316,7 @@ recall the transpose() function (final state)
var move = false;
if (goOn) {
if (continueGame) {
if (event.keyCode === 87 || event.keyCode === 38) { // w or up arrow
move = mov('w');

View File

@ -25,7 +25,7 @@ ddg_goodie_test(
id => "game2048",
name => 2048,
templates => {
group => "base",
group => "text",
item => 0,
options => {
content => "DDH.game2048.content"
@ -46,7 +46,7 @@ ddg_goodie_test(
id => "game2048",
name => 2048,
templates => {
group => "base",
group => "text",
item => 0,
options => {
content => "DDH.game2048.content"