-
-
Save anonymous/994bb95bffbb451e9c03 to your computer and use it in GitHub Desktop.
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.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