Author: Phat Tran
Course: DGL-114
Assignment: Coding Assignment 3
Submitted: March 28th, 2022
Time Spent: 2-3 days
This is an upgraded version of coding assignment 2. I keep the idea of making a game in which the player harvests the fruits and sells them to the store.
Every 5 seconds, the amount of fruits within the land increases by 6. Then, the player taps the land to collect the fruits and can sell them in the Market.
The game contains three screens:
-
Home screen
This is the landing screen when the application opens. The player can access other screens from here. It displays 9 lands in the middle of the screen. The user can harvest the fruits by tapping the land. After being harvested, the land will switch to "soil" land, and the player has to wait until the next harvesting.
-
Inventory screen
This is where the player sees the available fruits in the inventory.
-
Market screen
This is where the player sells the fruits and gets the money. Depending on the input amount and price of the fruit, the player can sell them and earn an equivalent value. It also contains a "Sell All" button to sell all the fruits just with one tap. Otherwise, the player can buy each fruit separately.
There are 3 fragments representing three different screens:
- HomeFragment (default home screen)
- InventoryFragment
- MarketFragment
It requires 3 entities to play:
-
Fruit
This entity contains information about the fruit, such as name, price, quantity in stock.
-
Land
This entity includes information about the land, such as the planted fruit, status, and available harvest amount.
-
User
This entity is just created to store the amount of money because this is a single-player game.
The project contains two lists implementing the recycler view:
- In Inventory screen, the list is rendered by the recycler view.
- In Market screen, the list of fruits is displayed with the recycler view.
The two lists in Inventory screen and Market screen are attached by the LiveData. Any changes from the database will affect concurrent to the content.
Due to having three entities, there are 3 view models for each screen:
- FruitViewModel.kt
- LandViewModel.kt
- UserViewModel.kt
This game is only a single-player game, so there are no API calls in this project. Thus, the project can upgrade by becoming a multiple-player game. On the other hand, the money in the project has not been used to buy anything yet. A new feature that requires money to spend is also a good idea.
Fortunately, in this project, I learned a way to fix a bug that the Coding assignment 2 that I did not find a way to fix it. If you inflate an item of the Recycler View by Binding, remember to attach it to the parent. Otherwise, the item of the list will not expand as wide as the parent if the width is set to "match_parent"