ICSAPI::parseVerbose is now used instead of VariantInfo::getVerboseMove.
[tagua/yd.git] / src / variants / tictactoe.rb
blob4a4e566b3393eaa100134d7fbe2ad43e208b8655
1 require 'tagua'
3 module TicTacToe
5 class Piece
6   attr_reader :color
7   
8   def initialize(color)
9     @color = color
10   end
12   def to_s
13     "#{@color}_man"
14   end
15 end
17 Move = Tagua::Point
19 class Pool
20   def clear
21   end
22 end
24 class Position
25   include GridAccess
26   attr_accessor :turn
27   def previous_turn
28     if @turn == :white
29       :black
30     else
31       :white
32     end
33   end
34   
35   def initialize
36     @turn = :white
37     @board = Grid.new(size)
38   end
39   
40   def size
41     Point.new(3,3)
42   end
43   
44   def setup
45   end
46   
47   def test_move(m)
48     not @board[m]
49   end
50   
51   def move(m)
52     @board[m] = Piece.new(@turn)
53     switch_turn
54   end
55   
56   def switch_turn
57     @turn = if @turn == :white
58       :black
59     else
60       :white
61     end
62   end
63   
64   def status
65     each_line do |line|
66       res = check_line(line)
67       return "#{res} wins" if res
68     end
69     @grid.each_square do |i|
70       return "in play" unless @grid[i]
71     end
72     "draw"
73   end
74   
75   def clone
76     res = self.class.new
77     res.turn = @turn
78     res.board = board.clone
79     res
80   end
81   
82   def ==(other)
83     @turn == other.turn and @board == other.board
84   end
85 protected
86   def each_line
87     (0..2).each do |i|
88       yield (0..2).map{|y| Point.new(i, y) }
89       yield (0..2).map{|x| Point.new(x, i) }
90     end
91     
92     yield (0..2).map{|x| Point.new(x, x) }
93     yield (0..2).map{|x| Point.new(x, 2 - x) }
94   end
95   
96   def check_line(line)
97     col = false
98     line.each do |val|
99       return false if col and col != val.color
100       col = val.color
101     end
102     col
103   end
104   
105   attr_accessor :board
110 Tagua.register "Tic Tac Toe", TicTacToe