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 2 of 2

Thread: TicTacToe

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default TicTacToe

    My Lab is to create a ticktacktoe program against the computer. I haven't looked at my lab and started typing and here is what I have so far. My problem is it compiles but gives me a main error and I don't know what to do about the AI part.

    Lab:
    Lab13: Write program Lab13.java which plays the game of Tic Tac Toe against the computer. The user will be X and the computer will be O. The user and the computer will take turns to select their moves in the TicTacToe board. Once there is a match, the winner will be announced and the game will be over. In case all the entries in the TicTacToe board have been filled and there is no winner, a TIE should be declared. See the execution examples given below.
    The TicTacToe board will be represented by a double dimensional array with 3 rows and 3 columns of characters. In every entry of the double dimensional array, we will have an ‘X’, an ‘O’ or a ‘_’. The ‘_’ will mean that the entry is available, i.e., it has not been chosen yet. At the beginning of the game all the entries in the double dimensional array will be set to ‘_’.
    Every time that the game begins, the user chooses his entry first. Then the computer will pick its entry, then the user, then the computer, and so on...
    If we make the computer pick its entry in an intelligent way, this program will be too difficult for you. Therefore, we will make the computer pick a random available entry every time that it’s the computer's turn.

    Your job will be just to complete the user defined function gameStatus( ), modify user defined method display( ), and of course complete the main( ) method.
    import java.util.*;
     
    public class TicTacToe
    {
     
      private char[][] board;
     
      /** 
       * Creates a board where each
       * square holds the underline '_' character.
       */
      public TicTacToe()
      {
        board = new char[3][3];
        for (int row = 0; row < 3; row ++)
    	{
          for (int col =  0; col < 3; col++)
    	  {
            board[row][col] = '_';
          } // end of inner loop
        } // end of outer loop
      } 
     
      // Methods
     
     
      public boolean set(int row, int col, char c)
      {
        if (row >= 3 || row < 0) 
          return false;
        if (col >= 3 || col < 0) 
          return false; 
        if (board[row][col] != '_')
          return false;
        if ( !(c == 'X' || c == 'O'))
          return false;
        // assertion: row, col, c are valid
        board[row][col] = c;
        return true;
      }
     
      public char get(int row, int col)
      {
        return board[row][col];
      }
     
      /**
       * Returns whether or not the board is full.
       * @return False if any space in the board is '_', otherwise True
       */
      public boolean isFull()
      {
        TicTacToe t = new TicTacToe();
    	t.isFull();
    	for (int row = 0; row < 3; row++)
    	{
    		for(int col = 0; col < 3; col++)
    		{
    		t.set(row, col, 'X');
    		}
    	}
    	t.isFull();
     
        return true;
      }
     
      public char winnerVertical()
      {
        TicTacToe t = new TicTacToe();
    	t.winnerVertical();
    	t.set(0,0,'X');
    	t.set(1,0,'O');
    	t.set(2,0,'O');
    	t.winnerVertical();
    	t.set(0,1,'O');
    	t.set(1,1,'O');
    	t.set(2,1,'O');
    	t.print();
    	t.winnerVertical();
     
        return '_';
      }
     
      public char winnerHorizontal()
      {
      TicTacToe t = new TicTacToe();
        t.winnerHorizontal();
    	t.set(0,0,'X');
    	t.set(0,1,'O');
    	t.set(0,2,'O');
    	t.winnerHorizontal();
    	t.set(1,0,'O');
    	t.set(1,1,'O');
    	t.set(1,2,'O');
    	t.print();
    	t.winnerHorizontal();
     
        return '_';
      }
     
      public char winnerDiagonal()
      {
        TicTacToe t = new TicTacToe();
    	t.winnerDiagonal();
    	t.set(0,0,'X');
    	t.set(1,1,'O');
    	t.set(2,2,'O');
    	t.winnerDiagonal();
    	t.set(0,2,'O');
    	t.set(1,1,'O');
    	t.set(2,0,'O');
    	t.print();
    	t.winnerDiagonal();
     
        return '_';
      }
     
     
      public char winner()
      {
     
        return '_';
      }
     
      /**
       * Plays a full game of TicTacToe.
       * Assumes board is empty (variables are initialized
       * but no moves have been made).
       */
      public void play()
      {
        Scanner sc = new Scanner(System.in);
        int row, col;
        char currPlayer = 'X';
        System.out.println("Enter -1 to quit at any time.");
        while(!isFull() && winner() != 'X' && winner() != 'O')
    	{
          try{
            print();
            System.out.println("Player "+currPlayer+"'s Turn");
            System.out.print("Enter Row:");
            System.out.println();
            row = sc.nextInt();
            if (row == -1){break;}
            System.out.print("Enter Col:");
            System.out.println();
            col = sc.nextInt();
            if (col == -1){break;}
            if (!set(row,col,currPlayer))
    		{
              throw new Exception("Error Setting.");
            }
          }catch(Exception e){
            if(e instanceof InputMismatchException)
    		{
              sc.nextLine();
            }
            System.out.println("Bad input");
            if(currPlayer == 'X'){
              currPlayer = 'O';
            }
            else{
              currPlayer = 'X';
            }
          }
     
          if(currPlayer == 'X'){
            currPlayer = 'O';
          }
          else{
            currPlayer = 'X';
          }
        }
     
        sc.close();
        print();
        if (winner() == 'X')
    	{
          System.out.println("Player X wins!!");
        }
        else if (winner() == 'O')
    	{
          System.out.println("Player O wins!!");
        }
        else{
          System.out.println("Everybody wins!! (Depending on how you look at it)");
        }
      }
     
      /**
       * Prints the state of the board.
       */
      public void print()
      {
        int col;
        System.out.println("      col");
        System.out.print("      ");
        for (col = 0; col < 3; col++)
    	{
          System.out.print(col+" ");
        }
        System.out.println();
        for (int row = 0; row < 3; row ++)
    	{
          if(row == 0)
    	  {
            System.out.print("row "+row+" ");
          }
          else 
    	  {
            System.out.print("    "+row+" ");
          }
          for (col =  0; col < 3; col++)
    	  {
            System.out.print(board[row][col] + " ");
          } // end of inner loop
          System.out.println();
        } // end of outer loop   
      }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 600 Times in 515 Posts

    Default Re: TicTacToe

    ...and what would this "main error" be? On what line? Did you google the error?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Tictactoe problem
    By Nostromo in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 26th, 2011, 03:38 PM
  2. i neeeed help in tictactoe in java !!
    By yinky in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 29th, 2009, 10:17 PM