Results 1 to 10 of 10

Thread: New To Java

  1. #1
    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0

    Default New To Java

    I am planning a Java Tic Tac Toe game. I want to make a Arrays to handle the game update and if someone won tied. It will check for X and O. I will use try/catch block to handle the exception from the set method. The contents of the main[] method should resemble the following. I am interested in learning how to implement these into a Tic Tac Toe game.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    992
    Rep Power
    2

    Default Re: New To Java

    Either the forum ate your code and your question, or you forgot to put it in, because I can't see both of them.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0

    Default Re: New To Java

    I am working on my winOrTie method I will define it and then use it after every move.
    Last edited by FrenchJavaBean; 12-20-2013 at 01:15 AM.

  4. #4
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: New To Java

    Quote Originally Posted by SurfMan View Post
    Either the forum ate your code and your question, or you forgot to put it in, because I can't see both of them.
    I think he wants to know how to start this off with those things being used.

    Unless this is a assignment I think you're trying to hard. Tic Tac Toe is basic and can be written without throwing any exceptions whatsoever.

    I wrote Tic Tac Toe with Arrays and without them. It shouldn't be to hard if you aren't brand-new to Java.

  5. #5
    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0

    Default Re: New To Java

    Quote Originally Posted by LetsG0Blue View Post
    I think he wants to know how to start this off with those things being used.

    Unless this is a assignment I think you're trying to hard. Tic Tac Toe is basic and can be written without throwing any exceptions whatsoever.

    I wrote Tic Tac Toe with Arrays and without them. It shouldn't be to hard if you aren't brand-new to Java.


    Hey,

    I thanks you for your help so far so good. I have my code almost complete but still hung up on a few errors so once I fix those errors I think it will run. I can show you what I have so far I just got into jave.

  6. #6
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: New To Java

    Quote Originally Posted by FrenchJavaBean View Post
    Hey,

    I thanks you for your help so far so good. I have my code almost complete but still hung up on a few errors so once I fix those errors I think it will run. I can show you what I have so far I just got into jave.
    No problem, post your code here and where your problem may be.

  7. #7
    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0

    Default Re: New To Java

    Quote Originally Posted by LetsG0Blue View Post
    No problem, post your code here and where your problem may be.
    Java Code:
    package Good;    
        
    import java.util.Scanner;
    
    /**
     *
     * @author FrenchJavaBean
     */
    public class Good {    
        
        public static int[][] gameboard;    
        public static final int EMPTY = 0;    
        public static final int NOUGHT = -1;      // Player O
        public static final int CROSS = 1;        // Player X
        
        
        // Utility methods    
        static void set(int val, int row, int col) throws    
                IllegalArgumentException {    
            if (gameboard[row][col] == EMPTY) gameboard[row][col] = val;    
            else throw new IllegalArgumentException("Player already there!");    
                
        }    
            
        static void displayBoard() {    
            for( int r = 0; r < gameboard.length; r++) {    
                System.out.print("|");    
                for (int c = 0; c < gameboard[r].length; c++) {    
                    switch(gameboard[r][c]) {    
                        case NOUGHT:    
                            System.out.print("O");    
                            break;    
                        case CROSS:    
                            System.out.print("X");    
                            break;    
                        default:        //EMPTY    
                            System.out.print(" ");    
                    }    
                    System.out.print("|");    
                }    
                System.out.println("\n-------\n");    
            }    
        }      
        // Define createBoard method    
        static void createBoard(int row, int col) {  
            // Initialize gameboard
            int[] [] createBoard = new int[rows][cols];
            for (int r = 0; r < 3; r++) {
                for (int c = 0; c < 3; c++) {
                    creatBoard[rows][cols] = -1;
                }
            }
        }       
    // Define winOrTie method        
        static boolean winOrTie() {        
    //   Define the winOrTie method. Check first for a win with
    //   rows and columns and then diagonally. Finally, check to
    //   see if there are any empty cells without a cross or naught.
            int winner = -2;
            
            for (int r = 0; r < 3; r++)
                if ((gameboard[r][0]== winner)&& (gameboard[r][1] == winner)
                        && (gameboard[r][0] == winner)) {
            return true;
                }
            for (int j = 0 j < 3; j++)
                if ((gameboard[0][j] == winner) && (gameboard[1][j] == winner)
                        && (gameboard[2][j] == winner)){
                    }
            if ((gameboard [0][0] == winner) && (gameboard[1][1] == winner)
                    && (gameboard[2][2] == winner)) {
                   return true;
            }
            if ((gameboard[0][2] == winner) && (gameboard[1][1] == winner)
                    && (gameboard[2][0] == winner)) {
                return true;
            }
            return false;
            
        }      
    // Initializing Multidimensional Arrays Ilerating Over Multidimensional Arrays
    // A player wins if all the cells in a row or column are the same mark or diagonally through the center
    // The players tie if all the cells have a cross or nough, but no player has three in a row
    // horizontally, vertically, or diagonally. Return Nought if Nought wins, Cross if Cross wins,
    // 0 if there's a tie, and another value (like -2, for exaple) if there are empty 
    // cells on the board.
           
      
         
        public static void main(String[] args) {  
    // main contens
        createBoard (3,3);
        int turn = 0;
        int playerVal;
        int outcome;
        int outcome = -2;    
        boolean b = true;  
    Scanner scan = new Scanner(System.in);    
            boolean boolVal;
    
     
    //  The board is created and initialize a turn counter, player value, and game outcome.
    // For nought, the value is -1, while 1 is the value for cross.
    // While there's no winner or tie, display the board and prompt for a row and column for the current player.
    // I will use try/catch block to handle the ezception from the set method. I will use the System.err.println 
    // method So I can display the message in red.
    // Display the final board and message on which player won or if there's a tie.
    // The contents of the main[] method should resemble the following.
     
                
            do {    
                displayBoard();    
                playerVal = (turn % 2 == 0)? NOUGHT : CROSS;    
                if (playerVal == NOUGHT) System.out.println("\n-O's turn-");    
                else System.out.println("\n-X's turn-");    
                System.out.print("Enter row and column:");    
                try {    
                        set(playerVal, scan.nextInt(), scan.nextInt());    
            } catch (IllegalArgumentException ex) {System.err.println(ex);}    
            turn ++;    
            outcome = winOrTie();   
            return false;  
            } while ( outcome == -2 );    
            displayBoard();    
            switch (outcome) {    
                case NOUGHT:    
                    System.out.println("O wins!");    
                    break;    
                case CROSS:    
                    System.out.println("X wins!");    
                    break;    
                case EMPTY:    
                    System.out.println("Tie");    
                    break;    
                        
        
                        
                        
            }    
        }    
            
    }

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,098
    Rep Power
    6

    Default Re: New To Java

    Java Code:
    static void createBoard(int row, int col) { 
            // Initialize gameboard
            int[] [] createBoard = new int[rows][cols]; // eh? You create a new board as a LOCAL variable? Is that really what you want to do?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: New To Java

    How will this be a tic tac toe game? Will it have any GUI or will it be text based...? Are you using Swing?

  10. #10
    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0

    Default Re: New To Java

    I will be just on the console for now later I hope to have it in a GUI. This is the first stage of my program.

Posting Permissions

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