Code Java du chifoumi

/*
* Copyright (c) Régis Clouard
* ENSICAEN, École publique d'ingénieurs et centres de recherche, Caen, FRANCE.
* https://www.ensicaen.fr
*
* This file is licenced under the MIT License.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package fr.ensicaen.gl2a;

import java.util.Random;

/*
* Ceci est un exemple de multiple dispatching. La solution est proche du patron Visiteur.
* Il suffit de remplacer eval() by visit() pour retrouver le patron Visiteur.
*/

public final class Chifoumi {
static final int SIZE = 20;

public static void main( String[] args ) {
int score1 = 0;
int score2 = 0;
for (int i = 0; i < SIZE; i++) {
Item a = ItemGenerator.newItem();
Item b = ItemGenerator.newItem();
Outcome outcome = a.compete(b);
System.out.println(a + " " + outcome + " vs. " + b);
switch (outcome) {
case WIN:
score1++;
case LOSE:
score2++;
}
}
System.out.format("\\nRésutat final \nscore du joueur 1 : %d\\nscore du joueur 2 : %d\\n", score1, score2);
}
}

enum Outcome {WIN, LOSE, DRAW;}

class ItemGenerator {
private static Random rand = new Random();

public static Item newItem() {
switch (rand.nextInt(3)) {
default:
case 0:
return new Scissors();
case 1:
return new Paper();
case 2:
return new Rock();
}
}
}

interface Item {
public Outcome compete( Item it );

public Outcome eval( Paper p );

public Outcome eval( Scissors s );

public Outcome eval( Rock r );
}

final class Paper implements Item {
public Outcome compete( Item it ) {
return it.eval(this);
}

public Outcome eval( Paper p ) {
return Outcome.DRAW;
}

public Outcome eval( Scissors s ) {
return Outcome.WIN;
}

public Outcome eval( Rock r ) {
return Outcome.LOSE;
}

public String toString() {
return "Paper";
}
}

final class Scissors implements Item {
public Outcome compete( Item it ) {
return it.eval(this);
}

public Outcome eval( Paper p ) {
return Outcome.LOSE;
}

public Outcome eval( Scissors s ) {
return Outcome.DRAW;
}

public Outcome eval( Rock r ) {
return Outcome.WIN;
}

public String toString() {
return "Scissors";
}
}

final class Rock implements Item {
public Outcome compete( Item it ) {
return it.eval(this);
}

public Outcome eval( Paper p ) {
return Outcome.WIN;
}

public Outcome eval( Scissors s ) {
return Outcome.LOSE;
}

public Outcome eval( Rock r ) {
return Outcome.DRAW;
}

public String toString() {
return "Rock";
}
}
Modifié le: Thursday 9 November 2017, 07:58