/******************************* MAIN HEADER **********************************
    Title:            Tic Tac Toe, version 1
    Files:            TicTacToe1.java, Rules,class, Player.class,
                      Board.class, Move.class, Mark.class
    
    Author:           James D. Skrentny, skrentny@cs.wisc.edu
                      copyright 2000 all rights reserved
    Course:           CS 302, Fall 2000, Lectures 1 & 2
        
    Compiler:         CodeWarrior IDE 4.0 (JDK 1.2)
    Platform:         Windows NT 4.0
 **************************** 80 columns wide *********************************/

/**
 * This program demonstrates basic concepts of object-oriented programming with
 * the game Tic Tac Toe without using control structures (e.g. if, while).
 *
 * Bugs: none known
 **/

/*
 * The following concepts are illustrated:
 *   -  a main method
 *   -  object reference declarations (e.g. board, playerX, playerO, move, mark)
 *   -  object construction (e.g. new Board())
 *   -  sending messages to objects (e.g. board) and classes (e.g Board, Rules)
 *      - having no arguments or return value (e.g. board.display())
 *      - having arguments but no return value (e.g. Rules.checkWin(board,
 *                                                                  move, mark))
 *      - having no arguments but having a return value (e.g. playerX.getMove())
 *   -  objects as arguments (e.g. move, board)
 *   -  objects as return values (e.g. move, mark)
 */

public class TicTacToe1 {

    public static void main (String[] args) {
        Board     board;
        board   = new Board();
        Player    playerX;
        playerX = new Player("X");
        Player    playerO;
        playerO = new Player("O");
        Move      move;                     // constructed by Player objects
        Mark      mark;                     // constructed by Player objects
        
        // five steps initially required for each move:
        board.display();                            // 1. display board
        move = playerX.getMove();                   // 2. get move
        Rules.checkMove(move, board);               // 3. check move for errors
        mark = playerX.getMark();                   // 4. get player's mark
        board.markMove(move, mark);                 // 5. mark valid move
      
        // alternate between players - now it's O's turn
        board.display();
        move = playerO.getMove();
        Rules.checkMove(move, board);
        mark = playerO.getMark();
        board.markMove(move, mark);
                
        board.display();
        move = playerX.getMove();
        Rules.checkMove(move, board);
        mark = playerX.getMark();
        board.markMove(move, mark);
        
        board.display();
        move = playerO.getMove();
        Rules.checkMove(move, board);
        mark = playerO.getMark();
        board.markMove(move, mark);
        
        // It is now possible for X to win, add 6th step:
        board.display();
        move = playerX.getMove();
        Rules.checkMove(move, board);
        mark = playerX.getMark();
        board.markMove(move, mark);
        Rules.checkWin(board, move, mark); // 6. check for winner
        
        board.display();
        move = playerO.getMove();
        Rules.checkMove(move, board);
        mark = playerO.getMark();
        board.markMove(move, mark);
        Rules.checkWin(board, move, mark);
        
        board.display();
        move = playerX.getMove();
        Rules.checkMove(move, board);
        mark = playerX.getMark();
        board.markMove(move, mark);
        Rules.checkWin(board, move, mark);
        
        board.display();
        move = playerO.getMove();
        Rules.checkMove(move, board);
        mark = playerO.getMark();
        board.markMove(move, mark);
        Rules.checkWin(board, move, mark);
        
        board.display();
        move = playerX.getMove();
        Rules.checkMove(move, board);
        mark = playerX.getMark();
        board.markMove(move, mark);
        Rules.checkWin(board, move, mark);
        
        // at this point there must be a tie
        System.out.println("\n\nCat\'s Game.  BYE!");
    }
    
}
