Sha256: 282f4e5f0bb93961fce9abe77065b106d30fe95223fc7f2b03351a856ec751ab
Contents?: true
Size: 1.19 KB
Versions: 1
Compression:
Stored size: 1.19 KB
Contents
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
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
rubymotionr-0.0.1 | spec/multiple-rubymotion-apps/TicTacToe/app/board.rb |