Dashboard > Ian's Ropes Project > Creating a Multi-player Online Game > Current Program Build
  Ian's Ropes Project Log In   View a printable version of the current page.  
  Current Program Build
Added by Ian, last edited by Ian on Mar 31, 2008  (view change)
Labels: 
(None)

This is the current coding for my tic-tac-toe game.

require '/Users/ianandrews/RopesProject/lib/Player.rb'
require '/Users/ianandrews/RopesProject/lib/Players.rb'
require '/Users/ianandrews/RopesProject/lib/Square.rb'
require '/Users/ianandrews/RopesProject/lib/Board.rb'
require '/Users/ianandrews/RopesProject/lib/Remote.rb'
require '/Users/ianandrews/RopesProject/lib/Listener.rb'

Shoes.app {
  $app = self
  
  players = Players.new
  players.add(Player.new("Ian","192.168.2.11","/Users/ianandrews/RopesProject/resources/x-image.png"))
  players.add(Player.new("Larry","192.168.2.7","/Users/ianandrews/RopesProject/resources/o-image.png"))
  # players.add(Player.new("Larry","192.168.2.7","/Users/ianandrews/RopesProject/resources/x-image.png"))
  players.me = players.player_list[0]
  board = Board.new(players, 3, 3)
  # we are the master
  
  remote = Remote.new(players.player_list[1].ip_address)
  board.remote_object = remote.obj
  #board.thread = Thread.new
  #sleep
  #game = Game.new(board, players)
  
}
require '/Users/ianandrews/RopesProject/lib/Player.rb'
require '/Users/ianandrews/RopesProject/lib/Players.rb'
require '/Users/ianandrews/RopesProject/lib/Square.rb'
require '/Users/ianandrews/RopesProject/lib/Board.rb'
require '/Users/ianandrews/RopesProject/lib/Remote.rb'
require '/Users/ianandrews/RopesProject/lib/Listener.rb'

Shoes.app {
  $app = self
  
  players = Players.new
  players.add(Player.new("Ian","192.168.2.11","/Users/ianandrews/RopesProject/resources/x-image.png"))
  players.add(Player.new("Larry","192.168.2.7","/Users/ianandrews/RopesProject/resources/o-image.png"))
  # players.add(Player.new("Larry","192.168.2.7","/Users/ianandrews/RopesProject/resources/x-image.png"))
  players.me = players.player_list[0]
  board = Board.new(players, 3, 3)
  
  # we are not the master, we listen for directives
  game_server = GameServer.new()
  game_server.board = board
  Listener.new(game_server)
  board.remote_object = nil
  
  # sleep
  #DRb.thread.join
  #game = Game.new(board, players)
  
}
require 'drb'

  class Board
  
    attr_accessor :mutex, :thread, :remote_object, :players, :rowCount, :colCount, :last_move
    
    def initialize(players, rowCount, colCount)
      @players = players      
      @rowCount = rowCount
      @colCount = colCount
      @thread = Thread.current
      @mutex = Mutex.new
      
      nobody = Player.new("nobody", "127.0.0.1", "/Users/ianandrews/RopesProject/resources/blank-image.png")
      @squares = []
      
      @rowCount.times do |row |
        @squares[row] = []
        $app.flow :margin => 30 do
          @colCount.times do | col |
            this_square = Square.new(row, col)
            this_square.image = $app.image nobody.marker_path, :height => 100, :width => 100 do
             @my_square = which(this_square.image)
             my_move(@my_square)
              update_other(@my_square)
            end
            @squares[row][col] = this_square
          end
        end
      end
      
    end
    
    def which(image)
      @rowCount.times do |row|
        @colCount.times do |col|
          my_square = @squares[row][col]
          if my_square.image == image 
            return my_square
          end
        end
      end
    end

    def my_move(square)
      if @players.my_turn?
        if nil == square.player 
          square.player = @players.me
          square.image.path = square.player.marker_path
          square.image.hide
          square.image.show
          @last_move = square
        else 
          alert("Not allowed; square already occupied!")
        end
      else
        alert("Not allowed: not your turn!")
      end
    end
    
    def opportunity
      @mutex.synchronize do
      
      end
    end
    
    # if we are the server (slave) we sleep and wait for the master to query us,
    # else we are the master so we update the server
    def update_other(square)
      if nil == remote_object
#        sleep
      else
        remote_object.receive_move(square)
      end
    end
    
    #if we are server this is called by listener on behalf of remote client
    def other_move(changed_square)
      row = changed_square.row
      col = changed_square.col
      this_square = @squares[row][col]
      this_square.player = changed_square.player
      this_square.image.path = changed_square.image.path
      this_square.image.hide
      this_square.image.show
#      @players.next
#      sleep
    end
    
  end
class Game
  
    attr_accessor :board, :players
    
    def initialize(is_host, board, players)
      @is_host = is_host
      @board = board
      @players = players
    end
        
    def connect_to(ip_address)
      if is_host
        listener = Thread.new do
          @server = TCPServer.new(12121)
          @socket = server.accept
        end 
      else
        @socket = TCPSocket.new(@ip_address, 12121)
      end
    end
    
    def send(stuff)
      @socket.puts stuff
    end
    
    def receive
      @socket.gets
    end
  
  
  
  
  
  
  
  
  
  end
require 'drb'
require '/Users/ianandrews/RopesProject/lib/Square.rb'
require '/Users/ianandrews/RopesProject/lib/GameServer.rb'

class GameServer
  
  def board=(board)
    @board = board
  end
  
  def receive_move(square)
#    @board.thread.run
    @board.other_move(square)
  end
   
  def originate_move
    previous_move = @board.last_move
#    @board.thread.run
    watcher = Thread.new do
      sleep 2
      Thread.exit if previous_move != @board.last_move
    end
    watcher.join
#    @board.opportunity
#    @board.mutex.synchronize
#    @board.thread.run
#    sleep
    # we sleep until a move on board awakens us
    return @board.last_move
  end

end
require 'drb'
require '/Users/ianandrews/RopesProject/lib/GameServer.rb'

class Listener

  attr_accessor :obj

  def initialize(game_server)
    DRb.start_service('druby://192.168.2.11:9000', game_server)
  end
  
end
require '/Users/larrytalley/RopesProject/lib/Player.rb'
require '/Users/larrytalley/RopesProject/lib/Players.rb'
require '/Users/larrytalley/RopesProject/lib/Square.rb'
require '/Users/larrytalley/RopesProject/lib/Board.rb'
require '/Users/larrytalley/RopesProject/lib/Game.rb'

Shoes.app {
  $app = self
  
  players = Players.new
  players.add(Player.new("Larry","192.168.2.7","/Users/larrytalley/RopesProject/resources/o-image.png"))
  players.add(Player.new("Ian","192.168.2.7","/Users/larrytalley/RopesProject/resources/x-image.png"))
  # players.add(Player.new("Larry","192.168.2.7","/Users/ianandrews/RopesProject/resources/x-image.png"))
  board = Board.new(false, players, 3, 3)
  #game = Game.new(board, players)
  
}
class Player

  attr_accessor :name, :ip_address, :marker_path

  def initialize(name, ip_address, marker_path)
    @name = name
    @ip_address = ip_address
    @marker_path = marker_path
  end

end
class Players

  attr_accessor :player_list, :me

  def initialize
    @player_list = []
    @currentPlayerIndex = 0
  end

  def add(newPlayer)
    @player_list << newPlayer
  end
  
  def my_turn?
    #@player_list[@currentPlayerIndex] == @me
    return true
  end

  def next
    next_player = @player_list[@currentPlayerIndex]
    @currentPlayerIndex = @currentPlayerIndex + 1
    if @currentPlayerIndex >= @player_list.length 
      @currentPlayerIndex = 0
    end
    next_player
  end

end
require 'drb'
require '/Users/ianandrews/RopesProject/lib/GameServer.rb'

class Remote

  attr_accessor :obj

  def initialize(ip_address)
    DRb.start_service
    @obj = DRbObject.new(nil, 'druby://' + ip_address + ':9000')
  end
  
end
class Square
  
    attr_accessor :row, :col, :image, :player
    
    def initialize(row, col)
      @row = row
      @col = col
      @player = nil
      @image = nil
    end
    
#    def marshal_dump
#      str = "<square>"
#      str = str + Marshal.dump(@row)
#      str = str + Marshal.dump(@col)
#      str = str + Marshal.dump(@image.path)
#      str = str + Marshal.dump(@player)
#      str = str + "</square>"
#    end
    
    #def marshal_load(str)
    #  self.instance_eval do
    #    initialize(str)
    #  end
    #end
    
  end

Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.2 Build:#807 May 20, 2007) - Bug/feature request - Contact Administrators