diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..59eff20 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode +/.vscode diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 1bf1540..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,64 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "name": "Debug Local File", - "type": "Ruby", - "request": "launch", - "cwd": "${workspaceRoot}", - "program": "${workspaceRoot}/main.rb" - }, - { - "name": "Listen for rdebug-ide", - "type": "Ruby", - "request": "attach", - "cwd": "${workspaceRoot}", - "remoteHost": "127.0.0.1", - "remotePort": "1234", - "remoteWorkspaceRoot": "${workspaceRoot}" - }, - { - "name": "Rails server", - "type": "Ruby", - "request": "launch", - "cwd": "${workspaceRoot}", - "program": "${workspaceRoot}/bin/rails", - "args": [ - "server" - ] - }, - { - "name": "RSpec - all", - "type": "Ruby", - "request": "launch", - "cwd": "${workspaceRoot}", - "program": "${workspaceRoot}/bin/rspec", - "args": [ - "-I", - "${workspaceRoot}" - ] - }, - { - "name": "RSpec - active spec file only", - "type": "Ruby", - "request": "launch", - "cwd": "${workspaceRoot}", - "program": "${workspaceRoot}/bin/rspec", - "args": [ - "-I", - "${workspaceRoot}", - "${file}" - ] - }, - { - "name": "Cucumber", - "type": "Ruby", - "request": "launch", - "cwd": "${workspaceRoot}", - "program": "${workspaceRoot}/bin/cucumber" - } - ] -} \ No newline at end of file diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..a1d9b5c --- /dev/null +++ b/Gemfile @@ -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" diff --git a/game.rb b/Minesweeper.rb similarity index 95% rename from game.rb rename to Minesweeper.rb index f3a099c..2ab34ac 100644 --- a/game.rb +++ b/Minesweeper.rb @@ -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 @@ -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 @@ -160,6 +161,10 @@ 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 @@ -167,7 +172,12 @@ def still_playing @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 @@ -192,5 +202,4 @@ def openArea(x, y) end end - end \ No newline at end of file diff --git a/PrettyPrinter.rb b/PrettyPrinter.rb index d3af655..4d13257 100644 --- a/PrettyPrinter.rb +++ b/PrettyPrinter.rb @@ -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 \ No newline at end of file diff --git a/README.md b/README.md index 5876f0a..ebc0e50 100644 --- a/README.md +++ b/README.md @@ -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. \ No newline at end of file +- `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 +``` \ No newline at end of file diff --git a/gameTest.rb b/gameTest.rb index 272749e..73fbdf6 100644 --- a/gameTest.rb +++ b/gameTest.rb @@ -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 @@ -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") diff --git a/main.rb b/main.rb index 79392b8..1522edf 100644 --- a/main.rb +++ b/main.rb @@ -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 \ No newline at end of file diff --git a/simplePrinter.rb b/simplePrinter.rb index c1adba2..95b6891 100644 --- a/simplePrinter.rb +++ b/simplePrinter.rb @@ -1,7 +1,7 @@ class SimplePrinter def print(board_state) - return board_state.board_format + puts board_state.board_format end end \ No newline at end of file