diff --git a/VPs-Game2/README.md b/VPs-Game2/README.md new file mode 100644 index 0000000..c912517 --- /dev/null +++ b/VPs-Game2/README.md @@ -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 diff --git a/VPs-Game2/controller.rb b/VPs-Game2/controller.rb new file mode 100644 index 0000000..6897a9b --- /dev/null +++ b/VPs-Game2/controller.rb @@ -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! diff --git a/VPs-Game2/model.rb b/VPs-Game2/model.rb new file mode 100644 index 0000000..d88cb38 --- /dev/null +++ b/VPs-Game2/model.rb @@ -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 + + diff --git a/VPs-Game2/view.rb b/VPs-Game2/view.rb new file mode 100644 index 0000000..c163709 --- /dev/null +++ b/VPs-Game2/view.rb @@ -0,0 +1,79 @@ + +module View +INITIAL = <