Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/samples/expert-othello.rb
blob: 5442d80aa76a11750a69d397a52dad573763e2f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# Othello
# by tieg
# 1/13/08
# with help: DeeJay, Ryan M. from mailing list
#
# FIXME bug if it memorizes it but it's not a valid move
#
module Othello

  PIECE_WIDTH  = 62
  PIECE_HEIGHT = 62
  TOP_OFFSET = 47
  LEFT_OFFSET = 12

  class Game
    BOARD_SIZE = [8,8]

    attr_accessor :p1, :p2, :board, :board_history
        
    def initialize
      @board_history = []
      @p1            = Player.new(:black, pieces_per_player)
      @p2            = Player.new(:white, pieces_per_player)
      @board         = new_board
      lay_initial_pieces
    end

    def next_turn(check_available_moves=true)
      @current_player = next_player
      if check_available_moves && skip_turn?
        # FIXME Possible infinite loop if neither player has a good move?
        next_turn
        raise "Player #{@current_player.piece} (#{@current_player.color}) has no available moves. Player #{next_player.piece}'s (#{next_player.color}) turn."
      end
    end

    def current_player
      @current_player ||= @p1
    end

    def next_player
      current_player == @p1 ? @p2 : @p1
    end

    # Build the array for the board, with zero-based arrays.
    def new_board
      Array.new(BOARD_SIZE[0]) do # build each cols L to R
        Array.new(BOARD_SIZE[1]) do # insert cells in each col
          0
        end
      end
    end
    
    # Lay the initial 4 pieces in the middle
    def lay_initial_pieces
      lay_piece([4, 3], false)
      next_turn(false)
      lay_piece([3, 3], false)
      next_turn(false)
      lay_piece([3, 4], false)
      next_turn(false)
      lay_piece([4, 4], false)
      next_turn(false)
    end
    
    def lay_piece(c=[0,0], check_adjacent_pieces=true)
      memorize_board
      piece = current_player.piece
      opp_piece = current_player.opp_piece
      raise "Spot already taken." if board_at(c) != 0
      if check_adjacent_pieces
        pieces_to_change = []
        pieces_to_change << check_direction(c, [ 0, 1], piece, opp_piece) # N
        pieces_to_change << check_direction(c, [ 1, 1], piece, opp_piece) # NE
        pieces_to_change << check_direction(c, [ 1, 0], piece, opp_piece) # E
        pieces_to_change << check_direction(c, [ 1,-1], piece, opp_piece) # SE
        pieces_to_change << check_direction(c, [ 0,-1], piece, opp_piece) # S
        pieces_to_change << check_direction(c, [-1,-1], piece, opp_piece) # SW
        pieces_to_change << check_direction(c, [-1, 0], piece, opp_piece) # W
        pieces_to_change << check_direction(c, [-1, 1], piece, opp_piece) # NW
        raise "You must move to a spot that will turn your opponent's piece." if pieces_to_change.compact.all? { |a| a.empty? }
        pieces_to_change.compact.each { |direction| direction.each { |i| @board[i[0]][i[1]] = piece } }
      end
      current_player.pieces -= 1
      @board[c[0]][c[1]] = piece
      current_winner = calculate_current_winner
      raise "Game over. Player #{current_winner.piece} wins with #{current_winner.pieces_on_board} pieces!" if @p1.pieces + @p2.pieces == 0
    end
    
    def skip_turn?
      possibles = []
      @board.each_with_index { |col,col_index| 
        col.each_with_index { |cell,row_index| 
          return false if possible_move?([col_index,row_index])
        } 
      }
      true
    end
    
    def possible_move?(c=[0,0])
      return nil if board_at(c) != 0
      possible_moves = []
      piece = current_player.piece
      opp_piece = current_player.opp_piece
      pieces_to_change = []
      pieces_to_change << check_direction(c, [ 0, 1], piece, opp_piece) # N
      pieces_to_change << check_direction(c, [ 1, 1], piece, opp_piece) # NE
      pieces_to_change << check_direction(c, [ 1, 0], piece, opp_piece) # E
      pieces_to_change << check_direction(c, [ 1,-1], piece, opp_piece) # SE
      pieces_to_change << check_direction(c, [ 0,-1], piece, opp_piece) # S
      pieces_to_change << check_direction(c, [-1,-1], piece, opp_piece) # SW
      pieces_to_change << check_direction(c, [-1, 0], piece, opp_piece) # W
      pieces_to_change << check_direction(c, [-1, 1], piece, opp_piece) # NW
      return nil if pieces_to_change.compact.all? { |a| a.empty? }
      true
    end
    
    def memorize_board
      dup_board = new_board
      dup_board = []
      @board.each do |col|
        dup_board << col.dup
      end
      @board_history << { :player => current_player, :board => dup_board }
    end

    def undo!
      last_move = @board_history.pop
      @board = last_move[:board]
      @current_player = last_move[:player]
      @current_player.pieces += 1
    end

    def calculate_current_winner
      @p1.pieces_on_board, @p2.pieces_on_board = 0, 0
      @board.each { |row| 
        row.each { |cell|
          if cell == 1
            @p1.pieces_on_board += 1
          else 
            @p2.pieces_on_board += 1
          end
        }
      }
      @p1.pieces_on_board > @p2.pieces_on_board ? @p1 : @p2
    end

    def check_direction(c, dir, piece, opp_piece)
      c_adjacent = next_in_direction(c.dup, dir)
      c_last = nil
      pieces_in_between = []
      # Find the last piece if there is one.
      while(valid_location?(c_adjacent))
        if board_at(c_adjacent) == opp_piece
          pieces_in_between << c_adjacent.dup
        elsif board_at(c_adjacent) == piece && pieces_in_between.size > 0
          c_last = c_adjacent
          break
        else
          break
        end
        c_adjacent = next_in_direction(c_adjacent, dir)
      end

      pieces_in_between.empty? || c_last.nil? ? nil : pieces_in_between
    end
    
    # Find the value of the board at the given coordinate.
    def board_at(c)
      @board[c[0]][c[1]]
    end

    # Is this a valid location on board?
    def valid_location?(c=[1,1])
      c[0] >= 0 && c[1] >= 0 && c[0] < BOARD_SIZE[0] && c[1] < BOARD_SIZE[1]
    end

    # Perform the operations to get the next spot in the appropriate direction
    def next_in_direction(c, dir)
      c[0] += dir[0]
      c[1] += dir[1]
      c
    end

    private
    def pieces_per_player
      total_squares / 2
    end
    
    # The total number of squares
    def total_squares
      BOARD_SIZE[0] * BOARD_SIZE[1]
    end

    class Player
      attr_accessor :pieces, :color, :pieces_on_board

      def initialize(color=:black,pieces=0)
        @pieces = pieces
        @pieces_on_board = 0 # used only in calculating winner
        @color = color
      end
      
      def piece
        color == :black ? 1 : 2
      end
      
      def opp_piece
        color == :black ? 2 : 1
      end
    end 
  end

  def draw_player_1(first_turn=false)
    stack :margin => 10 do
      if GAME.current_player==GAME.p1
        background yellow
        para span("Player 1 (#{GAME.current_player.color}) turn", :stroke => black, :font => "Trebuchet 20px bold"), :margin => 4
      else
        background white
        para span("Player 1", :stroke => black, :font => "Trebuchet 10px bold"), :margin => 4

        button("Undo last move", :top => 0, :left => -150) { GAME.undo!; draw_board } unless GAME.board_history.empty?
      end
    end
  end

  def draw_player_2(first_turn=false)
    stack :top => 550, :left => 0, :margin => 10 do
      if GAME.current_player==GAME.p2 
        background yellow
        para span("Player 2's (#{GAME.current_player.color}) turn", :stroke => black, :font => "Trebuchet 20px bold"), :margin => 4
      else
        background white
        para span("Player 2", :stroke => black, :font => "Trebuchet 10px bold"), :margin => 4

        button("Undo last move", :top => 0, :left => -150) { GAME.undo!; draw_board } unless GAME.board_history.empty?
      end
    end
  end


  def draw_board
    clear do
      background black
      draw_player_1
      stack :margin => 10 do
        fill rgb(0, 190, 0)
        rect :left => 0, :top => 0, :width => 495, :height => 495

        GAME.board.each_with_index do |col, col_index|
          col.each_with_index do |cell, row_index|
            left, top = left_top_corner_of_piece(col_index, row_index)
            left = left - LEFT_OFFSET
            top = top - TOP_OFFSET
            fill rgb(0, 440, 0, 90)
            strokewidth 1
            stroke rgb(0, 100, 0)
            rect :left => left, :top => top, :width => PIECE_WIDTH, :height => PIECE_HEIGHT

            if cell != 0
              strokewidth 0
              fill (cell == 1 ? rgb(100,100,100) : rgb(155,155,155))
              oval(left+3, top+4, PIECE_WIDTH-10, PIECE_HEIGHT-10)

              fill (cell == 1 ? black : white)
              oval(left+5, top+5, PIECE_WIDTH-10, PIECE_HEIGHT-10)
            end
          end
        end
      end
      draw_player_2
    end
  end

  def left_top_corner_of_piece(a,b)
    [(a*PIECE_WIDTH+LEFT_OFFSET), (b*PIECE_HEIGHT+TOP_OFFSET)]
  end

  def right_bottom_corner_of_piece(a,b)
    left_top_corner_of_piece(a,b).map { |coord| coord + PIECE_WIDTH }
  end

  def find_piece(x,y)
    GAME.board.each_with_index { |row_array, row| 
      row_array.each_with_index { |col_array, col| 
        left, top = left_top_corner_of_piece(col, row).map { |i| i - 5}
        right, bottom = right_bottom_corner_of_piece(col, row).map { |i| i -5 }
        return [col, row] if x >= left && x <= right && y >= top && y <= bottom
      } 
    }
    return false
  end

  GAME = Othello::Game.new
end


Shoes.app :width => 520, :height => 600 do
  extend Othello

  draw_board
  
  click { |button, x, y| 
    if coords = find_piece(x,y)
      begin
        GAME.lay_piece(coords)
        GAME.next_turn
        draw_board
      rescue => e
        draw_board
        alert(e.message)
      end
    else
      # alert("Not a piece.")
    end
  }
end