Day 4 of #100DaysOfCode: Elements Clash Game

Unfortunately Day 4 blog post and project was delayed by quite some time because I had some other commitments this week and I faced quite a few problems with the mini project I was working on after learning the following python concepts from Dr Angela Yu's 100DaysOfCode: The Complete Python Bootcamp course:

  • Random Module

  • Nested Lists

I have created a mini project for Day 4 of #100DaysOfCode called Elements Clash Game. In this game, the players select from three elements. Those elements are Fire, Water and Earth. Once both players selected the elements, the game revels the elements that player 1 and player 2 (controlled by the computer) have chosen. The aim of the game is the player that chooses the strongest element wins. For example, if player 1 chooses Water and player 2 (controlled by the computer). Player 2 wins because Earth has it's strength where it can absorb water.

Python Concepts Used:

Random Module

To make the Elements Clash Game unpredictable, it uses the random module which allows to generate a random whole number between 1 and 3. To make this possible and achieve unpredictability, it uses the random.randint() function. Each of those whole numbers are assigned to each element. Precisely, number one is assigned to the Fire element, number two is assigned to Water element and number 3 is assigned to Earth element. After this, when player 2 (controlled by the computer) randomly generates a whole number ranging between 1 and 3, it indicates the their choice of one of the three elements: Fire, Water or Earth.

player2_choice = random.randint(1, 3)

Nested List

During this mini project, I have decided to create three individual lists for each element (Fire, Water and Earth) then combine those lists into one which is known as nested lists by using the variable elements_sw. The reason for a nested list is because the Fire, Water and Earth element always have some sort of relationship with each other whether it's the strengths and weaknesses of each element or knowing which element wins over the other.

fire_element = [["Fire", ["Water", "Extinguishes"], ["Earth", "Burns"]]]
water_element = [["Water", ["Fire", "Extinguishes"], ["Earth", "Absorbs"]]]
earth_element = [["Earth", ["Water", "Absorbs"], ["Fire", "Burns"]]]

elements_sw = [fire_element, water_element, earth_element]

The purpose of me creating nested lists is to make it easier to compare the element that player 1 and 2 have chosen to determine which element is stronger. It also provides a structured way of printing the outcome of how the strongest element defeats the weaker one.

Takeaways from Elements Clash Game

This mini project has been the hardest project and it did bring me valuable lessons. One of the most important learnings is using breakpoints to see how strings are accessed in lists. This has helped printing values easier. Another importance learning was using print function for debugging which helps find errors much easier and it helped get a much better understanding of nested lists and random functions.

In conclusion, this was a very fun and exciting mini project to build. To access the full code of this mini project, please visit my GitHub Repository - https://github.com/keiransystem14/Python100daysofcode/tree/main/Day%204%20Project%20-%20Elements%20Clash%20Game.