Skip to content

Instantly share code, notes, and snippets.

@sergiorgiraldo
Created August 27, 2015 17:17
Show Gist options
  • Save sergiorgiraldo/741e59bc4c31d63f897d to your computer and use it in GitHub Desktop.
Save sergiorgiraldo/741e59bc4c31d63f897d to your computer and use it in GitHub Desktop.
//GUESS.JS
var register;
function Register(){
this.low = 0;
this.high = 100;
this.last = 0;
}
//Guess strategies
var Guesser = function(){
this.direction = "";
};
Guesser.prototype = {
setDirection:function(direction){
this.direction = direction;
},
guess:function(){
return this.direction.guess();
}
};
var guessInitial = function(){
this.guess = function(){
return Math.floor((Math.random() * 100) + 1);
}
};
var guessHigher = function(){
this.guess = function(){
register.low = register.last;
return Math.floor((register.high + register.last)/2);
}
};
var guessLower = function(){
this.guess = function(){
register.high = register.last;
return Math.floor((register.low + register.last)/2);
}
};
//END - Guess strategies
//UI methods
var template = {
strategy:null,
setup:function(){
},
exec:function(){
this.setup();
if (!this.strategy){
alert("UPS, DEFINE STRATEGY FIRST");
return;
}
var guesser = new Guesser();
guesser.setDirection(this.strategy);
register.last = guesser.guess();
updateUi();
}
};
function getTemplate(){
var F = function(){};
F.prototype = template;
return new F();
}
function start(){
var impl = getTemplate();
impl.setup = function(){
impl.strategy = new guessInitial();
register = new Register()
};
impl.exec();
}
function higher(){
var impl = getTemplate();
impl.setup = function(){
impl.strategy = new guessHigher();
};
impl.exec();
}
function lower(){
var impl = getTemplate();
impl.setup = function(){
impl.strategy = new guessLower();
};
impl.exec();
}
function gotIt(){
alert("Heeeeeeeeee");
}
function $(el){
return document.getElementById(el);
}
function updateUi(){
$("nb").innerText = register.last;
doTable();
}
function createCell(value){
var cell = document.createElement("td");
var cellText = document.createTextNode(value);
cell.appendChild(cellText);
return cell;
}
function doTable(){
var row = document.createElement("tr");
row.appendChild(createCell(register.low));
row.appendChild(createCell(register.high));
row.appendChild(createCell(register.last));
$("tbody").appendChild(row);
}
//END UI methods
//GUESS.HTML
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="./guess.js"></script><!-- SUT -->
</head>
<body>
Pense em um número entre 1 e 100, aperte OK e eu vou adivinhar!<br/>
<button id="start" onclick="start()">OK</button>
<p>Seu número é <label id="nb"></label> ?</p>
<p>
<button id="higher" onclick="higher();">Meu número é higher</button>
<button id="lower" onclick="lower();">Meu número é lower</button>
<button id="gotIt" onclick="gotIt();">Acertou!</button>
</p>
<table name="tbl" id="tbl" border="1">
<thead>
<tr>
<td>low</td>
<td>high</td>
<td>last</td>
</tr>
</thead>
<tbody id="tbody">
</tbody>
</table>
</body>
</html>
//GUESSTESTS.HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Guess unit tests</title>
<link rel="stylesheet" type="text/css" href="jasmine/jasmine.css">
<script type="text/javascript" src="jasmine/jasmine.js"></script>
<script type="text/javascript" src="jasmine/jasmine-html.js"></script>
<script type="text/javascript" src="jasmine/boot.js"></script>
<script type="text/javascript" src="./guess.js"></script><!-- SUT -->
<script type="text/javascript">
describe("generic tests", function(){
it( "register must initialize with 0 as low", function() {
var r = new Register();
expect(r.low).toBe(0);
});
it( "register must initialize with 100 as high", function() {
var r = new Register();
expect(r.high).toBe(100);
});
it("initial guess must multiply by 100 and sum 1 - random value", function(){
spyOn(Math, "random").and.callFake(function(o){
return 0.4;
});
expect(new guessInitial().guess()).toEqual(41);
expect(Math.random).toHaveBeenCalled();
});
it("initial guess must multiply by 100 and sum 1 - edge value 0", function(){
spyOn(Math, "random").and.callFake(function(o){
return 0;
});
expect(new guessInitial().guess()).toEqual(1);
expect(Math.random).toHaveBeenCalled();
});
it("initial guess must multiply by 100 and sum 1 - edge value 0.99", function(){
spyOn(Math, "random").and.callFake(function(o){
return 0.99;
});
expect(new guessInitial().guess()).toEqual(100);
expect(Math.random).toHaveBeenCalled();
});
it("guess high must update low register", function(){
register = new Register();
register.high = 11;
register.low = 12;
register.last = 13;
new guessHigher().guess();
expect(register.low).toEqual(register.last);
});
it("guess high must be media from high and last", function(){
register = new Register();
register.high = 11;
register.low = 8;
register.last = 13;
var nb = new guessHigher().guess();
expect(nb).toEqual((register.high + register.last)/2);
});
it("guess low must update high register", function(){
register = new Register();
register.high = 31;
register.low = 32;
register.last = 33;
new guessLower().guess();
expect(register.high).toEqual(register.last);
});
it("guess low must be media from low and last", function(){
register = new Register();
register.high = 8;
register.low = 11;
register.last = 13;
var nb = new guessLower().guess();
expect(nb).toEqual((register.low + register.last)/2);
});
});
</script>
</head>
<body></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment