Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

TicTacToe sample #16

Merged
merged 1 commit into from

2 participants

@jwbuurlage

No description provided.

Jan-Willem Buurlage Added TicTacToe sample 4b33a5d
@lrz
Owner

Thank you very much for contributing this sample!

@lrz lrz merged commit 6efd779 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 18, 2012
  1. Added TicTacToe sample

    Jan-Willem Buurlage authored
This page is out of date. Refresh to see the latest.
View
5 TicTacToe/.gitignore
@@ -0,0 +1,5 @@
+.repl_history
+build
+resources/*.nib
+resources/*.momd
+resources/*.storyboardc
View
7 TicTacToe/Rakefile
@@ -0,0 +1,7 @@
+$:.unshift("/Library/RubyMotion/lib")
+require 'motion/project'
+
+Motion::Project::App.setup do |app|
+ # Use `rake config' to see complete project settings.
+ app.name = 'TicTacMotion'
+end
View
8 TicTacToe/app/app_delegate.rb
@@ -0,0 +1,8 @@
+class AppDelegate
+ def application(application, didFinishLaunchingWithOptions:launchOptions)
+ @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
+ @window.rootViewController = BoardViewController.alloc.init
+ @window.makeKeyAndVisible
+ true
+ end
+end
View
59 TicTacToe/app/board.rb
@@ -0,0 +1,59 @@
+class Board
+
+ # @grid:
+ # +---+---+---+
+ # | 0 | 1 | 2 |
+ # +---+---+---+
+ # | 3 | 4 | 5 |
+ # +---+---+---+
+ # | 6 | 7 | 8 |
+ # +---+---+---+
+
+ attr_reader :grid
+
+ def initialize
+ # -1 for unset squares
+ @grid = Array.new(9, -1)
+ end
+
+ # returns false if illegal move
+ # and player id if it is legal
+ def move(square, player)
+ # check if square is taken
+ if not @grid[square] == -1
+ return false
+ end
+ @grid[square] = player
+ end
+
+ # resets the grid
+ def reset
+ @grid.replace(Array.new(9, -1))
+ end
+
+ # returns the id of the winner
+ # -1 for stalemate or false if
+ # game is not over
+ def game_over?
+ for i in 0..2
+ if @grid[3*i] != -1 && @grid[3*i] == @grid[3*i + 1] && @grid[3*i + 1] == @grid[3*i + 2]
+ return @grid[3*i]
+ elsif @grid[i] != -1 && @grid[i] == @grid[i + 3] && @grid[i + 3] == @grid[i + 6]
+ return @grid[i]
+ end
+ end
+
+ if @grid[0] != -1 && @grid[0] == @grid[4] && @grid[4] == @grid[8]
+ return @grid[0]
+ elsif @grid[2] != -1 && @grid[2] == @grid[4] && @grid[4] == @grid[6]
+ return @grid[2]
+ end
+
+ if not @grid.include?(-1)
+ return -1
+ end
+
+ false
+ end
+
+end
View
86 TicTacToe/app/board_view_controller.rb
@@ -0,0 +1,86 @@
+class BoardViewController < UIViewController
+ SQUARE_SIZE = 80
+ COLORS = [UIColor.greenColor, UIColor.redColor, UIColor.whiteColor]
+
+ def viewDidLoad
+ self.init_views
+ self.new_game
+ self.board_changed
+ end
+
+ def init_views
+ view.backgroundColor = UIColor.blackColor
+
+ # view for the board
+ @board_view = UIView.alloc.initWithFrame([[0, 0], [SQUARE_SIZE * 3, SQUARE_SIZE * 3]])
+ @board_view.center = view.center
+
+ # views for the square
+ @square_views = []
+ for i in 0..2
+ for j in 0..2
+ square_view = UIView.alloc.initWithFrame([[j * (SQUARE_SIZE + 2), i * (SQUARE_SIZE + 2)], [SQUARE_SIZE - 4, SQUARE_SIZE - 4]])
+ square_view.layer.cornerRadius = 5.0
+ @square_views[3*i + j] = square_view
+ @board_view.addSubview(square_view)
+ end
+ end
+
+ # label for game information
+ @label = UILabel.alloc.initWithFrame([[0, 0], [0, 0]])
+ @label.textColor = UIColor.whiteColor
+ @label.font = UIFont.systemFontOfSize(30)
+ @label.backgroundColor = nil
+
+ view.addSubview(@label)
+ view.addSubview(@board_view)
+ end
+
+ def show_info(text)
+ @label.text = text
+ @label.sizeToFit
+ @label.center = [160, 50]
+ end
+
+ def new_game
+ # initalize the game
+ @board = Board.new
+ @current_player = 0
+ end
+
+ def touchesEnded(touches, withEvent:event)
+ # check if we're touching a square
+ for i in 0..8
+ if event.touchesForView(@square_views[i])
+ if not @board.move(i, @current_player) then
+ show_info("Illegal move!")
+ break
+ end
+
+ @current_player += 1
+ @current_player %= 2
+ break
+ end
+ end
+
+ # check if the game is over
+ if winner = @board.game_over?
+ @board.reset
+ if winner == -1
+ show_info("No one wins!")
+ elsif winner
+ show_info("Player #{winner + 1} wins!")
+ end
+ end
+
+ self.board_changed
+ end
+
+ # update the square views
+ def board_changed
+ for i in 0..8
+ @square_views[i].backgroundColor = COLORS[@board.grid[i]]
+ end
+ end
+
+end
View
9 TicTacToe/spec/main_spec.rb
@@ -0,0 +1,9 @@
+describe "Application 'TicTacToe'" do
+ before do
+ @app = UIApplication.sharedApplication
+ end
+
+ it "has one window" do
+ @app.windows.size.should == 1
+ end
+end
Something went wrong with that request. Please try again.