Do not make any of these problems or your answers posted or available in Internet or do not share with any others. All of the assignment should be done by yourself.

Submission: Submit (upload) softcopy of (1) a word document [this file with your answers and program listing and log of compile-run with test cases], and (2) a zip file of all the files (including all the programs and files that you worked and are needed to compile or run it) via elearning.

Scoresheet
Part 1
Prolog puzzle 
40% #1  
#2 
Demo 
Part 2
Prolog sudoku
30% #1 sudoku
#2 hexadoku Demo
Part 3
Prolog poker
30% #1 #2 #3 Demo


Part1 Part2 Part3 Documentation
(up to -10%) Demo
(up to -10%) Total 100%



** Demo is required (check for the demo schedule from TA to make a reservation) or -10%.

** Your documentation here should be well-organized and presented, to follow the flow of your work with ease. Place course information and your name & email (UTD) to the header and page number in footer. Keep 1" margin each side and font-size of 10, single-spaced. 

A poor documentation (a word file – this file with your answers) may result in 0 for documentation (10%).

Note. (If your program run is not manageable) Some problems as your program run may take too long to complete (or aborted out of memory or overflow, etc). If your program runs over 30+ minutes, or producing over a few hundreds of solutions (or the depth of search tree is too big, etc.), please stop and make a note of it in your submission (here and to TA during Demo) and/or to adjust your search to be a bit manageable. One thing that you can do is to output first few dozens (to show that your program is working or to make the length of the move or depth to be shorter, etc.), in order to make the run manageable. Make a note of this and clearly state it in your documentation for the run and solutions and to TA during the demo. Another option is to make your program smarter, to be manageable (instead of running in brute-force manner).

Warning: All of the assignment should be done by yourself and for this course. Do not make any of the problems and course-materials, or your answers to be posted, do not share or make it available in Internet. 


Part1. #1 Cryptarithmetic Puzzle

#1. Design and implement Prolog CLP program to solve cryptarithmetic problem. As discussed in the class, the cryptarithmetic program is "general" puzzle-solver to solve the general cases (for example, the following test cases listed below). 

Name your program to be: crypt1.pl

Here are a few more requirements for this puzzle.
(1) Your program can handle an operand of flexible length as shown below for test cases.
The length of each operand should be in a range of (N-1) to (N+1) for some N.
Usually two operands are of length N and the sum is of length N+1.
(2) If no solution is found, you may assume the leading digit of the longest operand to be 0.

Test Cases
1. puzzle1([A,M]+[P,M]=[D,A,Y]).
2. puzzle1([A,M]+[I]=[O,K]).
3. puzzle1([Y,O,U]+[A,R,E]=[D,O,N,E]).
4. puzzle1([S,E,N,D]+[M,O,R,E]=[M,O,N,E,Y]).
5. puzzle1([R,O,A,D]+[I,N]=[U,S,A]).
6. puzzle1([B,I,K,E]+[F,O,R]=[R,I,D,E]).

Each solution should be printed as follow.

?- puzzle1([A,M]+[P,M]=[D,A,Y]).
 Solution: [2,5]+[9,5]=[1,2,0]

and check for the next alternative solution(s).


%% Hint – how to collect all the variables in As, Bs, and Cs.
puzzle1(As + Bs = Cs) :- 
           append([As,Bs,Cs],Ds),
           term_variables(Ds,Var),   %% this will get all Var
           Var ins 0..9, 
           all_different(Var), 
           …

?- As=[A,1,2], Bs=[3,B,4], Cs=[5,X,Y], append([As,Bs,Cs],Ds), term_variables(Ds,Var).
As = [A, 1, 2],
Bs = [3, B, 4],
Cs = [5, X, Y],
Ds = [A, 1, 2, 3, B, 4, 5, X, Y],
Var = [A, B, X, Y].

Part1 #1. Solution

Your program code listing here





Test Case 1. Your program run of the program. Show the first solution for each test case.


Test Case 2. Your program run of the program. Show the first solution for each test case.




and so on

Part1. #2

#2. (Continuing #1) 
Design and implement a prolog program with the following requirements.

1. Your program should find all the solutions without any duplicate(s). Sort your solution in descending order of As, Bs, Cs (for As+Bs=Cs) where As is the first operand, Bs is the second operand, and Cs is the third operand.

2. Output to the console (1) the first solution to the console, (2) total number of the solutions found (without duplicate) and to a file (0) the problem, (1) the first solution, (2) the total number of the solutions found, and (3) all the solutions in sorted order in a file. If there is no solution, output "No solution!"

?- crypto2([A,M]+[P,M]=[D,A,Y],"crypto2sol1.txt").
First Solution is: [2,5]+[9,5]=[1,2,0]
There are total NN solutions found & output to the file: puzzle2sol1.txt
where NN is the total number of the solutions for this case.

3. Name your program to be: crypt2.pl

Note. If you cannot complete Part1 #1 (the previous part), then you may use the sample code provided in the class (or for weekly activity) as your base code to work for this part. You may also use the test case(s) instead of the ones provided here.



%% code example to open a file for write

puzzle2(As + Bs = Cs, OutputFile) :- 
%% get all the Solutions 

  open(OutputFile, write, OutStream),
  write_solutions(Solutions, OutStream),
close(OutStream).


Part1 #2 Solution

Your program code listing here





Test Cases
1. crypto2([A,M]+[P,M]=[D,A,Y],"crypto2sol1.txt").
2. crypto2([A,M]+[P,M]=[U,P],"crypto2sol1.txt").
3. crypto2([S,E,N,D]+[M,O,R,E]=[M,O,N,E,Y],"crptyo2sol3.txt").
4. crypto2([R,O,A,D]+[I,N]=[U,S,A],"crptyo2sol4.txt").
5. crypto2([B,I,K,E]+[F,O,R]=[R,I,D,E],"crptyo2sol5.txt").

Test Case 1. Output of your program execution 
Output to the console


And the screenshot of the output in the file




Test Case 2. Output of your program execution.
Output to the console


And the screenshot of the output in the file



And so on …



Part 2. #1. Sudoku

Sudoku. As we discussed in the class, here is the sudoku prolog program listed below. 

Design and implement puzzle/1 to do the following tasks: 
(1) to run the program with a specified problem (problem/2) with problem-id (N), for example,
?- sudoku1(1). %% to run the problem #1 as listed below.
(2) to print the problem itself and its first solution in a format of 9x9 as shown below. 

Your program name should be: sudoku1.pl

Place your answer here: (a) the program listing and (b) the result of the program run with a proper heading. 
And upload (submit) this document and a zip file (containing all your codes and the results of execution).

Test cases are listed below.
problem(1, P) :- 
 P = [[_,_,_,_,_,_,_,_,_],[_,_,_,_,_,3,_,8,5],[_,_,1,_,2,_,_,_,_],
       [_,_,_,5,_,7,_,_,_],[_,_,4,_,_,_,1,_,_],[_,9,_,_,_,_,_,_,_], 
     [5,_,_,_,_,_,_,7,3],[_,_,2,_,1,_,_,_,_],[_,_,_,_,4,_,_,_,9]]

problem(2, P) :- 
        P = [[1,_,_,8,_,4,_,_,_],[_,2,_,_,_,_,4,5,6],[_,_,3,2,_,5,_,_,_],
             [_,_,_,4,_,_,8,_,5],[7,8,9,_,5,_,_,_,_],[_,_,_,_,_,6,2,_,3],
             [8,_,1,_,_,_,7,_,_],[_,_,_,1,2,3,_,8,_],[2,_,5,_,_,_,_,_,9]].

problem(3, P) :- 
        P = [[_,_,2,_,3,_,1,_,_],[_,4,_,_,_,_,_,3,_],[1,_,5,_,_,_,_,8,2],
             [_,_,_,2,_,_,6,5,_],[9,_,_,_,8,7,_,_,3],[_,_,_,_,4,_,_,_,_],
             [8,_,_,_,7,_,_,_,4],[_,9,3,1,_,_,_,6,_],[_,_,7,_,6,_,5,_,_]].

problem(4, P) :-
        P = [[1,_,_,_,_,_,_,_,_],[_,_,2,7,4,_,_,_,_],[_,_,_,5,_,_,_,_,4],
             [_,3,_,_,_,_,_,_,_],[7,5,_,_,_,_,_,_,_],[_,_,_,_,_,9,6,_,_],
             [_,4,_,_,_,6,_,_,_],[_,_,_,_,_,_,_,7,1],[_,_,_,_,_,1,_,3,_]].

Part2 #1. Solution

#1. Your program code - listing here.





Your program runs for test case (as shown above in the figure). Place here only the relevant run log only, and for each task with a proper heading. 

Test Case 1



Test Case 2





:- use_module(library(clpfd)). 
sudoku(Rows) :- 
       length(Rows, 9), 
       maplist(length_list(9), Rows), 
append(Rows, Vs), 
Vs ins 1..9, 
       maplist(all_distinct, Rows), 
transpose(Rows, Columns), 
maplist(all_distinct, Columns), 
Rows = [A,B,C,D,E,F,G,H,I], 
blocks(A, B, C), blocks(D, E, F), blocks(G, H, I).
length_list(L, Ls) :- length(Ls, L). 
blocks([], [], []). 
blocks([A,B,C|Bs1], [D,E,F|Bs2], [G,H,I|Bs3]) :- all_distinct([A,B,C,D,E,F,G,H,I]), blocks(Bs1, Bs2, Bs3). 
%% the following problem/2 with problem#=1 will be added (with assert) to be added.  
problem(1,[[_,_,_,_,_,_,_,_,_], [_,_,_,_,_,3,_,8,5], [_,_,1,_,2,_,_,_,_],
   [_,_,_,5,_,7,_,_,_], [_,_,4,_,_,_,1,_,_], [_,9,_,_,_,_,_,_,_], 
   [5,_,_,_,_,_,_,7,3], [_,_,2,_,1,_,_,_,_], [_,_,_,_,4,_,_,_,9]]).
%% to run the Sudoku program with problem(1,…) shown above. 
%% The first argument is the problem ID number.

Sample run and output
?- puzzle(1). 

** 1. Sudoku problem generated from the solution in 1.
[_,_,_,_,_,_,_,_,_]
[_,_,_,_,_,3,_,8,5]
[_,_,1,_,2,_,_,_,_]
[_,_,_,5,_,7,_,_,_]
[_,_,4,_,_,_,1,_,_]
[_,9,_,_,_,_,_,_,_] 
[5,_,_,_,_,_,_,7,3]
[_,_,2,_,1,_,_,_,_]
[_,_,_,_,4,_,_,_,9]

** 2. Sudoku solution for the problem.
[9,8,7,6,5,4,3,2,1] 
[2,4,6,1,7,3,9,8,5] 
[3,5,1,9,2,8,7,4,6] 
[1,2,8,5,3,7,6,9,4] 
[6,3,4,8,9,2,1,5,7] 
[7,9,5,4,6,1,8,3,2] 
[5,1,9,2,8,6,4,7,3] 
[4,7,2,3,1,9,5,6,8] 
[8,6,3,7,4,5,2,1,9]

Note. Every spot in the puzzle belongs to a (horizontal) row and a (vertical) column, as well as to one single 3x3 square (which we call "square" for short). At the beginning, some of the spots carry a single-digit number between 1 and 9. The problem is to fill the missing spots with digits in such a way that every number between 1 and 9 appears exactly once in each row, in each column, and in each square.

For more information on Sudoku and Hexadoku
https://en.wikipedia.org/wiki/Sudoku
https://krazydad.com/hexsudoku/
Sample problems: https://krazydad.com/hexsudoku/sfiles/KD_HexSudoku_IMH_8_v1.pdf
http://www.conceptispuzzles.com/index.aspx?uri=puzzle/sudoku/mega



Part 2. #2. Hexadoku

#2. Next, design and implement hexadoku (extending sudoku program) to do the following tasks
(3) to run the program with a specified problem (problem/2) with problem-id (N), for example,
?- sudoku2(1, "sol.txt"). %% to run the problem #1 as listed below.
(4) to find all the solutions, to be sorted (in ascending order of elements in row by row). 
(5) to output all the solutions (sorted) to a file named "hexadoku-sols.txt"
(6) to output the total number of the solutions (to the console). 
There are *** solutions found.  

Your program name should be: hexadoku.pl

Problem
 
Solution

Part2 #2. Solution
#2. Your program code - listing here.



Your program runs for test case. Place here only the relevant run log only, and for each task with a proper heading. 
Test Case 1



Output file (the first page screenshot or to copy and paste the first 30-40 lines).





Part2. #2.
Hexadoku is 16x16 squares (instead of 9 by 9 squares for sudoku) and similar to a classic sudoku in how to solve it. 

To represent the digits, we use hexadecimal digits (0-9 with a-f for the digits from 10 to 15). You may check the following sites for more information on Hexadoku and to generate Hexadoku problem with its solution.

https://www.sudoku-puzzles-online.com/hexadoku/choose-hexadoku-grid.php

Problem
 
Solution


Part 3 - Prolog card game for Poker.
For poker game, you may check some of the following sites or resources.
https://en.wikipedia.org/wiki/Poker
https://en.wikipedia.org/wiki/List_of_poker_hands
https://en.wikipedia.org/wiki/Poker_probability
http://www.contrib.andrew.cmu.edu/~gc00/reviews/pokerrules

A Poker Game Description Language by Correia et al. In Proceedings of the 2013 IEEE/WIC/ACM International Joint Conference on Web Intelligence (WI) and Intelligent Agent Technologies (IAT). v.2 pages 353-360. https://dl.acm.org/citation.cfm?id=2569333

#1. Design and implement a poker game of one player with the following tasks: 
(1) to shuffle a deck of cards (or to order a deck of cards in random order) 
(2) to deal the cards to the player to have five cards at once
(3) to display five cards of the player
(4) to show the hand (rank) of the five cards
(5) to find the probability of the hand (rank) of the five cards, from the table of Poker hands.

The name of this program is: poker1.pl

Your program listing 




Your program run and output





Source: https://en.wikipedia.org/wiki/Poker_probability
   
nCk

Hand Distinct hands Frequency Probability Cumulative 
probability Odds Mathematical expression 
of absolute frequency 
Royal flush
1 4 0.000154% 0.000154% 649,740 : 1
Straight flush (excluding royal flush)
9 36 0.00139% 0.0015% 72,192 : 1
Four of a kind
156 624 0.0240% 0.0256% 4,165 : 1
Full house
156 3,744 0.1441% 0.17% 693 : 1
Flush (excluding royal flush and straight flush)
1,277 5,108 0.1965% 0.367% 508 : 1
Straight (excluding royal flush and straight flush)
10 10,200 0.3925% 0.76% 254 : 1
Three of a kind
858 54,912 2.1128% 2.87% 46.3 : 1
Two pair
858 123,552 4.7539% 7.62% 21.0 : 1
One pair
2,860 1,098,240 42.2569% 49.9% 1.37 : 1
No pair / High card
1,277 1,302,540 50.1177% 100% 0.995 : 1
Total 7,462 2,598,960 100% --- 0 : 1

Some Hints 

1. How to select 2 elements out of a list and generate all pairs (permutation).

?- findall([X,Y], (select(X,[a,b,c],L), select(Y,L,L1)), Pair).
Pair = [[a, b], [a, c], [b, a], [b, c], [c, a], [c, b]].

Note. You may use something similar to collect all possible cases of 5 cards picked up out of 52 cards (instead of 2 out of 3 elements in this case).


2. Sample code for a deck and to select one card randomly out of a deck.

deck([
[ace,heart],[ace,diamond],[ace,club],[ace,spade],
[2,heart],[2,diamond],[2,club],[2,spade],
[3,heart],[3,diamond],[3,club],[3,spade],
[4,heart],[4,diamond],[4,club],[4,spade],
[5,heart],[5,diamond],[5,club],[5,spade],
[6,heart],[6,diamond],[6,club],[6,spade],
[7,heart],[7,diamond],[7,club],[7,spade],
[8,heart],[8,diamond],[8,club],[8,spade],
[9,heart],[9,diamond],[9,club],[9,spade],
[10,heart],[10,diamond],[10,club],[10,spade],
[jack,heart],[jack,diamond],[jack,club],[jack,spade],
[queen,heart],[queen,diamond],[queen,club],[queen,spade],
[king,heart],[king,diamond],[king,club],[king,spade]
]).


select1(Card,Deckbefore,Deckafter) :- 
random_select(Card,Deckbefore,Deckafter).


#2. Design and implement a poker game of one player with the following tasks: 
(1) to define each hand as defined in the poker hand table, and using this definition,
(2) to count the number of all the cases of each hand when five cards are drawn, and using this
(3) to compute the probability of each hand
(4) to output the table of poker hands as shown (e.g., https://en.wikipedia.org/wiki/Poker_probability)

(3/14 rev2. clarification on Part3 #2) 
For example, you have "one pair" when only two cards out of five cards are same number (and it is none of any higher hands). Given this definition (in Prolog), you can compute all the possible cases of a pair, its count, and thus the theoretical chance of having one pair. 

Note. You do not have to draw the card images in the first column. 
Note. You should make sure that your probability computed is correct.

The name of this program is: poker2.pl

Your program listing 




Your program run and output







#3. Design and implement a poker game of one player with the following tasks (extending #2). 
For N times, do the simulation
(1) to shuffle a deck of cards (or to order a deck of cards in random order) 
(2) to deal the cards to the player to have five cards at once
(3) to find the hand (rank) of the five cards, to keep the tally of the hand
After N times, output the table of Poker Hands for each hand with
(4) the "theoretical" probability computed in #2
(5) the "experimental" probability computed in #3 
(6) the relative difference between (4) and (5) for each hand
https://en.wikipedia.org/wiki/Relative_change_and_difference

Do this experiment for N=100, 1000, 10000, 100000, 1000000.
Note. If your program runs over 5 minutes for N, then stop this experiment.

N=100 N=1000 N=10000 N=100000 N=1000000
C Name (4) (5) (6) (5) (6) (5) (6) (5) (6) (5) (6)
0 Five of a kind
1 Straight flush
2 Four of a kind
3 Full house
4 Flush
5 Straight
6 Three of a kind
7 Two pair
8 One pair
9 High card

(7) Provide your summary of this experiment.




(8) For sample-size estimate, what would be an acceptable N for a "good" or "reasonable" estimate of poker hands?
https://en.wikipedia.org/wiki/Sample_size_determination




The name of this program is: poker3.pl

Your program listing 




Your program run and output