Permalink
Browse files

Have the CLI display a presentable view

  • Loading branch information...
1 parent 43c5f5c commit c7b2692fd4318b7c6759b1dd7abf38d1b4a475fa @JoshCheek committed Aug 3, 2011
Showing with 57 additions and 10 deletions.
  1. +6 −2 lib/ttt/interface/cli.rb
  2. +5 −5 lib/ttt/interface/cli/players.rb
  3. +46 −3 lib/ttt/interface/cli/views.rb
View
@@ -65,9 +65,9 @@ def create_player2
def create_player(letter, position)
type = prompt "#{letter} will go #{position}, would you like #{letter} to be a human or a computer? (h/c) ", :validate => /^[hc]$/i
if type =~ /c/i
- ComputerPlayer.new game, self
+ ComputerPlayer.new game, self, letter
else
- HumanPlayer.new game, self
+ HumanPlayer.new game, self, letter
end
end
@@ -86,6 +86,10 @@ def prompt(message, validation={})
def display_board
Views.new(self).display_board
end
+
+ def board
+ game.board
+ end
end
end
end
@@ -7,16 +7,16 @@ class CLI
class Player
- attr_accessor :game, :cli
+ attr_accessor :game, :cli, :marker
extend Forwardable
def_delegators :cli, :fileout, :filein, :fileerr, :prompt
- def initialize(game, cli)
- self.game = game
- self.cli = cli
+ def initialize(game, cli, marker)
+ self.game = game
+ self.cli = cli
+ self.marker = marker
end
-
end
@@ -7,7 +7,7 @@ class CLI
class Views
attr_accessor :cli
-
+
def initialize(cli)
self.cli = cli
end
@@ -18,8 +18,51 @@ def method_missing(meth, *args, &block)
end
def display_board
- fileout.puts game.board(:ttt)
- fileout.puts
+ fileout.print row(0) << horizontal_separator <<
+ row(1) << horizontal_separator <<
+ row(2) << "\n\n"
+ end
+
+ def row(n)
+ n *= 3
+ line(n) << line(n+1) << line(n+2)
+ end
+
+ def line(n)
+ offset, col = (n/3)*3, (n%3)
+ square(offset+1, col) << vertical_separator <<
+ square(offset+2, col) << vertical_separator <<
+ square(offset+3, col) << "\n"
+ end
+
+ def horizontal_separator
+ "-----|-----|-----\n"
+ end
+
+ def vertical_separator
+ "|"
+ end
+
+ def square(num, line)
+ if line == 0
+ " "
+ elsif line == 1
+ " %s " % char_for(num)
+ elsif
+ line == 2
+ " "
+ end
+ end
+
+ def char_for(position)
+ case game[position]
+ when nil
+ ' '
+ when 1
+ player1.marker
+ when 2
+ player2.marker
+ end
end
end

0 comments on commit c7b2692

Please sign in to comment.