Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Driver {
- public static void main(String args[]) {
- Piece player1 = new Piece('o');
- Piece player2 = new Piece('x');
- Piece player3 = new Piece('v');
- Piece players[] = {player1, player2, player3};
- Game game = new Game(players);
- game.board.printBoard();
- System.out.println("Type 1, 2, or 3 to move a piece, type q to quit.");
- Scanner scan = new Scanner (System.in);
- boolean running = true;
- while(running) {
- String input = scan.next();
- if(input.equals("1")){
- game.movePiece(player1);
- }else if(input.equals("2")){
- game.movePiece(player2);
- }else if(input.equals("3")){
- game.movePiece(player3);
- }
- game.board.printBoard();
- if(game.hasWinner) {
- System.out.println(game.winner.icon + " wins!");
- running=false;
- }
- if(input.equals("q")) {
- running = false;
- }
- }
- }
- }
- public class Board {
- int size; //board is square, size in both dimensions
- Piece tile[][]; //holds pieces, spots are null when empty
- public Board(int size) {
- this.size = size;
- tile = new Piece[size][size];
- }
- public void printBoard() {
- //print the column header
- System.out.print(" ");
- for(int col=0; col<size; col++) {
- System.out.print(col + " ");
- }
- System.out.println();
- //print the board and row header
- for(int row=0; row<size; row++) {
- System.out.print(row + " ");
- for(int col=0; col<size; col++) {
- if(tile[row][col]==null) {
- System.out.print("- ");
- }else {
- System.out.print(tile[row][col].icon + " ");
- }
- }
- System.out.println();
- }
- System.out.println();
- }
- }
- public class Game {
- public Piece player;
- public Board board;
- public int players;
- public Game(Piece player[]) {
- board= new Board(8);
- this.player[1]=player[players];
- for(int i=0; i<player.length;i++) {
- player[0]=board.tile[0][0];
- }
- }
- public void movePiece(Piece player) {
- this.player= player;
- }
- }
- public class Piece {
- char icon;
- int x;
- int y;
- public Piece(char icon) {
- this.icon = icon;
- }
- }
Add Comment
Please, Sign In to add comment