Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

niikita's snakes and ladders w/ gameplay n flow #33

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
101 changes: 36 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Project Name (Start editing here)
# Snakes and Ladders - Childhood game now on the web!
<!---
Read Me Contents
-->
Expand All @@ -7,90 +7,61 @@ Read Me Contents

### Overview

Let's start out with something fun - **a game!**
The classic and ancient board game of **snakes and ladders** is now available a click away. Play with a friend or play with computer.

Everyone will get a chance to **be creative**, and work through some really **tough programming challenges** – since you've already gotten your feet wet with Tic Tac Toe, it's up to you to come up with a fun and interesting game to build.
### Link to the game
[-->Access The game here<--](https://nikitas89.github.io/project-1/) OR
https://nikitas89.github.io/project-1/

**You will be working individually for this project**, but we'll be guiding you along the process and helping as you go. Show us what you've got!
### How to play - user instructions
Easy, just:
1. Throw the dice and whoever gets the highest score starts the game.
2. Player throws dice again
3. Play turns
1. The player's token moves across the board over the squares based on the dice value
2. Other player gets turn
4. For snakes and ladders
1. If player lands on a ladder's bottom rung, whohoo! you get to climb up to the end of the ladder and fast forward many paces.
2. If player lands on a snake head, rough luck. Move back down to the square in the snake tail.
5. Keep playing between players


---

### Technical Requirements

Your app must:

* **Render a game in the browser**
* **Any number of players** will be okay, switch turns will be great
* **Design logic for winning** & **visually display which player won**
* **Include separate HTML / CSS / JavaScript files**
* Stick with **KISS (Keep It Simple Stupid)** and **DRY (Don't Repeat Yourself)** principles
* Use **Javascript** for **DOM manipulation**, jQuery is not compulsory
* **Deploy your game online**, where the rest of the world can access it
* Use **semantic markup** for HTML and CSS (adhere to best practices)
* **No canvas** project will be accepted, only HTML5 + CSS3 + JS please
### When to Win
Whoever reaches 100 first wins! That simple. :)

---

### Necessary Deliverables

* A **working game, built by you**, hosted somewhere on the internet
* A **link to your hosted working game** in the URL section of your GitHub repo
* A **git repository hosted on GitHub**, with a link to your hosted game, and frequent commits dating back to the very beginning of the project
* **A ``readme.md`` file** with explanations of the technologies used, the approach taken, installation instructions, unsolved problems, etc.
### FlowChart
![](/assets/images/flow.jpg)

---

### Suggested Ways to Get Started

* **Break the project down into different components** (data, presentation, views, style, DOM manipulation) and brainstorm each component individually. Use whiteboards!
* **Use your Development Tools** (console.log, inspector, alert statements, etc) to debug and solve problems
* Work through the lessons in class & ask questions when you need to! Think about adding relevant code to your game each night, instead of, you know... _procrastinating_.
* **Commit early, commit often.** Don’t be afraid to break something because you can always go back in time to a previous version.
* **Consult documentation resources** (MDN, jQuery, etc.) at home to better understand what you’ll be getting into.
* **Don’t be afraid to write code that you know you will have to remove later.** Create temporary elements (buttons, links, etc) that trigger events if real data is not available. For example, if you’re trying to figure out how to change some text when the game is over but you haven’t solved the win/lose game logic, you can create a button to simulate that until then.
### Expansion plan in future
* add multi-player beyond 2
* leaderboard across multiple games
* points based on # of moves

---

### Potential Project Ideas
### Project Progress by Days
1. Created a game logic including snakes and ladders position jumps. Able to simulate gameplay
2. Added grid display and buttons in HTML and CSS and jQuery to handle dice throw to play various turns. Also refactored some codes.
3. Tried to dynamically populate grid with snakes and ladders based on the jump values. E.g ladder of length 4 for jump from 4 to 14, and snake of length 8 for 84 to 27. It was difficult to find suitable snake and ladder graphics of various lengths. Also had to set up calculations to determine start positions in the grid and angle of rotation based on end position.

##### Blackjack
Make a one player game where people down on their luck can lose all their money by guessing which card the computer will deal next!
Finally decides to use a game board graphic instead. This board was the initial reference for the jump values, thus my jump values did not need to be updated.

##### Self-scoring Trivia
Test your wits & knowledge with whatever-the-heck you know about (so you can actually win). Guess answers, have the computer tell you how right you are!
Introduced basic graphics to act as player tokens. Also added CSS reset to remove extra formatting and padding on elements.
4. Started work on second game, battleship due to greater scope for logic and algorithm.

---
### Built With

### Useful Resources

* **[MDN Javascript Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript)** _(a great reference for all things Vanilla Javascript)_
* **[jQuery Docs](http://api.jquery.com)** _(if you're using jQuery)_
* **[GitHub Pages](https://pages.github.com)** _(for hosting your game)_
* **[How to write readme - Markdown CheatSheet](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)** _(for editing this readme)_
* **[How to write a good readme for github repo!](https://gist.github.com/PurpleBooth/109311bb0361f32d87a2)** _(to make it better)_
* Javascript
* jQuery
* HTML
* CSS

---

### Project Feedback + Evaluation

* __Project Workflow__: Did you complete the user stories, wireframes, task tracking, and/or ERDs, as specified above? Did you use source control as expected for the phase of the program you’re in (detailed above)?

* __Technical Requirements__: Did you deliver a project that met all the technical requirements? Given what the class has covered so far, did you build something that was reasonably complex?

* __Creativity__: Did you add a personal spin or creative element into your project submission? Did you deliver something of value to the end user (not just a login button and an index page)?

* __Code Quality__: Did you follow code style guidance and best practices covered in class, such as spacing, modularity, and semantic naming? Did you comment your code as your instructors have in class?

* __Deployment__: Did you deploy your application to a public url using GitHub Pages?

* __Total__: Your instructors will give you a total score on your project between:

Score | Expectations
----- | ------------
**0** | _Incomplete._
**1** | _Does not meet expectations._
**2** | _Meets expectations, good job!_
**3** | _Exceeds expectations, you wonderful creature, you!_

This will serve as a helpful overall gauge of whether you met the project goals, but __the more important scores are the individual ones__ above, which can help you identify where to focus your efforts for the next project!
48 changes: 48 additions & 0 deletions assets/css/reset.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* http://meyerweb.com/eric/tools/css/reset/
v2.0 | 20110126
License: none (public domain)
*/

html, body, div, span, applet, object, iframe,
p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
68 changes: 68 additions & 0 deletions assets/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation style is not consistent

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reset.css is not done by her. It is resetting helper.

.container {
width: 1050px;
margin: 0 auto;
}

.wrapper {
width: 802px;
margin: 0 auto;
display: flex;
flex-flow: row wrap;
position: relative;
background-image: url(../images/gameboard-s.gif);
background-size: 100%;
float: left;
}
.left{
float: right;

}
.box {
border: 0px solid black;
width: 80px;
height: 80px;
transition: all .2s ease-in;
opacity: 0;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is opacity needed? there is no background.

}

button {
background: #4479BA;
color: #FFF;
font-family: Josefin Slab;
font-size: 20px;
text-align: center;
border: 1px solid black;
margin: 25px;
padding: 15px 25px;
border-radius: 10px;
box-shadow: 2px 2px black;
}

button:hover{
background: #356094;
}

img.dice{
width: 50px;
/*padding: 15px 25px;*/
}

.player1{
/*background: #356094;*/

background-image: url(../images/grumpycat-m.png);
background-repeat: no-repeat;
background-position: center;
background-size: 100%;
opacity: 1
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

}
.player2{
/*background: #ffa400;*/
background-image: url(../images/ham-m.png);
background-repeat: no-repeat;
background-position: center;
background-size: 100%;
opacity: 1

}
Empty file removed assets/css/stylesheet.css
Empty file.
Binary file added assets/images/Dice-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/Dice-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/Dice-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/Dice-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/Dice-5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/Dice-6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/flow.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/gameboard-s.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/grumpycat-m.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/ham-m.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/kitten.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/player1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/player2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
91 changes: 91 additions & 0 deletions assets/js/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
var grid = Array(100).fill(0)
Copy link

@alexkimin alexkimin Oct 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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. :)


//store snakes and ladders keys. jump refers to change of position
var jumps = {
1: 38,
4: 14,
9: 31,
17: 7,
21: 42,
28: 84,
51: 67,
54: 34,
62: 19,
64: 60,
72: 91,
80: 99,
87: 36,
93: 73,
95: 75,
98: 79,
}

function getJump(rollValue) {
if (jumps[rollValue]) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe update to
if key is valid, return value.
instead of if value is valid, return value. Both works the same though

// console.log(jumps[rollValue]);
return jumps[rollValue]
} else return 0

}

var player = 1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you may put variables to the top.

var move = 0

function whoWon() {
if (grid[100] > 0) {
$("#winner").text(`Winner is : ${grid[100]}`)
return grid[100]
}
}

//function set grid
function playTurn() {
// console.log('PLAYER START:', player);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, delete console.log() that is commented out. :)

//get last move of this player, last index of player. Set to 1 at start of game (index will be -1).
var lastMove = grid.lastIndexOf(player) < 1 ? 0 : grid.lastIndexOf(player)
//get random value 1-6
var roll = Math.floor(Math.random() * 6) + 1
// console.log('rollValue', roll);
var nextPos = lastMove + roll
// console.log('jump', getJump(nextPos));
move = getJump(nextPos) === 0 ? nextPos : getJump(nextPos)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree. grid should have only one 1 and 2.

// console.log('move', move)
showTexts(lastMove, roll, nextPos, move, player)
move > 100 ? move = 100 : grid[move] = player
Copy link

@alexkimin alexkimin Oct 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may do this with if statement.

If(move > 100) move = 100
grid[move] = player

grid[move] = player
changePlayer()
}

function changePlayer() {
player === 1 ? player = 2 : player = 1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you may try this player = player === 1 ? 2 : 1

}

$(document).ready(function() {
Copy link

@alexkimin alexkimin Oct 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

var $dice = $("#dice")
$dice.on('click', runGame)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if I put runGame() as a second argument? And why?

})

var runGame = () => {
$('div#gametext').show()
Copy link

@soemn soemn Oct 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$('#gametext') is enough

if (whoWon()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may use ternary operator here.

whoWon()
} else
playTurn()
}

var $diceImg = $('img.dice')
var $playerNo = $('#playerNo')
var $currPos = $("#currPos")
var $newPos = $("#newPos")
var $jump = $("#jump")

function showTexts(lastMove, roll, newPos, move, player) {
$diceImg.attr('src', "./assets/images/Dice-" + roll + ".png")
Copy link

@alexkimin alexkimin Oct 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$playerNo.text('Current Player: ' + player)
$currPos.text('Last Position is: ' + lastMove)
$newPos.text('New position is: ' + newPos)
$jump.text("Ok, jumping to: " + move)
$('div').removeClass(`player${player}`);
// $(`[data-id='${newPos}']`).addClass(`player${player}`)
$(`[data-id='${move}']`).addClass(`player${player}`);
}
Loading