Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: i neeeed help in tictactoe in java !!

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default i neeeed help in tictactoe in java !!

    Hello every one i have ma assignment for ma class is to make tictactoe game using arryas in java..i don't know if neeed two codes or no..i did big part so far..but i need help.
    and ma prof. needs only one player and da computer be the other one.... in 5x5 tictactoe :

    two codes

    import java.util.Scanner;
     
    public class TTTGame
    {
     
     
    public static void main(String[] args)
    {
     
    TTTBoard myGame = new TTTBoard();
    Scanner input = new Scanner(System.in);
    int row;
    int col;
     
     
    while(myGame.determineWinner() == 0 && !myGame.isBoardComplete())
    {
     
    myGame.displayBoard();
     
     
    System.out.println("Player " + myGame.getCurrentPlayer());
    System.out.println("Make your move.");
    System.out.print("Row please (1-5):");
    row = input.nextInt();
     
     
    while(row < 1 || row > 5)
    {
    System.out.println("Invalid Row.");
    System.out.print("Try again (1-5):");
    row = input.nextInt();
    }
     
    System.out.print("Col please (1-5):");
    col = input.nextInt();
     
    // error trap for valid col
    while(col < 1 || col > 5)
    {
    System.out.println("Invalid Col.");
    System.out.print("Try again (1-5):");
    col = input.nextInt();
    }
     
    while(!myGame.makeMove(row, col))
    {
    System.out.println("Invalid Move... Try Again.");
    System.out.print("Row please (1-5):");
    row = input.nextInt();
     
     
    while(row < 1 || row > 5)
    {
    System.out.println("Invalid Row.");
    System.out.print("Try again (1-5):");
    row = input.nextInt();
    }
     
    System.out.print("Col please (1-5):");
    col = input.nextInt();
     
     
    while(col < 1 || col > 5)
    {
    System.out.println("Invalid Col.");
    System.out.print("Try again (1-5):");
    col = input.nextInt();
    }
    }
    }
     
     
    if (myGame.determineWinner() == 0)
    {
    System.out.println("Sorry - Cat's Game");
    }
    else
    {
     
    System.out.print("The Winner is Player ");
    if (myGame.getCurrentPlayer() == 1)
    {
    System.out.println("2");
    }
    else
    {
    System.out.println("1");
    }
    }
    }
     
    }
    __________________________________________________

    public class TTTBoard
    {
    private int _player;
    private int _board[][];
     
    public TTTBoard()
    {
     
    _player = 1;
     
    _board = new int[5][5];
     
     
    for (int i = 0; i < 5; i++)
    {
    for (int j = 0; j < 5; j++)
    {
    _board[i][j] = 0;
    }
    }
    }
     
     
    public boolean makeMove(int row, int col)
    {
     
    boolean move;
     
     
    if (_board[row][col] == 0)
    {
    _board[row][col] = _player;
    return true;
     
    }
    else
    {
     
    move = false;
    }
    return move;
    }
     
     
    public boolean isBoardComplete()
    {
    boolean complete = true;
     
    for (int i = 0; i < 5; i++)
    {
    for(int j =0; j < 5; j++)
    {
    if (_board[i][j] == 0)
    {
    complete = false;
    }
    }
    }
    return complete;
    }
     
     
    public int determineWinner()
    {
    int winner = 0;
     
     
    if (_board[0][0] == _board[0][1] && _board[0][0] == _board[0][2] &&
    _board[0][0] != 0)
    {
    winner = _board[0][0];
    }
     
     
    if (_board[1][0] == _board[1][1] && _board[1][0] == _board[1][2] &&
    _board[1][0] != 0)
    {
    winner = _board[1][0];
    }
     
    if (_board[2][0] == _board[2][1] && _board[2][0] == _board[2][2] &&
    _board[2][0] != 0)
    {
    winner = _board[2][0];
    }
     
    // Check for winner in col 1
    if (_board[0][0] == _board[1][0] && _board[0][0] == _board[2][0] &&
    _board[0][0] != 0)
    {
    winner = _board[0][0];
    }
     
    // Check for winner in col 2
    if (_board[0][1] == _board[1][1] && _board[0][1] == _board[2][1] &&
    _board[0][1] != 0)
    {
    winner = _board[0][1];
    }
     
    // Check for winner in col 3
    if (_board[0][2] == _board[1][2] && _board[0][2] == _board[2][2] &&
    _board[0][2] != 0)
    {
    winner = _board[0][2];
    }
     
    // Check for winner in first diagonal
    if (_board[0][0] == _board[1][1] && _board[0][0] == _board[2][2] &&
    _board[0][0] != 0)
    {
    winner = _board[0][0];
    }
     
    // Check for winner in 2nd diagonal
    if (_board[2][0] == _board[1][1] && _board[2][0] == _board[0][2] &&
    _board[2][0] != 0)
    {
    winner = _board[2][0];
    }
    return winner;
    }
     
     
    public void displayBoard()
    {
     
    for ( int i = 0; i < 5; i++)
    {
    System.out.println(" ");
     
    for( int j = 0; j < 5; j++)
    {
    System.out.print(_board[i][j] + " ");
    }
     
    System.out.println("\n-|-|-|-|-\n");
    }
    }
     
     
    public int getCurrentPlayer()
    {
    return _player;
    }
    }
    Last edited by yinky; November 29th, 2009 at 11:05 PM.