/*
From the command line
1) create a directory scissors
2) cd into directory scissors
3) save this source code as main.nice in directory scissors
4) compile the source code
\scissors> nicec --sourcepath=.. -a scissors.jar scissors
5) run the jar file
\scissors> java -jar scissors.jar
*/
abstract class GameMove {}
class Scissors extends GameMove {}
class Paper extends GameMove {}
class Rock extends GameMove {}
String plays(GameMove m1, GameMove m2){
if (m1 == m2) return m1 + " draws " + m2;
else return m2.plays(m1);
}
plays(Scissors m1, Paper m2) = "Scissors cut Paper";
plays(Paper m1, Rock m2) = "Paper wraps Rock";
plays(Scissors m1, Rock m2) = "Rock breaks Scissors";
toString(Scissors m)="Scissors";
toString(Paper m)="Paper";
toString(Rock m)="Rock";
void main(String[] args){
let scissors = new Scissors();
let rock = new Rock();
let paper = new Paper();
println( rock.plays(scissors) );
println( rock.plays(paper) );
println( scissors.plays(paper) );
println( scissors.plays(scissors) );
}
/* Notes - language
abstract class GameMove {}
Declare an abstract class and 3 subclasses: Scissors, Paper, Rock.
String plays(GameMove m1, GameMove m2){
Declare a multimethod with an implementation - if the moves are
the same it's a draw, if the moves are different then there
should be a more specific implementation so reverse the parameters
and try again (double dispatch).
plays(Scissors m1, Paper m2) = "Scissors cut Paper";
This implementation will be selected when the first parameter value
is an instance of Scissors and the second parameter value is an
instance of Paper (or a subclass).
plays(Paper m1, Rock m2) = "Paper wraps Rock";
This implementation will be selected when the first parameter value
is an instance of Paper and the second parameter value is an
instance of Rock (or a subclass).
plays(Scissors m1, Rock m2) = "Rock breaks Scissors";
This implementation will be selected when the first parameter value
is an instance of Scissors and the second parameter value is an
instance of Rock (or a subclass).
We could implement the game using value dispatch.
Can you rewrite Scissors, Paper, Rock; using an enum,
or int constants or string literals?
*/
/* Notes - Compile & Run
The Nice compiler compiles PACKAGES (directories) not files,
so we must tell the compiler which directory contains the
package directory (scissors) not which directory contains
the file (main.nice).
\projects> nicec scissors
OK
\projects\scissors> nicec --sourcepath=.. scissors
OK
\projects\scissors> nicec scissors
FAILS! the compiler will try to find \projects\scissors\scissors
Let's make the compiler output to a jar file (scissors.jar):
\projects\scissors> nicec --sourcepath=.. -a scissors.jar scissors
nice.lang: parsing
scissors: parsing
scissors: typechecking
scissors: generating code
scissors: linking
scissors: writing in archive
nice.lang: writing in archive
Let's run the program:
\projects\scissors> java -jar scissors.jar
Rock breaks Scissors
Paper wraps Rock
Scissors cut Paper
Scissors draws Scissors
*/
-- IsaacGouy - 10 Feb 2004
| Topic ScissorsPaperRockNoviceExample . { Edit | Attach | Ref-By | Printable | Diffs | r1.6 | > | r1.5 | > | r1.4 | More } |
|
Revision r1.6 - 28 Apr 2005 - 11:55 GMT - TWikiGuest Parents: WebHome > CodeExamples |
Copyright © 1999-2003 by the contributing authors.
All material on this collaboration platform is the property of the contributing authors. Ideas, requests, problems regarding TWiki? Send feedback. |