// Exercise 04.02 Boolean logic // // Use boolean logic to solve the liar problem. // 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 (!) and different from (!=). // 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 = (implies(a, b) && implies(!a, !b) && implies(b, (a!=b))); // 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!");