OCaml Question 6

Question 6 1
Question 6
Object-oriented programming: It’s super effective!
You have been recently hired as a game developer for the mobile app company Mintendo. This
company is a content mill that pumps out ripoffs of existing games, filled with mierotrans.
actions. Mintendo is currently working on a game called Bokemon. This game is just like Pokemon: you play as a 10-year-old kid who travels the world training their bokemon to fight
other bokemon.
Each bokemon has a type that determines the nature of its attacks. There are only three types
of boktemon in Bokemon: water, fire, and grass. A fire bokemon’s attacks do double damage to grass bokemon; a grass bokemon’s attacks do double damage to water bokemon; and a water bokemon’s attacks do double damage to fire bokemon. In these double-damage cases, we say that the attack is super effective!
1. (5 points)
Here is how we define the different types of bokemon:
type btype = Fire | Grass | Water
Implement the function is_super: btype -> btype -› bool such that is_super b1 b2 decides whether an attack from a bokemon of type b1 is super effective against a bokemon of type b2.
let is_super b1 b2 =
Programming Help
(* Hint: the solution should be <=5 lines and not use 'if' *) match b1 b2 with | Fire, Grass | Grass, Water | Water, Fire -> true
| _, _ -> false
2. (15 points)
The way Mintendo makes money with Bokemon is that attacking is a microtransaction. It cost 10 B-bucks to attcak. Moreover, the game is pay-to-win! In addition to the above type-based way of deciding whether an attack is super-effective, the player can also choose to pay an addition 20 B-bucks to make any attack super effective. This is possible, and still charges the player’s account, even when the attack would already have been super effective. Defeating the opponent bokemon rewards the player with 1000 B-bucks.
We define the interface of a bokemon as follows
type pay2win = bool
type bmon = {
attack : bmon -> pay2win -> unit:
bt : btype;(* the type of this bokemon*)
take_damage : int -> bool;
(* returns if the bokemon is defeated*)
That is, the method attack takes a target bokemon as input as well as a boolean indicating whether the attack should cost an extra 20 B-bucks to make it super effective.
Below is the beginning of a function make_bmon. Complete it by implementing attack. The amount of damage to deal to the target is simply the strength (str) of the bokemon, multiplied by 2 in case the attack is super effective. The cost of the attack must be subtracted from the player’s account balance (bbucks). If the account balance of the player is insufficient, attack should raise the exception BuyMoreBBucks. To deal damage to the target bokemon, you must use its take_damage method. Notice that this method returns a boolean indicating whether the attack is fatal; in that case, attack must add 1000 to the player’s account balance.
Question 6 2
CS Help, Email: tutorcs@163.com
let make_bmon (str: int)(bt: btype)(bbucks: int ref) (max_hp: int)=
let hp = ref max hp in {
take_damage = (fun dmg -> hp := !hp – dmg; !hp <= 0); attack = fun target p2w ->
Question 6 3
Programming Help, Add QQ: 749389476