samedi 26 octobre 2019

Build A Ruby program code or Rubymine code with following:

Chapter 9 Assignment

CIS 116 Introduction to Programming

Build a Ruby code or Rubymine code with following:

You work for a software development company that wants to build and sell games that run in Windows. The company is in the process of creating a basic game engine. It wants everything to be object-oriented, so it needs to build the various classes that will be used in any of the games built in the future.

You have been assigned the task of building the Player class based on the specifications listed below. The class will be further developed in the future, but this initial class will help the team get started. Put all of your code in the same source file and name your file Ch9Asg.rb.

  • Create a Player class with a name and a health attribute. The health attribute must be an integer between 0 and 100. A Player with a health value of 100 is in excellent condition, and a Player whose health is 0 is essentially dead.

  • Create a class constructor that accepts two arguments that are assigned to the attributes.

  • Create a method that sets the health attribute given a value. If an attempt is made to set it below 0, set it 0; likewise, if an attempt is made to set the value higher than 100, set it to 100.

  • Create an attack() method that accepts a Player object named opponent as a parameter. Within the method, generate a random number between 0 and 25. If the value falls between 0 and 15, reduce the opponent’s health by that amount. If the value is over 15, consider the attack a failed attempt.

  • As the loop repeats, keep the user informed with what’s going on by showing the name and health of each player.

To test your class, create two Player objects, p1 and p2; give them different names. Build a loop that has p1 attacking p2; then, if p2 is still alive, it should attack p1. The loop should keep repeating until either p2 or p1 is dead, i.e. has a health value of 0. Display the winner at the end.

Submit your Ch9Asg.rb file on or before the due date. Be sure to comment your Player class well so that other programmers who use it know how it works and how to use it.

Aucun commentaire:

Enregistrer un commentaire