Compare commits
22
Commits
5a3c8c5413
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
60078c40ff | ||
|
|
37ca9f581a | ||
|
|
11d3efdb2c | ||
|
|
6fd0fc10a9 | ||
|
|
c2c5d5e46f | ||
|
|
2230cac96e | ||
|
|
a4d5299514 | ||
|
|
d581bd6c02 | ||
|
|
c14d1d812f | ||
|
|
83d5217151 | ||
|
|
4745c1403e | ||
|
|
ba5b297078 | ||
|
|
be9db10803 | ||
|
|
7dd8ede2f0 | ||
|
|
5d8d06c793 | ||
|
|
960dd89f4e | ||
|
|
ebced2aa74 | ||
|
|
70ae98f31a | ||
|
|
35044f91c7 | ||
|
|
a96bb62966 | ||
|
|
899b9a892e | ||
|
|
b1c59453c1 |
@@ -1,6 +1,5 @@
|
||||
bin/
|
||||
doc/
|
||||
out/
|
||||
deliverable/
|
||||
.classpath
|
||||
.project
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -2,3 +2,5 @@
|
||||
*.log
|
||||
*.out
|
||||
*.gz
|
||||
*.svg
|
||||
*.xcf
|
||||
|
||||
+1
-1
Submodule report/config updated: c27d16e915...5523c571a9
Binary file not shown.
|
After Width: | Height: | Size: 527 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 267 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 133 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 26 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 50 KiB |
Binary file not shown.
+364
-9
@@ -1,26 +1,40 @@
|
||||
%
|
||||
% Report for data structures 2020-2021 assignment part 1.
|
||||
% Report for data structures 2020-2021 assignment part A.
|
||||
%
|
||||
% authors:
|
||||
% Αναστασία Φώτη ΑΕΜ 8959
|
||||
% anastaskf@ece.auth.gr
|
||||
%
|
||||
% Χρήστος Χουτουρίδης ΑΕΜ 8997
|
||||
% cchoutou@ece.auth.gr
|
||||
|
||||
% AuthReportConfig requirements
|
||||
% ===============================================================
|
||||
\newcommand{\AuthorName}{Αναστασία Φώτη}
|
||||
\newcommand{\AuthorMail}{anastaskf@ece.auth.gr}
|
||||
\newcommand{\AuthorAEM}{8959}
|
||||
|
||||
% Document configuration
|
||||
\newcommand{\CoAuthorName}{Χρήστος Χουτουρίδης}
|
||||
\newcommand{\CoAuthorMail}{cchoutou@ece.auth.gr}
|
||||
\newcommand{\CoAuthorAEM}{8997}
|
||||
|
||||
\newcommand{\WorkGroup}{Ομάδα 182}
|
||||
|
||||
\newcommand{\DocTitle}{Λαβύρινθος: Ο Θησέας και ο Μινώταυρος III}
|
||||
\newcommand{\Department}{Τμημα ΗΜΜΥ. Τομεάς Ηλεκτρονικής}
|
||||
\newcommand{\ClassName}{Δομές δεδομένων}
|
||||
\newcommand{\DocTitle}{Λαβύρινθος: Ο Θησέας και ο Μινώταυρος}
|
||||
\newcommand{\InstructorName}{Σιάχαλου Σταυρούλα}
|
||||
|
||||
\newcommand{\InstructorName}{Σταυρούλα Σιάχαλου}
|
||||
\newcommand{\InstructorMail}{ssiachal@auth.gr}
|
||||
\newcommand{\CurrentDate}{\today}
|
||||
|
||||
\input{config/AuthReportConfig.tex}
|
||||
|
||||
%\renewcommand{\AuthorName}{Χρήστος Χουτουρίδης}
|
||||
%\renewcommand{\AuthorMail}{cchoutou@ece.auth.gr}
|
||||
%\renewcommand{\AuthorAEM}{8997}
|
||||
|
||||
\setFancyHeadLR{\ClassName}{\DocTitle}
|
||||
%\setFancyHeadLERO{\ClassName}{\DocTitle}
|
||||
%\BottomTitleSpace{8em}
|
||||
|
||||
\renewcommand{\bottomtitlespace}{0.12\textheight}
|
||||
|
||||
% Document
|
||||
% =================
|
||||
@@ -32,7 +46,348 @@
|
||||
%\listoffigures
|
||||
%\listoftables
|
||||
|
||||
\section{Εισαγωγή}
|
||||
\section{Εισαγωγή}
|
||||
Η παρούσα εργασία αφορά την δημιουργία ενός παιχνιδιού με τίτλο “Μία νύχτα στο μουσείο”.
|
||||
Στο συγκεκριμένο παιχνίδι καλούμαστε να υλοποιήσουμε ένα λαβύρινθο μέσα στον οποίο κινούνται δύο παίκτες, ο Μινώταυρος και ο Θησέας.
|
||||
Στόχος του Μινώταυρου είναι να “πιάσει” τον Θησέα και στόχος του Θησέα είναι να βρει όλα τα εφόδια πού βρίσκονται κατανεμημένα στον ταμπλό, πριν ξημερώσει και πριν τον “πιάσει” ο Μινώταυρος.
|
||||
|
||||
\par Υπενθυμίζουμε πως στις πρώτες εργασίες κληθήκαμε να υλοποιήσουμε το ταμπλό και τα στοιχεία που το απαρτίζουν καθώς και τις βασικές λειτουργίες του παιχνιδιού.
|
||||
Στη δεύτερη εργασία κληθήκαμε επιπρόσθετα να υλοποιήσουμε ένα παίκτη ο οποίος χειρίζεται το ζάρι προς όφελός του.
|
||||
Στην παρούσα εργασία ασχοληθήκαμε με τη δημιουργία ενός παίκτη που κάνει χρήση του αλγόριθμου \eng{minimax}, με σκοπό να προβλέψει καλύτερα την κίνησή του, αναλύοντας τις πιθανές κινήσεις του αντιπάλου.
|
||||
Φυσικά όλα τα βασικά συστατικά του παιχνιδιού που αναπτύξαμε στις δύο πρώτες εργασίες είναι παρόντα.
|
||||
Οι τροποποιήσεις σε αυτά είναι ελάχιστες και αφορούν κυρίως την συμβατότητα με τις νέες λειτουργίες, όπως αυτές περιγράφονται στην εκφώνηση.
|
||||
|
||||
\section{Παραδοτέα}
|
||||
Τα επισυναπτόμενα παραδοτέα αποτελούνται από:
|
||||
\begin{itemize}
|
||||
\item Τον \eng{\textbf{root}} κατάλογο στον οποίο υπάρχει και το \eng{project} του \eng{eclipse}.
|
||||
\item Ένας υποκατάλογος \eng{\textbf{src/}} με τον κώδικα της \eng{java}, αποτελούμενο από ένα αριθμό αντικειμένων ενσωματωμένο στο πακέτο \eng{host.labyrinth}.
|
||||
\item Ένας υποκατάλογος \eng{\textbf{out/}} που περιέχει το παραγόμενο \eng{command line jar} του παιχνιδιού.
|
||||
\item Ένας υποκατάλογος \eng{\textbf{doc/}} με την τεκμηρίωση του κώδικα όπως αυτή έχει παραχθεί από τα σχόλια, με το εργαλείο \eng{doxygen}.
|
||||
Το αρχείο ρυθμίσεων του \eng{doxygen} είναι στον \eng{root} με το όνομα \eng{Doxyfile}.
|
||||
Η πλοήγηση στην τεκμηρίωση μπορεί να γίνει ανοίγοντας το αρχείο \eng{doc/html/index.html}
|
||||
\item Το αρχείο \eng{\textbf{report.pdf}} που αποτελεί την \textbf{παρούσα αναφορά}.
|
||||
\end{itemize}
|
||||
Εκτός από τα επισυναπτόμενα αρχεία διαθέσιμο υπάρχει και το \textbf{\eng{git} αποθετήριο} ολόκληρης της εργασίας \href{https://git.hoo2.net/hoo2/Labyrinth}{εδώ}.
|
||||
Αυτό περιέχει τόσο τον κώδικα της εφαρμογής όσο και τον κώδικα της αναφοράς.
|
||||
|
||||
\section{Σχεδιαστικές επιλογές}
|
||||
Πριν ασχοληθούμε όμως με τα ζητηθέντα αντικείμενα του προγράμματος, θα πρέπει να αναφερθούμε σε ορισμένες δομές που προστέθηκαν, αλλά και κάποιες σχεδιαστικές επιλογές που έγιναν για να απλοποιήσουν τον κώδικα.
|
||||
Αρκετές από αυτές προέρχονται από τις προηγούμενες εργασίες.
|
||||
Τις παραθέτουμε όμως και εδώ καθώς παίζουν σημαντικό ρόλο, τόσο στην λειτουργία του προγράμματος όσο και στην καλύτερη κατανόηση της τρέχουσας εργασίας.
|
||||
|
||||
\subsection{\eng{Accessor - mutator idiom}}
|
||||
Στις προδιαγραφές της εργασίας αφήνεται να εννοηθεί πως ζητείται η χρήση του \eng{\textit{accessor - mutator idiom}}.
|
||||
Θα πρέπει να παραδεχτούμε όμως, πως \textbf{θεωρούμε το συγκεκριμένο ιδίωμα ιδιαίτερα προβληματικό}.
|
||||
Ο κύριος λόγος είναι πως παραβιάζει θεμελιακά τις αφαιρέσεις προδίδοντας τον εσωτερικό σχεδιασμό του αντικειμένου.
|
||||
Ακόμα δίνει πρόσβαση στην εσωτερική δομή του, “πίσω από την πλάτη” του αντικειμένου.
|
||||
Εν αντιθέτως με το ιδίωμα αυτό, \textbf{τα αντικείμενα που υλοποιούνται ως αφαιρέσεις θα μπορούσαν να προσφέρουν μεθόδους που εκτελούν κάποια λειτουργία, κρύβοντας τελείως τις εσωτερικές λεπτομέρειες της υλοποίησης}.
|
||||
Αυτός είναι και ο δρόμος που διαλέξαμε για το σχεδιασμό του προγράμματος.
|
||||
Η κάθε τάξη του προγράμματός μας προσφέρει δημόσια ένα αριθμό από μεθόδους που είναι απαραίτητες για την εκάστοτε απαιτούμενη λειτουργικότητα και κρύβει όσο καλύτερα γίνεται την εσωτερική υλοποίηση.
|
||||
Ενώ λοιπόν υλοποιήσαμε το ζητηθέν \eng{get-set} ζευγάρι για την κάθε μεταβλητή των τάξεων, δεν το χρησιμοποιήσαμε σχεδόν καθόλου μέσα στο πρόγραμμα.
|
||||
|
||||
\subsection{Ενοποιημένο σύστημα συντεταγμένων}
|
||||
Στις προδιαγραφές της 1ης εργασίας περιγράφεται επίσης ένα διπλό σύστημα συντεταγμένων, τόσο για τα πλακίδια όσο και για τα εφόδια.
|
||||
Ένα καρτεσιανό που διευθυνσιοδοτεί ως προς δύο άξονες και περιέχει ένα ζευγάρι γραμμής και στήλης και ένα μονοδιάστατο που αποτελείται από τον γραμμικό συνδυασμό των προηγουμένων.
|
||||
Το μονοδιάστατο αντικατοπτρίζει και την απεικόνιση στη μνήμη ενός πίνακα 2 διαστάσεων σε \eng{row major order}.
|
||||
|
||||
\par Γενικά θεωρούμε πως κάτι τέτοιο δημιουργεί πλεονασμό δεδομένων και επομένως είναι κακή πρακτική.
|
||||
Αυτό γιατί μεταξύ άλλων μειονεκτημάτων, που κυρίως αφορά τον πολυνηματικό προγραμματισμό, οδηγεί και σε προγραμματιστικά λάθη που πιθανώς θα αφήνουν τα δύο συστήματα ασυγχρόνιστα.
|
||||
Για να λύσουμε αυτό το πρόβλημα \textbf{δημιουργήσαμε την τάξη \eng{Position}}, στην οποία εσωτερικά χρησιμοποιούμε μόνο το ένα από τα δύο συστήματα, για την ακρίβεια το μονοδιάστατο και ταυτόχρονα παρέχουμε μεθόδους για την πρόσβαση στη θέση και από τα δύο συστήματα.
|
||||
Η τάξη μεταξύ άλλων προσφέρει και \textbf{στατικές μεθόδους για τις μετατροπές} προσφέροντας έτσι μια είδους εργαλειοθήκη για την εφαρμογή.
|
||||
Για την παρούσα εργασία χρησιμοποιήσαμε την \eng{Position} όπου ήταν δυνατό.
|
||||
|
||||
\subsection{Αναβάθμιση της τάξης \eng{Tile}}
|
||||
Κατά τον προγραμματισμό του παιχνιδιού παρατηρήσαμε πως τόσο η τάξη \eng{Tile} όσο και η \eng{Supply} έχουν πληροφορίες για τη θέση τους στο ταμπλό.
|
||||
Αυτό σημαίνει πως τόσο τα πλακίδια όσο και τα εφόδια ανήκουν στο ταμπλό.
|
||||
Ακόμα σημαίνει πως δημιουργούν επιπλέον πλεονασμό σε δεδομένα, καθώς απαιτείται οι συντεταγμένες του πλακιδίου που βρίσκεται το εφόδιο να επαναληφθούν μέσα στην \eng{Supply}.
|
||||
Κάτι τέτοιο γίνεται αντιληπτό και από τις προδιαγραφές της \eng{Board} η οποία είναι αυτή που περιέχει τους πίνακες αναφορών τόσο των πλακιδίων όσο και των εφοδίων.
|
||||
\textbf{Μια πιο διαισθητική προσέγγιση βέβαια θα ήθελε τα εφόδια να ανήκουν στα πλακίδια} και όχι στο ταμπλό.
|
||||
Με αυτό τον τρόπο η τάξη \eng{Supply} δεν θα είχε τις επαναλαμβανόμενες πληροφορίες θέσης, αλλά αντίθετα η τάξη \eng{Tile} θα είχε απλώς μια επιπρόσθετη πληροφορία για το αν φιλοξενεί κάποιο εφόδιο ή όχι.
|
||||
|
||||
\par Όπως είναι φυσικό θελήσαμε να υλοποιήσουμε αυτή την προσέγγιση.
|
||||
Αν όμως μετακινούσαμε τις αναφορές των εφοδίων στην \eng{Tile} θα αλλοιώναμε τις προδιαγραφές της εκφώνησης.
|
||||
Επομένως επιλέξαμε μια μέση οδό.
|
||||
Εμπλουτίσαμε την \eng{Tile} με μεθόδους που αφορούν τα εφόδια.
|
||||
Αυτές είναι οι:
|
||||
\begin{itemize}
|
||||
\item \eng{\textbf{\textit{int hasSupply (Supply[] supplies)}}}\\
|
||||
που δίνει την δυνατότητα να ελέγξουμε αν ένα πλακίδιο έχει κάποιο ενεργό εφόδιο. Και
|
||||
\item \eng{\textbf{\textit{void pickSupply (Supply[] supplies, int supplyId)}}}\\
|
||||
που δίνει την δυνατότητα σε κάποιο παίκτη να “σηκώσει” το εφόδιο.
|
||||
\end{itemize}
|
||||
Έτσι έχουμε ομοιομορφία με τις αντίστοιχες μεθόδους \eng{\textit{boolean hasWall(int direction)}} και \eng{\textit{int hasWalls()}} της \eng{Tile}.
|
||||
\par Ένας παρατηρητικός αναγνώστης θα διαπιστώσει πως οι συναρτήσεις για τα εφόδια είναι αναγκασμένες να πάρουν τον πίνακα αναφορών στα εφόδια ως όρισμα.
|
||||
Αυτό είναι το τίμημα που πρέπει να πληρώσουμε προωθώντας τις μεθόδους αυτές στην \eng{Tile}.
|
||||
|
||||
\subsection{Αναβάθμιση της τάξης \eng{Board}}
|
||||
Στην διάρκεια του σχεδιασμού θεωρήσαμε ωφέλιμη την εισαγωγή του πίνακα \eng{\textit{int[][] moves}} στην τάξη \eng{Board}.
|
||||
Σε αυτόν καταχωρούμε τις πληροφορίες της τελευταίας κίνησης του κάθε παίκτη, όπως το \eng{id} του πλακιδίου που βρίσκεται ο παίκτης, οι συντεταγμένες του και η “ζαριά” της κίνησης.
|
||||
Στην προσθήκη αυτή οδηγηθήκαμε αφού παρατηρήσαμε πως για την τάξη \eng{HeuristicPlayer} είναι απαραίτητη η πρόσβαση στην θέση και στο \eng{id} του αντιπάλου.
|
||||
Φυσικά την ίδια απαίτηση έχουμε και με την τάξη \eng{MinMaxPlayer}.
|
||||
Η πρόσβαση στις κινήσεις πραγματοποιείται μέσω της μεταβλητής \eng{moves} και της συνάρτησης \eng{\textit{getOpponentMove()}}.
|
||||
Έτσι ο κάθε \eng{HeuristicPlayer} ή \eng{MinMaxPlayer} μπορεί να διαπιστώσει αν στο οπτικό του πεδίο βρίσκεται κάποιος αντίπαλος και να κινηθεί κατάλληλα.
|
||||
|
||||
\par Ακόμα μέσω του ίδιου μηχανισμού δίνουμε την δυνατότητα στους παίκτες να μαθαίνουν το \eng{ID} του αντιπάλου.
|
||||
Αυτό είναι χρήσιμο για τον \eng{MinMaxPlayer} όταν προσπαθεί να σκηνοθετήσει τις κινήσεις του αντιπάλου.
|
||||
Θα αναλύσουμε περισσότερα όμως γύρω από αυτό στην παράγραφο \ref{minimax}.
|
||||
|
||||
\subsection{Αναβάθμιση της τάξης \eng{Player}}
|
||||
Για την επέμβαση σε αυτή την τάξη κινηθήκαμε με γνώμονα την αντιμετώπιση δύο προβλημάτων που μας παρουσιάστηκαν.
|
||||
Αρχικά για την σωστή λειτουργία τη κλάσης \eng{HeuristicPlayer}, όπως αναφέραμε και παραπάνω, είναι αναγκαία η πρόσβαση στο \eng{id} του αντιπάλου.
|
||||
Για τον λόγο αυτό επιλέξαμε να δημιουργείται ένα \eng{id} για κάθε παίκτη μέσω συνάρτησης της τάξης \eng{Board}, στο οποίο θα έχουμε πρόσβαση μέσω του πίνακα \eng{\textit{int[][] moves}}.
|
||||
Τη λειτουργία αυτή αναλαμβάνει η συνάρτηση \eng{\textit{int generatePlayerId()}}.
|
||||
|
||||
\par
|
||||
Επιπρόσθετα, θελήσαμε να ενοποιήσουμε τις υλοποιήσεις των τάξεων \eng{Player}, \eng{HeuristicPlayer} και \eng{MinMaxPlayer}, ώστε να τις κάνουμε συμβατές με την αρχή του \eng{Liskov} \footnote{\eng{\url{https://en.wikipedia.org/wiki/Liskov_substitution_principle}}}.
|
||||
Για το λόγο αυτό οι συναρτήσεις \eng{\textit{move(), statistics()}} και \eng{\textit{final\_statistice()}} είναι πλέον υλοποιημένες και σε όλα τα αντικείμενα.
|
||||
Αυτό μας ανάγκασε να μετακινήσουμε την μεταβλητή \eng{path} στο \eng{base object} δηλαδή στην \eng{Player}.
|
||||
|
||||
\section{\eng{Concepts}}
|
||||
Αν και τα \eng{concepts} υλοποιήθηκαν για την εκπόνηση του πρώτου μέρους, τα παραθέτουμε συνοπτικά και σε αυτή την αναφορά, καθώς τα θεωρούμε ουσιαστικό κομμάτι για όλα τα μέρη της εργασίας.
|
||||
Τα \eng{”concepts”} μας έχουν τη μορφή συναρτήσεων και αφορούν έννοιες σχετικές με την εφαρμογή.
|
||||
Μας δίνεται έτσι η δυνατότητα να ελέγχουμε αν κάποια είσοδός ενός \eng{predicate-concept} πληροί τις προδιαγραφές του ή όχι.
|
||||
Στο σχήμα \ref{fig:concepts} φαίνεται μια οπτικοποιημένη έκδοση τους.
|
||||
|
||||
\subsection{Πλακίδιο φρουρός - \textit{\eng{isSentinel()}}}
|
||||
\WrapFigure{0.45}{r}{fig:concepts}{images/concepts.png}{
|
||||
Οπτική αναπαράσταση των \eng{concepts} που χρησιμοποιούμε σε ένα ταμπλό $7x7$.
|
||||
Με πράσινο αναπαρίστανται όσα πληρούν κάποιο \eng{concept} και με κόκκινο όσα όχι.
|
||||
Τα πλακίδια με το γκρι χρώμα, είναι τα πλακίδια φρουροί.
|
||||
}
|
||||
Πρόκειται για \eng{concept} που μας επιτρέπει να ελέγξουμε αν το πλακίδιο είναι \textit{”πλακίδιο φρουρός”}.
|
||||
Αν δηλαδή βρίσκεται στα εξωτερικά άκρα του ταμπλό.
|
||||
Η υλοποίηση αυτού του \eng{concept} γίνεται μέσω τεσσάρων συναρτήσεων που ελέγχουν χωριστά τις τέσσερεις διευθύνσεις του ταμπλό.
|
||||
|
||||
\par Για την παράδειγμα η \eng{\textit{boolean isLeftSentinel (int tileId)}} μας δίνει την δυνατότητα να ελέγξουμε αν το πλακίδιο είναι \textit{πλακίδιο φρουρού} αριστερά του ταμπλό.
|
||||
Αυτό είναι χρήσιμο για λειτουργίες όπως για παράδειγμα αν θέλουμε να δούμε μήπως χρειάζεται να τοποθετηθεί εξωτερικός τοίχος αριστερά του πλακιδίου.
|
||||
Αντίστοιχα υπάρχουν και οι υπόλοιπες συναρτήσεις για τις υπόλοιπες διευθύνσεις.
|
||||
|
||||
\subsection{Διασχίσιμη διεύθυνση - \textit{\eng{isWalkable()}}}
|
||||
Πρόκειται για \eng{predicate} που μας επιτρέπει να ελέγξουμε αν μια διεύθυνση σε κάποιο πλακίδιο είναι \textit{”διασχίσημη διεύθυνση”}.
|
||||
Αν δηλαδή κάποιος παίκτης μπορεί να κινηθεί σε αυτή.
|
||||
Για να ισχύει κάτι τέτοιο θα πρέπει:
|
||||
\begin{itemize}
|
||||
\item Η διεύθυνση να μην έχει τοίχο.
|
||||
\item Η διεύθυνση να μην είναι η “Κάτω” διεύθυνση του πλακιδίου εισόδου στο λαβύρινθο.
|
||||
\end{itemize}
|
||||
|
||||
\subsection{Χτίσιμη διεύθυνση - \textit{\eng{isWallableDir()}}}
|
||||
Πρόκειται για \eng{predicate} που μας επιτρέπει να ελέγξουμε αν μια διεύθυνση κάποιου πλακιδίου είναι \textit{”χτίσιμη διεύθυνση”}.
|
||||
Αν δηλαδή μπορούμε να τοποθετήσουμε τοίχο στη διεύθυνση αυτή.
|
||||
Για να είναι μια διεύθυνση χτίσιμη θα πρέπει:
|
||||
\begin{itemize}
|
||||
\item Η διεύθυνση να είναι \textit{διασχίσιμη}.
|
||||
\item Το γειτονικό πλακίδιο σε αυτή τη διεύθυνση να μην περιέχει ήδη τον μέγιστο επιτρεπτό αριθμό τοίχων.
|
||||
\item Ο τοίχος να μην δημιουργεί κάποιο κλειστό δωμάτιο.\\
|
||||
\end{itemize}
|
||||
Αυτή η τελευταία \textbf{απαίτηση δεν υπάρχει στις προδιαγραφές} και την παίρνουμε υπόψιν, όπως θα δούμε και στην ενότητα \ref{sec:excecutable}, μόνο αν ο χρήστης την έχει ζητήσει από την γραμμή εντολών.
|
||||
Ο λόγος είναι γιατί ο υπολογισμός της κοστίζει και αυτό μπορεί να μην παίζει ρόλο για ταμπλό μεγέθους $15x15$, αλλά αν ζητηθεί κάποιο πολύ μεγαλύτερο τότε ο χρόνος είναι υπολογίσιμος.
|
||||
Φυσικά στον κώδικα κάνουμε χρήση αυτού του \eng{concept} μόνο κατά τη δημιουργία του ταμπλό, με αποτέλεσμα να μην επιβαρύνεται καθόλου η λειτουργία του προγράμματος κατά τη διάρκεια του παιχνιδιού.
|
||||
Έτσι προτείνουμε στον αναγνώστη και χρήστη της εφαρμογής μας να κάνει χρήση αυτής της επιλογής.
|
||||
Αναφερόμαστε αναλυτικά σε αυτό τον αλγόριθμο στην ενότητα \ref{sec:isRoomCreator}
|
||||
|
||||
\subsection{Χτίσιμο πλακίδιο - \textit{\eng{isWallable()}}}
|
||||
Πρόκειται για \eng{predicate} που μας επιτρέπει να ελέγξουμε αν κάποιο πλακίδιο είναι \textit{”χτίσιμo πλακίδιο”}.
|
||||
Αν δηλαδή υπάρχει κάποια πλευρά του πλακιδίου στην οποία μπορούμε να τοποθετήσουμε τοίχο.
|
||||
Για να ισχύει αυτό θα πρέπει:
|
||||
\begin{itemize}
|
||||
\item Να υπάρχει τουλάχιστον μία \textit{χτίσιμη διεύθυνση} στο πλακίδιο.
|
||||
\item Το πλακίδιο να μην έχει ήδη τον μέγιστο επιτρεπτό αριθμό τοίχων.
|
||||
\end{itemize}
|
||||
|
||||
\subsection{Δημιουργός κλειστού δωματίου - \textit{\eng{isRoomCreator()}}} \label{sec:isRoomCreator}
|
||||
Οι προδιαγραφές της εργασίας που αφορούν τη δημιουργία του ταμπλό, δεν αποτρέπουν την εμφάνιση κλειστών δωματίων.
|
||||
Για το λόγο αυτό υλοποιήσαμε ένα αλγόριθμο που ανιχνεύει την πιθανότητα δημιουργίας κλειστών δωματίων του οποίου η ενεργοποίηση γίνεται κατόπιν επιλογής του χρήστη από τη γραμμή εντολών.
|
||||
|
||||
\par Ένας αλγόριθμος για να είναι λειτουργικός χρειάζεται δεδομένα.
|
||||
Στη δική μας περίπτωση τα δεδομένα είναι οι τοίχοι.
|
||||
Θέλαμε λοιπόν ένα τρόπο αναπαράστασης των τοίχων που να βολεύει για το συγκεκριμένο πρόβλημα.
|
||||
Η λύση που χρησιμοποιήσαμε συνοψίζεται στα εξής:
|
||||
\begin{itemize}
|
||||
\item Οι γωνίες των πλακιδίων ονομάζονται κόμβοι και η θέση τους αναπαρίστανται με έναν αύξοντα αριθμό που ισοδυναμεί με την αναπαράσταση δισδιάστατου πίνακα σε \eng{row major order}.
|
||||
\item Ο κάθε κόμβος αποτελεί τον κόμβο-γωνία \eng{(vertex)} ενός γράφου.
|
||||
\item Ο κάθε τοίχος αναπαρίσταται ως ακμή \eng{(edge)} στον γράφο.
|
||||
\end{itemize}
|
||||
Για παράδειγμα στο σχήμα \ref{fig:graph}, που φαίνεται και ένα παράδειγμα αναπαράστασης, ο τοίχος αριστερά του πλακιδίου `3` αναπαρίσταται ως η ακμή `(4,8)`.
|
||||
\InsertFigure{0.6}{fig:graph}{images/graph.png}{
|
||||
\\(α) Διευθυνσιοδότηση πλακιδίων(τετραγωνάκια) και κόμβων(σφαίρες) ενός ταμπλό $3x3$.\\
|
||||
(β) Ένας συνεκτικός γράφος που προκύπτει από το (α) ξεκινώντας από τον τοίχο (7, 11).
|
||||
Για τον εν λόγο τοίχο θέλουμε να διαπιστώσουμε αν δημιουργεί κλειστό δωμάτιο.
|
||||
Κάτι που σε αυτή την περίπτωση δεν συμβαίνει.
|
||||
}
|
||||
|
||||
\par Για τον αλγόριθμο υλοποιήσαμε δύο τάξεις.
|
||||
Την \eng{\textbf{Edge}} που δημιουργεί ζευγάρια κόμβων ώστε να μπορεί να αποθηκεύσει τον κάθε τοίχο(ακμή) και την \eng{\textbf{Graph}} που προσφέρει λειτουργίες δημιουργίας συνεκτικού γράφου λαμβάνοντας ως είσοδο τοίχους(ακμές).
|
||||
|
||||
\par Η λειτουργία του είναι απλή.
|
||||
Κάθε φορά που ελέγχουμε αν μία διεύθυνση πλακιδίου είναι \textit{χτίσιμη}, \textbf{δημιουργούμε το μεγαλύτερο δυνατό συνεκτικό γράφο που περιέχει τον εν λόγο τοίχο} και όλους τους ήδη τοποθετημένους τοίχους.
|
||||
Αν στον γράφο που προκύπτει υπάρχει κάποιος κόμβος περισσότερες από μία φορές, δηλαδή ο γράφος δεν είναι απλός, τότε αυτό σημαίνει πως στον εν λόγο κόμβο μπορούμε να πάμε ακολουθώντας τοίχους από τουλάχιστον δύο κατευθύνσεις.
|
||||
Άρα το ταμπλό περιέχει κάποιο κλειστό δωμάτιο.
|
||||
Αφού τον αλγόριθμο τον εκτελούμε για κάθε πιθανή \textit{χτίσιμη διεύθυνση}, τότε ο τοίχος που προκαλεί το κλειστό δωμάτιο είναι ο τρέχον.
|
||||
|
||||
\par Για την λειτουργία του αλγόριθμου χρειαζόμαστε όλους τους τοίχους που είναι ήδη τοποθετημένοι στο ταμπλό στη μορφή \eng{Edge}.
|
||||
Γιαυτό προσθέσαμε στην τάξη \eng{Board} μια λίστα αναφορών \eng{(ArrayList)} και σε αυτήν αποθηκεύουμε κάθε τοίχο που δημιουργούμε.
|
||||
Τον αλγόριθμο μπορούμε να τον ενεργοποιήσουμε αν περάσουμε στο πρόγραμμα ως επιλογή το όρισμα \eng{\texttt{-{}-norooms}} από τη γραμμή εντολών
|
||||
\footnote{Συνίσταται ανεπιφύλακτα η χρήση αυτής της επιλογής}.
|
||||
|
||||
\par Δυστυχώς η κωδικοποίηση που χρησιμοποιούμε εδώ δεν ταιριάζει με αυτή της υπόλοιπης εφαρμογής.
|
||||
Αυτό έχει σαν αποτέλεσμα να πρέπει να δημιουργούμε τον γράφο κάθε φορά.
|
||||
Αυτό έχει κόστος σε χρόνο.
|
||||
Για την ακρίβεια $O(N^{2}logN)$, όπου $Ν$, ο συνολικός αριθμός πλακιδίων του ταμπλό.
|
||||
Αυτό το κόστος αφορά τον έλεγχο του κάθε πλακιδίου.
|
||||
Για όλο το ταμπλό το κόστος είναι $O(N^{4}logN)$.
|
||||
Φυσικά θα μπορούσαμε να χρησιμοποιήσουμε υπομνηματισμό και να αποθηκεύουμε τους γράφους
|
||||
Όμως λόγο του ότι η επιβάρυνση λαμβάνει χώρα μόνο μία φορά κατά την εκκίνηση, σε συνδυασμό με το μικρό μέγεθος του ταμπλό, αποφασίσαμε να μην προχωρήσουμε σε περαιτέρω βελτιστοποίηση.
|
||||
|
||||
\section{Υλοποίηση}
|
||||
Για την μεταγλώττιση της εφαρμογής, απαιτείται \eng{java} έκδοση 8 ή και μεταγενέστερη καθώς έχουμε κάνει χρήση \eng{lambdas}.
|
||||
Για υπενθύμιση, αναφέρουμε συνοπτικά τη λειτουργία ορισμένων αντικειμένων που προσθέσαμε κατά την πρώτη και δεύτερη εργασία.
|
||||
\begin{itemize}
|
||||
\item \eng{\textbf{Const}}\\
|
||||
Το αντικείμενο αυτό περιέχει σταθερές για όλη την εφαρμογή.
|
||||
\item \eng{\textbf{Session}}\\
|
||||
Το αντικείμενο αυτό περιέχει όλες τις τιμές της εφαρμογής που αποτελούν ρυθμίσεις ή επιλογές, όπως πχ το μέγεθος του ταμπλό, τον αριθμό των εφοδίων κτλ.
|
||||
\item \eng{\textbf{Direction}}\\
|
||||
Το αντικείμενο αυτό λειτουργεί σαν \eng{C++ enumerator} και παρέχει ονοματολογία στις διευθύνσεις που χρησιμοποιούμε στην εφαρμογή.
|
||||
\item \eng{\textbf{DirRange}}\\
|
||||
Ομοίως ένα βοηθητικό αντικείμενο αυτή τη φορά για την αυτόματη δημιουργία διευθύνσεων σε βρόχους επανάληψης.
|
||||
\item \eng{\textbf{Edge}}\\
|
||||
Το αντικείμενο αυτό όπως είδαμε και στην ενότητα \ref{sec:isRoomCreator}, λειτουργεί ως αναπαράσταση των τοίχων με τη μορφή ακμών ενός γράφου.
|
||||
Προσφέρει \eng{constructor} που δέχεται για ορίσματα συντεταγμένες από πλακίδια και διευθύνσεις.
|
||||
Με αυτό τον τρόπο λειτουργεί ως διεπαφή ανάμεσα στην κωδικοποίηση που χρησιμοποιείται στην υπόλοιπη εφαρμογή και στην κωδικοποίηση που χρησιμοποιείται για την εύρεση κλειστών δωματίων.
|
||||
\item \eng{\textbf{Graph}}\\
|
||||
Το αντικείμενο αυτό υλοποιεί λειτουργίες ενός συνεκτικού γράφου.
|
||||
Ο \eng{constructor} της τάξης δέχεται ως όρισμα μια ακμή και στην ουσία δημιουργεί τους 2 πρώτους κόμβους.
|
||||
Οι βασικές λειτουργίες είναι η \eng{\textit{attach()}} η οποία δέχεται μια ακμή και προσπαθεί να τοποθετήσει τους κόμβους της στο γράφο με συνεκτικό τρόπο.
|
||||
Και η \eng{\textit{count()}} η οποία δέχεται ένα κόμβο και μετράει πόσες φορές ο κόμβος αυτός περιέχεται στον γράφο.
|
||||
\item \eng{\textbf{Position}}\\
|
||||
Το αντικείμενο αυτό χρησιμοποιείται ως ένα κοινό σύστημα αναπαράστασης συντεταγμένων για την εφαρμογή.
|
||||
Ακόμα προσφέρει μεθόδους μετατροπής της μιας αναπαράστασης στην άλλη.
|
||||
Οι αναπαραστάσεις αυτές όπως αναφερθήκαμε και παραπάνω είναι η καρτεσιανή που περιέχει δύο μεταβλητές για την γραμμή και τη στήλη και η μονοδιάστατη \eng{(id)}.
|
||||
\item \eng{\textbf{Range}}\\
|
||||
Το αντικείμενο χρησιμοποιείται για να δημιουργεί εύρη τιμών.
|
||||
\item \eng{\textbf{ShuffledRange}}\\
|
||||
Το αντικείμενο αυτό χρησιμοποιείται για να δημιουργεί \textit{“τυχαίως διατεταγμένα”} εύρη τιμών.
|
||||
Η τάξη αυτή κληρονομεί την \eng{Range} και προσθέτει τη λειτουργία του τυχαίου ανακατέματος των τιμών.
|
||||
\end{itemize}
|
||||
|
||||
\subsection{Αλγόριθμος \eng{minimax}} \label{minimax}
|
||||
\WrapFigure{0.45}{r}{fig:maxValue}{images/maxValue.png}{
|
||||
Διάγραμμα ροής της \eng{\textit{maxValue()}}, όπου γίνεται αναδρομική κλήση της \eng{\textit{minValue()}}.
|
||||
}
|
||||
Ο σκοπός της παρούσας εργασίας θεωρούμε πως ήταν η υλοποίηση του αλγόριθμου \eng{minimax}.
|
||||
Για τον αλγόριθμο αυτό αρχικά απαιτείται η δημιουργία ενός δέντρου.
|
||||
Το κάθε επίπεδο του δέντρου έχει για κόμβους όλες τις δυνατές κινήσεις των παικτών εναλλάξ.
|
||||
Έτσι ο κάθε κόμβος αναπαριστά μια κίνηση και τα παιδιά του κάθε κόμβου αναπαριστούν όλες τις δυνατές κινήσεις-απαντήσεις του αντίπαλου παίκτη στην κίνηση αυτή.
|
||||
Καθώς εκτελείται ο αλγόριθμος η αξιολόγηση της κάθε κίνησης από τους κατώτερους κόμβους ανεβαίνει προς τα πάνω.
|
||||
Η επιλογή γίνεται εναλλάξ από τον κάθε παίκτη.
|
||||
Έτσι ο αντίπαλος επιλέγει για τιμή του κάθε κόμβου την τιμή από τα παιδιά με την μικρότερη αξιολόγηση, ενώ ο παίκτης μας την τιμή με την μεγαλύτερη.
|
||||
Στο τέλος ο βασικός κόμβος περιέχει την μέγιστη δυνατή τιμή λαμβάνοντας υπόψιν την καλύτερη δυνατή κίνηση-απάντηση του αντιπάλου.
|
||||
|
||||
\par Για τον κάθε κόμβο υλοποιήσαμε την τάξη \eng{Node} ακολουθώντας τις οδηγίες της εκφώνησης.
|
||||
Πέρα από αυτές όμως χρειαστήκαμε και μια επιπλέον πληροφορία, που αφορά το μονοπάτι της επιλογής των κόμβων από τον αλγόριθμο.
|
||||
Δηλαδή, σε κάθε κόμβο αποθηκεύουμε επιπλέον ένα δείκτη προς τον κόμβο από τον οποίο έγινε η επιλογή της τιμής του.
|
||||
Με αυτό τον τρόπο ήμαστε σε θέση, μετά την εκτέλεση του αλγόριθμου, να γνωρίζουμε τόσο την τιμής της αξιολόγησης, όσο και την κίνηση από την οποία προήλθε.
|
||||
|
||||
\par Η υλοποίηση του \eng{minimax} αλγόριθμου έγινε στην τάξη \eng{MinMaxPlayer}.
|
||||
Η τάξη αυτή κληρονομεί την \eng{Player} και μαζί και τις βασικές λειτουργίες όπως τους \eng{Constructors} και το \eng{accessor - mutator idiom}.
|
||||
Ακολουθώντας τον σχεδιασμό της \eng{HeuristicPlayer}, υλοποιήσαμε την \eng{MinMaxPlayer}, δίχως κανένα \eng{data member}.
|
||||
Αυτά ανήκουν όλα στην \eng{Player}.
|
||||
Έτσι όλη η επιπρόσθετη λειτουργικότητα της τάξης αφορά τον αλγόριθμο \eng{nimimax}.
|
||||
Οι κύριες μέθοδοι που υλοποιήθηκαν για την δημιουργία του δέντρου είναι:\\
|
||||
\begin{itemize}
|
||||
\item \eng{\textbf{\textit{int[] dryMove()}}}\\
|
||||
Η συνάρτηση αυτή προσομοιώνει την κίνηση κάποιου παίκτη χρησιμοποιώντας για ταμπλό αυτό από τα ορίσματά της.
|
||||
Έτσι μπορούμε να πραγματοποιήσουμε διαφορετικές κινήσεις σε διαφορετικούς κλώνους του ταμπλό χωρίς να αλλοιώσουμε το βασικό ταμπλό του παιχνιδιού.
|
||||
|
||||
\item \eng{\textbf{\textit{void createMySubtree()}}}\\
|
||||
Η συνάρτηση αυτή είναι η μία από τις δύο μεθόδους που υλοποιούν το δέντρο με τις δυνατές κινήσεις των παικτών.
|
||||
Ξεκινώντας από τη τρέχουσα θέση του παίκτη και για κάθε “διασχίσιμη διεύθυνση” δημιουργούμε και ένα αντίγραφο του ταμπλό και σε αυτό εκτελούμε την κίνηση του παίκτη εικονικά μέσω της συνάρτησης \eng{\textit{dryMove()}}.
|
||||
Έπειτα αποθηκεύουμε στο δέντρο την κίνηση αυτή ώς κόμβο και καλούμε την συνάρτηση \eng{\textit{createOppSubtree()}} με αυτόν σαν όρισμα.
|
||||
|
||||
\item \eng{\textbf{\textit{void createOppSubtree()}}}\\
|
||||
Η συνάρτηση εκτελεί την ίδια διαδικασία με την \eng{\textit{createMySubtree()}} αλλά για τον αντίπαλο αυτή τη φορά.
|
||||
|
||||
\item \eng{\textbf{\textit{double evaluation()}}}\\
|
||||
Η συνάρτηση αυτή είναι υπεύθυνη για την αξιολόγηση της κάθε κίνησης.
|
||||
Τόσο στην περίπτωση της του παίκτη, όσο και στην περίπτωση του αντιπάλου.
|
||||
Για να είναι συμβατή με τα ζητούμενα, αξιολογεί την κίνηση “κοιτώντας” το ταμπλό στην κατεύθυνση της κίνησης και μόνο.
|
||||
Αυτή η συμπεριφορά είναι αρκετά περιοριστική και οδηγεί σε πολύ φτωχά αποτελέσματα.
|
||||
Για παράδειγμα ενώ μια κίνηση προς τον αντίπαλο αξιολογείται αρνητικά καθώς ο παίκτης μπορεί να τον “δει”, η κατεύθυνση μακριά από τον αντίπαλο δεν αξιολογείται θετικά.
|
||||
Ακόμα στην περίπτωση που η αξιολόγηση αφορά κίνηση του αντιπάλου, δεν έχουμε την δυνατότητα να αξιολογήσουμε την κατάσταση της πίστας συνολικά.
|
||||
Για να αντιμετωπίσουμε αυτόν τον τελευταίο περιορισμό, μετά την προσομοίωση της κίνησης του αντιπάλου, αξιολογούμε ξανά την τελευταία κίνηση του παίκτη.
|
||||
Μόνο που πλέον ο αντίπαλος είναι σε νέα θέση.
|
||||
\end{itemize}
|
||||
|
||||
\par Τόσο η \eng{\textit{createMySubtree()}}, όσο και η \eng{\textit{createOppSubtree()}} εσωτερικά αξιολογούν την κάθε κίνηση.
|
||||
Αυτό είναι περιττό καθώς οι μόνοι κόμβοι που απαιτείται να αξιολογηθούν είναι τα φύλλα.
|
||||
Παρόλα αυτά αποφασίσαμε να εκτελέσουμε την \eng{\textit{evaluate()}} που αξιολογεί την κίνηση σε κάθε κόμβο ώστε να συμβαδίζουμε με τις οδηγίες της εκφώνησης.
|
||||
|
||||
\par Ο αλγόριθμος ολοκληρώνεται με την υλοποίηση των συναρτήσεων που ανάγουν τις αξιολογήσεις των κόμβων προς τα πάνω.
|
||||
Αυτές είναι οι \eng{\textit{maxValue()}} και \eng{\textit{minValue()}} και εκτελούνται αναδρομικά εναλλάξ από τον παίκτη και τον αντίπαλο.
|
||||
Στο σχήμα \ref{fig:maxValue} φαίνεται το διάγραμμα ροής της \eng{\textit{maxValue()}}.
|
||||
Η \eng{\textit{minValue()}} είναι αντίστοιχη.
|
||||
Ο παίκτης προσπαθεί μέσω της \eng{\textit{maxValue()}} να επιλέξει τη μεγαλύτερη αξιολόγηση ενώ ο αντίπαλος, μέσω της \eng{\textit{minValue()}}, επιλέγει την μικρότερη.
|
||||
|
||||
\section{Εκτελέσιμο} \label{sec:excecutable}
|
||||
Όπως αναφέραμε και στην παράγραφο με τα παραδοτέα, σε αυτά υπάρχει και το παραγόμενο εκτελέσιμο για την γραμμή εντολών.
|
||||
Πρόκειται για ένα \eng{jar} αρχείο το οποίο μπορεί κάποιος να εκτελέσει σε ένα τερματικό, μέσω της εντολής \eng{\texttt{java -jar labyrinth}}.
|
||||
\InsertFigure{0.9}{fig:executable_help}{images/screenshot_help.png}{
|
||||
Οι διαθέσιμες επιλογές και οι τρόποι με τους οποίους μπορούμε να εκτελέσουμε το παιχνίδι.
|
||||
}
|
||||
Στον κώδικά μας χρησιμοποιούμε \eng{asserions} ώστε να ελέγξουμε την είσοδο και τις επιλογές του χρήστη.
|
||||
Επομένως αν κάποιος θέλει να πειραματιστεί με τις επιλογές καλό θα ήταν να τα ενεργοποιήσει στην \eng{VM} της \eng{java} με την παράμετρο \eng{\texttt{-ea}}.
|
||||
Σε αυτή την περίπτωση θα μπορούσε να εκτελέσει \eng{\texttt{java -ea -jar labyrinth -i -{}-norooms ...etc}}
|
||||
|
||||
\par Το παραγόμενο \eng{jar} παρέχει ένα αριθμό από επιλογές-ορίσματα τα οποία ελέγχουν την λειτουργία του παιχνιδιού.
|
||||
Τις επιλογές αυτές μπορούμε να δούμε και από την γραμμή εντολών απλώς εκτελώντας την εντολή \eng{\texttt{java -jar labyrinth -h}}.
|
||||
Στο σχήμα \ref{fig:executable_help} μπορούμε να δούμε ένα στιγμιότυπο με τις διαθέσιμες επιλογές-ορίσματα.
|
||||
Αναλυτικά, εκτός από την \eng{\texttt{-h}} αυτές είναι:
|
||||
\begin{itemize}
|
||||
\item \eng{\textbf{-i}} ή \eng{\textbf{-{}-interactive}}\\
|
||||
Αυτό το όρισμα ενεργοποιεί το \eng{\textit{“interactive mode”}}.
|
||||
Πρόκειται για λειτουργία κατά την οποία το παιχνίδι μετά από κάθε γύρο σταματά και περιμένει είσοδο από τον χρήστη για να προχωρήσει στον επόμενο.
|
||||
Αν αυτή η λειτουργία δεν είναι ενεργοποιημένη, τότε το παιχνίδι εκτελείται μονομιάς και στην έξοδο εκτυπώνονται όλοι οι γύροι αμέσως μετά την είσοδο της εντολής.
|
||||
\textit{Αυτή η επιλογή είναι απενεργοποιημένη ως προεπιλογή}.
|
||||
|
||||
\item \eng{\textbf{-{}-norooms}}\\
|
||||
Αυτό το όρισμα ενεργοποιεί την λειτουργία της εύρεσης και αποτροπής των κλειστών δωματίων.
|
||||
Αν δεν γίνει χρήση αυτή της επιλογής, τότε η λειτουργία αυτή είναι απενεργοποιημένη ως προεπιλογή.
|
||||
Η ενεργοποίησή της θεωρούμε πως είναι καλή επιλογή.
|
||||
Ο λόγος που δεν την προ-επιλέγουμε είναι γιατί δεν ζητείται ρητά από την εκφώνηση.
|
||||
|
||||
\item \eng{\textbf{-b}} ή \eng{\textbf{-{}-board}}\\
|
||||
Αυτό το όρισμα απαιτείται να ακολουθηθεί από ένα θετικό ακέραιο που περιγράφει το επιθυμητό μέγεθος της πλευράς του ταμπλό.
|
||||
Πχ:\\[1.2ex]
|
||||
\eng{\texttt{java -jar labyrinth -b 11}}\\[1.2ex]
|
||||
Αν ο χρήστης δεν χρησιμοποιήσει την επιλογή τότε το προεπιλεγμένο μέγεθος είναι 15.
|
||||
|
||||
\item \eng{\textbf{-s}} ή \eng{\textbf{-{}-supplies}}\\
|
||||
Αυτό το όρισμα απαιτείται να ακολουθηθεί από ένα θετικό ακέραιο που περιγράφει τον επιθυμητό αριθμό των εφοδίων για το παιχνίδι.
|
||||
Πχ:\\[1.2ex]
|
||||
\eng{\texttt{java -jar labyrinth -b 11 -s 7}}\\[1.2ex]
|
||||
Αν ο χρήστης δεν χρησιμοποιήσει την επιλογή τότε ο προεπιλεγμένος αριθμός είναι 4.
|
||||
|
||||
\item \eng{\textbf{-r}} ή \eng{\textbf{-{}-rounds}}\\
|
||||
Αυτό το όρισμα απαιτείται να ακολουθηθεί από ένα θετικό ακέραιο που περιγράφει τον επιθυμητό αριθμό γύρων μέχρι να ολοκληρωθεί το παιχνίδι.
|
||||
Πχ:\\[1.2ex]
|
||||
\eng{\texttt{java -jar labyrinth -r 200 -s 7}}\\[1.2ex]
|
||||
Αν ο χρήστης δεν χρησιμοποιήσει την επιλογή τότε ο προεπιλεγμένος αριθμός είναι 100.
|
||||
\end{itemize}
|
||||
\InsertFigure{0.6}{fig:executable}{images/screenshot.png}{
|
||||
Στιγμιότυπο από την εκτέλεση του προγράμματος σε \eng{interactive mode} και με αποφυγή κλειστών δωματίων.
|
||||
Η εντολή που χρησιμοποιήθηκε είναι \eng{\texttt{java -jar labyrinth -i -{}-norooms}}
|
||||
}
|
||||
|
||||
\section{Παρατηρήσεις}
|
||||
Κλείνοντας θα θέλαμε να πούμε πως η παρούσα εργασία ήταν μια προσπάθεια να υλοποιήσουμε τα ζητούμενα χρησιμοποιώντας όμως πρακτικές που θεωρούμε αναγκαίες για την συγγραφή κώδικα.
|
||||
Πρακτικές που στόχο έχουν να αποτρέψουν την πιθανότητα λανθασμένης χρήσης σε βάθος χρόνου.
|
||||
Για το λόγο αυτό προσπαθήσαμε να περιορίσουμε, όπου αυτό δεν ήταν αντίθετο με τα ζητούμενα, την δυνατότητα της λανθασμένης χρήσης εισάγοντας αρκετά επιπλέον εργαλεία για την εφαρμογή.
|
||||
Τέτοια για παράδειγμα είναι η τάξη \eng{Position} ή η αποφυγή των \eng{setters} κτλ.
|
||||
Ακόμα προσπαθήσαμε να οργανώσουμε τις δομές δεδομένων με τέτοιο τρόπο ώστε να περιορίσουμε τον απαιτούμενο κώδικά όσο το δυνατό περισσότερο.
|
||||
Αυτό έχει ως αποτέλεσμα ένα κάπως πιο μακροσκελές κώδικα, με πολλά αντικείμενα και συναρτήσεις, που ίσως είναι δυσανάλογος των μικρών απαιτήσεων της εργασίας.
|
||||
Ελπίζουμε να μην θεωρηθούν υπερβολικά.
|
||||
|
||||
% References
|
||||
% ============================
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
/**
|
||||
* @file Board.java
|
||||
*
|
||||
* @author Christos Choutouridis AEM:8997
|
||||
* @email cchoutou@ece.auth.gr
|
||||
* @author
|
||||
* Anastasia Foti AEM:8959
|
||||
* <anastaskf@ece.auth.gr>
|
||||
*
|
||||
* @author
|
||||
* Christos Choutouridis AEM:8997
|
||||
* <cchoutou@ece.auth.gr>
|
||||
*/
|
||||
|
||||
package net.hoo2.auth.labyrinth;
|
||||
package host.labyrinth;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.function.IntFunction;
|
||||
@@ -29,8 +34,10 @@ class Board {
|
||||
this.S = 0;
|
||||
this.W = 0;
|
||||
tiles = null;
|
||||
supplies =null;
|
||||
supplies = null;
|
||||
walls = new ArrayList<Edge>();
|
||||
moves = new int[Const.numOfPlayers][Player.MOVE_DATA_SIZE];
|
||||
playerCount =0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -40,12 +47,15 @@ class Board {
|
||||
*/
|
||||
Board(int N, int S) {
|
||||
assert (N%2 != 0) : "Board's size has to be an odd number.";
|
||||
assert (S <= (N*N-2)) : "At least 2 tiles has to be without supplies.";
|
||||
this.N = Session.boardSize = N;
|
||||
this.S = S;
|
||||
this.W = 0;
|
||||
tiles = new Tile[N*N];
|
||||
supplies = new Supply[S];
|
||||
walls = new ArrayList<Edge>();
|
||||
moves = new int[Const.numOfPlayers][Player.MOVE_DATA_SIZE];
|
||||
playerCount =0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,13 +73,28 @@ class Board {
|
||||
*/
|
||||
Board(Board b) {
|
||||
// Copy primitives
|
||||
this.N = b.N;
|
||||
this.S = b.S;
|
||||
this.W = b.W;
|
||||
// Clone arrays
|
||||
this.tiles = b.tiles.clone();
|
||||
this.supplies = b.supplies.clone();
|
||||
this.walls = b.walls;
|
||||
this.N = b.N;
|
||||
this.S = b.S;
|
||||
this.W = b.W;
|
||||
tiles = new Tile[b.tiles.length];
|
||||
supplies = new Supply[b.supplies.length];
|
||||
walls = new ArrayList<Edge>();
|
||||
moves = new int[Const.numOfPlayers][Player.MOVE_DATA_SIZE];
|
||||
playerCount =b.playerCount;
|
||||
|
||||
// clone moves array of array of primitives
|
||||
for (int i=0 ; i<b.moves.length ; ++i)
|
||||
this.moves[i] = b.moves[i].clone();
|
||||
|
||||
// Clone arrays of objects
|
||||
for (int i=0 ; i<b.tiles.length ; ++i)
|
||||
this.tiles[i] = new Tile(b.tiles[i]);
|
||||
for (int i=0 ; i<b.supplies.length ; ++i)
|
||||
this.supplies[i] = new Supply(b.supplies[i]);
|
||||
|
||||
// clone vectors
|
||||
for (Edge it: b.walls)
|
||||
this.walls.add(new Edge(it));
|
||||
}
|
||||
/** @} */
|
||||
|
||||
@@ -131,7 +156,14 @@ class Board {
|
||||
}
|
||||
|
||||
/**
|
||||
* Predicate to check if a direction is Walkable
|
||||
* Predicate to check if a direction is Walkable.
|
||||
*
|
||||
* A `walkable` direction is a tile direction where:
|
||||
* <ul>
|
||||
* <li>The wall is not the DOWN wall from tile (0, 0).
|
||||
* <li>There is not already a wall in the desired direction. (Implies no sentinel tile).
|
||||
* </ul>
|
||||
*
|
||||
* @param tileId The starting tileId.
|
||||
* @param direction The desired direction.
|
||||
* @return True if it is walkable.
|
||||
@@ -142,9 +174,16 @@ class Board {
|
||||
}
|
||||
|
||||
/**
|
||||
* Predicate to check if a direction is Walkable
|
||||
* Predicate to check if a direction is Walkable.
|
||||
*
|
||||
* A `walkable` direction is a tile direction where:
|
||||
* <ul>
|
||||
* <li>The wall is not the DOWN wall from tile (0, 0).
|
||||
* <li>There is not already a wall in the desired direction. (Implies no sentinel tile).
|
||||
* </ul>
|
||||
*
|
||||
* @param row Row position of the starting tile.
|
||||
* @param column Column position of the starting tile.
|
||||
* @param col Column position of the starting tile.
|
||||
* @param direction The desired direction.
|
||||
* @return True if it is walkable.
|
||||
*/
|
||||
@@ -153,6 +192,15 @@ class Board {
|
||||
&& !(Position.toID(row, col) == 0 && direction == Direction.DOWN);
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility function to check if there is a supply on the tile or not
|
||||
* @param tileId The tile to check
|
||||
* @return Yes/no
|
||||
*/
|
||||
boolean hasSupply (int tileId) {
|
||||
return (Const.noSupply != tiles[tileId].hasSupply(supplies)) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to pick supply from a tile. If succeed it also erases the
|
||||
* supply from the board.
|
||||
@@ -170,17 +218,65 @@ class Board {
|
||||
return supplyId;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A plain fair dice functionality provided by the board.
|
||||
* @return A random direction;
|
||||
*/
|
||||
int dice () {
|
||||
ShuffledRange d = new ShuffledRange(Direction.Begin, Direction.End, Direction.Step);
|
||||
ShuffledRange d = new ShuffledRange(DirRange.Begin, DirRange.End, DirRange.Step);
|
||||
return d.get();
|
||||
}
|
||||
|
||||
/** @return the size of each site of the board. */
|
||||
int size () { return N; }
|
||||
|
||||
/**
|
||||
* Utility function to create player IDs
|
||||
* @return The generated player id.
|
||||
*/
|
||||
int generatePlayerId () throws Exception {
|
||||
if (playerCount < Const.numOfPlayers)
|
||||
return playerCount++;
|
||||
else
|
||||
throw new Exception("Maximum number of players exceeded");
|
||||
}
|
||||
|
||||
/**
|
||||
* Boards utility to give access to other player Id.
|
||||
*
|
||||
* @param playerId The id of player who asks
|
||||
* @return The other player's Id.
|
||||
*/
|
||||
int getOpponentId(int playerId) {
|
||||
return Const.numOfPlayers - (playerId +1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Boards utility to give access to other player moves.
|
||||
*
|
||||
* @param playerId The id of player who asks
|
||||
* @return The moves data of other player
|
||||
*/
|
||||
int[] getOpponentMove (int playerId) {
|
||||
return moves[getOpponentId(playerId)];
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility to update the moves of each player.
|
||||
*
|
||||
* This function is used by the players to update their position on the board.
|
||||
* After that a player can read other player positions using getOpponentMoves()
|
||||
* @see getOpponentMoves()
|
||||
*
|
||||
* @param m Reference to new move data
|
||||
* @param playerId The id of the player who update his/her data.
|
||||
*/
|
||||
void updateMove(int[] m, int playerId) {
|
||||
//moves.set(playerId, Arrays.stream(m).boxed().toArray(Integer[]::new));
|
||||
moves[playerId] = m;
|
||||
}
|
||||
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
@@ -205,6 +301,20 @@ class Board {
|
||||
*/
|
||||
Supply[] getSupplies() { return supplies; }
|
||||
|
||||
/**
|
||||
* @note Use it with care. Any use of this function results to what Sean Parent calls "incidental data-structure".
|
||||
* <a href="https://github.com/sean-parent/sean-parent.github.io/blob/master/better-code/03-data-structures.md"> see also here</a>
|
||||
* @return Reference to inner walls array.
|
||||
*/
|
||||
ArrayList<Edge> getWalls() { return walls; }
|
||||
|
||||
/**
|
||||
* @note Use it with care. Any use of this function results to what Sean Parent calls "incidental data-structure".
|
||||
* <a href="https://github.com/sean-parent/sean-parent.github.io/blob/master/better-code/03-data-structures.md"> see also here</a>
|
||||
* @return Reference to inner walls array.
|
||||
*/
|
||||
int[][] getMoves() { return moves; }
|
||||
|
||||
void setN(int N) { this.N = N; }
|
||||
void setS(int S) { this.S = S; }
|
||||
void setW(int W) { this.W = W; }
|
||||
@@ -221,6 +331,21 @@ class Board {
|
||||
* Any call to this function will probably add memory for the garbage collector.
|
||||
*/
|
||||
void setSupplies(Supply[] supplies) { this.supplies= supplies; }
|
||||
|
||||
/**
|
||||
* @param walls Reference to walls that we want to act as replacement for the inner walls vector.
|
||||
* @note Use with care.
|
||||
* Any call to this function will probably add memory for the garbage collector.
|
||||
*/
|
||||
void setWalls (ArrayList<Edge> walls) { this.walls= walls; }
|
||||
|
||||
/**
|
||||
* @param moves Reference to moves that we want to act as replacement for the inner moves vector.
|
||||
* @note Use with care.
|
||||
* Any call to this function will probably add memory for the garbage collector.
|
||||
*/
|
||||
void setMoves(int[][] moves) { this.moves =moves; }
|
||||
|
||||
/** @} */
|
||||
|
||||
|
||||
@@ -269,32 +394,32 @@ class Board {
|
||||
/**
|
||||
* Predicate to check if a wall creates a closed room.
|
||||
*
|
||||
* This algorithm has a complexity of O(N^2) where N represents the total
|
||||
* number of tiles it should be used with care.
|
||||
* This algorithm has a complexity of @f$ O(N^2logN) @f$ where N represents the total
|
||||
* number of tiles.
|
||||
* It should be used with care.
|
||||
*
|
||||
* @param tileId The tileId of the wall where the wall is.
|
||||
* @param direction The wall's relative direction from the tile.
|
||||
* @param tileId The tileId of the wall.
|
||||
* @param direction The wall's relative direction.
|
||||
* @return True if the wall creates a closed room, false otherwise.
|
||||
*/
|
||||
private boolean createsClosedRoom (int tileId, int direction) {
|
||||
// Get a snapshot of the current walls
|
||||
private boolean isRoomCreator (int tileId, int direction) {
|
||||
// Clone the list of all the walls locally.
|
||||
ArrayList<Edge> w = new ArrayList<Edge>();
|
||||
for (Edge it : walls)
|
||||
w.add(new Edge(it));
|
||||
// Create a graph from the current wall(edge)
|
||||
// and populate the graph with all the edges we can attach.
|
||||
// Create the largest possible coherent graph from the list of walls(edges)
|
||||
Graph g = new Graph(new Edge(tileId, direction));
|
||||
int size;
|
||||
do {
|
||||
size = w.size();
|
||||
for (int i =0, S=w.size() ; i<S ; ++i)
|
||||
if (g.attach(w.get(i))) {
|
||||
w.remove(i);
|
||||
--i; --S;
|
||||
size = w.size(); // mark the size (before the pass)
|
||||
for (int i =0, S=w.size() ; i<S ; ++i) // for each edge(wall) on the local wall list
|
||||
if (g.attach(w.get(i))) { // can we attach the edge(wall) to the graph ?
|
||||
w.remove(i); // if yes remove it from the local wall list
|
||||
--i; --S; // decrease iterator and size to match ArrayList's new values
|
||||
}
|
||||
} while (size != w.size());
|
||||
} while (size != w.size()); // If the size hasn't change(no new graph leafs) exit
|
||||
|
||||
// Search if a vertex is attached more than once.
|
||||
// Search if a vertex is attached to the graph more than once.
|
||||
// This means that there is at least 2 links to the same node
|
||||
// so the graph has a closed loop
|
||||
for (Edge it : walls) {
|
||||
@@ -324,9 +449,7 @@ class Board {
|
||||
*/
|
||||
private boolean isWallableDir (int tileId, int direction) {
|
||||
// Check list
|
||||
if (tileId == 0 && direction == Direction.DOWN)
|
||||
return false;
|
||||
if (tiles[tileId].hasWall(direction))
|
||||
if (!isWalkable(tileId, direction))
|
||||
return false;
|
||||
switch (direction) {
|
||||
case Direction.UP:
|
||||
@@ -342,7 +465,7 @@ class Board {
|
||||
if (tiles[rightTileId.apply(tileId)].hasWalls() >= Const.maxTileWalls) return false;
|
||||
break;
|
||||
}
|
||||
if (Session.loopGuard && createsClosedRoom(tileId, direction))
|
||||
if (Session.loopGuard && isRoomCreator(tileId, direction))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
@@ -364,8 +487,8 @@ class Board {
|
||||
return false;
|
||||
if (tiles[tileId].hasWalls() >= Const.maxTileWalls)
|
||||
return false;
|
||||
Range dirs = new Range(Direction.Begin, Direction.End, Direction.Step);
|
||||
for (int dir ; (dir = dirs.get()) != Const.noTileId ; )
|
||||
Range dirs = new Range(DirRange.Begin, DirRange.End, DirRange.Step);
|
||||
for (int dir = dirs.get() ; dir != Const.EOR ; dir = dirs.get())
|
||||
if (isWallableDir(tileId, dir))
|
||||
return true;
|
||||
return false;
|
||||
@@ -387,6 +510,7 @@ class Board {
|
||||
boolean right = isRightSentinel(i);
|
||||
wallCount += ((up?1:0) + (down?1:0) + (left?1:0) + (right?1:0));
|
||||
tiles[i] = new Tile (i, up, down, left, right);
|
||||
// If we have loopGuard enable we populate walls also.
|
||||
if (Session.loopGuard) {
|
||||
if (up) walls.add(new Edge(i, Direction.UP));
|
||||
if (down) walls.add(new Edge(i, Direction.DOWN));
|
||||
@@ -403,14 +527,16 @@ class Board {
|
||||
*/
|
||||
private void createInnerWall(int tileId) {
|
||||
// Randomly pick a wallable direction in that tile.
|
||||
ShuffledRange randDirections = new ShuffledRange(Direction.Begin, Direction.End, Direction.Step);
|
||||
ShuffledRange randDirections = new ShuffledRange(DirRange.Begin, DirRange.End, DirRange.Step);
|
||||
int dir;
|
||||
do
|
||||
dir = randDirections.get();
|
||||
while (!isWallableDir(tileId, dir));
|
||||
// Add wall to tileId and the adjacent tileId
|
||||
Position neighbor = new Position(Position.toRow(tileId), Position.toCol(tileId), dir);
|
||||
tiles[tileId].setWall(dir);
|
||||
tiles[neighbor.getId()].setWall(Direction.opposite(dir));
|
||||
// If we have loopGuard enable we populate walls also.
|
||||
if (Session.loopGuard)
|
||||
walls.add(new Edge(tileId, dir));
|
||||
}
|
||||
@@ -418,20 +544,19 @@ class Board {
|
||||
/**
|
||||
* This utility creates the inner walls of the board.
|
||||
*
|
||||
* @param walls The number of walls to create
|
||||
* @return The number of walls failed to create.
|
||||
*/
|
||||
private int createInnerWalls () {
|
||||
ShuffledRange randTiles = new ShuffledRange(0, N*N);
|
||||
for (int tileId, i =0, walls =0, shuffleMark =0 ; true ; ) {
|
||||
for (int tileId, walls =0, shuffleMark =0 ; true ; ) {
|
||||
// randomly pick a wallable tile.
|
||||
do {
|
||||
if ((tileId = randTiles.get())== Const.noTileId) {
|
||||
if (i == shuffleMark) // Wallable tiles exhausted.
|
||||
if ((tileId = randTiles.get())== Const.EOR) {
|
||||
if (walls == shuffleMark) // Wallable tiles exhausted.
|
||||
return walls;
|
||||
else { // Re-shuffle and continue.
|
||||
randTiles = new ShuffledRange(0, N*N);
|
||||
shuffleMark =i;
|
||||
shuffleMark =walls;
|
||||
}
|
||||
}
|
||||
} while (!isWallable(tileId));
|
||||
@@ -526,5 +651,7 @@ class Board {
|
||||
* Array to hold all the walls using the edge representation
|
||||
* required by the closed room preventing algorithm.
|
||||
*/
|
||||
private int[][] moves;
|
||||
private int playerCount;
|
||||
/** @} */
|
||||
}
|
||||
@@ -1,21 +1,41 @@
|
||||
/**
|
||||
* @file Common.java
|
||||
*
|
||||
* @author Christos Choutouridis AEM:8997
|
||||
* @email cchoutou@ece.auth.gr
|
||||
* @author
|
||||
* Anastasia Foti AEM:8959
|
||||
* <anastaskf@ece.auth.gr>
|
||||
*
|
||||
* @author
|
||||
* Christos Choutouridis AEM:8997
|
||||
* <cchoutou@ece.auth.gr>
|
||||
*/
|
||||
package net.hoo2.auth.labyrinth;
|
||||
package host.labyrinth;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
//import java.util.function.IntFunction;
|
||||
|
||||
/**
|
||||
* Class to hold constant values for entire application
|
||||
*/
|
||||
class Const {
|
||||
static final int numOfPlayers = 2;
|
||||
static final int maxTileWalls = 2; /**< Number of maximum walls for each tile on the board */
|
||||
static final int noSupply =-1; /**< Number to indicate the absent of supply */
|
||||
static final int noOpponent =-1; /**< Number to indicate the absent of supply */
|
||||
static final int noTileId =-1; /**< Number to indicate wrong tileId */
|
||||
static final int EOR =-1; /**< Number to indicate the End Of Range */
|
||||
static final int viewDistance =3; /**< The max distance of the Heuristic player's ability to see */
|
||||
static final int noView = viewDistance+1;
|
||||
|
||||
/** Parameters to control move evaluation */
|
||||
/** @{ */
|
||||
static final double opponentFactor = 1.0; /**< opponent distance factor */
|
||||
static final double supplyFactor = 0.65; /**< supply distance factor */
|
||||
static final double preMoveFactor = 0.65; /**< pre move distances factor */
|
||||
static final double postMoveFactor = 0.35; /**< post move distances factor */
|
||||
static final int minimaxTreeDepth = 4; /**< The maximum depth of the minimax tree */
|
||||
/** @} */
|
||||
}
|
||||
/**
|
||||
* Application wide object to hold settings like values for the session.
|
||||
@@ -28,6 +48,25 @@ class Session {
|
||||
static boolean interactive = false; /**< When true each round of the game requires user input */
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper C++ like enumerator class for direction ranged loops.
|
||||
*
|
||||
* We can make use of this in loops like:
|
||||
* <pre>
|
||||
* for (int i=DirRange.Begin ; i<DirRange.End ; i += DirRange.Step) { }
|
||||
*
|
||||
* or
|
||||
*
|
||||
* Range directions = new Range(DirRange.Begin, DirRange.End, DirRange.Step);
|
||||
* </pre>
|
||||
*/
|
||||
class DirRange {
|
||||
static final int Begin =1; /**< Iterator style begin of range direction (starting north) */
|
||||
static final int End =8; /**< Iterator style end of range direction (one place after the last) */
|
||||
static final int Step =2; /**< Step for iterator style direction */
|
||||
static final int numOfDirections =4;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper C++-like enumerator class to hold direction
|
||||
*/
|
||||
@@ -36,16 +75,29 @@ class Direction {
|
||||
static final int RIGHT =3; /**< East direction */
|
||||
static final int DOWN =5; /**< South direction */
|
||||
static final int LEFT =7; /**< West direction */
|
||||
static final int Begin =1; /**< Iterator style begin of range direction (starting north) */
|
||||
static final int End =8; /**< Iterator style end of range direction (one place after the last) */
|
||||
static final int Step =2; /**< Step for iterator style direction */
|
||||
static final int NONE =8; /**< No direction */
|
||||
|
||||
/**
|
||||
* Utility to get the opposite
|
||||
* Utility to get the opposite direction.
|
||||
* @param direction Input direction
|
||||
* @return The opposite direction
|
||||
*/
|
||||
static int opposite (int direction) { return (direction+4)%End; }
|
||||
static int opposite (int direction) {
|
||||
return (direction != NONE) ? (direction+4)%DirRange.End : NONE;
|
||||
}
|
||||
|
||||
static int get (int fromId, int toId) {
|
||||
if (Position.toID(Position.toRow(fromId), Position.toCol(fromId)-1) == toId)
|
||||
return Direction.LEFT;
|
||||
else if (Position.toID(Position.toRow(fromId), Position.toCol(fromId)+1) == toId)
|
||||
return Direction.RIGHT;
|
||||
else if (Position.toID(Position.toRow(fromId)+1, Position.toCol(fromId) ) == toId)
|
||||
return Direction.UP;
|
||||
else if (Position.toID(Position.toRow(fromId)-1, Position.toCol(fromId) ) == toId)
|
||||
return Direction.DOWN;
|
||||
else
|
||||
return DirRange.End;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -55,7 +107,7 @@ class Direction {
|
||||
* Position is a helper class to enable us cope with the redundant position data (id and coordinates).
|
||||
* This class provide both static conversion functionalities between id and coordinates
|
||||
* and data representation in the coordinates system.
|
||||
* For clarity we adopt a row-column naming convention.
|
||||
* For clarity we adopt a tileId convention.
|
||||
*/
|
||||
class Position {
|
||||
|
||||
@@ -91,6 +143,7 @@ class Position {
|
||||
case Direction.DOWN: this.id = toID(row-1, col); break;
|
||||
case Direction.LEFT: this.id = toID(row, col-1); break;
|
||||
case Direction.RIGHT:this.id = toID(row, col+1); break;
|
||||
case Direction.NONE: this.id = toID(row, col); break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,7 +229,14 @@ class Range {
|
||||
int get () {
|
||||
if (!numbers.isEmpty())
|
||||
return numbers.remove(0);
|
||||
return Const.noTileId;
|
||||
return Const.EOR;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The size of the underline structure
|
||||
*/
|
||||
int size () {
|
||||
return numbers.size();
|
||||
}
|
||||
|
||||
/** @name protected data types */
|
||||
@@ -219,18 +279,21 @@ class ShuffledRange extends Range {
|
||||
* This class is the wall representation we use in the room preventing algorithm.
|
||||
* In this algorithm we represent the crosses between tiles as nodes (V) of a graph and the
|
||||
* walls as edges. So for example:
|
||||
*
|
||||
* _ V = 15
|
||||
* /
|
||||
* +---+---+---+ We have a 4x4=16 vertices board(nodes) and 14 edges(walls).
|
||||
* | | To represent the vertices on the board we use the
|
||||
* + +---+ + same trick as the tileId.
|
||||
* | | | The edges are represented as vertices pairs.
|
||||
* + + + + <.
|
||||
* | | | \_ V = 7
|
||||
* + +---+---+
|
||||
* ^ ^
|
||||
* V = 0 V = 3
|
||||
* <pre>
|
||||
* 12--13--14---15
|
||||
* | |
|
||||
* 8 9--10 11
|
||||
* | | |
|
||||
* 4 5 6 7
|
||||
* | | |
|
||||
* 0 1---2---3
|
||||
* </pre>
|
||||
* In this example we have a 4x4=16 vertices board(nodes) and 14 edges(walls).
|
||||
* To represent the vertices on the board we use the same trick as the tileId
|
||||
*
|
||||
* V = Row*(N+1) + Column, where N is the board's tile size.
|
||||
*
|
||||
* The edges are represented as vertices pairs. For example (0, 4) or (13, 14).
|
||||
*
|
||||
* @note
|
||||
* Beside the fact that we prefer this kind of representation of the walls in
|
||||
@@ -242,7 +305,7 @@ class ShuffledRange extends Range {
|
||||
*/
|
||||
class Edge {
|
||||
/**
|
||||
* This constructor as as the interface between the application's wall
|
||||
* This constructor acts as the interface between the application's wall
|
||||
* representation and the one based on graph.
|
||||
* @param tileId The tile id of the wall.
|
||||
* @param direction The direction of the tile where the wall should be.
|
||||
@@ -286,13 +349,15 @@ class Edge {
|
||||
* @brief
|
||||
* Provides a graph functionality for the room preventing algorithm.
|
||||
* We use a graph to represent the wall structure of the walls. This way
|
||||
* is easy to find any closed loops. Using graph we transform the problem
|
||||
* of the closed room in the problem of finding a non simple graph.
|
||||
* its easy to find any closed loops. Using graph we transform the problem
|
||||
* of the closed room into the problem of finding a non simple graph.
|
||||
*
|
||||
* If the board has non connected wall structure then we need more than
|
||||
* one graph to represent it.
|
||||
* If the board has non connected wall structure then we would need a non
|
||||
* coherent graph to represent it. This class provides constructors and
|
||||
* methods to create coherent graphs
|
||||
*
|
||||
* An example graph from a board, starting from V=1 is:
|
||||
* An example of the biggest coherent graph we can create from the board bellow,
|
||||
* starting from V=1 is:
|
||||
* <pre>
|
||||
* 6---7 8 (1)
|
||||
* | | / \
|
||||
@@ -304,7 +369,7 @@ class Edge {
|
||||
class Graph {
|
||||
/**
|
||||
* Constructs a node of the graph using the value of a vertex(node).
|
||||
* @param v The verteg to attach.
|
||||
* @param v The vertex to attach.
|
||||
*/
|
||||
Graph (int v) {
|
||||
V = v;
|
||||
@@ -327,7 +392,7 @@ class Graph {
|
||||
|
||||
/**
|
||||
* Attach an edge into a graph IFF the graph already has a vertex
|
||||
* with the same value of one of the vertices of the edge.
|
||||
* with the same value as one of the vertices of the edge.
|
||||
* @param e The edge to attach.
|
||||
* @return The status of the operation.
|
||||
* @arg True on success
|
||||
@@ -348,17 +413,18 @@ class Graph {
|
||||
|
||||
/**
|
||||
* Recursive algorithm that tries to attach an edge into a graph
|
||||
* IFF the graph already has a vertex.
|
||||
* with the same value of one of the vertices of the edge.
|
||||
* IFF the graph already has a vertex with the same value as one
|
||||
* of the vertices of the edge.
|
||||
*
|
||||
* @param e The edge to attach.
|
||||
* @param count An initial count value to feed to the algorithm.
|
||||
* @param count An initial count value to feed the algorithm.
|
||||
* @return The status of the operation.
|
||||
* @arg True on success
|
||||
* @arg False on failure
|
||||
*/
|
||||
private int tryAttach (Edge e, int count) {
|
||||
for (Graph n: E)
|
||||
count += n.tryAttach (e, count);
|
||||
count = n.tryAttach (e, count);
|
||||
if (V == e.getV1()) {
|
||||
E.add(new Graph(e.getV2()));
|
||||
++count;
|
||||
@@ -372,7 +438,8 @@ class Graph {
|
||||
|
||||
/**
|
||||
* Recursive algorithm that tries to count the number of vertices
|
||||
* on the graph with the value of `v`
|
||||
* on the graph with the same value as `v`.
|
||||
*
|
||||
* @param v The vertex to count
|
||||
* @param count An initial count value to feed to the algorithm.
|
||||
* @return The number of vertices with value `v`
|
||||
@@ -384,6 +451,7 @@ class Graph {
|
||||
return ++count;
|
||||
return count;
|
||||
}
|
||||
|
||||
private int V; /**< The value of the current vertex/node */
|
||||
private ArrayList<Graph> E; /**< A list of all the child nodes */
|
||||
}
|
||||
@@ -1,11 +1,45 @@
|
||||
/**
|
||||
* @file Game.java
|
||||
*
|
||||
* @author Christos Choutouridis AEM:8997
|
||||
* @email cchoutou@ece.auth.gr
|
||||
* @author
|
||||
* Anastasia Foti AEM:8959
|
||||
* <anastaskf@ece.auth.gr>
|
||||
*
|
||||
* @author
|
||||
* Christos Choutouridis AEM:8997
|
||||
* <cchoutou@ece.auth.gr>
|
||||
*/
|
||||
|
||||
package net.hoo2.auth.labyrinth;
|
||||
/**
|
||||
* @mainpage A labyrinth board game
|
||||
*
|
||||
* This is a console game, played by 2 players. The Theseus and Minotaur.
|
||||
* The Minotaur goal is to capture Theseus. The Theseus's goal is to collect
|
||||
* all the supplies of the board before Minotaur catches him and before the
|
||||
* game ends.
|
||||
*
|
||||
* In this 3rd assignment we deal with the creation of a new minimax player
|
||||
* who can cheat and manipulate the dice. Documented classes:
|
||||
* - Tile
|
||||
* - Supply
|
||||
* - Board
|
||||
* - Player
|
||||
* - HeuristicPlayer
|
||||
* - MinMaxPlayer
|
||||
* - Game
|
||||
*
|
||||
* Which are the requested classes. We also provide some extra functionalities in:
|
||||
* - Const
|
||||
* - Session
|
||||
* - Direction
|
||||
* - DirRange
|
||||
* - Edge
|
||||
* - Graph
|
||||
* - Position
|
||||
* - Range
|
||||
* - ShuffledRange
|
||||
*/
|
||||
package host.labyrinth;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
@@ -28,8 +62,8 @@ public class Game {
|
||||
int nextRound() { return ++round; }
|
||||
|
||||
/**
|
||||
* Utility to hold the execution of the program waiting for user input.
|
||||
* This is true only if the user passed the interactive flag.
|
||||
* Utility to hold the execution of the program waiting for user input.
|
||||
* This is true only if the user passed the interactive flag.
|
||||
*/
|
||||
void waitUser () {
|
||||
if(Session.interactive) {
|
||||
@@ -69,7 +103,7 @@ public class Game {
|
||||
break;
|
||||
|
||||
case "-s":
|
||||
case "--suplies":
|
||||
case "--supplies":
|
||||
if (i+1 < args.length)
|
||||
Session.supplySize = Integer.parseInt(args[++i]);
|
||||
break;
|
||||
@@ -83,7 +117,7 @@ public class Game {
|
||||
case "--norooms":
|
||||
Session.loopGuard = true;
|
||||
break;
|
||||
|
||||
|
||||
case "-i":
|
||||
case "--interactive":
|
||||
Session.interactive = true;
|
||||
@@ -121,10 +155,11 @@ public class Game {
|
||||
if (!Game.getArguments(args)) throw new Exception("");
|
||||
|
||||
// Create a game, a board and 2 players.
|
||||
Game game = new Game();
|
||||
Board board = new Board(Session.boardSize, Session.supplySize);
|
||||
Player T = new Player(1, "Theseus", true, board, 0);
|
||||
Player M = new Player(2, "Minotaur", false, board, Position.toID(Session.boardSize/2, Session.boardSize/2));
|
||||
Game game = new Game();
|
||||
Board board = new Board(Session.boardSize, Session.supplySize);
|
||||
Player T = new MinMaxPlayer("Theseus", true, board, 0);
|
||||
Player M = new Player("Minotaur", false, board, Position.toID(Session.boardSize/2, Session.boardSize/2));
|
||||
Player players [] = {M, T};
|
||||
|
||||
// Populate data to the board
|
||||
board.createBoard(T.playerTileId(), M.playerTileId());
|
||||
@@ -137,34 +172,36 @@ public class Game {
|
||||
game.waitUser ();
|
||||
// Main game loop
|
||||
while (true) {
|
||||
int[] m;
|
||||
System.out.println();
|
||||
System.out.println("State after round: " + (game.round()+1));
|
||||
System.out.println("Round: " + (game.round()+1));
|
||||
|
||||
// Player moves
|
||||
m = T.move(T.playerTileId());
|
||||
System.out.println(T.getName() + ":\t tileId =" + m[0] + " (" + m[1] + ", " + m[2] + ")");
|
||||
m = M.move(M.playerTileId());
|
||||
System.out.println(M.getName() + ":\t tileId =" + m[0] + " (" + m[1] + ", " + m[2] + ")");
|
||||
// Players moves
|
||||
for (Player p : players) {
|
||||
p.move(p.playerTileId());
|
||||
p.statistics();
|
||||
}
|
||||
board.printBoard(
|
||||
board.getStringRepresentation(T.playerTileId(), M.playerTileId())
|
||||
);
|
||||
|
||||
// Loop termination cases
|
||||
if (T.getScore() == 4) {
|
||||
System.out.println(T.getName() + " Wins!!! Score =" + T.getScore());
|
||||
System.exit(0);
|
||||
if (T.getScore() == Session.supplySize) {
|
||||
System.out.println("**** " + T.getName() + " Wins!!! Score =" + T.getScore() + " ****");
|
||||
break;
|
||||
}
|
||||
if (M.getScore() == 4 || M.playerTileId() == T.playerTileId()) {
|
||||
System.out.println(M.getName() + " Wins!!! Score =" + M.getScore());
|
||||
System.exit(0);
|
||||
if (M.playerTileId() == T.playerTileId()) {
|
||||
System.out.println("**** " + M.getName() + " Wins!!!");
|
||||
break;
|
||||
}
|
||||
if (!(game.nextRound() < Session.maxRounds)) {
|
||||
System.out.println("New day has come... Tie!!!");
|
||||
System.exit(0);
|
||||
System.out.println("**** New day has come... Tie! ****");
|
||||
break;
|
||||
}
|
||||
game.waitUser ();
|
||||
}
|
||||
for (Player p : players)
|
||||
p.final_statistics();
|
||||
System.exit(0);
|
||||
}
|
||||
catch (Exception e) {
|
||||
// We don't handle exceptions. Print error and exit with error status.
|
||||
@@ -0,0 +1,270 @@
|
||||
/**
|
||||
* @file HeuristicPlayer.java
|
||||
*
|
||||
* @author
|
||||
* Anastasia Foti AEM:8959
|
||||
* <anastaskf@ece.auth.gr>
|
||||
*
|
||||
* @author
|
||||
* Christos Choutouridis AEM:8997
|
||||
* <cchoutou@ece.auth.gr>
|
||||
*/
|
||||
|
||||
package host.labyrinth;
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* This class represents the game's player who cheats.
|
||||
*/
|
||||
class HeuristicPlayer extends Player {
|
||||
|
||||
/** @name Constructors */
|
||||
/** @{ */
|
||||
|
||||
/**
|
||||
* Create a new player and put him at the row-column coordinates
|
||||
* @param name The name of the player
|
||||
* @param champion Flag to indicate if a player is a `champion`
|
||||
* @param board Reference to the board of the game
|
||||
* @param row The row coordinate of initial player position
|
||||
* @param column The column coordinate of initial player's position
|
||||
*/
|
||||
public HeuristicPlayer(String name, boolean champion, Board board, int row, int column) throws Exception {
|
||||
super(name, champion, board, row, column);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new player and put him at the row-column coordinates
|
||||
* @param name The name of the player
|
||||
* @param champion Flag to indicate if a player is a `champion`
|
||||
* @param board Reference to the board of the game
|
||||
* @param tileId The tileId coordinate of player's initial position
|
||||
*/
|
||||
public HeuristicPlayer(String name, boolean champion, Board board, int tileId) throws Exception {
|
||||
super(name, champion, board, tileId);
|
||||
}
|
||||
/** @} */
|
||||
|
||||
/** @name Board's main application interface */
|
||||
/** @{ */
|
||||
|
||||
/**
|
||||
* Utility to get the distance of a possible supply in some direction
|
||||
* @param currentPos The current position of the player
|
||||
* @param direction The direction to check
|
||||
* @return The distance or Const.noSupply
|
||||
*/
|
||||
int supplyInDirection(int currentPos, int direction) {
|
||||
Position pos = new Position(Position.toRow(currentPos), Position.toCol(currentPos));
|
||||
|
||||
for (int i=0 ; board.isWalkable(pos.getId(), direction) && i<Const.viewDistance ; ++i) {
|
||||
pos = new Position(pos.getRow(), pos.getCol(), direction);
|
||||
if (board.hasSupply(pos.getId()))
|
||||
return i+1;
|
||||
}
|
||||
return Const.noSupply;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility to get the distance of a possible opponent in some direction
|
||||
* @param currentPos The current position of the player
|
||||
* @param direction The direction to check
|
||||
* @return The distance or Const.noOpponent
|
||||
*/
|
||||
int opponetInDirection(int currentPos, int direction) {
|
||||
Position pos = new Position(Position.toRow(currentPos), Position.toCol(currentPos));
|
||||
int [] opp = board.getOpponentMove(playerId);
|
||||
|
||||
for (int i=0 ; board.isWalkable(pos.getId(), direction) && i<Const.viewDistance ; ++i) {
|
||||
pos = new Position(pos.getRow(), pos.getCol(), direction);
|
||||
if (opp[MOVE_TILE_ID] == pos.getId())
|
||||
return i+1;
|
||||
}
|
||||
return Const.noOpponent;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the main move evaluation function.
|
||||
* @param currentPos The current position of the player (before the move to evaluate)
|
||||
* @param direction The direction (a.k.a. the move) to evaluate
|
||||
* @return A signed real number. The higher the output, the higher the evaluation.
|
||||
*/
|
||||
double evaluate (int currentPos, int direction) {
|
||||
int opDist = opponetInDirection (currentPos, direction);
|
||||
int supDist = supplyInDirection(currentPos, direction);
|
||||
|
||||
// saturate
|
||||
opDist = (opDist == Const.noOpponent) ? 0: opDist;
|
||||
supDist = (supDist == Const.noSupply) ? 0 : supDist;
|
||||
return ((supDist != 0)? (1.0/supDist * Const.supplyFactor) : 0)
|
||||
- ((opDist != 0) ? (1.0/opDist * Const.opponentFactor) : 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Selects the best possible move to return
|
||||
* @param currentPos Player's current position to the board
|
||||
* @return The move array
|
||||
*
|
||||
* @note
|
||||
* This function always return a new move.
|
||||
*/
|
||||
int[] getNextMove(int currentPos) {
|
||||
Range dirs = new Range(DirRange.Begin, DirRange.End, DirRange.Step);
|
||||
int N = dirs.size();
|
||||
double[] eval = new double[N];
|
||||
int [] eval_dir = new int[N];
|
||||
|
||||
for (int i =0, dir = dirs.get() ; dir != Const.EOR ; dir = dirs.get(), ++i) {
|
||||
if (board.isWalkable(currentPos, dir))
|
||||
eval[i] = evaluate(currentPos, dir);
|
||||
else
|
||||
eval[i] = Double.NEGATIVE_INFINITY;
|
||||
eval_dir[i] = dir;
|
||||
}
|
||||
int dir;
|
||||
if (isUnevaluated(eval, N)) {
|
||||
ShuffledRange r = new ShuffledRange(DirRange.Begin, DirRange.End, DirRange.Step);
|
||||
do
|
||||
dir = r.get();
|
||||
while (!board.isWalkable(currentPos, dir));
|
||||
}
|
||||
else {
|
||||
dir = directionOfMax (eval, eval_dir, N);
|
||||
}
|
||||
Position new_pos = new Position( Position.toRow(currentPos), Position.toCol(currentPos), dir );
|
||||
int [] ret = {new_pos.getId(), new_pos.getRow(), new_pos.getCol(), dir};
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* HeuristicPlayer's move.
|
||||
*
|
||||
* A player of this kind cheats. He does not throw a dice to get a direction. In contrary he
|
||||
* calculates his next move very carefully.
|
||||
* If the player is a champion then he also picks up a possible supply from the tile.
|
||||
*
|
||||
* @param id The id of the starting tile.
|
||||
* @return An array containing player's final position and possible supply of that position.
|
||||
* The array format is:
|
||||
* <ul>
|
||||
* <li> int[0]: The tileId of the final player's position.
|
||||
* <li> int[1]: The row of the final player's position.
|
||||
* <li> int[2]: The column of the final player's position.
|
||||
* <li> int[3]: The dice/direction of the move.
|
||||
* </ul>
|
||||
*/
|
||||
@Override
|
||||
int[] move(int id) {
|
||||
// Initialize return array with the current data
|
||||
int[] ret = getNextMove(id);
|
||||
y = Position.toRow(ret[MOVE_TILE_ID]);
|
||||
x = Position.toCol(ret[MOVE_TILE_ID]);
|
||||
|
||||
int supplyFlag =0, moveFlag =1;
|
||||
// In case of a champion player, try also to pick a supply
|
||||
if (champion && (board.tryPickSupply(ret[MOVE_TILE_ID]) != Const.noSupply)) {
|
||||
++score; // keep score
|
||||
++supplyFlag;
|
||||
}
|
||||
++dirCounter[ret[MOVE_DICE]]; // update direction counters
|
||||
board.updateMove(ret, playerId);
|
||||
|
||||
// Update supply and opponent distance
|
||||
int smin =DirRange.End, omin =DirRange.End;
|
||||
for (int d = DirRange.Begin ; d<DirRange.End ; d += DirRange.Step) {
|
||||
int s = supplyInDirection (ret[MOVE_TILE_ID], d);
|
||||
int o = opponetInDirection(ret[MOVE_TILE_ID], d);
|
||||
if (s >= 0 && s < smin) smin = s;
|
||||
if (o >= 0 && o < omin) omin = o;
|
||||
}
|
||||
// update path
|
||||
Integer[] p = {
|
||||
ret[MOVE_TILE_ID], ret[MOVE_DICE], moveFlag, supplyFlag,
|
||||
dirCounter[Direction.UP], dirCounter[Direction.RIGHT], dirCounter[Direction.DOWN], dirCounter[Direction.LEFT],
|
||||
(smin != DirRange.End)? smin:Const.noSupply, (omin != DirRange.End)? omin:Const.noOpponent
|
||||
};
|
||||
path.add(p);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints round information for the player
|
||||
*/
|
||||
void statistics() {
|
||||
if (!path.isEmpty()) {
|
||||
Integer[] last = path.get(path.size()-1);
|
||||
String who = String.format("%12s", name);
|
||||
System.out.print(who + ": score[" + score + "]" + ", dice =" + last[1] + ", tileId =" + last[0] + " (" + Position.toRow(last[0]) + ", " + Position.toCol(last[0]) + ")");
|
||||
if (last[2] == 0)
|
||||
System.out.println(" *Can not move.");
|
||||
else if (last[3] != 0)
|
||||
System.out.println(" *Found a supply.");
|
||||
else
|
||||
System.out.println("");
|
||||
// extra prints for heuristic
|
||||
if (last[8] != Const.noSupply) System.out.println(" supply =" + last[8]);
|
||||
else System.out.println(" supply = blind");
|
||||
if (last[9] != Const.noOpponent) System.out.println(" opponent =" + last[9]);
|
||||
else System.out.println(" opponent = blind");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints final statistics for the player
|
||||
*/
|
||||
void final_statistics () {
|
||||
String who = String.format("%12s", name);
|
||||
System.out.println();
|
||||
System.out.println(who + ": score[" + score + "]");
|
||||
System.out.println(" Moves up: " + dirCounter[Direction.UP]);
|
||||
System.out.println(" Moves right: " + dirCounter[Direction.RIGHT]);
|
||||
System.out.println(" Moves down: " + dirCounter[Direction.DOWN]);
|
||||
System.out.println(" Moves left: " + dirCounter[Direction.LEFT]);
|
||||
}
|
||||
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* A small utility to extract the direction of maximum evaluation result.
|
||||
*
|
||||
* We search into the \c eval results and find the index of the maximum evaluation.
|
||||
* Then we return the direction of \c eval_dir matrix at the same index we found.
|
||||
*
|
||||
* @param eval Array with evaluation results for each direction
|
||||
* @param eval_dir Array with the matching direction to \c eval array
|
||||
* @param N The size of both arrays
|
||||
* @return The direction of maximum evaluation. If \c eval is empty returns the first item \c eval[0]
|
||||
* @note
|
||||
* This function should not be called if there is at least one evaluation result in \c eval
|
||||
*/
|
||||
private int directionOfMax (double[] eval, int[] eval_dir, int N) {
|
||||
double M = Double.NEGATIVE_INFINITY;
|
||||
int M_idx = 0;
|
||||
for (int i =0; i < N ; ++i) {
|
||||
if (eval[i] > M) {
|
||||
M = eval[i];
|
||||
M_idx = i;
|
||||
}
|
||||
}
|
||||
return eval_dir[M_idx];
|
||||
}
|
||||
|
||||
/**
|
||||
* A small utility to check if there is at least one evaluation result in the \c eval array
|
||||
* @param eval The array to check
|
||||
* @param N The size of the array
|
||||
* @return True if there is none, false otherwise
|
||||
*/
|
||||
private boolean isUnevaluated (double[] eval, int N) {
|
||||
for (int i =0 ; i<N ; ++i)
|
||||
if (eval[i] != 0 && eval[i] != Double.NEGATIVE_INFINITY)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @name Class data */
|
||||
/** @{ */
|
||||
|
||||
|
||||
/** @} */
|
||||
}
|
||||
@@ -0,0 +1,388 @@
|
||||
/**
|
||||
* @file MinMaxPlayer.java
|
||||
*
|
||||
* @author
|
||||
* Anastasia Foti AEM:8959
|
||||
* <anastaskf@ece.auth.gr>
|
||||
*
|
||||
* @author
|
||||
* Christos Choutouridis AEM:8997
|
||||
* <cchoutou@ece.auth.gr>
|
||||
*/
|
||||
|
||||
package host.labyrinth;
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* This class represents the game's minimax player.
|
||||
*/
|
||||
class MinMaxPlayer extends Player {
|
||||
|
||||
/** @name Constructors */
|
||||
/** @{ */
|
||||
|
||||
/**
|
||||
* Create a new player and put him at the row-column coordinates
|
||||
* @param name The name of the player
|
||||
* @param champion Flag to indicate if a player is a `champion`
|
||||
* @param board Reference to the board of the game
|
||||
* @param row The row coordinate of initial player position
|
||||
* @param column The column coordinate of initial player's position
|
||||
*/
|
||||
public MinMaxPlayer(String name, boolean champion, Board board, int row, int column) throws Exception {
|
||||
super(name, champion, board, row, column);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new player and put him at the row-column coordinates
|
||||
* @param name The name of the player
|
||||
* @param champion Flag to indicate if a player is a `champion`
|
||||
* @param board Reference to the board of the game
|
||||
* @param tileId The tileId coordinate of player's initial position
|
||||
*/
|
||||
public MinMaxPlayer(String name, boolean champion, Board board, int tileId) throws Exception {
|
||||
super(name, champion, board, tileId);
|
||||
}
|
||||
/** @} */
|
||||
/** @name Board's main application interface */
|
||||
/** @{ */
|
||||
|
||||
/**
|
||||
* Utility to get the distance of a possible supply in some direction
|
||||
* @param currentPos The current position of the player
|
||||
* @param direction The direction to check
|
||||
* @param board Reference to the Board object to use
|
||||
*
|
||||
* @return The distance or Const.noView
|
||||
*/
|
||||
int supplyInDirection(int currentPos, int direction, Board board) {
|
||||
Position pos = new Position(Position.toRow(currentPos), Position.toCol(currentPos));
|
||||
|
||||
for (int i=0 ; i<=Const.viewDistance ; ++i) {
|
||||
if (board.hasSupply(pos.getId()))
|
||||
return i;
|
||||
if (board.isWalkable(pos.getId(), direction))
|
||||
pos = new Position(pos.getRow(), pos.getCol(), direction);
|
||||
else
|
||||
break;
|
||||
}
|
||||
return Const.noView;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility to get the distance of a possible opponent in some direction
|
||||
* @param currentPos The current position of the player
|
||||
* @param direction The direction to check
|
||||
* @param board Reference to the Board object to use
|
||||
*
|
||||
* @return The distance or Const.noView
|
||||
*/
|
||||
int opponetInDirection(int currentPos, int direction, Board board) {
|
||||
Position pos = new Position(Position.toRow(currentPos), Position.toCol(currentPos));
|
||||
int[] opp = board.getOpponentMove(playerId);
|
||||
|
||||
for (int i=0 ; i<=Const.viewDistance ; ++i) {
|
||||
if (opp[MOVE_TILE_ID] == pos.getId())
|
||||
return i;
|
||||
if (board.isWalkable(pos.getId(), direction))
|
||||
pos = new Position(pos.getRow(), pos.getCol(), direction);
|
||||
else
|
||||
break;
|
||||
}
|
||||
return Const.noView;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the main move evaluation function.
|
||||
*
|
||||
* @param currentPos The current position of the player (before the move to evaluate)
|
||||
* @param direction The direction (a.k.a. the move) to evaluate
|
||||
* @param board Reference to the Board object to use
|
||||
* @return A signed real number. The higher the output, the higher the evaluation.
|
||||
*/
|
||||
double evaluate (int currentPos, int direction, Board board) {
|
||||
Position next = new Position (Position.toRow(currentPos), Position.toCol(currentPos), direction);
|
||||
|
||||
int preOpDist = opponetInDirection (currentPos, direction, board);
|
||||
int preSupDist = supplyInDirection(currentPos, direction, board);
|
||||
int postOpDist = opponetInDirection (next.getId(), direction, board);
|
||||
int postSupDist = supplyInDirection(next.getId(), direction, board);
|
||||
|
||||
return ((preSupDist != Const.noView) ? Const.preMoveFactor *(1.0/(preSupDist+1) * Const.supplyFactor) : 0)
|
||||
- ((preOpDist != Const.noView) ? Const.preMoveFactor *(1.0/(preOpDist+1) * Const.opponentFactor) : 0)
|
||||
+ ((postSupDist != Const.noView)? Const.postMoveFactor*(1.0/(preSupDist+1) * Const.supplyFactor) : 0)
|
||||
- ((postOpDist != Const.noView) ? Const.postMoveFactor*(1.0/(preOpDist+1) * Const.opponentFactor) : 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the minimax algorithm and return a reference to selected move
|
||||
* @param node The root node to start
|
||||
* @return Reference to the selected move
|
||||
*/
|
||||
Node chooseMinMaxMove(Node node) {
|
||||
node.setNodeEvaluation(maxValue(node));
|
||||
return node.getPath();
|
||||
}
|
||||
|
||||
/**
|
||||
* Selects the best possible move to return
|
||||
* @param currentPos Player's current position to the board
|
||||
* @return The move array
|
||||
*
|
||||
* @note
|
||||
* This function always return a new move.
|
||||
*/
|
||||
int[] getNextMove(int currentPos) {
|
||||
Node root = new Node (board);
|
||||
int [] opp = board.getOpponentMove(playerId);
|
||||
|
||||
createMySubtree(currentPos, opp[MOVE_TILE_ID], root, root.getNodeDepth()+1);
|
||||
|
||||
return chooseMinMaxMove(root).getNodeMove();
|
||||
}
|
||||
|
||||
/**
|
||||
* MinMaxPlayer's move.
|
||||
*
|
||||
* A player of this kind cheats. He does not throw a dice to get a direction. In contrary he
|
||||
* calculates his next move very carefully.
|
||||
* If the player is a champion then he also picks up a possible supply from the tile.
|
||||
*
|
||||
* @param id The id of the starting tile.
|
||||
* @return An array containing player's final position and possible supply of that position.
|
||||
* The array format is:
|
||||
* <ul>
|
||||
* <li> int[0]: The tileId of the final player's position.
|
||||
* <li> int[1]: The row of the final player's position.
|
||||
* <li> int[2]: The column of the final player's position.
|
||||
* <li> int[3]: The dice/direction of the move.
|
||||
* </ul>
|
||||
*/
|
||||
@Override
|
||||
int[] move(int id) {
|
||||
// Initialize return array with the current data
|
||||
int[] ret = getNextMove(id);
|
||||
y = Position.toRow(ret[MOVE_TILE_ID]);
|
||||
x = Position.toCol(ret[MOVE_TILE_ID]);
|
||||
|
||||
int supplyFlag =0, moveFlag =1;
|
||||
// In case of a champion player, try also to pick a supply
|
||||
if (champion && (board.tryPickSupply(ret[MOVE_TILE_ID]) != Const.noSupply)) {
|
||||
++score; // keep score
|
||||
++supplyFlag;
|
||||
}
|
||||
++dirCounter[ret[MOVE_DICE]]; // update direction counters
|
||||
board.updateMove(ret, playerId);
|
||||
|
||||
// Update supply and opponent distance
|
||||
int smin =Const.noView, omin =Const.noView;
|
||||
for (int d = DirRange.Begin ; d<DirRange.End ; d += DirRange.Step) {
|
||||
int s = supplyInDirection (ret[MOVE_TILE_ID], d, board);
|
||||
int o = opponetInDirection(ret[MOVE_TILE_ID], d, board);
|
||||
if (s < smin) smin = s;
|
||||
if (o < omin) omin = o;
|
||||
}
|
||||
// update path
|
||||
Integer[] p = {
|
||||
ret[MOVE_TILE_ID], ret[MOVE_DICE], moveFlag, supplyFlag,
|
||||
dirCounter[Direction.UP], dirCounter[Direction.RIGHT], dirCounter[Direction.DOWN], dirCounter[Direction.LEFT],
|
||||
smin, omin
|
||||
};
|
||||
path.add(p);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints round information for the player
|
||||
*/
|
||||
void statistics() {
|
||||
if (!path.isEmpty()) {
|
||||
Integer[] last = path.get(path.size()-1);
|
||||
String who = String.format("%12s", name);
|
||||
System.out.print(who + ": score[" + score + "]" + ", dice =" + last[1] + ", tileId =" + last[0] + " (" + Position.toRow(last[0]) + ", " + Position.toCol(last[0]) + ")");
|
||||
if (last[2] == 0)
|
||||
System.out.println(" *Can not move.");
|
||||
else if (last[3] != 0)
|
||||
System.out.println(" *Found a supply.");
|
||||
else
|
||||
System.out.println("");
|
||||
// extra prints for minimax player
|
||||
if (last[8] != Const.noView) System.out.println(" supply =" + last[8]);
|
||||
else System.out.println(" supply = blind");
|
||||
if (last[9] != Const.noView) System.out.println(" opponent =" + last[9]);
|
||||
else System.out.println(" opponent = blind");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints final statistics for the player
|
||||
*/
|
||||
void final_statistics () {
|
||||
String who = String.format("%12s", name);
|
||||
System.out.println();
|
||||
System.out.println(who + ": score[" + score + "]");
|
||||
System.out.println(" Moves up: " + dirCounter[Direction.UP]);
|
||||
System.out.println(" Moves right: " + dirCounter[Direction.RIGHT]);
|
||||
System.out.println(" Moves down: " + dirCounter[Direction.DOWN]);
|
||||
System.out.println(" Moves left: " + dirCounter[Direction.LEFT]);
|
||||
}
|
||||
|
||||
/** @} */
|
||||
|
||||
/** @name Minimax algorithm related part */
|
||||
/** @{ */
|
||||
|
||||
/**
|
||||
* Get the previous direction of the player
|
||||
* @param parent Reference to previous nNode
|
||||
* @return The previous direction if exist, else return Direction.NONE
|
||||
*/
|
||||
private int prevDirection(Node parent) {
|
||||
if (parent != null && parent.getParent() != null)
|
||||
return parent.getParent().getNodeMove()[MOVE_DICE];
|
||||
return Direction.NONE;
|
||||
}
|
||||
|
||||
/**
|
||||
* A simulated move in a copy of the bard.
|
||||
*
|
||||
* @param board The board on witch we simulate the move
|
||||
* @param currentPos The current position of the player to the @c board
|
||||
* @param dir The direction of the move
|
||||
* @param champion Flag to indicate if the player is champion or not
|
||||
* @return The move array
|
||||
*/
|
||||
private int[] dryMove (Board board, int currentPos, int dir, boolean champion) {
|
||||
int[] ret = new int[MOVE_DATA_SIZE];
|
||||
Position p = new Position(Position.toRow(currentPos), Position.toCol(currentPos), dir);
|
||||
ret[MOVE_TILE_ID] = p.getId();
|
||||
ret[MOVE_ROW] = p.getRow();
|
||||
ret[MOVE_COLUMN] = p.getCol();
|
||||
ret[MOVE_DICE] = dir;
|
||||
|
||||
board.updateMove(ret, (champion) ? playerId : board.getOpponentId(playerId));
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* One of the 2 recursive functions for creating the minimax tree. This one
|
||||
* creates children for the MinMax player.
|
||||
*
|
||||
* @param parent The parent Node
|
||||
* @param depth The current depth for the children
|
||||
* @param currentPos The tile of MinMax player
|
||||
* @param oppCurrentPos The tile of the opponent
|
||||
*
|
||||
* @note
|
||||
* Even though unnecessary we calculate the evaluation for every node and not only for the leafs.
|
||||
* This follows the exercise instructions. We could also rely on lazy evaluation of "evaluation()"
|
||||
* and use AB pruning but the depth of the tree is not worth the try.
|
||||
*/
|
||||
private void createMySubtree (int currentPos, int oppCurrentPos, Node parent, int depth) {
|
||||
ShuffledRange dirs = new ShuffledRange(DirRange.Begin, DirRange.End, DirRange.Step);
|
||||
int [] nodeMove;
|
||||
|
||||
for (int dir = dirs.get() ; dir != Const.EOR ; dir = dirs.get()) {
|
||||
if ((dir != Direction.opposite(prevDirection(parent)))
|
||||
&& parent.getNodeBoard().isWalkable(currentPos, dir)) {
|
||||
Board nodeBoard = new Board (parent.getNodeBoard()); // clone board
|
||||
double eval = evaluate (currentPos, dir, nodeBoard); // evaluate the move
|
||||
nodeMove = dryMove (nodeBoard, currentPos, dir, true); // simulate the move
|
||||
// make child Node
|
||||
Node child = new Node (parent, depth, nodeMove, nodeBoard, eval);
|
||||
parent.addChild(child); // add child to tree
|
||||
createOppSubtree (nodeMove[MOVE_TILE_ID], oppCurrentPos, child, depth+1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* One of the 2 recursive functions for creating the minimax tree. This one
|
||||
* creates children for the opponent player.
|
||||
*
|
||||
* @param parent The parent Node
|
||||
* @param depth The current depth for the children
|
||||
* @param currentPos The tile of MinMax player
|
||||
* @param oppCurrentPos The tile of the opponent
|
||||
*
|
||||
* @note
|
||||
* Even though unnecessary we calculate the evaluation for every node and not only for the leafs.
|
||||
* This follows the exercise instructions. We could also rely on lazy evaluation of "evaluation()"
|
||||
* and use AB pruning but the depth of the tree is not worth the try.
|
||||
*/
|
||||
private void createOppSubtree (int currentPos, int oppCurrentPos, Node parent, int depth) {
|
||||
ShuffledRange dirs = new ShuffledRange(DirRange.Begin, DirRange.End, DirRange.Step);
|
||||
int [] nodeMove;
|
||||
|
||||
for (int dir = dirs.get() ; dir != Const.EOR ; dir = dirs.get()) {
|
||||
if ((dir != Direction.opposite(prevDirection(parent)))
|
||||
&& parent.getNodeBoard().isWalkable(oppCurrentPos, dir)) {
|
||||
Board nodeBoard = new Board(parent.getNodeBoard()); // clone board
|
||||
nodeMove = dryMove (nodeBoard, oppCurrentPos, dir, false); // simulate move
|
||||
Position init = new Position( // evaluate from "My" perspective the move
|
||||
parent.getNodeMove()[MOVE_ROW],
|
||||
parent.getNodeMove()[MOVE_COLUMN],
|
||||
Direction.opposite(parent.getNodeMove()[MOVE_DICE])
|
||||
);
|
||||
double eval = evaluate(init.getId(), parent.getNodeMove()[MOVE_DICE], nodeBoard);
|
||||
// make child Node
|
||||
Node child = new Node (parent, depth, nodeMove, nodeBoard, eval);
|
||||
parent.addChild(child); // add child to tree
|
||||
if (depth < Const.minimaxTreeDepth) {
|
||||
createMySubtree (currentPos, nodeMove[MOVE_TILE_ID], child, depth+1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The Minimax recursive function for the maximizing part.
|
||||
*
|
||||
* @param node The current Node
|
||||
* @return The selected Node
|
||||
*/
|
||||
private double maxValue (Node node) {
|
||||
if (node.getChildren() == null) {
|
||||
//node.setPath(node);
|
||||
return node.getNodeEvaluation();
|
||||
}
|
||||
else {
|
||||
double M = Double.NEGATIVE_INFINITY;
|
||||
for (Node n : node.getChildren()) {
|
||||
n.setNodeEvaluation(minValue(n)); // evaluation propagation
|
||||
if (M < n.getNodeEvaluation()) {
|
||||
M = n.getNodeEvaluation();
|
||||
node.setPath(n); // path propagation
|
||||
}
|
||||
}
|
||||
return M;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The Minimax recursive function for the minimizing part.
|
||||
*
|
||||
* @param node The current Node
|
||||
* @return The selected Node
|
||||
*/
|
||||
private double minValue (Node node) {
|
||||
if (node.getChildren() == null) {
|
||||
//node.setPath(node);
|
||||
return node.getNodeEvaluation();
|
||||
}
|
||||
else {
|
||||
double m = Double.POSITIVE_INFINITY;
|
||||
for (Node n : node.getChildren()) {
|
||||
n.setNodeEvaluation(maxValue(n)); // evaluation propagation
|
||||
if (m > n.getNodeEvaluation()) {
|
||||
m = n.getNodeEvaluation();
|
||||
node.setPath(n); // path propagation
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/** @} */
|
||||
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* @file Node.java
|
||||
*
|
||||
* @author
|
||||
* Anastasia Foti AEM:8959
|
||||
* <anastaskf@ece.auth.gr>
|
||||
*
|
||||
* @author
|
||||
* Christos Choutouridis AEM:8997
|
||||
* <cchoutou@ece.auth.gr>
|
||||
*/
|
||||
|
||||
package host.labyrinth;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Node object for minimax tree.
|
||||
*/
|
||||
class Node {
|
||||
|
||||
/** @name Constructors */
|
||||
/** @{ */
|
||||
/** Null initialize constructor */
|
||||
Node () { }
|
||||
/** The main constructor for the Node */
|
||||
Node (Node parent, int nodeDepth, int [] nodeMove, Board nodeBoard, double nodeEvaluation) {
|
||||
this.parent = parent;
|
||||
this.children = null;
|
||||
this.nodeDepth = nodeDepth;
|
||||
this.nodeMove = nodeMove;
|
||||
this.nodeBoard = nodeBoard;
|
||||
this.nodeEvaluation = nodeEvaluation;
|
||||
this.path = null;
|
||||
}
|
||||
/** A special constructor for creating a root Node */
|
||||
Node (Board nodeBoard) {
|
||||
this.parent = null;
|
||||
this.children = null;
|
||||
this.nodeDepth = 0;
|
||||
this.nodeMove = new int [4];
|
||||
this.nodeBoard = nodeBoard;
|
||||
this.nodeEvaluation = 0;
|
||||
this.path = null;
|
||||
}
|
||||
/**@} */
|
||||
|
||||
/** @name Get/Set interface */
|
||||
/** @{ */
|
||||
/** Get parent */
|
||||
Node getParent() { return parent; }
|
||||
/** get children */
|
||||
ArrayList<Node>
|
||||
getChildren() { return children; }
|
||||
/** get nodeDepth */
|
||||
int getNodeDepth() { return nodeDepth; }
|
||||
/** get nodeMove */
|
||||
int[] getNodeMove() { return nodeMove; }
|
||||
/** get nodeBoard */
|
||||
Board getNodeBoard() { return nodeBoard; }
|
||||
/** get nodeEvluation */
|
||||
double getNodeEvaluation (){ return nodeEvaluation; }
|
||||
/** get path */
|
||||
Node getPath() { return path; }
|
||||
|
||||
/** set parent */
|
||||
void setParent(Node parent) { this.parent = parent; }
|
||||
/** set children */
|
||||
void setChildren(ArrayList<Node> children) {
|
||||
this.children = children;
|
||||
}
|
||||
/** set nodeDepth */
|
||||
void setNodeDepth(int nodeDepth) {
|
||||
this.nodeDepth = nodeDepth;
|
||||
}
|
||||
/** set nodeMove */
|
||||
void setNodeMove(int[] nodeMove) {
|
||||
this.nodeMove = nodeMove;
|
||||
}
|
||||
/** set nodeBoard */
|
||||
void setNodeBoard(Board nodeBoard) {
|
||||
this.nodeBoard = nodeBoard;
|
||||
}
|
||||
/** set nodeEvaluation */
|
||||
void setNodeEvaluation(double nodeEvaluation) {
|
||||
this.nodeEvaluation = nodeEvaluation;
|
||||
}
|
||||
/** set path */
|
||||
void setPath (Node path) {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
/**@}*/
|
||||
|
||||
/** @name Public API */
|
||||
/** @{ */
|
||||
/**
|
||||
* Add a child to the tree
|
||||
* @param child The child to add
|
||||
* @return the status of the operation
|
||||
*/
|
||||
boolean addChild (Node child) {
|
||||
if (children == null)
|
||||
children = new ArrayList<>();
|
||||
return children.add(child);
|
||||
}
|
||||
/**@}*/
|
||||
|
||||
/** @name Data members */
|
||||
/** @{ */
|
||||
private Node parent; /**< Back reference to parent Node */
|
||||
private ArrayList<Node> children; /**< Fwd reference to leaf Nodes */
|
||||
private int nodeDepth; /**< The Node's depth */
|
||||
private int[] nodeMove; /**< The Node's move data [tile, initTile, points, roll]*/
|
||||
private Board nodeBoard; /**< Reference to Board's copy of the current node*/
|
||||
private double nodeEvaluation; /**< The Node's evaluation result */
|
||||
private Node path; /**< The minimax evaluation path */
|
||||
/**@}*/
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
/**
|
||||
* @file Player.java
|
||||
*
|
||||
* @author
|
||||
* Anastasia Foti AEM:8959
|
||||
* <anastaskf@ece.auth.gr>
|
||||
*
|
||||
* @author
|
||||
* Christos Choutouridis AEM:8997
|
||||
* <cchoutou@ece.auth.gr>
|
||||
*/
|
||||
|
||||
package host.labyrinth;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* This class represents the game's player
|
||||
*/
|
||||
class Player {
|
||||
/** Helper variables to keep track of the move() return values @see move() */
|
||||
static final int MOVE_DATA_SIZE = 4; /**< The move return data array size */
|
||||
static final int MOVE_TILE_ID = 0; /**< Index of the tileId information of the move */
|
||||
static final int MOVE_ROW = 1; /**< The index of row information */
|
||||
static final int MOVE_COLUMN = 2; /**< The index of column information */
|
||||
static final int MOVE_DICE = 3; /**< The index of dice information */
|
||||
|
||||
/** @name Constructors */
|
||||
/** @{ */
|
||||
|
||||
/**
|
||||
* Create a new player and put him at the row-column coordinates
|
||||
* @param name The name of the player
|
||||
* @param champion Flag to indicate if a player is a `champion`
|
||||
* @param board Reference to the board of the game
|
||||
* @param row The row coordinate of initial player position
|
||||
* @param column The column coordinate of initial player's position
|
||||
*/
|
||||
Player(String name, boolean champion, Board board, int row, int column) throws Exception {
|
||||
this.playerId = board.generatePlayerId();
|
||||
this.name = name;
|
||||
this.board = board;
|
||||
this.score = 0;
|
||||
this.x = column;
|
||||
this.y = row;
|
||||
this.champion = champion;
|
||||
this.dirCounter= new int[DirRange.End]; // yes we spoil some memory. Java is the worst.
|
||||
this.path = new ArrayList<Integer[]>();
|
||||
int[] m = {
|
||||
Position.toID(row, column), row, column, Const.noSupply
|
||||
};
|
||||
board.updateMove(m, playerId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new player and put him at the row-column coordinates
|
||||
* @param name The name of the player
|
||||
* @param champion Flag to indicate if a player is a `champion`
|
||||
* @param board Reference to the board of the game
|
||||
* @param tileId The tileId coordinate of player's initial position
|
||||
*/
|
||||
Player(String name, boolean champion, Board board, int tileId) throws Exception {
|
||||
this.playerId = board.generatePlayerId();
|
||||
this.name = name;
|
||||
this.board = board;
|
||||
this.score = 0;
|
||||
this.x = Position.toCol(tileId);
|
||||
this.y = Position.toRow(tileId);
|
||||
this.champion = champion;
|
||||
this.dirCounter= new int[DirRange.End]; // yes we spoil some memory. Java is the worst.
|
||||
this.path = new ArrayList<Integer[]>();
|
||||
int[] m = {
|
||||
tileId, Position.toRow(tileId), Position.toCol(tileId), Const.noSupply
|
||||
};
|
||||
board.updateMove(m, playerId);
|
||||
}
|
||||
/** @} */
|
||||
|
||||
/** @name Player main application interface */
|
||||
/** @{ */
|
||||
|
||||
/**
|
||||
* Player's move.
|
||||
*
|
||||
* A player first throws a dice to get a random direction. Then checks if the direction
|
||||
* is walkable. If it is, then goes to that tile and update player's data.
|
||||
* If the player is a champion then he also picks up a possible supply from the tile.
|
||||
*
|
||||
* @param id The id of the starting tile.
|
||||
* @return An array containing player's final position and possible supply of that position.
|
||||
* The array format is:
|
||||
* <ul>
|
||||
* <li> int[0]: The tileId of the final player's position.
|
||||
* <li> int[1]: The row of the final player's position.
|
||||
* <li> int[2]: The column of the final player's position.
|
||||
* <li> int[3]: The dice/direction of the move.
|
||||
* </ul>
|
||||
*/
|
||||
int[] move(int id) {
|
||||
// Initialize return array with the current data
|
||||
int[] ret = new int[MOVE_DATA_SIZE];
|
||||
ret[MOVE_TILE_ID] = id;
|
||||
ret[MOVE_ROW] = Position.toRow(id);
|
||||
ret[MOVE_COLUMN] = Position.toCol(id);
|
||||
ret[MOVE_DICE] = Direction.NONE;
|
||||
int supplyFlag =0, moveFlag =0;
|
||||
|
||||
int diceDirection;
|
||||
do
|
||||
diceDirection = board.dice(); // throw the dice
|
||||
while (!board.isWalkable(id, diceDirection));
|
||||
moveFlag =1; // mark the successful move
|
||||
// Get next tile
|
||||
Position next = new Position(Position.toRow(id), Position.toCol(id), diceDirection);
|
||||
ret[MOVE_TILE_ID] = next.getId(); // Update move's return data
|
||||
ret[MOVE_ROW] = y = next.getRow();
|
||||
ret[MOVE_COLUMN] = x = next.getCol();
|
||||
ret[MOVE_DICE] = diceDirection;
|
||||
// In case of a champion player, try also to pick a supply
|
||||
if (champion && (board.tryPickSupply(next.getId()) != Const.noSupply)) {
|
||||
supplyFlag =1; // mark the successful supply pickup
|
||||
++score; // keep score
|
||||
}
|
||||
++dirCounter[diceDirection]; // update direction counters
|
||||
board.updateMove(ret, playerId);
|
||||
// update path
|
||||
Integer[] p = {
|
||||
ret[MOVE_TILE_ID], diceDirection, moveFlag, supplyFlag,
|
||||
dirCounter[Direction.UP], dirCounter[Direction.RIGHT], dirCounter[Direction.DOWN], dirCounter[Direction.LEFT],
|
||||
Const.noSupply, Const.noOpponent
|
||||
};
|
||||
path.add(p);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints round information for the player
|
||||
*/
|
||||
void statistics() {
|
||||
if (!path.isEmpty()) {
|
||||
Integer[] last = path.get(path.size()-1);
|
||||
String who = String.format("%12s", name);
|
||||
System.out.print(who + ": score[" + score + "]" + ", dice =" + last[1] + ", tileId =" + last[0] + " (" + Position.toRow(last[0]) + ", " + Position.toCol(last[0]) + ")");
|
||||
if (last[2] == 0)
|
||||
System.out.println(" *Can not move.");
|
||||
else if (last[3] != 0)
|
||||
System.out.println(" *Found a supply.");
|
||||
else
|
||||
System.out.println("");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints final statistics for the player
|
||||
* @note
|
||||
* We add this final_statistics() wrapper in order to provide polymorphism to
|
||||
* Player class hierarchy and to be sure that we are not braking the
|
||||
* Liskov substitution principle
|
||||
* @see https://en.wikipedia.org/wiki/Liskov_substitution_principle
|
||||
*/
|
||||
void final_statistics () { }
|
||||
|
||||
/** Utility to access player's tileID */
|
||||
int playerTileId() { return Position.toID(y, x); }
|
||||
/** Utility to access player's row position (row coordinate) */
|
||||
int playerRow() { return y; }
|
||||
/** Utility to access player's column position (column coordinate) */
|
||||
int playerCol() { return x; }
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @name Accessor/Mutator interface
|
||||
* @note
|
||||
* Please consider not to use mutator interface. Its the abstraction killer :(
|
||||
* We have added a bit of logic however, in order to make it a bit more safe.
|
||||
*/
|
||||
/** @{ */
|
||||
int getPlayerId () { return playerId; }
|
||||
String getName() { return name; }
|
||||
Board getBoard () { return board; }
|
||||
int getScore () { return score; }
|
||||
int getX() { return x; }
|
||||
int getY() { return y; }
|
||||
boolean getChampion(){ return champion; }
|
||||
int[] getDirCounter(){ return dirCounter; }
|
||||
ArrayList<Integer[]> getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
|
||||
void setPlayerId(int id) { playerId = id; }
|
||||
void setName(String name) { this.name = name; }
|
||||
void setBoard (Board board){ this.board = board; }
|
||||
void setScore(int score) { this.score = score; }
|
||||
void setX(int x) {
|
||||
assert (x >= 0 && x< Session.boardSize) : "X(column) coordinate must be in the range [0, Session.boardSize)";
|
||||
this.x = x;
|
||||
}
|
||||
void setY(int y) {
|
||||
assert (y >= 0 && y< Session.boardSize) : "Y(row) coordinate must be in the range [0, Session.boardSize)";
|
||||
this.y = y;
|
||||
}
|
||||
void setChampion (boolean champion) {
|
||||
this.champion = champion;
|
||||
}
|
||||
void setDirCounter(int[] dirCounter) {
|
||||
this.dirCounter = dirCounter;
|
||||
}
|
||||
|
||||
void setPath(ArrayList<Integer[]> path) {
|
||||
this.path = path;
|
||||
}
|
||||
/** @} */
|
||||
|
||||
/** @name Class data */
|
||||
/** @{ */
|
||||
protected int playerId; /**< The unique identifier of the player */
|
||||
protected String name; /**< The name of the player */
|
||||
protected Board board; /**< Reference to the session's boards */
|
||||
protected int score; /**< The current score of the player */
|
||||
protected int x; /**< The column coordinate of the player on the board */
|
||||
protected int y; /**< The row coordinate of the player on the board */
|
||||
protected boolean champion; /**< Champion indicate a player who plays against the Minotaur */
|
||||
protected int dirCounter[];
|
||||
protected ArrayList<Integer[]> path;
|
||||
/**<
|
||||
* our history. The integer[] format is:
|
||||
* <ul>
|
||||
* <li> Integer[0]: tileId - The tile id we choose for the move
|
||||
* <li> Integer[1]: dice - The dice (a.k.a direction) of move
|
||||
* <li> Integer[2]: moveStatus - True if it was successful (we can move in that direction)
|
||||
* <li> Integer[3]: tookSupply - True if we took supply
|
||||
* <li> Integer[4]: upCounter - Accumulator to count all the up moves
|
||||
* <li> Integer[5]: righrCounter - Accumulator to count all the right moves
|
||||
* <li> Integer[6]: downCounter - Accumulator to count all the down moves
|
||||
* <li> Integer[7]: leftCounter - Accumulator to count all the left moves
|
||||
* <li> Integer[8]: SupDistance - The distance of the nearest supply (only for heuristic players)
|
||||
* <li> Integer[9]: OppDistance - The distance of the nearest opponent (only for heuristic players)
|
||||
* </ul>
|
||||
* }
|
||||
*/
|
||||
/** @} */
|
||||
}
|
||||
@@ -1,11 +1,16 @@
|
||||
/**
|
||||
* @file Supply.java
|
||||
*
|
||||
* @author Christos Choutouridis AEM:8997
|
||||
* @email cchoutou@ece.auth.gr
|
||||
* @author
|
||||
* Anastasia Foti AEM:8959
|
||||
* <anastaskf@ece.auth.gr>
|
||||
*
|
||||
* @author
|
||||
* Christos Choutouridis AEM:8997
|
||||
* <cchoutou@ece.auth.gr>
|
||||
*/
|
||||
|
||||
package net.hoo2.auth.labyrinth;
|
||||
package host.labyrinth;
|
||||
|
||||
/**
|
||||
* @brief
|
||||
@@ -76,7 +81,7 @@ class Supply {
|
||||
int supplyId () { return supplyId; }
|
||||
/**
|
||||
* Set the supplyId
|
||||
* @param sId The Id to set
|
||||
* @param sID The Id to set
|
||||
* @return The supplyId
|
||||
* @note This function also returns the supplyId to help in chained expressions.
|
||||
*/
|
||||
@@ -1,11 +1,16 @@
|
||||
/**
|
||||
* @file Tile.java
|
||||
*
|
||||
* @author Christos Choutouridis AEM:8997
|
||||
* @email cchoutou@ece.auth.gr
|
||||
* @author
|
||||
* Anastasia Foti AEM:8959
|
||||
* <anastaskf@ece.auth.gr>
|
||||
*
|
||||
* @author
|
||||
* Christos Choutouridis AEM:8997
|
||||
* <cchoutou@ece.auth.gr>
|
||||
*/
|
||||
|
||||
package net.hoo2.auth.labyrinth;
|
||||
package host.labyrinth;
|
||||
|
||||
/**
|
||||
* @brief
|
||||
@@ -132,7 +137,7 @@ class Tile {
|
||||
|
||||
/**
|
||||
* Sets the tile's wall in the requested direction.
|
||||
* @param up The direction for the wall.
|
||||
* @param direction The direction for the wall.
|
||||
*/
|
||||
void setWall (int direction) {
|
||||
switch (direction) {
|
||||
@@ -145,7 +150,7 @@ class Tile {
|
||||
|
||||
/**
|
||||
* Clears the tile's wall in the requested direction.
|
||||
* @param up The direction for the wall
|
||||
* @param direction The direction for the wall
|
||||
*/
|
||||
void clearWall (int direction) {
|
||||
switch (direction) {
|
||||
@@ -1,133 +0,0 @@
|
||||
package net.hoo2.auth.labyrinth;
|
||||
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* This class represents the game's player
|
||||
*/
|
||||
class Player {
|
||||
/** @name Constructors */
|
||||
/** @{ */
|
||||
|
||||
/**
|
||||
* Create a new player and put him at the row-column coordinates
|
||||
* @param id The id of the player
|
||||
* @param name The name of the player
|
||||
* @param board Reference to the board of the game
|
||||
*/
|
||||
Player(int id, String name, boolean champion, Board board, int row, int column) {
|
||||
this.playerId = id;
|
||||
this.name = name;
|
||||
this.board = board;
|
||||
this.score = 0;
|
||||
this.x = column;
|
||||
this.y = row;
|
||||
this.champion = champion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new player and put him at the row-column coordinates
|
||||
* @param id The id of the player
|
||||
* @param name The name of the player
|
||||
* @param board Reference to the board of the game
|
||||
*/
|
||||
Player(int id, String name, boolean champion, Board board, int tileId) {
|
||||
this.playerId = id;
|
||||
this.name = name;
|
||||
this.board = board;
|
||||
this.score = 0;
|
||||
this.x = Position.toCol(tileId);
|
||||
this.y = Position.toRow(tileId);
|
||||
this.champion = champion;
|
||||
}
|
||||
/** @} */
|
||||
|
||||
/** @name Player main application interface */
|
||||
/** @{ */
|
||||
|
||||
/**
|
||||
* Player's move.
|
||||
*
|
||||
* A player first throws a dice to get a random direction. Then checks if the direction
|
||||
* is walkable. If it is, then goes to that tile and update player's data.
|
||||
* If the player is a champion then he also picks up a possible supply from the tile.
|
||||
*
|
||||
* @param id The id of the starting tile.
|
||||
* @return An array containing player's final position and possible supply of that position.
|
||||
* The array format is:
|
||||
* <ul>
|
||||
* <li> int[0]: The tileId of the final player's position.
|
||||
* <li> int[1]: The row of the final player's position.
|
||||
* <li> int[2]: The column of the final player's position.
|
||||
* <li> int[1]: The supplyId in case player picked one (Const.noSupply otherwise).
|
||||
* </ul>
|
||||
*/
|
||||
int[] move(int id) {
|
||||
// Initialize return array with the current data
|
||||
int[] ret = {id, Position.toRow(id), Position.toCol(id), Const.noSupply};
|
||||
|
||||
int diceDirection = board.dice(); // throw the dice
|
||||
if (board.isWalkable(id, diceDirection)) { // The result is walkable
|
||||
// Get next tile
|
||||
Position next = new Position(Position.toRow(id), Position.toCol(id), diceDirection);
|
||||
ret[0] = next.getId(); // Update player's and return data
|
||||
ret[1] = y = next.getRow();
|
||||
ret[2] = x = next.getCol();
|
||||
// In case of a champion player, try also to pick a supply
|
||||
if (champion && (ret[3] = board.tryPickSupply(next.getId())) != Const.noSupply) {
|
||||
++score; // keep score
|
||||
System.out.println(name + ":\t*Found a supply. [score: " + score + "]");
|
||||
}
|
||||
}
|
||||
else
|
||||
System.out.println(name + ":\t*Can not move.");
|
||||
return ret;
|
||||
}
|
||||
|
||||
/** Utility to access player's tileID */
|
||||
int playerTileId() { return Position.toID(y, x); }
|
||||
/** Utility to access player's row position (row coordinate) */
|
||||
int playerRow() { return y; }
|
||||
/** Utility to access player's column position (column coordinate) */
|
||||
int playerCol() { return x; }
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @name Accessor/Mutator interface
|
||||
* @note
|
||||
* Please consider not to use mutator interface. Its the abstraction killer :(
|
||||
* We have added a bit of logic however, in order to make it a bit more safe.
|
||||
*/
|
||||
/** @{ */
|
||||
int getPlayerId () { return playerId; }
|
||||
String getName() { return name; }
|
||||
Board getBoard () { return board; }
|
||||
int getScore () { return score; }
|
||||
int getX() { return x; }
|
||||
int getY() { return y; }
|
||||
|
||||
void setPlayerId(int id) { playerId = id; }
|
||||
void setName(String name) { this.name = name; }
|
||||
void setBoard (Board board){ this.board = board; }
|
||||
void setScore(int score) { this.score = score; }
|
||||
void setX(int x) {
|
||||
assert (x >= 0 && x< Session.boardSize) : "X(column) coordinate must be in the range [0, Session.boardSize)";
|
||||
this.x = x;
|
||||
}
|
||||
void setY(int y) {
|
||||
assert (y >= 0 && y< Session.boardSize) : "Y(row) coordinate must be in the range [0, Session.boardSize)";
|
||||
this.y = y;
|
||||
}
|
||||
/** @} */
|
||||
|
||||
/** @name Class data */
|
||||
/** @{ */
|
||||
private int playerId; /**< The unique identifier of the player */
|
||||
private String name; /**< The name of the player */
|
||||
private Board board; /**< Reference to the session's boards */
|
||||
private int score; /**< The current score of the player */
|
||||
private int x; /**< The column coordinate of the player on the board */
|
||||
private int y; /**< The row coordinate of the player on the board */
|
||||
private boolean champion; /**< Champion indicate a player who plays against the Minotaur */
|
||||
/** @} */
|
||||
}
|
||||
Reference in New Issue
Block a user