Git Product home page Git Product logo

knights-and-knaves's Introduction

Knights and Knaves Puzzles Solver

This repository contains a script to solve variations of the classic Knights and Knaves logic puzzles using propositional logic. In these puzzles, knights always tell the truth and knaves always lie. You will encounter different puzzles where characters make statements, and you must determine whether each character is a knight or a knave.

Dependencies

Usage

  1. Clone the repository to your local machine.
git clone https://github.com/caiogimenes/knights-and-knaves.git
cd knights-and-knaves
  1. Ensure you have the logic.py library file from the CS50 AI course in the same directory as the script.

  2. Run the script using Python.

python script.py
  1. The script will output the solutions to the specified puzzles, indicating whether each character is a knight or a knave.

Code Overview

The script defines symbols to represent whether each character (A, B, and C) is a knight or a knave. It then constructs knowledge bases for each puzzle using the statements made by the characters. The main function iterates through each puzzle, checking the knowledge base against each possible assignment of truth values to the symbols to find solutions that satisfy the knowledge base.

Here's a brief explanation of the major components of the code:

  • Symbols are defined using the Symbol class from the logic.py library:
AKnight = Symbol("A is a Knight")
AKnave = Symbol("A is a Knave")
# ... and so on for B and C
  • Knowledge bases are constructed using logical operators (And, Or, Implication, and Biconditional) to represent the rules of the puzzles and the statements made by the characters:
knowledge0 = And(
    # From the rules
    Or(AKnight, AKnave),
    Implication(AKnight, sentence0),
    Implication(AKnave, Not(sentence0)),
    # From the puzzle
    Implication(sentence0, And(AKnight, AKnave))
)
# ... and so on for other puzzles
  • The main function iterates through each puzzle, checking the knowledge base against each possible assignment of truth values to the symbols using the model_check function from the logic.py library:
def main():
    symbols = [AKnight, AKnave, BKnight, BKnave, CKnight, CKnave]
    puzzles = [
        ("Puzzle 0", knowledge0),
        # ... and so on for other puzzles
    ]
    for puzzle, knowledge in puzzles:
        print(puzzle)
        if len(knowledge.conjuncts) == 0:
            print("    Not yet implemented.")
        else:
            for symbol in symbols:
                if model_check(knowledge, symbol):
                    print(f"    {symbol}")

if __name__ == "__main__":
    main()

License

MIT

knights-and-knaves's People

Contributors

caiogimenes avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.