Created
October 7, 2013 18:49
-
-
Save burtlo/6872936 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ruby-processing' | |
class Board | |
def positions | |
@positions ||= [ [], [], [] ] | |
end | |
def at(x,y) | |
positions[x][y] | |
end | |
def set(x,y,mark) | |
return false if positions[x][y] | |
positions[x][y] = mark | |
end | |
def to_moves | |
moves = [] | |
positions.each_with_index do |row,row_index| | |
row.each_with_index do |column,column_index| | |
if column | |
moves.push [column,row_index,column_index] | |
end | |
end | |
end | |
moves | |
end | |
def game_over? | |
to_moves.count == 9 | |
end | |
def winner? | |
winner | |
end | |
def winner | |
diagonal_winner || horizontal_winner || vertical_winner | |
end | |
def horizontal_winner | |
win_goes_to = nil | |
positions.each do |row| | |
if row.count != 3 | |
nil | |
elsif row.all? { |move| move == :x } | |
win_goes_to = :x | |
elsif row.all? { |move| move == :o } | |
win_goes_to = :o | |
end | |
end | |
puts "Horizontal Winner: #{win_goes_to}" | |
win_goes_to | |
end | |
def vertical_winner | |
win_goes_to = nil | |
if positions[0][0] == :x and positions[1][0] == :x and positions[2][0] == :x | |
win_goes_to = :x | |
end | |
if positions[0][1] == :x and positions[1][1] == :x and positions[2][1] == :x | |
win_goes_to = :x | |
end | |
if positions[0][2] == :x and positions[1][2] == :x and positions[2][2] == :x | |
win_goes_to = :x | |
end | |
if positions[0][0] == :o and positions[1][0] == :o and positions[2][0] == :o | |
win_goes_to = :o | |
end | |
if positions[0][1] == :o and positions[1][1] == :o and positions[2][1] == :o | |
win_goes_to = :o | |
end | |
if positions[0][2] == :o and positions[1][2] == :o and positions[2][2] == :o | |
win_goes_to = :o | |
end | |
puts "Vertical Winner: #{win_goes_to}" | |
win_goes_to | |
end | |
def diagonal_winner | |
win_goes_to = nil | |
if positions[0][0] == :x and positions[1][1] == :x and positions[2][2] == :x | |
win_goes_to = :x | |
end | |
if positions[0][2] == :x and positions[1][1] == :x and positions[2][0] == :x | |
win_goes_to = :x | |
end | |
if positions[0][0] == :o and positions[1][1] == :o and positions[2][2] == :o | |
win_goes_to = :o | |
end | |
if positions[0][2] == :o and positions[1][1] == :o and positions[2][0] == :o | |
win_goes_to = :o | |
end | |
puts "Diagonal Winner: #{win_goes_to}" | |
win_goes_to | |
end | |
end | |
class BoardView | |
def initialize(app) | |
@app = app | |
end | |
attr_reader :app | |
def draw(options={}) | |
clear_board | |
app.stroke(255) | |
app.line(100,0,100,300) | |
app.line(200,0,200,300) | |
app.line(0,100,300,100) | |
app.line(0,200,300,200) | |
board = options[:board] | |
board.to_moves.each do |move| | |
send("#{move[0]}_move",move[1],move[2]) | |
end | |
if board.winner? | |
draw_winner(board.winner) | |
end | |
# if board.game_over? | |
# draw_winner(board.winner) | |
# end | |
if (options[:debug]) | |
x_debug | |
o_debug | |
end | |
end | |
def clear_board | |
app.stroke(0) | |
app.fill(0) | |
app.rect(0,0,300,300) | |
end | |
def draw_winner(winner) | |
send("draw_#{winner}_winner") | |
end | |
def draw_x_winner | |
app.stroke(255) | |
app.fill(255) | |
app.rotate Processing::App::PI / 4 | |
app.rect(10,0,280 * Math.sqrt(2),10) | |
app.rotate Processing::App::PI / 4 * 6 | |
app.rect(-200,210,280 * Math.sqrt(2),10) | |
# undo all the nastiness that I did above | |
app.rotate Processing::App::PI / 4 * 7 * -1 | |
end | |
def draw_o_winner | |
app.stroke(255) | |
app.fill(255) | |
app.ellipse_mode(Processing::App::CENTER) | |
app.ellipse(150,150,200,200) | |
end | |
def o_debug | |
o_move(0,0) | |
o_move(1,0) | |
o_move(2,0) | |
o_move(0,1) | |
o_move(0,2) | |
o_move(1,1) | |
o_move(2,2) | |
o_move(1,2) | |
o_move(2,1) | |
end | |
def o_move(x,y) | |
clear(x,y) | |
app.stroke(255) | |
app.fill(0) | |
app.ellipse_mode(Processing::App::CENTER) | |
offset_x = x * 100 | |
offset_y = y * 100 | |
app.ellipse(50 + offset_x,50 + offset_y,90,90) | |
end | |
def x_debug | |
x_move(0,0) | |
x_move(1,0) | |
x_move(2,0) | |
x_move(0,1) | |
x_move(0,2) | |
x_move(1,1) | |
x_move(2,2) | |
x_move(1,2) | |
x_move(2,1) | |
end | |
def x_move(x,y) | |
clear(x,y) | |
app.stroke(255) | |
offset_x = x * 100 | |
offset_y = y * 100 | |
app.line(offset_x + 10,offset_y + 10,offset_x + 90,offset_y + 90) | |
app.line(offset_x + 90,offset_y + 10,offset_x + 10,offset_y + 90) | |
end | |
def clear(x,y) | |
offset_x = x * 100 | |
offset_y = y * 100 | |
app.fill(0) | |
app.stroke(0) | |
app.rect(offset_x + 1,offset_y + 1,98,98) | |
end | |
end | |
class ProcessArtist < Processing::App | |
def setup | |
background(0, 0, 0) | |
end | |
def draw | |
draw_board | |
end | |
def current_move | |
@current_move ||= :o | |
end | |
def toggle_move | |
if @current_move == :x | |
@current_move = :o | |
else | |
@current_move = :x | |
end | |
end | |
def key_pressed | |
warn "A key #{key.inspect}" | |
if key == "r" | |
@board = nil | |
return | |
end | |
toggle_move | |
result = case key | |
when "1" then board.set(0,0,current_move) | |
when "2" then board.set(1,0,current_move) | |
when "3" then board.set(2,0,current_move) | |
when "4" then board.set(0,1,current_move) | |
when "5" then board.set(1,1,current_move) | |
when "6" then board.set(2,1,current_move) | |
when "7" then board.set(0,2,current_move) | |
when "8" then board.set(1,2,current_move) | |
when "9" then board.set(2,2,current_move) | |
end | |
toggle_move unless result | |
puts "Result: #{result}" | |
end | |
def board | |
@board ||= Board.new | |
end | |
#def debug_board | |
# def board | |
# some_board = Board.new | |
# some_board.set(0,0,:x) | |
# some_board.set(0,1,:x) | |
# some_board.set(0,2,:o) | |
# some_board.set(1,1,:o) | |
# some_board.set(2,0,:x) | |
# some_board.set(1,0,:o) | |
# some_board.set(1,2,:x) | |
# some_board.set(2,2,:o) | |
# some_board.set(2,1,:x) | |
# some_board | |
# end | |
def draw_board | |
# BoardView.new(self).draw(:debug => true) | |
BoardView.new(self).draw(:board => board) | |
end | |
end | |
ProcessArtist.new(:width => 300, :height => 300, | |
:title => "ProcessArtist", :full_screen => false) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment