Question

In: Computer Science

LISP FUNCTIONS Imagine you are writing the AI for a simple game that will require an...

LISP FUNCTIONS

Imagine you are writing the AI for a simple game that will require an adversarial search.

The state of the game can be represented using a list of 3 elements, such as (B B B). The list representing the game state can contain any combination of 3 values: A, B, or C. It will always have exactly 3 items. (Let's call it the "state list").

With that in mind, write the following LISP functions:

  • TERMINAL-TEST - this function takes a state list as it's only argument. If the list contains no B's, it is considered a terminal state and TERMINAL-TEST should return t, nil otherwise.
  • UTILITY - this function accepts a state list as it's only argument and evaluates the state list. If the list contains all A's, then the function returns 1. If the list contains all C's, then the function returns -1. Any other combination of A's, B's, or C's returns 0.
  • ACTIONS - this function accepts a state list as it's only argument and returns a list of possible actions according to the following table:
    State Action
    (B * *) A1
    (* B *) A2
    ( * * B) A3

    Where :
    * represents A, B, or C
    State is the state list passed to the function
    Action is the possible action returned given that state

    So, if the state list passed to the function was (B C B), then the function should return (A1 A3).
  • RESULT - this function accepts a state list and an action as it's only arguments. The function then returns a state list according to the following table:
    State Action Returned
    (B * *) A1 (A * *)
    (* B *) A2 (* A *)
    (* * B) A3 (* * A)
    Where:

    State and Action are both passed as arguments and
    Returned is the state list that is returned.
    The * represents A, B, or C.

    For example, if (B C B) and A3 were both passed as arguments, then the state list that gets returned would be (B C A).

Demonstrate your functions work by calling each one in turn, passing each of them appropriate arguments, and then printing to the screen the values they returned.

Solutions

Expert Solution

The code, including the test cases is given below, followed by the snapshot.

(defun TERMINAL-TEST (state)
        (and
            (and (not (eq (car state) 'B)) (not (eq (car (cdr state)) 'B) ))
            (not (eq (car (cdr (cdr state))) 'B))
        )
)

(defun UTILITY (state)
    (cond
        ((and
            (and (eq (car state) 'A) (eq (car (cdr state)) 'A) )
            (eq (car (cdr (cdr state))) 'A)
        ) 1 )
        ((and
            (and (eq (car state) 'C) (eq (car (cdr state)) 'C) )
            (eq (car (cdr (cdr state))) 'C)
        ) -1 )
        (T 0)
    )
)

(defun ACTIONS (state)
    (append
        (if (eq (car state) 'B) '(A1) '())
        (if (eq (car (cdr state)) 'B) '(A2) '())
        (if (eq (car (cdr (cdr state))) 'B) '(A3) '())
    )
)

(defun RESULT (state action)
    (cond
        ((and (eq action 'A1) (eq (car state) 'B)) (cons 'A (cdr state)))
        ((and (eq action 'A2) (eq (car (cdr state)) 'B))
            (cons (car state) (cons 'A (cdr (cdr state)))))
        ((and (eq action 'A3) (eq (car (cdr (cdr state))) 'B))
            (cons (car state) (cons (car (cdr state)) '(A))))
        (T state)
    )
)

(write-line "")(format t "(TERMINAL-TEST '(B B B))=~a" (TERMINAL-TEST '(B B B)))
(write-line "")(format t "(TERMINAL-TEST '(A B C))=~a" (TERMINAL-TEST '(A B C)))
(write-line "")(format t "(TERMINAL-TEST '(A C A))=~a" (TERMINAL-TEST '(A C A)))

(write-line "")(format t "(UTILITY '(A A A))=~a" (UTILITY '(A A A)))
(write-line "")(format t "(UTILITY '(C C C))=~a" (UTILITY '(C C C)))
(write-line "")(format t "(UTILITY '(A B C))=~a" (UTILITY '(A B C)))

(write-line "")(format t "(ACTIONS '(B C B))=~a" (ACTIONS '(B C B)))
(write-line "")(format t "(ACTIONS '(B B B))=~a" (ACTIONS '(B B B)))
(write-line "")(format t "(ACTIONS '(A C B))=~a" (ACTIONS '(A C B)))
(write-line "")(format t "(ACTIONS '(A C C))=~a" (ACTIONS '(A C C)))

(write-line "")(format t "(RESULT '(C C C) 'A1)=~a" (RESULT '(C C C) 'A1))
(write-line "")(format t "(RESULT '(B C C) 'A1)=~a" (RESULT '(B C C) 'A1))
(write-line "")(format t "(RESULT '(A B C) 'A2)=~a" (RESULT '(A B C) 'A2))
(write-line "")(format t "(RESULT '(B C B) 'A3)=~a" (RESULT '(B C B) 'A3))
(write-line "")(format t "(RESULT '(B C C) 'A2)=~a" (RESULT '(B C C) 'A2))


Related Solutions

You can only use built in Lisp functions and you cannot use setq function. Write a...
You can only use built in Lisp functions and you cannot use setq function. Write a function in Lisp called f1 that counts the number of lists in a list. Example: (f1 ‘(a (a b (b c)) c d (e))) returns 2
What is the imitation game and how is it a test for AI
What is the imitation game and how is it a test for AI
In this homework assignment, you will be writing three `void` functions. These functions each take a...
In this homework assignment, you will be writing three `void` functions. These functions each take a single integer parameter. The functions accomplish the following tasks. * The first function prints out the sum of the numbers from 1 up to and including the number passed to it as an argument. * The second function prints out the sum of the even numbers from 2 up to and including the number passed to it as an argument. * The third function...
Imagine you are on a the game show "Let's Make a Deal" and are given the...
Imagine you are on a the game show "Let's Make a Deal" and are given the opportunity to select one closed door of three, behind one of which there is a prize. The other two doors hide goats or some other such non-prize, or nothing at all. Once you have made your selection, Monty Hall will open one of the remaining doors, revealing that it does not contain the prize. He then asks you if you would like to switch...
Imagine you are working on a team and one of your team members is writing a...
Imagine you are working on a team and one of your team members is writing a library of C functions to work with strings. They decide to name their library mystringfunctions, and have two files: a source file mystringfunctions.c and a header file mystringfunctions.h. In this problem, you will write automated tests known as unit tests. This program has been partially written for you in testmystringfunctions.c. Write the body of functions that are marked with a comment that begins with...
Imagine you are writing questions for an algebra course. You want to write questions that include...
Imagine you are writing questions for an algebra course. You want to write questions that include finding the zeros of polynomials. To balance the difficulty level of the test’s questions, you decide to include two different types of questions, as described here: Question #1: A polynomial of degree at least 3 where all the zeros are positive whole numbers Question #2: A polynomial of degree at least 3 where one or more of the roots are fractions Find two polynomials...
PYTHON Exercise 4. Fantasy Game Inventory Imagine you have this inventory in a fantasy game: Item...
PYTHON Exercise 4. Fantasy Game Inventory Imagine you have this inventory in a fantasy game: Item Number of this items Rope 1 Torch 6 Gold coin 42 Dagger 1 Arrow 12 1. Save the inventory in a data structure. 2. Write a function that will take any possible inventory and display it in a pretty format. 3. Write a function to add new items to the data structure. Make sure new group of items can be created. 4. Write a...
Put It in Writing Activity: Imagine that you have just been hired as the principal of...
Put It in Writing Activity: Imagine that you have just been hired as the principal of a high school where students’ attendance and test performance has been poor and the dropout rate has been high. At a meeting with your teachers, you explain that the principles of classical and operant conditioning could be used to improve the situation. Write a page describing how these principles could be used to increase students’ class attendance, study skills, and test performance. Be sure...
In python make a simple code. You are writing a code for a program that converts...
In python make a simple code. You are writing a code for a program that converts Celsius and Fahrenheit degrees together. The program should first ask the user in which unit they are entering the temperature degree (c or C for Celcius, and f or F for Fahrenheit). Then it should ask for the temperature and call the proper function to do the conversion and display the result in another unit. It should display the result with a proper message....
PYTHON GAME OF PIG The game of Pig is a simple two player dice game in...
PYTHON GAME OF PIG The game of Pig is a simple two player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn a player rolls a six-sided die. After each roll: a) If the player rolls a 1 then the player gets no new points and it becomes the other player’s turn. b) If the player rolls 2-6 then they can either roll again or hold. If the player...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT