Project3-9340

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

CS/SE 4337 Project 03 Spring 2023

Project 03
Due: Friday, May 5th 11:59pm

1 Description
In this project, you will write two artificial intelligences for the card game Twenty. Project
2 had you write these AIs in scheme, but this project has you write them in Prolog. You
can find the rules at https://www.pagat.com/invented/20.html. The first AI will try
to maximize the number of cards it plays. The second will try to minimized the value of
its hand.

2 Specifications
You are required to write two predicates: project3 ai 1 and project3 ai 2. These predicates
implement the first and second AI, respectively. In addition to these two predicates you
will need a few other helper predicates. These predicates will be placed in a file named
“project3.pl”, and will be included from the file “base.pl”. To run the game you need to
follow three steps. First, create the player1/2 predicate which is a wrapper around the
player 1 AI predicate. Second, create the player2/2 predicate which is a wrapper around
the player 2 AI predicate. Finally, query the playgame/0 predicate. There are examples in
the provided “project3.pl” file.

2.1 AI Functions
The driver code contains all the logic for implementing the game. When ever it is your
AI’s turn, its predicate will be called. The predicate should have two arguments. The first
is a game state, which is a structure containing five arguemnts, detailed as follows.

1. Normally the constant no error, but if your AI give an invalid play, the predicate is
called a second time with this parameter as the constant error.

2. The current count


It will start at 1 and increment to 20 as the count is made.

3. The last play made by the previous player


It will be in the same format as your AI is required to yield.

4. the other player’s score

5. player state
This is the state for the player controlled by your AI. A player state is a structure,
with functor pstate, containing the player’s current score, hand, and a custom state
that can be anything you want. In the “Utilities” section of “base.pl”, you will find
the predicate pstate/3 that can be used to get parts of the player state.

Programming Language Paradigms Page 1


CS/SE 4337 Project 03 Spring 2023

The second argument to the AI should unify with a play structure. The play structure
has the functor play, and two arguments. The first argument is the play you want to make.
It can be the constant drawcard or a list of cards, pairs, and kings as wild. The second
argument is a custom state, and you can leave it as the constant empty. It is an advanced
feature the IA can use to remember information about previous turns, which is not needed
by either AI.
If the AI predicate fails, then the driver code considers this as giving up. It is similar
to returning the empty list in project 2.

2.1.1 Playing Your Turn


A play is either the symbolic atom 'drawcard, or a list of card plays. A card play can be
one of the following.

• A card
A card is a structure, with functor card and arity 2. The first argument is the atom
representing the suit. The second is an constant representing the value. You can see
the definition at the top of “base.pl”. There are helper predicates that work on cards
in the “Utilities” section of “base.pl”. This can be any card except a king.

• A wild card
Kings are wild cards in Twenty, and can take on any value between 1 and 20. A wild
card play is a structure, with functor card and arity 2. The first argument is a king
card. The second is the value (between 1 and 20) the card will be played as.

• A pair
A pair 2-arity structure, with functor pair, where the two argument are 10s jacks,
or queens with matching faces. A pair play indicates you want the play the pair of
cards as the value 10 rather than 20.

The values of all card plays in the list must sum to the current count.

2.2 AI 1 (project3 ai 1)
The first AI will always make the legal play that gets rid of the most cards. If there is
multiple ways to do this, the AI will prioritize keeping the most kings. All other choices
are arbitrary. This means if you can make the count with both a 3 and 4 or a 6 and an
ace, then either one may be chosen.

2.3 AI 2 (project3 ai 2)
The second AI will make the legal pay that leaves it with the lowest score hand. In general
this will mean getting rid of high scoring cards, but sometimes getting rid of more cards
is better. As an example, if the count is 10, then a pair of queens would make the count.
These queens are worth 20 points at the end of the hand, but 4 aces and a 6 are worth 25
points.

Programming Language Paradigms Page 2


CS/SE 4337 Project 03 Spring 2023

2.4 Base Code


Take a look at “base.pl”. In particular, the “Utilities” section contains helper predicates
that will be useful for dealing with the data representation I chose. Study the section,
and be sure you understand the data representation, and how to use the predicate in that
section.
In the “Config” section are some options that affects the run of the program. You can
use these options for testing. Setting the argument of use transcript to true will cause
every play to printed, along with the player’s hand after making that move. At the start
of the line is player number. After that, in brackets, will be the current count. You can
use this to see the decisions your AI is making. Both the hand size, and the number of
points to play to can be modified. If you want, for instance, to only play a single hand
while testing, you can change the argument to play to to a small number like 5.
To run the game, you simply need to run “base.pl”, and make the query ?- playgame..
It will include “project3.pl”, which should be in the same directory as “base.pl”. You will
need to define the player1/2 and player2/2 predicates that are wrappers around the AI
predicates you want to use. There are examples in the provided “project3.pl”.

3 Hints and Tips


• Solve the problem in stages.

1. Design the AI. Maybe write out psudocode.


2. Convert your algorithm to the prolog way of solving problems. In particular,
keep in mind that all variables are immutable, and the core of computation is
proving things about the relationship of objects.
3. Write your prolog code.

• AI 1 and AI 2 are very similar and will share most of their code.

• Before converting your algorithm to prolog, be sure to study the “Utilities” section of
“base.pl”. Make sure you understand how I am representing everything in the game.

• You do not need any predicates not presented in the book/class, but you may use
them to make things easier. There is no combinations predicate in prolog, so I created
one, and made it available in “base.pl”.

4 Challenge (Not Graded)


Both the AIs are greedy AIs. They do not try to figure out what the opponent might
be able to play or remember what move have been made before (and therefore cannot
make plans). Can you design a better AI? Implement a third AI predicate that uses a
more intelligent algorithm. If you have taken the Artificial Intelligence class, maybe use an
algorithm you learned there. This AI will not be graded, and cannot not affect your grade.
Therefore, it is optional, and you will not receive extra credit for doing it.

Programming Language Paradigms Page 3


CS/SE 4337 Project 03 Spring 2023

5 What to Turn in
Upload your submission as a zip archive containing the following:
• Source code (project3.pl)
– You should not submit base.pl.
– The TAs will use their own
• Readme (Plain text document)
– List the files included in the archive and their purpose
– Explain how to compile and run your project
– Include any other notes that the TA may need
• Write-up (Microsoft Word or pdf format)
– How did you approach the project?
– How did you organize the project? Why?
– What problems did you encounter?
– How did you fix them?
– What did you learn doing this project?
– If you did not complete some feature of the project, why not?
∗ What unsolvable problems did you encounter?
∗ How did you try to solve the problems?
∗ Where do you think the solution might lay?
· What would you do to try and solve the problem if you had more time?

6 Grading
The grade for this project will be out of 100. It must be written in scheme, and run in
racket. Violating this is an automatic zero. The grade is broken down as follows.
Followed Specifications 50
Use of Prolog style (tail recursion, non-imperative strategies, etc) 20
Output 20
Write-up 10
If you were not able to complete some part of the program discussing the problem and
potential solutions in the write-up will reduce the points deducted for it. For example,
suppose there is a bug in your code that sometimes allows two customers to approach the
same worker, and could not figure out the problem before the due date. You can write 2-3
paragraphs in the write-up to discuss this issue. Identify the error and discuss what you
have done to try to fix it/find the problem point, and discuss how you would proceed if
you had more time. Overall, inform me and the TA that you know the problem exists and
you seriously spend time trying to fix the problem. Normally you may lose 5 points (since
it is a rare error) but with the write-up you only lose 2. These points can make a large
difference if the problem is affecting a larger portion of the program.

Programming Language Paradigms Page 4

You might also like