Battle of the Robots
Explore creating a battle game using Ruby arrays to represent teams of robots. Learn array initialization, random index selection for attacks, counting living robots, and controlling the game flow with loops and methods.
We'll cover the following...
Problem statement
Let’s build a simple game together, to sum up, all information that we have gathered from the previous chapters. We’ll have 20 robots and two teams, with 10 robots in each team. Each team will be represented by its array, with a size of 10. Each element, or cell, of the array may have only one of these two values:
0when the robot is destroyed.1when the robot is still alive.
Define two arrays. 1 in the example below indicates that we defined an array with robots who are still alive
arr1 = Array.new(10, 1)
arr2 = Array.new(10, 1)
Each team will attack one after the other. But what does it mean “to attack” in this case? If 1 in the array represents a robot that is alive, and 0 signifies a dead robot, then “to attack” means to "change the value for a certain cell in the array from 1 to 0 ...