-
-
Save 8th-Light-Blog/1054218 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Blog Title: Limelight Tutorial: Tic Tac Toe Example (Part 4) | |
Author: Paul Pagel | |
Date: September 29th, 2008 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it "should make first move in a game" do | |
@id = "cell_0_0" | |
@cell_one = Limelight::Prop.new | |
@scene = MockScene.new | |
@scene.register("cell_0_0", @cell_one) | |
self.stub!(:scene).and_return(@scene) | |
game = mock('game') | |
Game.should_receive(:current).and_return(game) | |
game.should_receive(:move).with(0, 0) | |
game.should_receive(:mark).and_return("X") | |
mouse_clicked(nil) | |
@cell_one.text.should == "X" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Cell | |
def mouse_clicked( event) | |
game = Game.current | |
x, y = get_coordinates | |
game.move(x, y) | |
cell_prop = scene.find(id) | |
cell_prop.text = game.mark | |
end | |
private ################################ | |
def get_coordinates() | |
x = id[(id.length - 1)..(id.length - 1)].to_i | |
y = id[(id.length - 3)..(id.length - 3)].to_i | |
return x, y | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it "should have message center for feedback to the user" do | |
@scene.find("message_center").should_not be(nil) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main do | |
board do | |
3.times do |row| | |
3.times do |col| | |
cell :id => "cell_#{row}_#{col}" | |
end | |
end | |
end | |
end | |
message_center :id => "message_center" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it "should display in the message center if the space is occupied." do | |
@id = "cell_0_0" | |
@cell_one = Limelight::Prop.new | |
@message_center = Limelight::Prop.new | |
@scene = MockScene.new | |
@scene.register("cell_0_0", @cell_one) | |
@scene.register("message_center", @message_center) | |
self.stub!(:scene).and_return(@scene) | |
game = mock('game') | |
Game.should_receive(:occupied?).with(0, 0).and_return(true) | |
mouse_clicked(nil) | |
@message_center.text.should == "This space is occupied, please move in an unoccupied square" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Cell | |
def mouse_clicked( event) | |
game = Game.current | |
x, y = get_coordinates | |
if game.occupied?(x, y) | |
message_center = scene.find("message_center") | |
message_center.text = "This space is occupied, please move in an unoccupied square" | |
else | |
game.move(x, y) | |
cell_prop = scene.find(id) | |
cell_prop.text = game.mark | |
end | |
end | |
private ################################ | |
def get_coordinates() | |
x = id[(id.length - 1)..(id.length - 1)].to_i | |
y = id[(id.length - 3)..(id.length - 3)].to_i | |
return x, y | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") | |
require 'cell' | |
describe Cell do | |
include Cell | |
attr_accessor :id | |
before(:each) do | |
@id = "cell_0_0" | |
@cell_one = Limelight::Prop.new | |
@scene = MockScene.new | |
@message_center = Limelight::Prop.new | |
@scene.register("message_center", @message_center) | |
@scene.register("cell_0_0", @cell_one) | |
self.stub!(:scene).and_return(@scene) | |
@game = mock('game', :occupied? => false) | |
Game.should_receive(:current).and_return(@game) | |
end | |
it "should make first move in a game" do | |
@game.should_receive(:move).with(0, 0) | |
@game.should_receive(:mark).and_return("X") | |
mouse_clicked(nil) | |
@cell_one.text.should == "X" | |
end | |
it "should display in the message center if the space is occupied." do | |
@game.should_receive(:occupied?).with(0, 0).and_return(true) | |
mouse_clicked(nil) | |
@message_center.text.should == "This space is occupied, please move in an unoccupied square" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it "should display there was a winner in the message center" do | |
@game.should_receive(:move).with(0, 0) | |
@game.should_receive(:is_winner?).and_return(true) | |
mouse_clicked(nil) | |
@message_center.text.should == "Player X has won the game, congratulations" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Cell | |
def mouse_clicked( event) | |
game = Game.current | |
x, y = get_coordinates | |
if game.occupied?(x, y) | |
message_center.text = "This space is occupied, please move in an unoccupied square" | |
else | |
game.move(x, y) | |
cell_prop = scene.find(id) | |
cell_prop.text = game.mark | |
message_center.text = "Player #{game.mark} has won the game, congratulations" if game.is_winner? | |
end | |
end | |
private ################################ | |
def get_coordinates() | |
x = id[(id.length - 1)..(id.length - 1)].to_i | |
y = id[(id.length - 3)..(id.length - 3)].to_i | |
return x, y | |
end | |
def message_center | |
return scene.find("message_center") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment