Skip to content

Commit

Permalink
Finalizacao do programa.
Browse files Browse the repository at this point in the history
Implementacao da biblioteca terminal-table para formatar a saida do jogo.
  • Loading branch information
fkfouri committed Dec 21, 2017
1 parent d14c6f6 commit 9b1d37f
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 119 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode
/.vscode
64 changes: 0 additions & 64 deletions .vscode/launch.json

This file was deleted.

9 changes: 9 additions & 0 deletions Gemfile
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"
19 changes: 14 additions & 5 deletions game.rb → Minesweeper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require 'matrix'

#http://www.programering.com/a/MTNykzMwATA.html
class Minesweeper
def initialize(width, height, num_mines, test = nil)
@width = width
Expand Down Expand Up @@ -84,13 +83,15 @@ def quantityFields
return @elements.length
end

def board_state
outStruct = Struct.new(:width, :height, :elements, :board_format)
def board_state(input = nil)
outStruct = Struct.new(:width, :height, :elements, :board_format, :input, :stillPlaying)
out = outStruct.new
out.width = @width
out.height = @height
out.elements = @elements
out.board_format = {unknown_cell: '.', clear_cell: '', bomb: '#', flag: 'F'}
out.input = input
out.stillPlaying = still_playing
return out# @elements
end

Expand Down Expand Up @@ -160,14 +161,23 @@ def victory
return false
end
end

def victory?
return victory
end

#retorna true (ainda em jogo), false (game over por derrota ou por vitoria)
def still_playing
if victory == true
@stillPlaying = false
end
return @stillPlaying
end
end

def still_playing?
return still_playing
end


#responsavel por abrir uma area de Zeros a partir do ponto selecionado
private
Expand All @@ -192,5 +202,4 @@ def openArea(x, y)
end
end


end
41 changes: 32 additions & 9 deletions PrettyPrinter.rb
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
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,28 @@ Os seguintes métodos foram implementados:
- `showValue()` Exibe o resultado da ultima ação válida
- `victory()` Exibe uma saída booleana se o jogador venceu a partida
- `still_playing()` Exibe uma saída booleana informando se a partida ainda está acontendo
- `board_state()` Esta é uma saída que retorna uma representação atual do tabuleiro.
- `board_state()` Esta é uma saída que retorna uma representação atual do tabuleiro. Este método aceita um pedido de raio-X somente se o jogo terminar. `board_state({xray: true})`

## Exemplo de Uso

```
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
```
37 changes: 35 additions & 2 deletions gameTest.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require_relative 'game'
require_relative 'Minesweeper'
require_relative 'PrettyPrinter'
require 'test/unit'
require 'test/unit/ui/console/testrunner'

#teste unitario
class GameTest #< Test::Unit::TestCase
class GameTest < Test::Unit::TestCase
#attr_reader :name

def start
Expand Down Expand Up @@ -74,6 +74,39 @@ def test_50_Victory
assert_equal(true, g.victory) # Vitoria do Jogador
assert_equal(false, g.still_playing) # acabou o jogo, nao continua jogando
end


=begin
def dd
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(1,1)
puts printer.print(g.board_state)
puts "======================================================="
g.flag(0,5)
g.play(1,5)
g.play(0,0)
g.play(0,1)
g.play(0,2)
g.play(1,0)
puts printer.print(g.board_state({xray: true}))
puts g.victory
end
=end


end

#my_tests = Test::Unit::TestSuite.new("Teste do MinesWeeper")
Expand Down
59 changes: 22 additions & 37 deletions main.rb
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
2 changes: 1 addition & 1 deletion simplePrinter.rb
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

0 comments on commit 9b1d37f

Please sign in to comment.