Created
February 24, 2011 18:31
-
-
Save jeantil/842621 to your computer and use it in GitHub Desktop.
dojo softeam 1
This file contains hidden or 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
| class Solver | |
| attr_reader :stones | |
| def initialize(heap=0) | |
| @win=false | |
| @stones=heap | |
| end | |
| def user_wins? | |
| @win | |
| end | |
| def play(take) | |
| if (take!=2 or @stones==2) | |
| @win=true | |
| end | |
| @stones -= take | |
| if @stones % 4 == 0 | |
| @stones -=3 | |
| else | |
| @stones -= 4-take | |
| end | |
| if @stones>=4 | |
| @win=false | |
| end | |
| end | |
| end |
This file contains hidden or 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
| require 'test/unit' | |
| require 'shoulda' | |
| require 'nimm' | |
| class TestNimm < Test::Unit::TestCase | |
| context "a Nimm solver" do | |
| should "assert false" do | |
| assert !false | |
| end | |
| should "win when there are no stones" do | |
| nim = Solver.new(0) | |
| assert !nim.user_wins? | |
| end | |
| should "lose when there is one stone" do | |
| nim = Solver.new(1) | |
| nim.play(1) | |
| assert nim.user_wins? | |
| end | |
| should "win when there are 4 stones and user takes 3" do | |
| nim = Solver.new(4) | |
| nim.play(3) | |
| assert !nim.user_wins? | |
| end | |
| should "lose when there are 5 stones and the players takes 1 twice" do | |
| nim = Solver.new(5) | |
| nim.play(1) | |
| nim.play(1) | |
| assert nim.user_wins? | |
| end | |
| should "win when there are 5 stones and the players takes 2 stones" do | |
| nim = Solver.new(5) | |
| nim.play(2) | |
| assert !nim.user_wins? | |
| end | |
| should "lose when there are 2 stones and the players takes 2 stones" do | |
| nim = Solver.new(2) | |
| nim.play(2) | |
| assert nim.user_wins? | |
| end | |
| should "show the number of remaining stones" do | |
| nim = Solver.new(4) | |
| assert_equal 4,nim.stones | |
| end | |
| should "show 0 stones when there are 4 stones and user takes 3" do | |
| nim= Solver.new(4) | |
| nim.play(3) | |
| assert_equal 0, nim.stones | |
| end | |
| should "show 1 stone when there are 5 stones and the players takes 1" do | |
| nim = Solver.new(5) | |
| nim.play(1) | |
| assert_equal 1, nim.stones | |
| end | |
| should "show 1 stone when there are 6 stones and the players takes 2" do | |
| nim = Solver.new(6) | |
| nim.play(2) | |
| assert_equal 1, nim.stones | |
| end | |
| should "not consider the user has won when there are 6 stones and the user takes one" do | |
| nim = Solver.new(6) | |
| nim.play(1) | |
| assert !nim.user_wins? | |
| end | |
| end | |
| end | |
| # nim = Nimm.new(10) | |
| # nim.play(2) => nim.win? =false nim.heap=5 | |
| # nim.play(1) => nim.win? =false nim.heap=1 | |
| # nim.play(1) => nim.win? =true nim.heap=0 | |
| # assert nim.win? | |
| # nim = Nimm.new(10) | |
| # nim.play(1) => nim.win? =false nim.heap=8 | |
| # nim.play(3) => nim.win? =false nim.heap=4 | |
| # nim.play(2) => nim.win? =false nim.heap=0 | |
| # assert !nim.win? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment