Advertisement
jules0707

puzzel.py

Jun 9th, 2023 (edited)
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.35 KB | None | 0 0
  1. from logic import *
  2.  
  3. AKnight = Symbol("A is a Knight")
  4. AKnave = Symbol("A is a Knave")
  5.  
  6. BKnight = Symbol("B is a Knight")
  7. BKnave = Symbol("B is a Knave")
  8.  
  9. CKnight = Symbol("C is a Knight")
  10. CKnave = Symbol("C is a Knave")
  11.  
  12. Knight = Symbol("I am a Knight")
  13. Knave = Symbol("I am a Knave")
  14.  
  15. # default game knowledge regarless of what is said
  16. knowledge00 = And(
  17.     Implication(AKnave, Not(Knave)),
  18.     Implication(AKnave, Not(Knight)),  # a Knave always lies whatever he says
  19.  
  20.     Implication(AKnight, Knight),
  21.     Implication(AKnight, Not(Knave)),  # a Knight always tells the truth
  22.  
  23.     # Each character is either a knight or a knave.
  24.     Or(AKnave, AKnight),  # Inclusive "or" by implementation choice
  25.     Not(And(AKnave, AKnight)),  # remove the inclusivity as cannot be both
  26.  
  27.     Or(BKnave, BKnight),
  28.     Not(And(BKnave, BKnight)),
  29.  
  30.     Or(CKnave, CKnight),
  31.     Not(And(CKnave, CKnight))
  32. )
  33.  
  34. # Puzzle 0
  35. # A says "I am both a knight and a knave."
  36. knowledge0 = And(
  37.     knowledge00,  # loads default game knowledge
  38.     # add what the character actually said
  39.     Implication(AKnave, Not(And(Knight, Knave))),
  40.     Implication(AKnight, And(Knight, Knave))
  41. )
  42.  
  43. # Puzzle 1
  44.  
  45. knowledge1 = And(
  46.     knowledge00,
  47.    
  48.     # A says "We are both knaves."
  49.     Implication(AKnight, And(AKnave, BKnave)),
  50.     Implication(AKnave, Not(And(AKnave, BKnave)))
  51.  
  52.     # B says nothing.
  53.  
  54. )
  55.  
  56. # Puzzle 2
  57. knowledge2 = And(
  58.     knowledge00,
  59.    
  60.     # A says "We are the same kind."
  61.     Implication(AKnight,
  62.                 Or(
  63.                     And(AKnight, BKnight),
  64.                     And(AKnave, BKnave)
  65.                     )
  66.                 ),
  67.     Implication(AKnave,
  68.                 And(
  69.                     Not(And(AKnight, BKnight)),
  70.                     Not(And(AKnave, BKnave))
  71.                     )
  72.                 ),
  73.    
  74.     # B says "We are of different kinds."
  75.     Implication(BKnight,
  76.                 Or(
  77.                     And(AKnight, BKnave),
  78.                     And(AKnave, BKnight)
  79.                     )
  80.                 ),
  81.     Implication(BKnave,
  82.                 And(
  83.                     Not(And(AKnight, BKnave)),
  84.                     Not(And(AKnave, BKnight))
  85.                     )
  86.                 )
  87. )
  88.  
  89. # Puzzle 3
  90. knowledge3 = And(
  91.     knowledge00,
  92.     # A says either "I am a knight." or "I am a knave.", but you don't know which.
  93.     Or(AKnight,AKnave),
  94.     Not(And(AKnight,AKnave)),
  95.     # B says "A said 'I am a knave'."
  96.     Implication(BKnight, Biconditional(AKnave,Knave)),
  97.    
  98.     # B says "C is a knave."
  99.     Implication(BKnight, CKnave),
  100.     Implication(BKnave, Not(CKnave)),
  101.    
  102.     # C says "A is a knight."
  103.     Implication(CKnight,AKnight),
  104.     Implication(CKnave, Not(AKnight))    
  105.    
  106. )
  107.  
  108.  
  109. def main():
  110.     symbols = [AKnight, AKnave, BKnight,
  111.                BKnave, CKnight, CKnave, Knight, Knave]
  112.     puzzles = [
  113.         ("Puzzle 0", knowledge0),
  114.         ("Puzzle 1", knowledge1),
  115.         ("Puzzle 2", knowledge2),
  116.         ("Puzzle 3", knowledge3)
  117.     ]
  118.     for puzzle, knowledge in puzzles:
  119.         print(puzzle)
  120.         if len(knowledge.conjuncts) == 0:
  121.             print("    Not yet implemented.")
  122.         else:
  123.             for symbol in symbols:
  124.                 if model_check(knowledge, symbol):
  125.                     print(f"    {symbol}")
  126.  
  127.  
  128. if __name__ == "__main__":
  129.     main()
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement