js version

This commit is contained in:
Alexander Popov 2017-02-17 03:38:54 +03:00
parent 4d37a8dd0a
commit c3d665c3b3
3 changed files with 135 additions and 0 deletions

22
html/genKeys.py Normal file
View File

@ -0,0 +1,22 @@
#!/usr/bin/env python3
__author__ = 'Alexander Popov'
__version__ = '0.1.0'
__license__ = 'MIT'
usQwertyKeyboard = 'abcdefghijklmnopqrstuvwxyz'.upper()
def genKeys(alphabet):
keys = list()
for letter in alphabet:
keys.append('<button onclick="offChar(\'%s\')">%s</button>' % (letter, letter)) # FIX IT
return(keys)
if __name__ == '__main__':
with open('keys.html', 'w+', encoding='utf-8') as f:
keyboard = genKeys(usQwertyKeyboard)
for item in keyboard:
f.write(item)

112
html/index.html Normal file
View File

@ -0,0 +1,112 @@
<!doctype html>
<html>
<!--
/_ ... _ /_
/_//_/ ////_//_//_//\
_/ _//
-->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>hgman</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<style type="text/css">
body {
font-size: 24px;
}
.wrap {
max-width: 400px;
margin: 10px auto;
}
div.word, div.lives {
text-align: center;
}
div.keyboard {
text-align: center;
}
div.keyboard button {
padding: 8px;
border: 1px solid #999;
box-sizing: border-box;
margin: 2px;
}
</style>
</head>
<body>
<div class="wrap">
<div id="WORD" class="word">
LOADING
</div>
<div id="lives" class="lives">
...
</div>
<div class="keyboard">
<button onclick="offChar('A')">A</button><button onclick="offChar('B')">B</button><button onclick="offChar('C')">C</button><button onclick="offChar('D')">D</button><button onclick="offChar('E')">E</button><button onclick="offChar('F')">F</button><button onclick="offChar('G')">G</button><button onclick="offChar('H')">H</button><button onclick="offChar('I')">I</button><button onclick="offChar('J')">J</button><button onclick="offChar('K')">K</button><button onclick="offChar('L')">L</button><button onclick="offChar('M')">M</button><button onclick="offChar('N')">N</button><button onclick="offChar('O')">O</button><button onclick="offChar('P')">P</button><button onclick="offChar('Q')">Q</button><button onclick="offChar('R')">R</button><button onclick="offChar('S')">S</button><button onclick="offChar('T')">T</button><button onclick="offChar('U')">U</button><button onclick="offChar('V')">V</button><button onclick="offChar('W')">W</button><button onclick="offChar('X')">X</button><button onclick="offChar('Y')">Y</button><button onclick="offChar('Z')">Z</button>
</div>
</div>
<script type="text/javascript">
var words = ['wood', 'speed'];
var lives = 6;
var gameWord = words[Math.floor(Math.random() * words.length)].toUpperCase();
var gameAnswered = new Array(gameWord.length + 1).join('-')
window.onload = function()
{
console.log(gameWord)
document.getElementById("WORD").innerHTML = gameAnswered;
document.getElementById("lives").innerHTML = lives;
}
function wrong()
{
lives -= 1;
document.getElementById("lives").innerHTML = lives;
}
function offChar(char)
{
if (gameAnswered.split('').indexOf('-') == -1)
{
alert('You Win');
return;
}
if (lives == 0) {
alert('You dead');
return;
}
var indices = [];
var idx = gameWord.split('').indexOf(char)
if (idx == -1)
{
wrong();
return;
}
while (idx != -1)
{
indices.push(idx);
idx = gameWord.split('').indexOf(char, idx + 1);
}
console.log(indices)
indices.forEach(function(item, indices)
{
var wordArray = gameAnswered.split('')
wordArray[item] = char;
gameAnswered = wordArray.join('');
document.getElementById("WORD").innerHTML = gameAnswered;
})
}
</script>
</body>
</html>

1
html/keys.html Normal file
View File

@ -0,0 +1 @@
<button onclick="offChar('A')">A</button><button onclick="offChar('B')">B</button><button onclick="offChar('C')">C</button><button onclick="offChar('D')">D</button><button onclick="offChar('E')">E</button><button onclick="offChar('F')">F</button><button onclick="offChar('G')">G</button><button onclick="offChar('H')">H</button><button onclick="offChar('I')">I</button><button onclick="offChar('J')">J</button><button onclick="offChar('K')">K</button><button onclick="offChar('L')">L</button><button onclick="offChar('M')">M</button><button onclick="offChar('N')">N</button><button onclick="offChar('O')">O</button><button onclick="offChar('P')">P</button><button onclick="offChar('Q')">Q</button><button onclick="offChar('R')">R</button><button onclick="offChar('S')">S</button><button onclick="offChar('T')">T</button><button onclick="offChar('U')">U</button><button onclick="offChar('V')">V</button><button onclick="offChar('W')">W</button><button onclick="offChar('X')">X</button><button onclick="offChar('Y')">Y</button><button onclick="offChar('Z')">Z</button>