Skip to content

Commit

Permalink
Merge pull request #3 from jeffque/bounty
Browse files Browse the repository at this point in the history
Bounty
  • Loading branch information
jeffque committed Nov 21, 2014
2 parents f9e8227 + e9b23c9 commit af06d93
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 23 deletions.
28 changes: 28 additions & 0 deletions css/pretty_html.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.left-nav {
margin-left: 5px;
top: 12px;
position: fixed;
width: 100px;
}


.main-content {
margin-left: 100px;
padding-left: 5px;
}

.left-nav button {
width: 100%;
}

#bounty {
height: 500px;
width: 400px;
background-position: center center;
background-repeat: no-repeat;
}

.float-left {
float: left;
margin: 5px;
}
38 changes: 28 additions & 10 deletions js/app/defines.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,34 @@
* @namespace
*/
PK = {
CODE_OBJ_VERSION : 1,
peg_code : function (brute_code) {
this.stringfiers = [ function(peg_object) { // version 0
return "#_" + peg_object.rows + "_" + peg_object.columns + "_" + peg_object.code;
},
function(peg_object) { // version 1
return "#_" + peg_object.rows + "_" + peg_object.columns + "_" + peg_object.code + "_" + peg_object.bounty_threshold + "_" + 1;
}
];
// version defaults to PK.CODE_OBJ_VERSION
this.stringfy = function(version) {
if (this.rows < 0) {
this.rows = 0;
}
if (this.columns < 0) {
this.columns = 0;
}
if (this.bounty_threshold === undefined) {
this.bounty_threshold = 1;
}

if (version === undefined) {
version = PK.CODE_OBJ_VERSION;
}
return this.stringfiers[version](this);
}


if ((brute_code == null) || (brute_code == "")) {
this.rows = this.columns = 0;
this.code = "";
Expand All @@ -13,16 +40,7 @@ PK = {
this.rows = code_split[1];
this.columns = code_split[2];
this.code = code_split[3];
}

this.stringfy = function() {
if (this.rows < 0) {
this.rows = 0;
}
if (this.columns < 0) {
this.columns = 0;
}
return "#_" + this.rows + "_" + this.columns + "_" + this.code;
this.bounty_threshold = code_split[4];
}
}
};
Expand Down
13 changes: 9 additions & 4 deletions js/app/peg_basic.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var gameboard;
var cells = null;
var bounty_threshold = 1;
var n_occupied = 44;
var old_n_rows = 0;
var old_n_columns = 0;
var n_rows = 9;
Expand Down Expand Up @@ -70,7 +72,7 @@ function vanilla_gameboard() {

/**
* code_brute syntax:
* #_<n_row>_<n_columns>_<<code>>
* #_<n_row>_<n_columns>_<<code>>_<bounty_threshold>_<version>
*
* where:
* -- n_row is the number of rows
Expand All @@ -86,22 +88,25 @@ function decodify_gameboard(code_brute) {
var i, j, n = 0;

var code_obj = new PK.peg_code(code_brute);
console.debug("code obj " + code_obj.code + "; rows " + code_obj.rows + "; columns " + code_obj.columns);
console.debug("code obj " + code_obj.code + "; rows " + code_obj.rows + "; columns " + code_obj.columns + "; bounty threshold: " + code_obj.bounty_threshold);
console.debug("code obj stringfy: " + code_obj.stringfy());

n_rows = code_obj.rows;
n_columns = code_obj.columns;
bounty_threshold = code_obj.bounty_threshold;

n_occupied = 0;

if (cells == null) {
new_gameboard();
}


for (i = 0; i < n_rows; i++) {
for (j = 0; j < n_columns; j++) {
switch(code_obj.code[n]) {
case 'O':
cells[i][j].className = 'occupied';
n_occupied++;
break;
case 'E':
cells[i][j].className = 'empty';
Expand Down Expand Up @@ -140,7 +145,7 @@ function codify_gameboard() {
code_obj.columns = n_columns;

console.debug(code_obj.stringfy());
estado = window.location.hash = code_obj.stringfy();
state = window.location.hash = code_obj.stringfy();
}

function set_gameboard() {
Expand Down
10 changes: 10 additions & 0 deletions js/app/peg_kitten.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ function move_piece(origin, destination) {
select = null;

codify_gameboard();
n_occupied--;
if (n_occupied == bounty_threshold) {
give_bounty();
}
}

function clicked(cell) {
Expand Down Expand Up @@ -142,3 +146,9 @@ function clicked(cell) {
default:
}
}

function give_bounty() {
console.debug("get your bounty");
$("#bounty").css("background-image", "url(" + next_kitten.img_url + ")");
fetch_next_kitten();
}
150 changes: 150 additions & 0 deletions js/app/written_kitten.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
Copyright (c) 2011 Alex "Skud" Bayley and Emily Turner All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
# Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
# Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*
* This piece of software was gotten from https://github.com/Skud/writtenkitten
* Please, visit the site http://writtenkitten.net/
*/
var search_for = 'kitten';
var valid_licenses="4,5,7";
/*
4 - Attribution License (http://creativecommons.org/licenses/by/2.0/)
5 - Attribution-ShareAlike License (http://creativecommons.org/licenses/by-sa/2.0/)
7 - No known copyright restrictions (http://flickr.com/commons/usage/)
*/
var warning;
if (typeof localStorage == "undefined") warning = "#warning-no-ls"
else warning = "#warning-ls";
var next_kitten = {
img_url: '',
page_url: '',
alt: ''
};
//Get license info
var license_url = "https://api.flickr.com/services/rest/?format=json&method=flickr.photos.licenses.getInfo&api_key=5dfc80756edad8d0566cf40f0909324e&jsoncallback=?";
var custom_shorts = {"No known copyright restrictions": "Flickr Commons"};
var license_list = [];
$.getJSON(license_url, function(data) {
if (data.stat == "ok") {
$.each(data.licenses.license,function(idx,el) {
licdata = {"name": el.name,"url": el.url};
//Assign short
if(shortcc = licdata.url.match(/creativecommons\.org\/licenses\/([^\/]+)\//)) {
licdata["shortname"] = 'CC-' + shortcc[1].toUpperCase();
} else if(custshort = custom_shorts[licdata.name]) {
licdata["shortname"] = custshort;
} else {
licdata["shortname"] = licdata.name;
}
license_list[el.id] = licdata;
});
}
});
function word_count(text, wc) {
if (typeof localStorage != "undefined") localStorage.text = text;
if (current_word_count >= 10 && warning_shown == false) {
show_warning();
}
text = text.replace(/^\s*|\s*$/g,''); //removes whitespace from front and end
text = text.replace(/\s+/g,' '); // collapse multiple consecutive spaces
var words = text.split(" ");
wc.value = words.length;
current_word_count = wc.value = words.length;
$("#displayWords").html(wc.value);
kittens_earned = current_word_count / words_for_reward;
if (kittens_earned >= kittens_shown+1) {
show_kitten();
}
}
function show_warning() {
$(warning).fadeIn("slow");
}
function hide_warning(immediate) {
if (immediate == true) {
$(warning).hide();
} else {
$(warning).fadeOut("slow");
}
warning_shown = true;
}
function show_kitten() {
hide_warning(true);
kittens_shown++;
$("#kittenFrame").css("background-image", "url(" + next_kitten.img_url + ")");
$("#kittenCredit").html("<a href='" + next_kitten.page_url + "'>" + next_kitten.alt + "</a>");
fetch_next_kitten();
}
function fetch_next_kitten() {
console.debug("entered");
if (getParameterByName("search")) {
// if they are using a URL param, take them very literally. They
// generally know what they're doing.
flickr_search_term = search_for;
} else {
// add "cute" to search if item is selected from dropdown. it just
// works better that way.
flickr_search_term = search_for + ",cute";
}
var flickr_url = "https://api.flickr.com/services/rest/?format=json&sort=interestingness-desc&method=flickr.photos.search&license=" + valid_licenses + "&extras=owner_name,license&tags=" + flickr_search_term + "&tag_mode=all&api_key=5dfc80756edad8d0566cf40f0909324e&jsoncallback=?";
$.getJSON(flickr_url, function(data) {
console.debug("JSON data stat: " + data.stat);
if (data.stat == "ok") {
var i = Math.ceil(Math.random() * data.photos.photo.length);
var photo = data.photos.photo[i];
var attrib = "";
if (license = license_list[photo.license]) {
if (license.url) {
attrib = " (<a href=\"" + license.url + "\">" + license.shortname + "</a>)";
} else {
attrib = " (" + license.shortname + ")";
}
}
next_kitten.img_url = "http://farm" + photo.farm + ".static.flickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + "_z.jpg";
next_kitten.page_url = "http://www.flickr.com/photos/" + photo.owner + "/" + photo.id;
next_kitten.alt = photo.title + " by " + photo.ownername + attrib;
kitten_obj_target.css("background-image", "url(" + next_kitten.img_url + ")");
$("#nextKitten").attr("src", next_kitten.img_url);
}
});
}
function set_reward(howmany) {
words_for_reward = howmany;
kittens_earned = current_word_count / howmany;
kittens_shown = parseInt(kittens_earned);
}
function set_search(search) {
if (tmp = getParameterByName("search")) {
tmp.replace(/</g, "&lt;").replace(/>/g, "&gt;"); // sanitize
search_for = tmp;
} else {
search_for = search;
}
set_title();
}
function set_title() {
if (search_for != "kitten") {
$("#titleKitten").html("<strike>Kitten!</strike>");
$("#titleSearch").html("&nbsp;" + search_for + "!");
}
}
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}

fetch_next_kitten();
11 changes: 7 additions & 4 deletions peg_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
<meta charset="utf-8"/>
<title>Peg Kitten Solitaire Editor</title>
<link rel="stylesheet" type="text/css" href="css/peg_kitten.css" />
<link rel="stylesheet" type="text/css" href="css/pretty_html.css"/>
</head>

<body>
<div id="div-gameboard">
<table id="gameboard">
</table>
<div id="game" class="main-content">
<div id="div-gameboard" class="float-left">
<table id="gameboard">
</table>
</div>
</div>
<div id="div-control">
<div id="div-control" class="left-nav">
<button id="play" type=button>Play this map!</button>
<button id="rows-minus" type=button>Less rows?</button>
<button id="rows-plus" type=button>More rows?</button>
Expand Down
15 changes: 10 additions & 5 deletions peg_kitten.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@
<meta charset="utf-8"/>
<title>Peg Kitten Solitaire</title>
<link rel="stylesheet" type="text/css" href="css/peg_kitten.css"/>
<link rel="stylesheet" type="text/css" href="css/pretty_html.css"/>
</head>

<body>
<div id="div-gameboard">
<table id="gameboard">
</table>
<div id="game" class="main-content">
<div id="div-gameboard" class="float-left">
<table id="gameboard"></table>
</div>
<div id="bounty" class="float-left"></div>
</div>
<div class="bounty"></div>
<button id="edit" type=button>Edit this map!</button>
<div id="div-control" class="left-nav">
<button id="edit" type=button>Edit this map!</button>
</div>
</body>

<script type="text/javascript" src="js/libs/jquery-1.10.2.js"></script>

<script type="text/javascript" src="js/app/defines.js"></script>
<script type="text/javascript" src="js/main.js"></script>

<script type="text/javascript" src="js/app/written_kitten.js"></script>
<script type="text/javascript" src="js/app/peg_basic.js"></script>
<script type="text/javascript" src="js/app/peg_kitten.js"></script>
</html>

0 comments on commit af06d93

Please sign in to comment.