Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions VPs-Game2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@


## TODOs / COULD DOs
- [ ] capturing ctrl-c (currently, `6` quits)
- [ ] generate random words
- [ ] better instructions
- [ ] fading trail of letters
- [ ] determine if win is possible
- [ ] jump key
- [ ] right hand keys
- [ ] switching keyboard inputs
46 changes: 46 additions & 0 deletions VPs-Game2/controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'pry'
require 'colorize'
require_relative 'model'
require_relative 'view'

module Controller
class << self





def run!
model = Model.new
View::initial(model.board)

loop do

system("stty raw -echo")
#input = gets.chomp.upcase
input = STDIN.getc
system("stty -raw echo")
case input.upcase
when /W|,/
model.move(:up)
when /S|O/
model.move(:down)
when /A|A/
model.move(:left)
when /D|E/
model.move(:right)
when "6"
exit
end
end
end




end
end



Controller::run!
163 changes: 163 additions & 0 deletions VPs-Game2/model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
require_relative 'view'

class Model
attr_accessor :board,
:xpos,
:ypos,
:xmax,
:ymax,
:word,
:is_empty,
:order


def initialize
@xmax = 8
@ymax = 8

@board = @xmax.times.map do |i|
@ymax.times.map do |j|
{
text: 'X'.red,
shale: nil,
cursor: nil,
}
end
end

@xpos = 0
@ypos = 0
@board[@xpos][@ypos][:cursor] = :right

@word = "hidesay".upcase
@word = "cat".upcase
@word = "apple".upcase
@is_empty = ->(x,y){@board[x][y][:shale].nil?}

@surface = -> (){
map_square{|s|
if s[:cursor]
if s[:shale]
s[:shale].yellow
else
s[:cursor]
end
else
s[:text]
end
}
}
@won = -> (){
map_square{|s|
if s[:shale]
s[:shale].green
else
' '
end
}
}

intersperse_word(@word)

@order = ""
end


def each_square()
@xmax.times do |x|
@ymax.times do |y|
yield(@board[x][y])
end
end
end

def map_square
@xmax.times.map do |x|
@ymax.times.map do |y|
yield(@board[x][y])
end
end
end


def board
@surface.()
end


def get_a_random_square
[rand(@xmax), rand(@ymax)]
end

def get_a_random_empty_square
x,y = get_a_random_square
if @is_empty.(x,y)
[x,y]
else
get_a_random_empty_square
end
end


def intersperse_word(word)
word.each_char do |c|
x,y = get_a_random_empty_square
@board[x][y][:shale] = c.upcase
end
end


def move(dir)

@board[@ypos][@xpos][:cursor] = nil
case dir
when :up
@ypos = ( @ypos + 1 == @ymax ? @ypos : @ypos + 1 )
when :down
@ypos = ( @ypos - 1 < 0 ? @ypos : @ypos - 1 )
when :left
@xpos = ( @xpos - 1 < 0 ? @xpos : @xpos - 1 )
when :right
@xpos = ( @xpos + 1 == @xmax ? @xpos : @xpos + 1 )
end
@board[@ypos][@xpos][:cursor] = :right

if !@board[@ypos][@xpos][:shale].nil?
Thread.new do
View::sayit(@board[@ypos][@xpos][:shale])
end
@order += @board[@ypos][@xpos][:shale]

if win_condition
Thread.new do
View::print_win_condition(@won.())
end
View::saysit(@word[-1])
sleep 1
View::sayit(@word.downcase)
sleep 1
View::saysit("YES")
sleep 1
View::saysit("yayy")
sleep 1

View::sayit("Good job. You win!")

exit
end
end


View::moved(@surface.(), @ypos, @xpos, dir)
end


def win_condition
@order[-(@word.size)..-1] == @word
end



end


79 changes: 79 additions & 0 deletions VPs-Game2/view.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@

module View
INITIAL = <<BLOCK
A*************************
B*************************
C****** HIDE SAY *********
D*************************
E*************************
F*************************
G*************************
H*************************
I*************************
J*************************
BLOCK

class << self

def initial(board)
msg = "MOVE WITH WASD".green
msg2 = "MAKE A WORD BY MOVING AROUND".green
puts INITIAL.sub(/F.*/, msg).sub(/^I.*/, msg2)
interpret_board(board)
print_board(board, 0)
end


def moved(board, xpos, ypos, dir)
interpret_board(board)
print_board(board)
#puts "moved #{dir}"
end

def print_win_condition(board)
print_board(board)
end

def print_board(board, lines = 10)
cls(lines)
x = board.size
y = board.first.size
x.times do |x|
puts board[board.size - x - 1].join("")
end
end

def interpret_board(board)
x = board.size
y = board.first.size
x.times do |x|
y.times do |y|
if board[x][y] == :right
board[x][y] = 'O'.green
end
end
end
end

def cls(lines = 10)
puts "\n" * lines
end



def sayit(it)
`say #{it}`
end

def saysit(it)
%w(Bruce Agnes Victoria).each do |voice|
Thread.new do
`say -v #{voice} #{it}`
end
end
end


end
end