From f78eb84a652a651eb86d9f89dfb9209627c9e5af Mon Sep 17 00:00:00 2001 From: Vasanth Pappu Date: Tue, 16 Sep 2014 19:15:19 -0400 Subject: [PATCH 01/12] init commit --- VPs-Game2/MVC.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 VPs-Game2/MVC.rb diff --git a/VPs-Game2/MVC.rb b/VPs-Game2/MVC.rb new file mode 100644 index 0000000..e69de29 From 4aea9f2138d262cde97b0fe9e5fbe5e5ad315266 Mon Sep 17 00:00:00 2001 From: Vasanth Pappu Date: Tue, 16 Sep 2014 21:11:09 -0400 Subject: [PATCH 02/12] split MVC.rb into three files, got user input to not wait for \n by looking it up --- VPs-Game2/MVC.rb | 62 +++++++++++++++++++++++++++++++++++++++++ VPs-Game2/controller.rb | 46 ++++++++++++++++++++++++++++++ VPs-Game2/model.rb | 40 ++++++++++++++++++++++++++ VPs-Game2/view.rb | 36 ++++++++++++++++++++++++ 4 files changed, 184 insertions(+) create mode 100644 VPs-Game2/controller.rb create mode 100644 VPs-Game2/model.rb create mode 100644 VPs-Game2/view.rb diff --git a/VPs-Game2/MVC.rb b/VPs-Game2/MVC.rb index e69de29..47a5afe 100644 --- a/VPs-Game2/MVC.rb +++ b/VPs-Game2/MVC.rb @@ -0,0 +1,62 @@ +require 'pry' +require 'colorize' + +INITIAL = < Date: Tue, 16 Sep 2014 21:29:08 -0400 Subject: [PATCH 03/12] dvorak compatible --- VPs-Game2/controller.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/VPs-Game2/controller.rb b/VPs-Game2/controller.rb index 7b558f8..923d6e0 100644 --- a/VPs-Game2/controller.rb +++ b/VPs-Game2/controller.rb @@ -4,7 +4,6 @@ require_relative 'view' module Controller - include Model class << self @@ -13,6 +12,7 @@ class << self def run! View::initial + model = Model.new loop do @@ -21,14 +21,14 @@ def run! input = STDIN.getc system("stty -raw echo") case input.upcase - when "W" - Model::move(:up) - when "S" - Model::move(:down) - when "A" - Model::move(:left) - when "D" - Model::move(:right) + 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 From 1426748259abade2c3c0501b869986e21f3e1101 Mon Sep 17 00:00:00 2001 From: Vasanth Pappu Date: Tue, 16 Sep 2014 21:51:13 -0400 Subject: [PATCH 04/12] board printing --- VPs-Game2/view.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/VPs-Game2/view.rb b/VPs-Game2/view.rb index ecbed37..62efdb6 100644 --- a/VPs-Game2/view.rb +++ b/VPs-Game2/view.rb @@ -21,10 +21,19 @@ def initial end - def moved(dir) + def moved(board, xpos, ypos, dir) + board[xpos][ypos] = 'O'.green + print_board(board) puts "moved #{dir}" end + def print_board(board) + x = board.size + y = board.first.size + x.times do |x| + puts board[board.size - x - 1].join("") + end + end From 2317cd6757e7ab0420803a48a95705f81b0552e4 Mon Sep 17 00:00:00 2001 From: Vasanth Pappu Date: Tue, 16 Sep 2014 21:51:32 -0400 Subject: [PATCH 05/12] enforce boundaries --- VPs-Game2/model.rb | 52 ++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/VPs-Game2/model.rb b/VPs-Game2/model.rb index 5bf0d0d..a4cf319 100644 --- a/VPs-Game2/model.rb +++ b/VPs-Game2/model.rb @@ -1,40 +1,42 @@ require_relative 'view' -module Model - BOARD = 8.times.map do |i| - 8.times.map do |j| - 'X'.red - end - end - - XPOS = 0 - YPOS = 0 - - class << self - +class Model + attr_accessor :board, :xpos, :ypos, :xmax, :ymax + def initialize + @xmax = 8 + @ymax = 8 + @board = @xmax.times.map do |i| + @ymax.times.map do |j| + 'X'.red + end + end + @xpos = 0 + @ypos = 0 + end - def move(dir) - case dir - when :up - YPOS += 1 - when :down - YPOS -= 1 - when :left - XPOS -= 1 - when :right - XPOS += 1 - end - View::moved(BOARD, XPOS, YPOS) + def move(dir) + @board[@xpos][@ypos] = 'X'.red + 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 + + View::moved(@board, @ypos, @xpos, dir) + end - end end From c8e65dab38c674c8e9d0f31e9ff0fad96e5ebfe8 Mon Sep 17 00:00:00 2001 From: Vasanth Pappu Date: Tue, 16 Sep 2014 21:53:14 -0400 Subject: [PATCH 06/12] remove orig file --- VPs-Game2/MVC.rb | 62 ------------------------------------------------ 1 file changed, 62 deletions(-) delete mode 100644 VPs-Game2/MVC.rb diff --git a/VPs-Game2/MVC.rb b/VPs-Game2/MVC.rb deleted file mode 100644 index 47a5afe..0000000 --- a/VPs-Game2/MVC.rb +++ /dev/null @@ -1,62 +0,0 @@ -require 'pry' -require 'colorize' - -INITIAL = < Date: Tue, 16 Sep 2014 22:01:53 -0400 Subject: [PATCH 07/12] clear screen after each input and reprint --- VPs-Game2/controller.rb | 2 +- VPs-Game2/model.rb | 2 +- VPs-Game2/view.rb | 10 ++++++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/VPs-Game2/controller.rb b/VPs-Game2/controller.rb index 923d6e0..6897a9b 100644 --- a/VPs-Game2/controller.rb +++ b/VPs-Game2/controller.rb @@ -11,8 +11,8 @@ class << self def run! - View::initial model = Model.new + View::initial(model.board) loop do diff --git a/VPs-Game2/model.rb b/VPs-Game2/model.rb index a4cf319..d518632 100644 --- a/VPs-Game2/model.rb +++ b/VPs-Game2/model.rb @@ -18,7 +18,7 @@ def initialize end def move(dir) - @board[@xpos][@ypos] = 'X'.red + @board[@ypos][@xpos] = 'X'.red case dir when :up @ypos = ( @ypos + 1 == @ymax ? @ypos : @ypos + 1 ) diff --git a/VPs-Game2/view.rb b/VPs-Game2/view.rb index 62efdb6..7d20cdc 100644 --- a/VPs-Game2/view.rb +++ b/VPs-Game2/view.rb @@ -15,9 +15,10 @@ module View class << self - def initial + def initial(board) msg = "MOVE WITH WASD" puts INITIAL.sub(/F.*/, msg) + print_board(board, 0) end @@ -27,7 +28,8 @@ def moved(board, xpos, ypos, dir) puts "moved #{dir}" end - def print_board(board) + def print_board(board, lines = 10) + cls(lines) x = board.size y = board.first.size x.times do |x| @@ -35,6 +37,10 @@ def print_board(board) end end + def cls(lines = 10) + puts "\n" * lines + end + From 83dccd21afcede89e5e5965588fe573de86a9fa8 Mon Sep 17 00:00:00 2001 From: Vasanth Pappu Date: Thu, 18 Sep 2014 18:36:01 -0400 Subject: [PATCH 08/12] made two layers of the board --- VPs-Game2/model.rb | 70 ++++++++++++++++++++++++++++++++++++++++++---- VPs-Game2/view.rb | 2 +- 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/VPs-Game2/model.rb b/VPs-Game2/model.rb index d518632..77d826b 100644 --- a/VPs-Game2/model.rb +++ b/VPs-Game2/model.rb @@ -1,7 +1,14 @@ require_relative 'view' class Model - attr_accessor :board, :xpos, :ypos, :xmax, :ymax + attr_accessor :board, + :xpos, + :ypos, + :xmax, + :ymax, + :word, + :is_empty + def initialize @xmax = 8 @@ -9,16 +16,69 @@ def initialize @board = @xmax.times.map do |i| @ymax.times.map do |j| - 'X'.red + {text: 'X'.red, + shale: nil} end end @xpos = 0 @ypos = 0 + + @word = "hidesay" + @is_empty = ->(x,y){@board[x][y][:shale].nil?} + + @surface = -> (){map_square{|s| s[:text]}} + @wordloc = intersperse_word(@word) + 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.yellow + end + end + + def move(dir) - @board[@ypos][@xpos] = 'X'.red + + @board[@ypos][@xpos][:text] = 'X'.red case dir when :up @ypos = ( @ypos + 1 == @ymax ? @ypos : @ypos + 1 ) @@ -29,8 +89,8 @@ def move(dir) when :right @xpos = ( @xpos + 1 == @xmax ? @xpos : @xpos + 1 ) end - - View::moved(@board, @ypos, @xpos, dir) + + View::moved(@surface.(), @ypos, @xpos, dir) end diff --git a/VPs-Game2/view.rb b/VPs-Game2/view.rb index 7d20cdc..1245471 100644 --- a/VPs-Game2/view.rb +++ b/VPs-Game2/view.rb @@ -3,7 +3,7 @@ module View INITIAL = < Date: Thu, 18 Sep 2014 19:34:23 -0400 Subject: [PATCH 09/12] say the letter when passed over --- VPs-Game2/model.rb | 31 ++++++++++++++++++++++++++----- VPs-Game2/view.rb | 18 +++++++++++++++++- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/VPs-Game2/model.rb b/VPs-Game2/model.rb index 77d826b..144445b 100644 --- a/VPs-Game2/model.rb +++ b/VPs-Game2/model.rb @@ -16,19 +16,36 @@ def initialize @board = @xmax.times.map do |i| @ymax.times.map do |j| - {text: 'X'.red, - shale: nil} + { + text: 'X'.red, + shale: nil, + cursor: nil, + } end end @xpos = 0 @ypos = 0 + @board[@xpos][@ypos][:cursor] = :right @word = "hidesay" @is_empty = ->(x,y){@board[x][y][:shale].nil?} - @surface = -> (){map_square{|s| s[:text]}} - @wordloc = intersperse_word(@word) + @surface = -> (){ + map_square{|s| + if s[:cursor] + if s[:shale] + s[:shale] + else + s[:cursor] + end + else + s[:text] + end + } + } + + intersperse_word(@word) end @@ -78,7 +95,7 @@ def intersperse_word(word) def move(dir) - @board[@ypos][@xpos][:text] = 'X'.red + @board[@ypos][@xpos][:cursor] = nil case dir when :up @ypos = ( @ypos + 1 == @ymax ? @ypos : @ypos + 1 ) @@ -89,6 +106,10 @@ def move(dir) when :right @xpos = ( @xpos + 1 == @xmax ? @xpos : @xpos + 1 ) end + @board[@ypos][@xpos][:cursor] = :right + if !@board[@ypos][@xpos][:shale].nil? + View::sayit(@board[@ypos][@xpos][:shale]) + end View::moved(@surface.(), @ypos, @xpos, dir) end diff --git a/VPs-Game2/view.rb b/VPs-Game2/view.rb index 1245471..62f9c22 100644 --- a/VPs-Game2/view.rb +++ b/VPs-Game2/view.rb @@ -18,12 +18,13 @@ class << self def initial(board) msg = "MOVE WITH WASD" puts INITIAL.sub(/F.*/, msg) + interpret_board(board) print_board(board, 0) end def moved(board, xpos, ypos, dir) - board[xpos][ypos] = 'O'.green + interpret_board(board) print_board(board) puts "moved #{dir}" end @@ -37,12 +38,27 @@ def print_board(board, lines = 10) 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 From 5b9681129ed599c6c7eef0a18fd09d589a84bbf6 Mon Sep 17 00:00:00 2001 From: Vasanth Pappu Date: Thu, 18 Sep 2014 19:40:50 -0400 Subject: [PATCH 10/12] threaded for speed --- VPs-Game2/model.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/VPs-Game2/model.rb b/VPs-Game2/model.rb index 144445b..bcff33d 100644 --- a/VPs-Game2/model.rb +++ b/VPs-Game2/model.rb @@ -35,7 +35,7 @@ def initialize map_square{|s| if s[:cursor] if s[:shale] - s[:shale] + s[:shale].yellow else s[:cursor] end @@ -88,7 +88,7 @@ def get_a_random_empty_square def intersperse_word(word) word.each_char do |c| x,y = get_a_random_empty_square - @board[x][y][:shale] = c.upcase.yellow + @board[x][y][:shale] = c.upcase end end @@ -108,7 +108,9 @@ def move(dir) end @board[@ypos][@xpos][:cursor] = :right if !@board[@ypos][@xpos][:shale].nil? - View::sayit(@board[@ypos][@xpos][:shale]) + Thread.new do + View::sayit(@board[@ypos][@xpos][:shale]) + end end View::moved(@surface.(), @ypos, @xpos, dir) From 5d02125d69adac8deabda7e640f77caddff75adb Mon Sep 17 00:00:00 2001 From: Vasanth Pappu Date: Thu, 18 Sep 2014 20:21:25 -0400 Subject: [PATCH 11/12] clearer display of win condition --- VPs-Game2/model.rb | 42 ++++++++++++++++++++++++++++++++++++++++-- VPs-Game2/view.rb | 18 +++++++++++++++--- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/VPs-Game2/model.rb b/VPs-Game2/model.rb index bcff33d..d88cb38 100644 --- a/VPs-Game2/model.rb +++ b/VPs-Game2/model.rb @@ -7,7 +7,8 @@ class Model :xmax, :ymax, :word, - :is_empty + :is_empty, + :order def initialize @@ -28,7 +29,9 @@ def initialize @ypos = 0 @board[@xpos][@ypos][:cursor] = :right - @word = "hidesay" + @word = "hidesay".upcase + @word = "cat".upcase + @word = "apple".upcase @is_empty = ->(x,y){@board[x][y][:shale].nil?} @surface = -> (){ @@ -44,8 +47,19 @@ def initialize end } } + @won = -> (){ + map_square{|s| + if s[:shale] + s[:shale].green + else + ' ' + end + } + } intersperse_word(@word) + + @order = "" end @@ -107,16 +121,40 @@ def move(dir) @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 diff --git a/VPs-Game2/view.rb b/VPs-Game2/view.rb index 62f9c22..c163709 100644 --- a/VPs-Game2/view.rb +++ b/VPs-Game2/view.rb @@ -16,8 +16,9 @@ module View class << self def initial(board) - msg = "MOVE WITH WASD" - puts INITIAL.sub(/F.*/, msg) + 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 @@ -26,7 +27,11 @@ def initial(board) def moved(board, xpos, ypos, dir) interpret_board(board) print_board(board) - puts "moved #{dir}" + #puts "moved #{dir}" + end + + def print_win_condition(board) + print_board(board) end def print_board(board, lines = 10) @@ -60,6 +65,13 @@ 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 From 2a5ea8fef8c9387847248d6fc138b52d3deea775 Mon Sep 17 00:00:00 2001 From: Vasanth Pappu Date: Thu, 18 Sep 2014 20:24:34 -0400 Subject: [PATCH 12/12] added todos --- VPs-Game2/README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 VPs-Game2/README.md 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