Created
November 12, 2010 06:50
-
-
Save arnab/673815 to your computer and use it in GitHub Desktop.
The number guess game in Common Lisp (from Land of Lisp), to compare to the Ruby implementation
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
(defparameter *small* 1) | |
(defparameter *big* 100) | |
(defun guess-my-number () | |
(ash (+ *small* *big*) -1)) | |
(defun smaller () | |
(setf *big* (1- (guess-my-number))) | |
(guess-my-number)) | |
(defun bigger () | |
(setf *small* (1+ (guess-my-number))) | |
(guess-my-number)) | |
(defun start-over () | |
(setf *small* 1) | |
(setf *big* 100) | |
(guess-my-number)) |
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
def start_over | |
@small, @big = 1, 100 | |
guess | |
end | |
def guess | |
(@small + @big) >> 1 | |
end | |
def smaller | |
@big = guess + 1 | |
guess | |
end | |
def bigger | |
@small = guess - 1 | |
guess | |
end | |
start_over |
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
import java.io.Console; | |
public class NumberGuess { | |
private int small; | |
private int big; | |
private int guess; | |
private void guess(){ | |
this.guess = nextGuess(); | |
String verdict = System.console().readLine(guess + ": smaller/bigger/start-over? "); | |
if ( "smaller".equals(verdict) ) { | |
smaller(); | |
} else if ( "bigger".equals(verdict) ) { | |
bigger(); | |
} else if ( "start-over".equals(verdict) ) { | |
startOver(); | |
} else { | |
throw new IllegalArgumentException("Don't know " + verdict); | |
} | |
} | |
private void startOver(){ | |
this.small = 1; | |
this.big = 100; | |
guess(); | |
} | |
private void smaller(){ | |
this.big = nextGuess() - 1; | |
guess(); | |
} | |
private void bigger(){ | |
this.small = nextGuess() + 1; | |
guess(); | |
} | |
private int nextGuess(){ | |
return ( this.small + this.big ) >> 1; | |
} | |
public static void main(String[] args){ | |
(new NumberGuess()).startOver(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment