teaching machines

CS 330 Lecture 37 – Ruby Objects

May 6, 2013 by . Filed under cs330, lectures, spring 2013.

Agenda

The Final

For the final, you are expected to:

TODO

Code

wrapper.cpp

#include <iostream>

using std::ostream;

class Integer {
  public:
    Integer(int i) : i(i) {}

    // pre increment
    Integer &operator++() {
      i += 1;
      return *this;
    }

    // post increment
    Integer operator++(int) {
      Integer tmp = *this;
      i += 1;
      return tmp;
    }

    int get() const {
      return i;
    }

  private:
    int i;
};

ostream &operator<<(ostream &out, const Integer &n) {
  return out << n.get();
}

int main(int argc, char **argv) {
  Integer count1(7); 
  std::cout << "count1++: " << count1++ << std::endl;
  Integer count2(7); 
  std::cout << "++count2: " << ++count2 << std::endl;
  return 0;
}

rpg.rb

#!/usr/bin/env ruby

class Character
  def initialize(name, hp, mp, charisma=0, luck=0)
    @name = name
    @hp = hp
    @mp = mp
  end

  def to_s
    "#{@name} [HP: #{@hp} | MP: #{@mp}]"
  end

  def takeHit(force)
    @hp -= force
  end

  # def mp 
    # @mp 
  # end 

  def mp= new_mp
    @mp = new_mp
  end

  attr_reader :mp, :hp, :name
end

class PotHead
  def takeHit(x)
    puts "..."
  end
end

class Dragon < Character
  def ignite(other)
    other.takeHit(100)
  end
end

class BlackMage < Character
  def ice(other)
    # other.takeHit(10) if @mp > 0 
    if @mp >= 5
      other.takeHit(10)
      @mp -= 5
    end
  end

  def ica(other)
    2.times {ice(other)}
  end

  def icaga(other)
    3.times {ice(other)}
  end
end

Haiku

class Haiku:
  def nsyllables x; [3, 12, 3][x]; end
Perversion