Skip to content

Instantly share code, notes, and snippets.

/Chessboard.java Secret

Created February 12, 2012 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/994bb95bffbb451e9c03 to your computer and use it in GitHub Desktop.
Save anonymous/994bb95bffbb451e9c03 to your computer and use it in GitHub Desktop.
import java.util.Arrays;
import java.util.Random;
/**
* Created by IntelliJ IDEA.
* User: ASUS
* Date: 12.02.12
* Time: 15:11
* To change this template use File | Settings | File Templates.
*/
interface Pieces { void move(); }
class King implements Pieces {
public void move() { System.out.println("King.move()"); }
public String toString() { return "King"; }
}
class Queen implements Pieces {
public void move() { System.out.println("Queen.move()"); }
public String toString() { return "Queen"; }
}
class Knight implements Pieces {
public void move() { System.out.println("Knight()"); }
public String toString() { return "Knight"; }
}
class Gen {
private static Random random = new Random();
static Pieces choice() {
switch(random.nextInt(3)) {
case 0 : return new King();
case 1 : return new Queen();
case 2 : return new Knight();
default : return null;
}
}
}
public class Chessboard{
static void moveAll(Pieces[] pieces) {
for(Pieces p : pieces)
p.move();
}
public static void main(String[] args) {
Pieces[] pieces = new Pieces[20];
for(int i = 0; i < pieces.length; i++)
pieces[i] = Gen.choice();
System.out.println("All pieces on a chessboard: " + Arrays.toString(pieces));
moveAll(pieces);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment