Last active
August 29, 2018 21:50
-
-
Save seanvikoren/dfd45c4f14bcecb78b33f81daccaf279 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
var Knave = false; | |
var Knight = true; | |
bool lie(bool i) { return !i; } | |
String islanderToString(bool i) { return i == Knight ? "Knight" : "Knave"; } | |
String whisperToString(bool w) { return w ? "Yes" : "No"; } | |
String t(bool n) { return n ? "tells the truth" : "lies"; } | |
void main() { | |
var r = Random(); | |
List<bool> islander = [Knight, Knight, Knight, Knight, Knight, Knave, Knave, Knave, Knave, Knave]; | |
islander.shuffle(); | |
bool firstIslander = r.nextInt(2) == 0 ? Knave : Knight; | |
islander.insert(0, firstIslander); | |
print("If I asked you if you were a Knight, what would you say?\n"); | |
bool w = true; | |
for (var i in islander) { | |
var a = "${islanderToString(i)}"; | |
w = i == Knight ? w : lie(w); | |
print("${a} => ${whisperToString(w)}"); | |
}; | |
print("\nThe last islander says \"${whisperToString(w)}\"."); | |
print("We deduce that the first islander is a ${islanderToString(!w)}."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment