| <<O>> Difference Topic ScissorsPaperRockNoviceExample (r1.6 - 28 Apr 2005 - TWikiGuest) |
| <<O>> Difference Topic ScissorsPaperRockNoviceExample (r1.5 - 26 Jan 2005 - LiYan) |
| <<O>> Difference Topic ScissorsPaperRockNoviceExample (r1.4 - 19 Jan 2005 - ArjanB) |
| <<O>> Difference Topic ScissorsPaperRockNoviceExample (r1.3 - 15 Feb 2004 - IsaacGouy) |
| Changed: | |
| < < | class Scissors extends GameMove? { toString()="Scissors"; } class Paper extends GameMove? { toString()="Paper"; } class Rock extends GameMove? { toString()="Rock"; } |
| > > | class Scissors extends GameMove? {} class Paper extends GameMove? {} class Rock extends GameMove? {} |
| Changed: | |
| < < | |
| > > | toString(Scissors m)="Scissors"; toString(Paper m)="Paper"; toString(Rock m)="Rock"; |
| Deleted: | |
| < < | toString()="Paper"; Specialize the toString() implementation for each subclass. |
| <<O>> Difference Topic ScissorsPaperRockNoviceExample (r1.2 - 11 Feb 2004 - IsaacGouy) |
| Changed: | |
| < < | plays(Scissors m1, Rock m2) = "Rock blunts Scissors"; |
| > > | plays(Scissors m1, Rock m2) = "Rock breaks Scissors"; |
| Changed: | |
| < < | plays(Scissors m1, Rock m2) = "Rock blunts Scissors"; |
| > > | plays(Scissors m1, Rock m2) = "Rock breaks Scissors"; |
| Changed: | |
| < < | Rock blunts Scissors |
| > > | Rock breaks Scissors |
| <<O>> Difference Topic ScissorsPaperRockNoviceExample (r1.1 - 10 Feb 2004 - IsaacGouy) |
| Added: | |
| > > |
%META:TOPICINFO{author="IsaacGouy" date="1076450880" format="1.0" version="1.1"}%
%META:TOPICPARENT{name="CodeExamples"}%
/*
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 {
toString()="Scissors";
}
class Paper extends GameMove {
toString()="Paper";
}
class Rock extends GameMove {
toString()="Rock";
}
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 blunts Scissors";
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.
toString()="Paper";
Specialize the toString() implementation for each subclass.
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 blunts 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 blunts Scissors
Paper wraps Rock
Scissors cut Paper
Scissors draws Scissors
*/
-- IsaacGouy - 10 Feb 2004 |
| Topic ScissorsPaperRockNoviceExample . { View | Diffs | r1.6 | > | r1.5 | > | r1.4 | More } |
|
Revision r1.1 - 10 Feb 2004 - 22:08 GMT - IsaacGouy Revision r1.6 - 28 Apr 2005 - 11:55 GMT - TWikiGuest |
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. |