niikita's snakes and ladders w/ gameplay n flow - #33
Conversation
| <!-- grid elements --> | ||
| <div class="wrapper"> | ||
|
|
||
| <div class="box" data-id="100"></div> |
There was a problem hiding this comment.
Recommend using a js loop to append these div elements to the .wrapper div. Data id can be added using jquery's .attr(), or .data(), or an id or class can also be used.
There was a problem hiding this comment.
append order can be 100-> 91, 81-> 90, 80-> 71 etc.
| <div class="box" data-id="9"></div> | ||
| <div class="box" data-id="10"></div> | ||
| </div> | ||
| <div class="left"> |
| }) | ||
|
|
||
| var runGame = () => { | ||
| $('div#gametext').show() |
| width: 80px; | ||
| height: 80px; | ||
| transition: all .2s ease-in; | ||
| opacity: 0; |
There was a problem hiding this comment.
is opacity needed? there is no background.
| background-repeat: no-repeat; | ||
| background-position: center; | ||
| background-size: 100%; | ||
| opacity: 1 |
There was a problem hiding this comment.
no-repeat not required if size is bigger than div and set to 100%.
Opacity do not need to be set to 1 if it was not set to 0 beforehand.
| <h3 id="newPos"></h4> | ||
| <h3 id="jump"></h4> | ||
| <h1 id="winner"></h1> | ||
| </div> |
There was a problem hiding this comment.
indentation for various parts need to be updated.
| @@ -0,0 +1,68 @@ | |||
|
|
|||
There was a problem hiding this comment.
reset.css is not done by her. It is resetting helper.
| // console.log('rollValue', roll); | ||
| var nextPos = lastMove + roll | ||
| // console.log('jump', getJump(nextPos)); | ||
| move = getJump(nextPos) === 0 ? nextPos : getJump(nextPos) |
There was a problem hiding this comment.
When the character move, the grid is updated but the old position is not deleted. Hence if the character takes a snake down. On the next character's turn, it takes the character's furthest traveled position due to grid.lastIndexOf() and starts the next position move from there, this invalidates the role of snakes in the game.
There was a problem hiding this comment.
agree. grid should have only one 1 and 2.
| } | ||
|
|
||
| function getJump(rollValue) { | ||
| if (jumps[rollValue]) { |
There was a problem hiding this comment.
maybe update to
if key is valid, return value.
instead of if value is valid, return value. Both works the same though
|
|
||
| } | ||
|
|
||
| var player = 1 |
|
|
||
| //function set grid | ||
| function playTurn() { | ||
| // console.log('PLAYER START:', player); |
There was a problem hiding this comment.
If possible, delete console.log() that is commented out. :)
| } | ||
|
|
||
| function changePlayer() { | ||
| player === 1 ? player = 2 : player = 1 |
There was a problem hiding this comment.
you may try this player = player === 1 ? 2 : 1
| player === 1 ? player = 2 : player = 1 | ||
| } | ||
|
|
||
| $(document).ready(function() { |
There was a problem hiding this comment.
I recommend you wrap all variables and functions into the ready function. $(function() { .... })
It will help you to avoid putting your variables and functions into global scope.
Everything in the global scope is weak to manipulation by others.
|
|
||
| $(document).ready(function() { | ||
| var $dice = $("#dice") | ||
| $dice.on('click', runGame) |
There was a problem hiding this comment.
What happens if I put runGame() as a second argument? And why?
|
|
||
| var runGame = () => { | ||
| $('div#gametext').show() | ||
| if (whoWon()) { |
| var $jump = $("#jump") | ||
|
|
||
| function showTexts(lastMove, roll, newPos, move, player) { | ||
| $diceImg.attr('src', "./assets/images/Dice-" + roll + ".png") |
There was a problem hiding this comment.
I hope you explorer this : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
| move = getJump(nextPos) === 0 ? nextPos : getJump(nextPos) | ||
| // console.log('move', move) | ||
| showTexts(lastMove, roll, nextPos, move, player) | ||
| move > 100 ? move = 100 : grid[move] = player |
There was a problem hiding this comment.
You may do this with if statement.
If(move > 100) move = 100
grid[move] = player
| // console.log('rollValue', roll); | ||
| var nextPos = lastMove + roll | ||
| // console.log('jump', getJump(nextPos)); | ||
| move = getJump(nextPos) === 0 ? nextPos : getJump(nextPos) |
There was a problem hiding this comment.
agree. grid should have only one 1 and 2.
| @@ -0,0 +1,91 @@ | |||
| var grid = Array(100).fill(0) | |||
There was a problem hiding this comment.
I gave you inappropriate answer about this, so remain new answer here.
Do you remember your var array = Array(10).fill(Array(10).fill(0)) was acting weird?
var array = Array(10).fill(Array(10).fill(0))
var shallowCopied = array.slice()
var deepCopied = array.map(e => e.slice())
array[0][3] = 1
console.log(shallowCopied)
console.log(deepCopied)
Try above.
That was because .slice() is just shallow copy, inside arrays of 2D were not copied at all. So, when we re-assign value there, our original array was changed also due to sharing references.
I hope you google about shallow copy vs deep copy if you want to know more. :)
No description provided.