-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementacao da biblioteca terminal-table para formatar a saida do jogo.
- Loading branch information
Showing
9 changed files
with
140 additions
and
119 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.vscode | ||
/.vscode |
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
source "https://rubygems.org" | ||
|
||
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } | ||
|
||
gem 'test-unit' | ||
gem 'terminal-table', '~> 1.8' | ||
# gem "rails" |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,44 @@ | ||
require 'matrix' | ||
require 'terminal-table' | ||
|
||
class PrettyPrinter | ||
|
||
def print(board_state) | ||
show = "value" | ||
#show = "value" | ||
show = "show" | ||
#show = "flag" | ||
#show = "find" | ||
#matrix = Matrix.build(board_state.width, board_state.height) {|x, y| board_state.elements.has_key?("#{x}-#{y}")? (board_state.elements["#{x}-#{y}"]["tipo"]!="bomb"? board_state.elements["#{x}-#{y}"][show].to_s.rjust(3).ljust(6) : "bomb".rjust(3).ljust(6)) : 88 } | ||
#matrix = Matrix.build(board_state.width, board_state.height) {|x, y| (board_state.elements["#{x}-#{y}"]["flag"]!=1? board_state.elements["#{x}-#{y}"][show].to_s.rjust(3).ljust(6) : "F".to_s.rjust(3).ljust(6)) } | ||
matrix = Matrix.build(board_state.width, board_state.height) {|x, y| board_state.elements["#{x}-#{y}"][show].to_s.rjust(3).ljust(6) } | ||
|
||
out =[] | ||
for i in 0..matrix.column_count-1 | ||
out.push matrix.column(i) | ||
|
||
#http://www.programering.com/a/MTNykzMwATA.html | ||
matrix = '' | ||
|
||
|
||
#Exibe xray se o jogo tiver terminado | ||
if board_state.input != nil and board_state.input[:xray] == true and board_state.stillPlaying == false | ||
matrix = Matrix.build(board_state.width, board_state.height) {|x, y| board_state.elements["#{x}-#{y}"]["tipo"]!="bomb"? board_state.elements["#{x}-#{y}"][show].to_s.rjust(3).ljust(5) : '#'.rjust(3).ljust(5) } | ||
else | ||
|
||
#matrix = Matrix.build(board_state.width, board_state.height) {|x, y| board_state.elements.has_key?("#{x}-#{y}")? (board_state.elements["#{x}-#{y}"]["tipo"]!="bomb"? board_state.elements["#{x}-#{y}"][show].to_s.rjust(3).ljust(6) : "bomb".rjust(3).ljust(6)) : 88 } | ||
#matrix = Matrix.build(board_state.width, board_state.height) {|x, y| (board_state.elements["#{x}-#{y}"]["flag"]!=1? board_state.elements["#{x}-#{y}"][show].to_s.rjust(3).ljust(5) : "F".to_s.rjust(3).ljust(5)) } | ||
matrix = Matrix.build(board_state.width, board_state.height) {|x, y| board_state.elements["#{x}-#{y}"][show].to_s.rjust(3).ljust(5) } | ||
end | ||
|
||
## Fomato tipo Vetor | ||
#outVector =[] | ||
#for i in 0..matrix.column_count-1 | ||
# outVector.push matrix.column(i) | ||
#end | ||
#puts outVector | ||
|
||
#https://github.com/tj/terminal-table | ||
table = Terminal::Table.new do |t| | ||
t.style = {:all_separators => true} | ||
for i in 0..matrix.column_count-1 | ||
t.add_row matrix.column(i).to_a #line | ||
#puts line | ||
end | ||
end | ||
return out | ||
puts table | ||
end | ||
|
||
end |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,34 @@ | ||
require_relative 'game' | ||
require_relative 'Minesweeper' | ||
require_relative 'PrettyPrinter' | ||
require_relative 'simplePrinter' | ||
require_relative 'gameTest' | ||
|
||
=begin | ||
|
||
width, height, num_mines = 10, 6, 10 | ||
=begin | ||
width, height, num_mines = 6, 10, 10 | ||
myGame = Minesweeper.new(width, height, num_mines) | ||
printer = PrettyPrinter.new | ||
#puts myGame.board_state | ||
puts printer.print(myGame.board_state) | ||
=end | ||
my_tests = GameTest.new | ||
g = my_tests.start | ||
#printer = SimplePrinter.new | ||
printer = PrettyPrinter.new | ||
puts printer.print(g.board_state) | ||
puts "=======================================================" | ||
g.play(4,2) | ||
puts printer.print(g.board_state) | ||
puts "=======================================================" | ||
|
||
g.play(4,2) | ||
g.flag(0,5) | ||
g.play(1,5) | ||
|
||
puts printer.print(g.board_state) | ||
puts "=======================================================" | ||
|
||
g.play(0,0) | ||
g.play(0,1) | ||
g.play(0,2) | ||
g.play(1,0) | ||
|
||
puts printer.print(g.board_state) | ||
puts g.victory | ||
#=end | ||
|
||
|
||
#my_tests = Test::Unit::TestSuite.new("Teste do MinesWeeper") | ||
#my_tests << GameTest.new('firstTest') | ||
#my_tests << GameTest.new('test_10') | ||
#my_tests << GameTest.new('test_20') | ||
#my_tests << GameTest.new('test_30') | ||
#Test::Unit::UI::Console::TestRunner.run(my_tests) | ||
|
||
|
||
width, height, num_mines = 6, 6, 10 | ||
game = Minesweeper.new(width, height, num_mines) | ||
|
||
while game.still_playing? | ||
valid_move = game.play(rand(width), rand(height)) | ||
valid_flag = game.flag(rand(width), rand(height)) | ||
if valid_move or valid_flag | ||
printer = (rand > 0.5) ? SimplePrinter.new : PrettyPrinter.new | ||
printer.print(game.board_state) | ||
end | ||
end | ||
|
||
puts "Fim do jogo!" | ||
if game.victory? | ||
puts "Você venceu!" | ||
else | ||
puts "Você perdeu! As minas eram:" | ||
PrettyPrinter.new.print(game.board_state(xray: true)) | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
class SimplePrinter | ||
|
||
def print(board_state) | ||
return board_state.board_format | ||
puts board_state.board_format | ||
end | ||
|
||
end |