Introducción a Swift y XCode
Swift y el ecosistema Apple
Instalación y configuración de XCode
Quiz: Introducción a Swift y XCode
Fundamentos de programación iOS
Variables, constantes y tipos de datos básicos
Condicionales
Funciones
Tipos de datos opcionales
Arreglos: append, insert, como crearlas
Arreglos: validar, editar y agregar una lista dentro de otra lista
Conjuntos: como agregar o eliminar elementos
Conjuntos: principales operaciones entre conjuntos
Diccionarios
Recorridos parte 1: while
Recorridos parte 2: for
Recorridos parte 3: los retos
Quiz: Fundamentos de programación iOS
POO en iOS
Programación orientada a objetos en iOS
Structs
Clases y herencia
Enums
Protocolos
Funciones de arreglos (filter, map, reduce)
Funciones de arreglos parte 2 (filter, map, reduce): playground
Quiz: POO en iOS
Manejo de errores y programación segura
Manejo de errores y programación segura
Propagación de errores
Do, try, catch
Quiz: Manejo de errores y programación segura
Programación en el ecosistema de Apple
Siguientes pasos para el desarrollo en iOS
You don't have access to this class
Keep learning! Join and start boosting your career
To talk about programming is to enter a world of logic and creativity, and nothing combines these concepts better than practical projects. One of them is the simulation of Pokémon battles, a challenge that invites us to apply fundamental skills such as the use of variables, conditionals and cycles. This exercise is not only fun, but also allows us to delve deeper into the handling of function inputs and outputs in Swift.
In this exercise, we seek to develop a function that simulates a battle between two Pokémon. The function will receive two dictionaries, each representing a Pokémon with its life points (LP) and attack points (AP). The battle will start with the first Pokémon attacking, and will end when one of the two Pokémon has no more HP. The function will output a message indicating the winner.
A crucial detail in Swift is that parameters within functions are constants. To manipulate the Pokémon data, we will need to create copies of these parameters and work with variables:
var pOne = pokemonOne var pTwo = pokemonTwo.
To simulate the turn of each Pokémon, we will use a while
loop to keep the battle active as long as both Pokémon have life points. Here, a clever use of conditionals will define which Pokémon is attacking at any given time.
var turn = 1 // Turn of the first Pokémon by default while (pOne["PB"]! > 0) && (pTwo["PB"]! > 0) { if turn == 1 { // Turn of Pokémon 1 // Logic of Pokémon 1 attacking Pokémon 2 } else { // Turn of Pokémon 2 // Logic of Pokémon 2 attacking Pokémon 1 } // Change turn }
It is essential to subtract the attacking Pokémon's attack points from the opponent Pokémon's life points. In addition, we must check after each attack if a Pokémon has lost all its life points.
if turn == 1 { pTwo["BP"]! -= pOne["PA"]! if pTwo["PB"]! <= 0 { return "Pokémon One won" } turn = 2 } else { pOne["PB"]! -= pTwo["PA"]! if pOne["PB"]! <= 0 { return "Pokémon Two won" } turn = 1 }
When executing our function, it is important to capture and display the result. This is accomplished by assigning the output of the function to a variable and then printing this result:
let result = simulateBattlePokemon(pikachu, charmander) print(result).
It is important to experiment with different scenarios and adjust life and attack points to explore how the outcome of the battle varies. Constant practice in modifying and executing this exercise will strengthen your programming skills.
Simulating Pokémon battles is an excellent way to consolidate programming fundamentals. These types of tasks not only promote logical reasoning but also creativity in writing efficient and functional code. We encourage you to continue exploring and developing your Swift skills to take on new and exciting projects.
Contributions 11
Questions 1
Want to see more contributions, questions and answers from the community?