All pastes #792327 Raw Edit

TicTacToe

public text v1 · immutable
·published 2007-11-21 09:17 UTC
rendered paste body
@author Luis Contreras
*************the Board Class*********************
import java.util.Scanner;
public class Board {

    private String [][] board;

    private int row=3;
    private int col=3;

    public Board () {


        board = new String [row][col];

        //for(int i=0; i < row; i++)
           //     for(int j=0; j < col; j++)

        board[0][0]="1";
        board[0][1]="2";
        board[0][2]="3";
        board[1][0]="4";
        board[1][1]="5";
        board[1][2]="6";
        board[2][0]="7";
        board[2][1]="8";
        board[2][2]="9";

}


/**
 * Returns a display of the current board.
 */


public void displayBoard() {

for (int row=0; row < board.length; row++) {
        for (int col=0; col < board.length; col++) {

            System.out.print(board[row][col]);

        }
        System.out.println();
    }

}


/**
 * Sets the move at the move position for a player.
 *
 * @param move The board position to play.
 * @param move The player to which the move is assigned.
*/


public void setMove(int move, String player) {

 while(move > 0 && move < 10) {
    if(board[row][col].equals(move))
    while (isOpenMove (move));
            board[row][col] =  player ;

}
}





/**
 * Tests to see if a board position is open for play.
 *
 * @return true if the position is open, otherwise false.
*/
public boolean isOpenMove(int move) {

     if ( board [row][col] !="X" && board [row][col] !="O") { // if(board[row][col] != "")?

                return true;

            } else {

                return false;

            }

    }

/**
 * Checks to see if the game has a winner.
 *
 * @return true if this board has a winner, otherwise false.
 */
public boolean isWinner() {
    for (int row=0; row<3;row++)
    for (int col=0; col<3;col++)

            //When player is x
            //Win position for every row
            if(board[0][0]=="X" && board[0][1]=="O" && board[0][2]=="O")
                return true;
            if(board[1][0]=="X" && board[1][1]=="O" && board[1][2]=="O")
                return true;
            if(board[2][0]=="X" && board[2][1]=="O" && board[2][2]=="O")
                return true;

            //Win position for every column
            if(board[0][0]=="X" && board[1][0]=="O" && board[2][0]=="O")
                return true;
            if(board[0][1]=="X" && board[1][1]=="O" && board[2][1]=="O")
                return true;
            if(board[0][2]=="X" && board[1][2]=="O" && board[2][2]=="O")
                return true;

            //Win position for diagnals
            if(board[0][1]=="X" && board[1][1]=="O" && board[2][2]=="O")
                return true;
            if(board[0][2]=="X" && board[1][1]=="O" && board[2][0]=="O")
                return true;

            //When player is o
            //Win position for every row
            if(board[0][0]=="O" && board[0][1]=="O" && board[0][2]=="O")
                return true;
            if(board[1][0]=="O" && board[1][1]=="O" && board[1][2]=="O")
                return true;
            if(board[2][0]=="O" && board[2][1]=="O" && board[2][2]=="O")
                return true;

            //Win position for every column
            if(board[0][0]=="O" && board[1][0]=="O" && board[2][0]=="O")
                return true;
            if(board[0][1]=="O" && board[1][1]=="O" && board[2][1]=="O")
                return true;
            if(board[0][2]=="O" && board[1][2]=="O" && board[2][2]=="O")
                return true;

            //Win position for diagnals
            if(board[0][1]=="O" && board[1][1]=="O" && board[2][2]=="O")
                return true;
            if(board[0][2]=="O" && board[1][1]=="O" && board[2][0]=="O")
                return true;

                else
                return false;

        }
}
**************************************************************************************************
The Player Class
**************************************************************************************************
import java.util.Scanner;

public class Player {
    private String name;
    private Board board;
    private Board setMove;
/**
 * Initialize Player to specified values.
 *
 * @param newBoard The Board to play upon.
 * @param newName The name of the player ('X' or 'O').
 */
public Player(Board newBoard, String newName) {
    board = newBoard;
    name= newName;

}


/**
 * Prompts for a move and makes the move if valid.
 * Otherwise, posts an error message and asks the user
 * to enter a correct move.
 */
public void makeMove() {


   Scanner input = new Scanner(System.in);
   boolean moveOpen = false;
    int move;

if (!moveOpen) {
    move = input.nextInt();



    moveOpen = isOpenMove(move);
      }else{
        System.out.println("Re enter your Input from 1-9");
    }

}
}

***********************************************************************************************
The GameApp Class
**********************************************************************************************

public class GameApp {
    public static void main(String[] args) {

        Board board = new Board(); // calls constructor in Board
        Player playerX = new Player(board, "X");
        Player playerO = new Player(board, "O");

        boolean gameOver = false;

        System.out.println("Enter your move using "
            + "the number of the square.");
        System.out.println("Press Ctrl-C keys together at any time to exit.");

        int whoMove = 0;
       // while (gameOver == false) {
            board.displayBoard();
            if(whoMove ==0) {
               playerX.makeMove();
               gameOver = board.isWinner();
               whoMove++;
               } else {
                  playerO.makeMove();
                  gameOver = board.isWinner();
                  whoMove--;
                  }
              }// end while
          }// end main
      //}//end cl