Results 1 to 3 of 3
  1. #1
    Eric is offline Senior Member
    Join Date
    Jun 2007
    Posts
    111
    Rep Power
    0

    Default Help with Tic Tac Program

    I have a project to create tic tac toe without a gui.
    I need Assistance with writing a program.
    Can anyone point me in the right direction of where to get some leads.
    This is my potential Tic Tac Program without GUI; written in pseudo.

    Java Code:
    package proyect;
     import java.util.*;
    
     public class TicTacToeMain
     {
         private char [][] board;
         private boolean xturn;
         private int numMoves;
         
         public static void main()
         {
             Scanner keyb = new Scanner(System.in);
             String response;
             
             do
             {
                 TicTacToeMain game = new TicTacToeMain();
                 play();
                 System.out.println("Would you like to play 
                                            again?");
                 response = keyb.nextLine();
             }while(response.equalsIgnoreCase("yes"));        
             
         }
         
         public TicTacToeMain()
         {
              board = new char [5][5];
              xturn = true;
              numMoves = 0;
         }
         
         public void play()
         {
             do
             {
                 //show board
                 //get valid move
                 //update board
                 //determine winner if possible
             }while((no one has won)&&(moves < 9));    
         }
         
         public boolean won(int row, int col)//computer indices
                                                             (inside)
         {
             if(allSquaresInRowAreSame) or board[0][0]
                ==board[2][2]&&board[0][2]==board[0][4]
               return true;
             if(allSquaresInColAreSame)
               return true;
             return wonOnDiagonal();
         }
         
         public boolean wonOnDiagonal()
         {
             if(board[0][0]==board[2][2]&&board[2][2]
                ==board[4][4]&&board[0][0]!=space or " ")
                 return true;
             return //same on positive and not space
         }
     }

    Thanks!

    Eric

  2. #2
    Daniel is offline Member
    Join Date
    Jun 2007
    Posts
    91
    Rep Power
    0

    Default

    Sure, check out the tutorials at The Java™ TutorialsThere are a ton of them.
    Good luck.

    Greetings.

    Daniel:o

  3. #3
    Albert is offline Senior Member
    Join Date
    Jun 2007
    Posts
    114
    Rep Power
    0

    Default

    First of all, you'll need a board.
    Java Code:
    private char[][] board = new char[3][3];
    I would recommend making final chars to represent what is in each sqaure.
    Java Code:
    private final char EMPTY = ' ';
     private final char PLAYER1 = 'X';
     private final char PLAYER2 = 'O';
    you need to initialize the board.
    Java Code:
     public void init() {
         for (int x = 0; x < 3; x++) {
             for (int y = 0; y < 3; y++) {
                board[x][y] = EMPTY;
             }
         }
     }
    to check the winner, this gets a bit tedious.
    Java Code:
    public char winner() {
         for (int i = 0; i < board.length; i++) {
             /* check horizontally */
             if (board[i][0] == board[i][1] &&
                     board[i][0] == board[i][2]) {
                 if (board[i][0] != EMPTY) {
                     return board[i][0];
                 }
             }
             /* check vertically */
             if (board[0][i] == board[1][i] &&
                     board[0][i] == board[2][i]) {
                 if (board[0][i] != EMPTY) {
                     return board[0][i];
                 }
             }
         }
          
         /* check diagonally */
         if (board[0][0] == board[1][1] &&
                 board[0][0] == board[2][2]) {
             if (board[0][0] != EMPTY) {
                 return board[0][0];
             }
         }
         if (board[0][2] == board[1][1] &&
                  board[0][2] == board[2][0]) {
             if (board[0][2] != EMPTY)) {
                 return board[0][2];
             }
         }
         return EMPTY;
     }
    Greetings

    Albert:rolleyes:

Similar Threads

  1. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 08:24 AM
  2. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  3. cannot run the program
    By amiey in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 04:13 AM
  4. Replies: 0
    Last Post: 10-04-2007, 09:33 PM
  5. Why does this program not end?
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •