vendredi 1 septembre 2017

Hangman Game - can't make the loop stop deducting lives - works otherwise

As a couple other folks here, I am trying to build a hangman game for class. All things work more or less fine, but somehow I am struggling to stop the script when lives === 0. The script keeps running and just keeps deducting lives...

I tried to append the if statement in several ways, but must be missing something...

Could somebody take a look and help me understand what I am doing wrong?

Git-Hub

 
var word = "Flower";
var gameOptions = ["Flower", "Fruit", "Table", "Chair", "Love", "Family", "Water", "Picture", "Refrigerator", "Giraffe"]
 // The word we will use to play, will be randomized later
var gameOptionsLength; // The comptuer needs to know the length of the array, so we can set the upper limit for the random function
var computerChoice; // Computer will pick random word from gameOptions array
var gameWord; // This variable will transform the computer choosen string into an upper case string
var gamewordLength; // In order to show the right character amout to be guessed, we need to know the word length
var blanks= "";
var winword="";
var userInput;
var lives = 6; // According to Wikipedia, the user has 6 lives
var alphabet;
var outputElement;
var guessedLetters = []; // stores the letters the user guessed

gameOptionsLength = gameOptions.length;

computerChoice=gameOptions[Math.floor(Math.random() * gameOptionsLength)];
        console.log("computer choice " + computerChoice);

gameWord = computerChoice.toUpperCase();
gamewordLength = gameWord.length;

var j = 0;  // Creates the blanks i.e. Flower has 6 characters and needs 6 blanks _ _ _ _ _ _
while (j < gamewordLength) {
    blanks += " _";
    j++;
   }

var j = 0;  // Creates the blanks i.e. Flower has 6 characters and needs 6 blanks _ _ _ _ _ _
while (j < gamewordLength) {

    winword += " "+ gameWord.charAt(j);
    j++;



alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
outputElement = $('#guessed-letters');


$(document).on('keyup', function (event) {
  var key = event.key.toLowerCase();
  if (alphabet.indexOf(key) !== -1) {
    // the key is a letter in the alphabet
    if (guessedLetters.indexOf(key) === -1) {
      // the key has not been guessed
      guessedLetters.push(key);
      var string = guessedLetters.join(''); // join the letters together to one single string
      outputElement.text(string);
    }
  }
});


var paragraph = document.getElementById("demo").innerHTML = "The word you are looking for has " + gamewordLength + " characters, can you guess it?";
var paragraph1 = document.getElementById("blanks").innerHTML =  "Word:"+blanks;
var pLives = document.getElementById("pLives").innerHTML = " Lives: " + lives;

document.onkeyup = function(event){
      userInput = event.key;
      userInput = userInput.toUpperCase();

      console.log("the userInput is " + userInput) // 
      
  

    var index = gameWord.indexOf(userInput);

        if (index >= 0) { 
        for (var i = 0; i < gamewordLength; i++){
        console.log("Here is the gamword from within the loop:" + " "+ blanks); 
        var index = gameWord.indexOf(userInput,i); // indexOf checks whether a character is within a string, if the character exists the return value is >=0
        console.log(index);
        console.log("that works, the right position is " +index);
                blanks = blanks.split('');
                blanks[index*2+1] = userInput; 
                blanks = blanks.join('');
                console.log("here are the new blanks:" + " "+ blanks);
                var paragraph1 = document.getElementById("blanks").innerHTML =  "Word:"+blanks;
                                
        }}


         else {  
        // deducting lives if indexOf === -1, as the user selected character does not exist in the string
                lives = lives -1;
                console.log("lives = " +lives)
                var pLives = document.getElementById("pLives").innerHTML = " Lives: " + lives;
        }


        if (lives === 0){
                var gameOver = document.getElementById("gameOver").innerHTML = " GameOver - refresh the site to play again!";
                }
        
                                
        else if (blanks === winword) {
                var winner = document.getElementById("winner").innerHTML = "Well done, you guessed the right word!!";
        }

}}
<!DOCTYPE html>
<html>
<head>
        <title>Hangman Game</title>
        <link rel="stylesheet" href="assets/css/style.css">
          <script type="text/javascript" src="http://ift.tt/2n9t8Vj"></script>


</head>
<body>

<h1>Hangman</h1>
<p id="demo"></p> 
  <!-- Js includes the following text: The word you are looking for has " + gamewordLength + " characters, can you guess it?"; -->
 <h1 id="blanks"></h1> <!-- If word is "Love", there will be 4 - - - -  -->
 <h1 id="pLives"></h1> <!-- Lives:  -->
 <h1 id="gameOver"></h1> <!-- GameOver -->
 <h1 id="winner"></h1> <!--Winner -->
 <h3>You already used the following letters:</h3>
 <h3 id="guessed-letters"></h3> <!--Displays used letters -->

<!-- <div id="display"></div>
  <div id="buttons"></div>
  <div id="AllKeys">
  <button id="allButtons">A</button> -->

 </div>

<script src="assets/javascript/game.js"></script>

</body>
</html>

Test if program exists in Bash Script - abbreviated version

I want to use the abbreviated if then else to determine if ccze exists before using it... and I just cannot get that first part right...

test() {
    [  $(hash ccze) 2>/dev/null ] && echo "yes"  || echo "no"
}

The above is just test.. what am I doing wrong? It does not matter if ccze exists or not - I'm getting "no"

Hi, could someone explain to me how each line of this code works? [on hold]

Loop challenge- I am aware that you have to create an array to start out with to hold numbers you have already printed

Can I write this if statement one line?

I was wondering if there was a way to put this on one line?

if (auto r = getGlobalObjectByName(word)) r->doSomething; // This works fine

if (!auto r = getGlobalObjectByName(word)) r->doSomething; // It says "expected an expression

if (auto r = getGlobalObjectByName(word) == false) r->doSomething; // Also doesn't work.

I also tried surrounding it extra brackets, doesn't seem to work. I find this really convenient doing it on one line.

Applying pre-defined filter on the looped files

I have some bash looping script which loops the files and do some things with them

for pdb in ${output}/*.pdb ; do
name=$(basename "$pdb")
echo "I am sending ${name} to some place!"
done

now I would like to introduce some filter within the loop to pass the files only without some keywords in any part of the $name , thus excluding all files which has the defined keywords.

For the realization I would like to set all keywords in an array

#For a file with that keywords within the name the script should stop and loop another file etc
declare -a keywords=('apo' 'Apo' 'APO' 'sauf');

#workflow for a signle keyword "apo"
 for traj in ${all_trr}/*.xtc; do
 traj_name3=${traj##*/[0-9][0-9]_[0-9][0-9]_[0-9][0-9][0-9][0-9].}
 if [[ "$traj_name" != *apo* ]]
 then
#begins loop from the another file
  echo "I am sending ${traj_name} to analysis"
  #break 
 else
  echo "I am not sending ${traj_name} to analysis"
  continue
 fi
done

So I need to adapt this work-flow for an array: 1) to compare each file name with the array element and 3) send it to the script only if it does not match it. E.g I have files with a complex name where keywords appear in different parts:

08_29_2017.gromacs_AT1_dry_sauf.1rep.step7_1.pdb
08_29_2017.gromacs_AT1_dry_Apo.1rep.step7_1.pdb
08_29_2017.gromacs_AT1_wat_apo.3rep.step7_1.pdb
08_30_2017.gromacs_AT1_wat_Na.2rep.step7_1.pdb

So only

08_30_2017.gromacs_AT1_wat_Na.2rep.step7_1.pdb

should pass to the

echo "I am sending ${name} to some place!"

at the same time if i have the files only with the one keyword e.g apo

08_29_2017.gromacs_AT1_dry_apo.1rep.step7_1.pdb
08_29_2017.gromacs_AT1_dry_apo.1rep.step7_1.pdb
08_29_2017.gromacs_AT1_wat_apo.3rep.step7_1.pdb
08_30_2017.gromacs_AT1_wat_Na.2rep.step7_1.pdb

also only the last file should be processed (but the array should consist of many 10-15 keywords including 'apo')

a click function that runs "if" then proceedes

what im trying to write is a function that will be activated by a click that will run if the class name of the clicked element is correct. then change the class name to another state so it can not be run again until another part of the function reverts it. it will roughly look like this:

king1.addEventListener("click", turn);

function turn() {
        if (this.className="nf") {
                this.className="f"
          ...run more functions after
        };
        
}

Multiple check for "None" - what would be the proper way in Python

I'm using BeautifulSoup for parsing html, I often find my self with the following code:

result.find("some_tag").attrs['some_attribute']

What would be the proper way to verify if find method didn't return None and also to check if there is a such key in attributes without nested "if" statement ?

thanks