17 Commits
Author SHA1 Message Date
hoo2 2230cac96e Release for version 2 2020-12-07 23:39:25 +02:00
hoo2 a4d5299514 DEV: 2nd version of the assignment (the code part) 2020-12-07 15:32:28 +02:00
hoo2 d581bd6c02 First draft of 2nd version 2020-12-06 23:03:49 +02:00
Anastasia Foti c14d1d812f Board true deep copy Constructor and createInnerWalls small bug 2020-11-17 17:08:25 +02:00
Anastasia Foti 83d5217151 New release file 2020-11-08 23:14:24 +02:00
Anastasia Foti 4745c1403e Anastasia joined the project 2020-11-08 22:51:08 +02:00
hoo2 ba5b297078 Some small changes 2020-11-08 21:10:22 +02:00
hoo2 be9db10803 Anastasia joined the project 2020-11-07 21:23:38 +02:00
hoo2 7dd8ede2f0 RC: part A 2020-10-28 18:18:17 +02:00
hoo2 5d8d06c793 Small code changes to match the current report state 2020-10-28 00:23:55 +02:00
hoo2 960dd89f4e First draft of the report 2020-10-28 00:23:29 +02:00
hoo2 ebced2aa74 WIP: report part 3 2020-10-26 23:27:22 +02:00
hoo2 70ae98f31a WIP: report part 2 2020-10-25 22:07:59 +02:00
hoo2 35044f91c7 DEV: Doxyfile added 2020-10-25 22:06:45 +02:00
hoo2 a96bb62966 WIP: Report part 1 2020-10-25 22:06:04 +02:00
hoo2 899b9a892e DEV: Comment rework 2020-10-24 22:40:12 +03:00
hoo2 b1c59453c1 DEV: Comment rework 2020-10-24 22:37:41 +03:00
20 changed files with 3634 additions and 249 deletions
-1
View File
@@ -1,6 +1,5 @@
bin/
doc/
out/
deliverable/
.classpath
.project
+2494
View File
File diff suppressed because it is too large Load Diff
Binary file not shown.
Binary file not shown.
+2
View File
@@ -2,3 +2,5 @@
*.log
*.out
*.gz
*.svg
*.xcf
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: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

BIN
View File
Binary file not shown.
+320 -8
View File
@@ -1,26 +1,38 @@
%
% 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{\DocTitle}{Λαβύρινθος: Ο Θησέας και ο Μινώταυρος Β}
\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.1\textheight}
% Document
% =================
@@ -33,6 +45,306 @@
%\listoftables
\section{Εισαγωγή}
Η παρούσα εργασία αφορά την δημιουργία ενός παιχνιδιού λαβυρίνθου “Μία νύχτα στο μουσείο”.
Στο συγκεκριμένο παιχνίδι καλούμαστε να υλοποιήσουμε ένα λαβύρινθο μέσα στον οποίο κινούνται δύο παίκτες, ο Μινώταυρος και ο Θησέας.
Στόχος του Μινώταυρου είναι να “πιάσει” τον Θησέα και στόχος του Θησέα είναι να βρει όλα τα εφόδια πού βρίσκονται κατανεμημένα στον ταμπλό, πριν ξημερώσει και πριν τον “πιάσει” ο Μινώταυρος.
\par Υπενθυμίζουμε ότι στην πρώτη εργασία κληθήκαμε να λύσουμε δύο βασικά προβλήματα.
Το πρόβλημα της δημιουργίας του λαβυρίνθου και το πρόβλημα της λειτουργίας των παικτών.
Το ταμπλό αποτελείται από πλακίδια και τοίχους.
Τα πλακίδια είναι διατεταγμένα σε τετραγωνικό σχήμα και ανάμεσά τους τοποθετούνται οι τοίχοι.
Το πρόβλημα έγκειται στην επιλογή και τοποθέτηση τοίχων με τέτοιο τρόπο ώστε να πληρούνται οι προδιαγραφές του παιχνιδιού όπως πχ κάθε πλακίδιο να έχει το πολύ δύο τοίχους ή τα εξωτερικά πλακίδια να έχουν τοίχο από την έξω μεριά.
Μετά από μια πιο λεπτομερή ανάλυση του προβλήματος, διαπιστώσαμε πως οι δοθείσες προδιαγραφές δεν αποτρέπουν τη δημιουργία κλειστών δωματίων, κάτι που θεωρήσαμε άδικο, με αποτέλεσμα, όπως περιγράφουμε και αναλυτικά παρακάτω, \textbf{να προσθέσουμε έναν ακόμη περιορισμό}.
\textbf{Την αποτροπή κλειστών δωματίων στο ταμπλό}.
\par Λαμβάνοντας υπόψιν τόσο τα δεδομένα που μας δόθηκαν, δηλαδή για την δομή του παιχνιδιού, όσο και τα δύο βασικά προβλήματα που χρειάστηκε να επεξεργαστούμε και να λύσουμε στο πρώτο μέρος, αποφασίσαμε να υλοποιήσουμε την δεύτερη εργασία με γνώμονα τις σχεδιαστικές επιλογές και τα \eng{concepts} που αναπτύξαμε και περιγράφονται αναλυτικά στην αναφορά του πρώτου μέρους.
Όσον αφορά επίσης τον τρόπο κίνησης των παικτών επιλέξαμε μόνο ο Θησέας να έχει την δυνατότητα να επιλέγει την κατεύθυνση της επόμενης κίνησης του, ενώ ο Μινώταυρος να κινείται με τυχαίο τρόπο.
\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/}} που περιέχει την \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{Ενοποιημένο σύστημα συντεταγμένων}
Στις προδιαγραφές της εργασίας περιγράφεται επίσης ένα διπλό σύστημα συντεταγμένων, τόσο για τα πλακίδια όσο και για τα εφόδια.
Ένα καρτεσιανό που διευθυνσιοδοτεί ως προς δύο άξονες και περιέχει ένα ζευγάρι γραμμής και στήλης και ένα μονοδιάστατο που αποτελείται από τον γραμμικό συνδυασμό των προηγουμένων.
Το μονοδιάστατο αντικατοπτρίζει και την απεικόνιση στη μνήμη ενός πίνακα 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{ArrayList<Integer[]> moves}} στην τάξη \eng{Board}.
Σε αυτή καταχωρούμε τις πληροφορίες της τελευταίας κίνησης του κάθε παίκτη, όπως το \eng{id} του πλακιδίου που βρίσκεται ο παίκτης, οι συντεταγμένες του και η συλλογή εφοδίου.
Στην προσθήκη αυτή οδηγηθήκαμε αφού παρατηρήσαμε πως για την τάξη \eng{HeuristicPlayer} είναι απαραίτητη η πρόσβαση στην θέση και στο \eng{id} του αντιπάλου.
Αυτή πραγματοποιείται μέσω της μεταβλητής \eng{moves} και της συνάρτησης \eng{\textit{getOpponentMoves()}}.
Έτσι ο κάθε \eng{HeuristicPlayer} μπορεί να διαπιστώσει αν στο οπτικό του πεδίο βρίσκεται κάποιος αντίπαλος και να κινηθεί κατάλληλα.
\subsection{Αναβάθμιση της τάξης \eng{Player}}
Για την επέμβαση σε αυτή την τάξη κινηθήκαμε με γνώμονα την αντιμετώπιση δύο προβλημάτων που μας παρουσιάστηκαν.
Αρχικά για την σωστή λειτουργία τη κλάσης \eng{HeuristicPlayer}, όπως αναφέραμε και παραπάνω, είναι αναγκαία η πρόσβαση στο \eng{id} του αντιπάλου.
Για τον λόγο αυτό επιλέξαμε να δημιουργείται ένα \eng{id} για κάθε παίκτη μέσω συνάρτησης της τάξης \eng{Board}, στο οποίο θα έχουμε πρόσβαση μέσω της μεταβλητής \eng{\textit{ArrayList<Integer[]> moves}}.
Τη λειτουργία αυτή αναλαμβάνει η συνάρτηση \eng{\textit{int generatePlayerId()}}.
\par
Επιπρόσθετα, θέλαμε να ενοποιήσουμε τις υλοποιήσεις των τάξεων \eng{Player} και \eng{HeuristicPlayer} και να τις κάνουμε συμβατές με την αρχή του \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{concepts} αυτά αφορούν έννοιες σχετικές με την εφαρμογή και έχουν την μορφή \eng{predicate}.
Μας δίνεται έτσι η δυνατότητα να ελέγχουμε αν κάποια είσοδός ενός \eng{predicate-concept} πληροί τις προδιαγραφές του ή όχι.
Στο σχήμα \ref{fig:concepts} φαίνεται μια οπτικοποιημένη έκδοση τους.
\subsection{Πλακίδιο φρουρός - \textit{\eng{isSentinel()}}}
Πρόκειται για \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()}}}
\WrapFigure{0.45}{r}{fig:concepts}{images/concepts.png}{
Οπτική αναπαράσταση των \eng{concepts} που χρησιμοποιούμε σε ένα ταμπλό $7x7$.
Με πράσινο αναπαρίστανται όσα πληρούν κάποιο \eng{concept} και με κόκκινο όσα όχι.
Τα πλακίδια με το γκρι χρώμα, είναι τα πλακίδια φρουροί.
}
Πρόκειται για \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{HeuristicPlayer}}
Η υλοποίηση της τάξης \eng{HeuristicPlayer} πραγματοποιήθηκε έτσι ώστε να ικανοποιούνται τα ζητήματα της εκφώνησης.
Η βασική λειτουργία της τάξης είναι η συνάρτηση \eng{\textit{double evaluate(int currentPos, int dice)}} η οποία ζητήθηκε και από την εκφώνηση.
Για τη δημιουργία της βασιστήκαμε στην ανάπτυξη κριτηρίων που θα βοηθήσουν τον παίκτη να “καταλήξει” στην βέλτιστη απόφαση με βάση το προκαθορισμένο οπτικό του πεδίο.
Τα κριτήρια αυτά είναι η απόσταση από κάποιο εφόδιο και η απόσταση από τον αντίπαλο.
Στην τελική αξιολόγηση της κίνησης τα κριτήρια συνδυάζονται με συντελεστές βάρους οι οποίοι αποτελούν σταθερές και θέτονται κατά τη μεταγλώττιση του προγράμματος.
\par
Ένα ακόμη χαρακτηριστικό της τάξης είναι η εκτενής χρήση της μεταβλητής \eng{path}.
Όπως αναφέραμε και παραπάνω η μεταβλητή αυτή είναι προσβάσιμη τόσο από την \eng{HeuristicPlayer}, όπως ζητήθηκε, όσο και από την \eng{Player}.
Και οι δύο τάξεις καταχωρούν πληροφορίες για την κίνησή του κάθε παίκτη, με την διαφορά πως η \eng{HeuristicPlayer} καταχωρεί περισσότερες.
\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.5}{fig:executable}{images/screenshot.png}{
Στιγμιότυπο από την εκτέλεση του προγράμματος σε \eng{interactive mode} και με αποφυγή κλειστών δωματίων.
Η εντολή που χρησιμοποιήθηκε είναι \eng{\texttt{java -jar labyrinth -i -{}-norooms}}
}
\section{Παρατηρήσεις}
Θα θέλαμε εδώ να σταθούμε και σε κάποιες διορθώσεις που χρειάστηκε να πραγματοποιήσουμε στον κώδικα που καταθέσαμε για την πρώτη εργασία.
Για την ακρίβεια παρατηρήσαμε πως ο \eng{copy constructor} της \eng{Board} αποτύγχανε να αντιγράψει το διάνυσμα αναφορών των τοίχων.
Ακόμα ο αλγόριθμος που αναλάμβανε να τοποθετήσει τους εσωτερικούς τοίχους στο ταμπλό αποτύγχανε να εξαντλήσει όλες τις δυνατές θέσεις με τους πιθανούς τοίχους.
Λεπτομέρειες για τις διορθώσεις μπορεί κάποιος να δει και \href{https://git.hoo2.net/hoo2/Labyrinth/commit/c14d1d812f312eda499c445e2827a8fbafa34100}{εδώ}.
\par Κλείνοντας θα θέλαμε να παρατηρήσουμε πως η παρούσα υλοποίηση ήταν μια προσπάθεια να ισορροπήσουμε μεταξύ των ζητηθέντων και μεταξύ πρακτικών που διευκολύνουν στη συγγραφή ευκολότερου και καθαρότερου κώδικα.
Γενικά, όπως είναι ίσως ήδη γνωστό, πιστεύουμε πως \textbf{αν ο χρήστης μιας βάσης κώδικα έχει τη δυνατότητα να χρησιμοποιήσει λανθασμένα τον κώδικα, κάποια στιγμή θα το κάνει.}
Ακόμα και αν ο χρήστης είναι ο ίδιος ο αρχικός συντάκτης του κώδικα.
Για το λόγο αυτό προσπαθήσαμε να περιορίσουμε, όπου αυτό δεν ήταν αντίθετο με τα ζητούμενα, την δυνατότητα της λανθασμένης χρήσης εισάγοντας αρκετά επιπλέον αντικείμενα και δομές ως εργαλεία για την εφαρμογή.
Τέτοια για παράδειγμα είναι η τάξη \eng{Position} ή η αποφυγή των \eng{setters} κτλ.
Ελπίζουμε να μην θεωρηθούν υπερβολικά.
% References
% ============================
@@ -1,13 +1,19 @@
/**
* @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.Arrays;
import java.util.function.IntFunction;
/**
@@ -31,6 +37,7 @@ class Board {
tiles = null;
supplies =null;
walls = new ArrayList<Edge>();
moves = new ArrayList<Integer[]>();
}
/**
@@ -40,12 +47,14 @@ 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 ArrayList<Integer[]>();
}
/**
@@ -69,7 +78,11 @@ class Board {
// Clone arrays
this.tiles = b.tiles.clone();
this.supplies = b.supplies.clone();
this.walls = b.walls;
for (Edge it: b.walls)
this.walls.add(new Edge(it));
for (Integer[] m : b.moves) {
this.moves.add(m);
}
}
/** @} */
@@ -131,7 +144,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 +162,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 +180,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 +206,59 @@ 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; }
/**
* Boards utility to give access to other player moves.
*
* @param playerId The id of player who asks
* @return The moves data of all other players
*/
int[][] getOpponentMoves (int playerId) {
int[][] ret = new int[moves.size()-1][Const.moveItems];
int ii= 0, ri =0;
for (Integer[] m : moves) {
if (ii != playerId)
ret[ri++] = Arrays.stream(m).mapToInt(i->i).toArray();
++ii;
}
return ret;
}
/**
* Utility function to create player IDs
* @return The generated player id.
*/
int generatePlayerId () {
moves.add(null);
return moves.size() -1;
}
/**
* 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));
}
/** @} */
/**
@@ -205,6 +283,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.
*/
ArrayList<Integer[]> 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 +313,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(ArrayList<Integer[]> moves) { this.moves =moves; }
/** @} */
@@ -269,32 +376,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 +431,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 +447,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 +469,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 +492,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 +509,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 +526,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 +633,6 @@ class Board {
* Array to hold all the walls using the edge representation
* required by the closed room preventing algorithm.
*/
private ArrayList<Integer[]> moves;
/** @} */
}
@@ -1,13 +1,19 @@
/**
* @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
@@ -15,7 +21,14 @@ import java.util.Collections;
class Const {
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 moveItems =4; /**< The number of items return by move() */
static final int viewDistance =3; /**< The max distance of the Heuristic player's ability to see */
static final double opponentFactor =1.0;
static final double supplyFactor =0.65;
}
/**
* Application wide object to hold settings like values for the session.
@@ -28,6 +41,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 +68,26 @@ 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 */
/**
* 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+4)%DirRange.End; }
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 +97,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 {
@@ -176,7 +218,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 +268,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:
* <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 = 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
* 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 +294,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 +338,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 +358,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 +381,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 +402,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 +427,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 +440,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,44 @@
/**
* @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 2nd assignment we deal with the creation of a new heuristic player
* who can cheat and manipulate the dice. Documented classes:
* - Tile
* - Supply
* - Board
* - Player
* - HeuristicPlayer
* - 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 +61,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 +102,7 @@ public class Game {
break;
case "-s":
case "--suplies":
case "--supplies":
if (i+1 < args.length)
Session.supplySize = Integer.parseInt(args[++i]);
break;
@@ -121,10 +154,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 HeuristicPlayer("Theseus", true, board, 0);
Player M = new Player("Minotaur", false, board, Position.toID(Session.boardSize/2, Session.boardSize/2));
Player players [] = {T, M};
// Populate data to the board
board.createBoard(T.playerTileId(), M.playerTileId());
@@ -137,34 +171,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.
+265
View File
@@ -0,0 +1,265 @@
/**
* @file Player.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) {
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) {
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(Position.toRow(pos.getId()), Position.toCol(pos.getId()), 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 [][] opps = board.getOpponentMoves(playerId);
for (int i=0 ; board.isWalkable(pos.getId(), direction) && i<Const.viewDistance ; ++i) {
pos = new Position(Position.toRow(pos.getId()), Position.toCol(pos.getId()), direction);
for (int o =0 ; o<opps.length; ++o) {
if (opps[o][0] == 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);
}
// Must return a new move always
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 );
return new_pos.getId();
}
/**
* 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 supplyId in case player picked one (Const.noSupply otherwise).
* </ul>
*/
@Override
int[] move(int id) {
// Initialize return array with the current data
int[] ret = new int[Const.moveItems];
ret[0] = getNextMove(id);
ret[1] = y = Position.toRow(ret[0]);
ret[2] = x = Position.toCol(ret[0]);
int supplyFlag =0, moveFlag =1;
// In case of a champion player, try also to pick a supply
if (champion && (ret[3] = board.tryPickSupply(ret[0])) != Const.noSupply) {
++score; // keep score
++supplyFlag;
}
int dir = Direction.get(id, ret[0]); // update direction counters
++dirCounter[dir];
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[0], d);
int o = opponetInDirection(ret[0], d);
if (s >= 0 && s < smin) smin = s;
if (o >= 0 && o < omin) omin = o;
}
// update path
Integer[] p = {
ret[0], dir, 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 distance =" + last[8]);
else System.out.println(" supply distance = blind");
if (last[9] != Const.noOpponent) System.out.println(" opponent distance =" + last[9]);
else System.out.println(" opponent distance = 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 */
/** @{ */
/** @} */
}
+235
View File
@@ -0,0 +1,235 @@
/**
* @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 {
/** @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) {
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 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) {
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 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[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 = new int[Const.moveItems];
ret[0] = id;
ret[1] = Position.toRow(id);
ret[2] = Position.toCol(id);
ret[3] = Const.noSupply;
int supplyFlag =0, moveFlag =0;
int diceDirection = board.dice(); // throw the dice
if (board.isWalkable(id, diceDirection)) { // The result is walkable
moveFlag =1; // mark the successful move
// 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) {
supplyFlag =1; // mark the successful supply pickup
++score; // keep score
}
++dirCounter[diceDirection]; // update direction counters
board.updateMove(ret, playerId);
}
// update path
Integer[] p = {
ret[0], 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;
}
public void setDirCounter(int[] dirCounter) {
this.dirCounter = dirCounter;
}
public 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) {
-133
View File
@@ -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 */
/** @} */
}