CUHK CSCI3230
Prolog Programming Assignment Pentago
Due date:: 23::559::559 (GGMT +008::000)),, 18 th November , 201 7
Introduction
Pentago is a two player board game played on a 66 board divided into four 333 sub boards (oor quadrants)) . This game is similar to tic tac toe.. One player owns the red playing pieces (ii..ee.. the red marbles)) and another one owns the black playing pieces (ii..ee.. the black marbles)).. In each turn,, the player place one o f their marble s and rotate a quadrant by 90 degrees . The goal of th is game is to create a vertical,, horizontal or diagonal row of five marbles.. This game is challenging and fun for both kids and adults..
In this assignment,, you are going to implement a simp le Pentago player program . T h is program can help you win the game .
Requirement
In this assignment,, you are required to implement Prolog predicate s :
threatening((BBoard,, Current Player , ThreatsCount )..
pentago_ai((BBoard,, CurrentPlayer , Best Move,, NextBoard))..
Board R epresentation
Pentago is a two player ( say Black player vs.. Red player)) board game.. As shown in f igure 1,, the 66 board has 36 positions for the players to put their marbles on . Besides,, it has 4 quadrants which are labelled as top left,, top right,, bottom left and bottom right . These quadrants can be rotated separately (ssee f igure 2)) .
Figure 1.. An empty 66 board with 33 sub boards
Figure 2.. An example for a rotation of 90 degrees on the bottom right quadrant in anti clockwise .
The holes on the 66 board are represented using a rectangular grid.. Each position i s labelled as shown in figure 3.. A position can only be in either one of the three states::
1.. Occupied by a black marble
2.. Occupied by a r ed marble
3.. Unoccupied (ii..ee.. no marble p ut on it))
We will use board((BBlackL,,RRedL)) to represent a board state.. BlackL is a list in Prolog and stores the positions occupied by black marbles;; RedL is a list in Prolog and stores the positions occupied by red marbles.. The unoccupied positions can be inferred from them.. For example,, board(([[]],,[[]])) represents the empty board in figure 1.. board(([[44,9,, 21,, 26 ],, [55,12,22,36 ])) represents the board in figure 4 .
Game F low
The goal of this game is to create a vertical,, horizontal or diagonal row of five marbles in your chosen color.. The game starts with an empty board.. The players take tur n during the game.. In every turn , a player are required to make the following two actions :
1.. Marble placement:: Place a marble of the chosen color on an unoccupied position
2.. Quadrant rotation:: Rotate a quadrant by 90 degrees either in anti clockwise or clockwi se
The game ends when a ny player create s a vertical,, horizontal or diagonal row of five marbles of the same color ( either before or after the quadrant rotation in their move ).. Looking at the board in the ending state,, the player wins if he//sshe owns five marbles of the same color in a row.. As listed in the Appendix,, there are 32 ways to place the five marbles in a row on an empty board.. Some winning cases are shown in figure 5 . Note that it is possible for the two players to win this game a t the same
F i gure 3.. Coordinates of each position in the board
F i gure 4.. A position can only be in one of the three states..
time (ssee f igure 5 c)).. The players draw when neither of them make five marbles of the same color in a row.. Under this ending condition,, because marble placement never prevent s the current player from winning but quadrant rotation can (ssee figure 5 d,, if Black pla yer rotates top right quadrant before putting a black marble,, Red player wins..)) , we assume that the player will always place a marble before quadrant rotation.. A move is a structure in Prolog::
move((PPosition,,DDirection,,QQuadrant))
where
Position refers to the place where the new marble will be put on the existing board,, {11,2,,,,336}}
Direction refers to the direction of rotation of a quadrant,, {aanti clockwise,, clockwise}}
Quadrant refers to the 4 possible quadrants,, {ttop left,, top right,, bottom left,, bottom rig ht}}
F igure 5 . (aa)) Five black marbles in a vertical row . (bb)) Five red marbles in a diagonal row . (cc)) Five black marbles in a diagonal row and at the same time f ive red marbles in a horizontal row.. (dd)) Nobody wins at the moment..
Threatening
This section describes the Prolog predicate
threaten ing (BBoard,, Current Player , ThreatsCount )..
As listed in the Appendix,, there are only 32 ways to place the five marbles in a row on an empty board . We call each of these ways as a winning row . The current state of the board is stored in Board , in board((BBlackL,,RRedL)) structure . CurrentP layer player is threatened when any of the winning rows is filled with exactly 4 opponents marbles with 1 empty slot 1 , which we call each of them as a threatening row . From the perspective of the Current Player player,, the
1 We ignore the formation of threatening rows after rotation of the quadrants..
a
b
c
d
degree of the threat is defined as the number of threatening rows produced on a board by the opponent and is stored in ThreatsCount . Refer to figure 6 , the number of threatening rows with respect to Black player is 3,, i..ee.. [11,8,15,22,29]] , [ 8,15,22,29 ,33 6 ] and [55,11,17,23,29]] . T he number of threatening rows with respect to Red player is 0 (ii..ee.. not threatened)).. T he more threatening rows you create for the opponent , the higher the chance of winning.. In your turn,, the less threatening rows you encounter,, the higher the chance of winning..
F i gure 6 . Coordinates of each position in the board..
On Choosing the Best M o ve
In this assignment,, we will implement a greedy strategy in the Pentagon AI program on choosing the best move((ss)) by using the predicate::
pentago_ai((BBoard,, CurrentPlayer ,BBestMove,,NNextBoard))..
Your program will search the states of the board one step ahead ( e . g . Black players turn Red players tur n)).. The choice of the move by CurrentPlayer player is stored in BestMove represented in move structure.. We denote the two turns as T n and T n++11 respectively , hence three layers of board states . A ssume it is CurrentPlayer players turn..
The goal of CurrentPlayer player is to win this game as soon as possible given the current board state Board . CurrentPlayer player always executes a winning move if it exists in turn T n . If not,, CurrentPlayer player will think one turn ahead and prevent the rational o pponent from winning in turn T n++11 unless both players win at the same time.. The opponent will try his//hher best to win the game and threaten CurrentPlayer player.. CurrentPlayer player prefers a draw over a loss and will choose any move as BestMove if he//ssh e must los e the game..
When neither of players wins after turn T n++11 ends,, CurrentPlayer player will reduce threat s made by the op ponent as much as possible.. How to count t he threat s has been discussed in the previous section.. After deciding the BestMove , it will be executed a give the new board NextBoard . The partial search tree formed by your program is shown in figure 7..
N ote that if the player can only win by placing a marble and without quadrant rotation,, the program should produce a NextBoard containing five marbles in a row.. Only the P osition of BestMove will be checked in this case while Direction and Quadrant will be ignored .
Figure 7.. Search tree by the AI program..
Illustration
Example 1
? threatening((bboard(([[ 3,, 4 ,99,10,21,26,27 ,333 ],,[[55 ,88,11,15,17,, 22,, 29,3 1 ])),, black , S )..
S = 3 .
? threatening((bboard(([[33,4,9,10,21,26,27 ,333 ],,[[55,8,11,15,17,, 22,, 29,3 1]])),,rred,,SS))..
S = 0..
Example 2
Note that the solution for this problem may not be unique..
? pentago_ai((bboard(([[ 2,4 ,110,16,2 1 ,226,27 ],,[[55,8,12,15,, 24,29 ])),, red , BestMove,,NNextBoard))..
BestMove = move(( 34,,aanti clockwise,, bottom right )..
NextBoard = board(([[ 2,, 4,10,16,21,26,2 7 ],,[[55,8,12,15,, 22,, 29,, 36 ])) .
Board
Board
? p entago_ai((bboard(([[ 2,, 4,10,16,2 1 ,226,2 7 ],,[[55,8,12,15,, 24,29 ])),, black ,BBestMove,,NNextBoard))..
BestMove = move(( 3,, clockwise,, top right )..
NextBoard = board(( [22,3,4,5,6,21,26,27]],, [88,12,15,17,24,29]] )..
More Examples
For simplicity,, one of the best moves is shown here..
? pentago_ai((bboard(([[11,4,10,16,21,26,27,30]],,[[55,8,12,15,24,29]])),,bbl ack,,BBestMove,,NNextBoard))..
BestMove = move((33,, clockwise,, top left))
? pentago_ai((bboard(([[11,4,10,13,16,21,26,27,30]],,[[55,6,8,12,15,22,24 ,229,31]])),,bblack,,BBestMove,,NNextBoard))..
BestMove = move((336,, clockwise,, top left))
? pentago_ai((bboard(([[11,4,7,10,18,21,26,27]],,[[33,5,8,12,22,24,25,29,, 31]])),,bblack,,BBestMove,,NN extBoard))..
BestMove = move((111,, anti clockwise,, top right))
? pentago_ai((bboard(([[11,5,6,13,16,26,32,33,36]],,[[33,8,11,12,17,19,20 ,222,24,29]])),,bblack,,BBestMove,,NNextBoard))..
BestMove = move((221,, anti clockwise,, top right))
Marking Scheme
In the marking,, we will use SWI P rolog version 5.10.4 . There will be ten test cases and each case contributes 10%% of the total marks of the assignment.. You can find old versions of SWI Prolog here
http::////wwww..sswi prolog..oorg /ddownload//sstable??sshow==aall
.
Submission
You should submit a single file student id .ppl through our online system,, e..gg.. if your student ID is 1301234567,, the name should be 1301234567..ppl.. Please limit the file size to be less than 1MB.. You can submit multiple times (bbefore the deadline)),, and the final grade will be the highest mark of all the submissions for this assignment.. Make sure that you have removed all
the unrelated predicates or statements in your .ppl file before submission.. Also,, m ake ensure that your program can return the results for all the test cases in 1 minute in our machine.. If your program fails to do so,, zero mark will be given..
Late submission will lead to marks deduction which is subject to the following penalty scheme..
Number of Days La te
Marks Deduction
1
10%%
2
30%%
3
60%%
4 or more
100%%
Plagiarism will be seriously punished..
Suggestions
1.. Study predicate for sets .
2.. Be careful on the minimax procedure .
3.. Do not perform exhaustive search and early stop is necessary .
4.. Use
profile(( pentago_ai((BBoard,,CCurrentPlayer,,BBestMove,,NNextBoard)) ) .
to optimize your program..
Extension of the assignment
This part is for students who are interested in extending the current program after submitting a workable version of your assignment.. You may modi fy the program so as to make it into real Pentago player program.. You may also use alpha beta pruning technique in choosing the best moves .
References
Rules of Pentago :
http::////wwww..ccreativedeployment..ccom//cclients//mmindtwister//sstrategy guides//MMTUSA_Pentago%%220Rules Strategy%%220Guide%%2202015..ppdf
Reference manual of SWI Prolog::
http::////wwww..sswi prolog..oorg//ppldoc//rrefman//
Appendix
32 ways to make five marbles in a vertical,, horizontal,, or diagonal row
[11,7,13,19,25]]
[55,11,17,23,29]]
[88,9,10,11,12]]
[113,14,15,16,17]]
[11,2,3,4,5]]
[55,10,15,20,25]]
[88,15,22,29,36]]
[114,15,16,17,18]]
[11,8,15,22,29]]
[66,12,18,24,30]]
[99,15,21,27,33]]
[119,20,21,22,23]]
[22,8,14,20,26]]
[66,11,16,21,26]]
[110,16,22,28,34]]
[220,21,22,23,24]]
[22,3,4,5,6]]
[77,13,19,25,31]]
[111,17,23,29,35]]
[225,26,27,28,29]]
[22,9,16,23,30]]
[77,8,9,10,11]]
[111,16,21,26,31]]
[226,27,28,29,30]]
[33,9,15,21,27]]
[77,14,21,28,35]]
[112,18,24,30,36]]
[331,32,33,34,35]]
[44,10,16,22,28]]
[88,14,20,26,32]]
[112,17,22,27,32]]
[332,33,34,35,36]]
Have fun!!
Reviews
There are no reviews yet.