Components
The application consists of 4 Activities:
- Number settings - main
- Name settings
- Roles
- Help - user manual
Other Java classes
- A Number picker implementation.
- A player delete confirmation dialog Activity.
- A Preference Manager that takes care of saving and reloading current state and preference data.
Operation
Setting umbers
The user can set the number of all players, and the desired number of Watchers and Winkers among them in the main Activity using the 3 number picker.
It is important to have correctly chosen numbers for the game and for that the Number pickers and the submit Button are the items in charge.
The Number picker class is an own implementations that defines the cooperation between the plus and minus Image buttons wrapping a plain Text view. By setting the minimum and maximum values (4-50, 1-5, 1-5)
we can make sure the that the numbers do not cross reasonable limits but the numbers relation between the numbers is even more important.
The submit button checks if at least the half of the players are in Citizen role for the enjoyable game. In case of not correct number set the user is warned by a Toast message and can not proceed.
Unfortunately testing the numbers on submit Button click Event is not the most elegant solution, because this way number pickers allow uncomfortable number sets (such as 4 players, 5 Winkers, 1 Watcher)
but it still ensures that the input numbers are appropriate.
The selected numbers are saved to Preferences on submitting and will be offered as default values in the future. The Preference manager class provides shared variables and comfortable functions to save and retrieve preference data.
The reset Button sets the Number pickers' values back to the system's default values (6,1,1).
Setting names
Adding the player names is not required but highly recommended because it can help to avoid the situation when two players take the same Role card (names will be less likely mistaken than numbers). In case the user wants simple number labelled
cards the dropdown Menu offers a Skip Menu item.
However if the user decides to name the players then all players must be named. The submit Button only appears when the previously defined amount of names are inserted using the input Text view and the plus Button.
Once there are names added to the list a dropdown Spinner and a remove button appears to provide the opportunity to remove any of the players. Removal must be confirmed also in a pop-up dialog.
The clear button removes all the names from the list.
Names are also saved to Preferences on the confirm Button click Event.
Roles
By submitting or skipping name settings the user proceeds to the Roles Activity where the first Random Role draw runs automatically. A new draw can be requested by clicking the Refresh Menu icon on the Toolbar.
The Activity jumps back to the first player's Role card after each new draw. The name or number labelled cards appear showing the '?' on their back and can be turned around to reveal the Role by clicking on them.
The user can navigate between cards using the previous and next arrows on the bottom. The first and last cards offer only one direction to step to.
Help
The Help Activity can be reached from all the other Activities through the Toolbar menu.
It provides a short summary, the explanation of the rules of the game and a user manual that describes the proper use of every tool and widget of the app.
The user can navigate through pressing the previous and next Buttons in logical order or can jump right to the desired section using the dropdown Menu on the Toolbar.
Permissions
The app requires no Permission.
Resources
Layout resource files were created basically using the Android Studios's design view tool. Multiplication of components and other smaller changes
were made by editing the XML file in text view.
Menu resource files were created by editing the XML file in text view.
The app does not support different screen sizes and is not compatible with Wear OS or Smart TV-s.
The default language is English but Hunigarian language is also provided as a Local.
The layouts use only Android built-in drawables or drawables designed by me.
The colors used by the application are defined in the colors.xml resource file.
The app and the dialog app themes, also the basic text and widget appearances are defined in the styles.xml resource file.
Random Role draw
In the draw we have to select randomly a certain number of Winkers and a ceratin number of Watchers from the players. It means that applying the random index pick on the player's Array repeatedly
is not a sufficient solution since the previously selected players has to be removed for the next rounds of the draw.
That implies that we should store the players in a dinamic container like some kind of List implementation. The players' only property is their name that is stored by the Preference manager in a string Array.
Unfortunately Lists can not be built from basic variable types such as Integers so creating a List of the indexes and remove esily the selected item is not an option.
There is an other possibility to create a List of the name Strings before each row where we can easily remove the selected name from but this way we can not use indexes as identifiers. Considering that because of removing items
from the List the indexes of the players would change so we should use the name Strings as identifiers. It means if we want to store the Role properties in an Array parallel to the names Array we have to search for the player index
in question by comparing the name Strings. It sounds like a lot of work.
Accessibility could be solved by creating a Player class where the Player object can have name, role, even index property. In this case we could use a Player Array to store the players but we also should create a temporary Player List for each draw?
Considering these options I finally decided to use player index Integers to draw and store the Roles in a simple Array parallel to the name Array. So we need an algorithm that can simulate the draw event only by integers.
Let's consider the happenings of a real-life draw:
We have N players sitting next to each other. We want to choose W < N Winkers and X < N Watchers.
- We randomly choose a number between 1 and N.
- We move the selected player to the group of the Winkers.
- Now we have N-1 players left to choose from.
- To have a better view for the next pick aks the "N-th" player sitting at the end of the line to fill the chair of the player who just left.
(Note that: If it was the player at the end who left even if the player "sits back to the last chair" it does not affect the next round because we only look at the other N-1 chairs from now on...)
As we repeat these steps W, then X times we are done with the draw.
And these actions can easily interpreted into code as
- We take one integer Array containing the player index numbers from 0 to length-1, one to contain Winker indexes with a length of W and one for Watchers with a length of X. Also a cursor integer and an integer for the currently used length.
- We repeat W times to fill the Winker Array then X times to fill the Watcher Array that:
- We generate a random number between 0 and current length to be the cursor.
- We copy the player index number the cursor points at into the upcoming cell of the Winker or Watcher Array.
- We take the player index at the end of the Array and copy it where the cursor points.
- Now we can reduce the current length by 1.
- This way we have a consequential line of player indexes in the Array for each round of the random pick although the ascending order probably gets messed up.
- Finally we can reset the Role Array that all players have Citizen Role label. Then change the labels of the players at the indexes specified in the Winker Array to Winker and process the Watcher array the same way.
With this implementation of the Random Role draw the Role Array can be refreshed fast and both player names and Roles are easy to access by running through the Array indexes.