Skip to content

Instantly share code, notes, and snippets.

@shunfunaki
Last active August 29, 2015 14:18
Show Gist options
  • Save shunfunaki/255d82b2f24f7e48ff6e to your computer and use it in GitHub Desktop.
Save shunfunaki/255d82b2f24f7e48ff6e to your computer and use it in GitHub Desktop.
クラス設計入門:すごろくゲーム(Ruby / Ruby on Rails)

課題

次のソースコードが動作するよう、すごろくゲームを開発せよ。

game = Game.new
game.setBoard(Board.new("public/board.csv"))
game.addPlayer(Player.new("Taro"))
game.addPlayer(Player.new("Jiro"))
game.setDice(Dice.new)
game.start()

Ruby on Railsで(無理矢理やるとき)の手順

アプリケーション作成

$ rails new r-sugoroku

libディレクトリにあるクラスを自動で読み込む設定にする

config/application.rb

module RSugoroku
  class Application < Rails::Application
  
	config.autoload_paths += %W(#{config.root}/lib)
	
  end
end

Homeコントローラの作成

$ rails g controller home index

ルートにも追加しておく

config/routes.rb

  root 'home#index'

メインロジック

上記のメインロジックをhome_controllerに書くことで、 http://localhost:3000/ にアクセスしたときに実行される

app/controllers/home_controller.rb

class HomeController < ApplicationController
  def index

  	game = Game.new
  	game.setBoard(Board.new("public/board.csv"))
  	game.addPlayer(Player.new("Taro"))
  	game.addPlayer(Player.new("Jiro"))
  	game.setDice(Dice.new)
  	game.start()

  end
end

開発スタート

libディレクトリにクラスを作成していく

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment