// Exercise 04.02 Boolean logic // // Use boolean logic to solve the knights and knaves puzzle number 20. // A very special island is inhabited only by knights and knaves. Knights always tell the truth, and knaves always lie. // You meet two inhabitants: Zed and Zoey. Zed says that it's false that Zoey is a knave. Zoey claims, “I and Zed are different.” // Can you determine who is a knight and who is a knave? // // The situation can be described as follows: // If a tells the truth then b tells the truth AND if a lies then b lies AND if b tells the truth then a is different from b. // Translate the above statement into a logical expression. If...then... translates to the imlication (=>) the other operations needed // are and (&&), not (!), different from (!=) and equal. // function implies(a,b) { result = (!a || b); return result; } // liar problem - models the situation described in the liar problem // as a boolean expression. function liarProblem(a, b) { // Your code starts after this line result = // Your code ends before this line return result; } print ("a=0, b=0 - (both lie)", liarProblem(0,0)); print ("a=0, b=1 - (only a lies)", liarProblem(0,1)); print ("a=1, b=0 - (only b lies)", liarProblem(1,0)); print ("a=1, b=1 - (no one lies)", liarProblem(1,1)); // The code below is for automatically checking the result. Please ignore it! // number = "" + liarProblem(0,0) + liarProblem(0,1) + liarProblem(1,0) + liarProblem(1,1); res = parseInt(number, 2) == (768/96); if (res) showMessage("That's right. Great, you did it!"); else showMessage("Your result is wrong! Please check your macro and try again!");