Results 1 to 5 of 5
  1. #1
    CoderJava is offline Member
    Join Date
    Jun 2012
    Posts
    1
    Rep Power
    0

    Default Java Code Critique

    Hello, I've been learning java for the past two weeks, and I decided to create a tic tac toe application. This is how I accomplished it:
    note please critique and tell if there are any bad practices, so I can adjust them. Thanks!
    Java Code:
    import java.util.Scanner;
    public class TicTacToe {
    	private static String[][] board = { {"1", "2", "3"},
    						      {"4", "5", "6"},
    						      {"7", "8", "9"} };
    	public TicTacToe() {
    		for(int i = 0; i < board.length; i++) {
    			for(int j = 0; j < board.length; j++) {
    				System.out.print(board[i][j] + " ");
    			}
    			System.out.println();
    		}
    	}
    	public void displayBoard() {
    		for(int i = 0; i < board.length; i++) {
    			for(int j = 0; j < board.length; j++) {
    				System.out.print(board[i][j] + " ");
    			}
    			System.out.println();
    		}
    	}
    	public boolean checkWinConditions() {
    		 if(board[0][0] == "X" && board[0][1] == "X" && board[0][2] == "X"){
    			 return true;
    		 } else if(board[1][0] == "X" && board[1][1] == "X" && board[1][2] == "X") {
    			 return true;
    		 } else if(board[2][0] == "X" && board[2][1] == "X" && board[2][2] == "X") {
    			 return true;
    		 } else if(board[0][0] == "X" && board[1][0] == "X" && board[2][0] == "X") {
    			 return true;
    		 } else if(board[0][1] == "X" && board[1][1] == "X" && board[2][1] == "X") {
    			 return true;
    		 } else if(board[0][2] == "X" && board[1][2] == "X" && board[2][2] == "X") {
    			 return true;
    		 } else if(board[0][0] == "X" && board[1][1] == "X" && board[2][2] == "X") {
    			 return true;
    		 } else if(board[0][2] == "X" && board[1][1] == "X" && board[0][2] == "X") {
    			 return true;
    		 }
    		 
    		 //player Y
    		 if(board[0][0] == "O" && board[0][1] == "O" && board[0][2] == "O"){
    			 return true;
    		 } else if(board[1][0] == "O" && board[1][1] == "O" && board[1][2] == "O") {
    			 return true;
    		 } else if(board[2][0] == "O" && board[2][1] == "O" && board[2][2] == "O") {
    			 return true;
    		 } else if(board[0][0] == "O" && board[1][0] == "O" && board[2][0] == "O") {
    			 return true;
    		 } else if(board[0][1] == "O" && board[1][1] == "O" && board[2][1] == "O") {
    			 return true;
    		 } else if(board[0][2] == "O" && board[1][2] == "O" && board[2][2] == "O") {
    			 return true;
    		 } else if(board[0][0] == "O" && board[1][1] == "O" && board[2][2] == "O") {
    			 return true;
    		 } else if(board[0][2] == "O" && board[1][1] == "O" && board[0][2] == "O") {
    			 return true;
    		 }
    		return false;
    	}
    	public static void main(String[] args) {
    		TicTacToe game = new TicTacToe();
    		player player1 = new player();
    		player player2 = new player();
    		int playerNumber = 1;
    		boolean gameOver = false;
    		boolean win;
    		if(playerNumber == 1){
    			player1.marker = "X";
    		} else if(playerNumber == 2) {
    			player2.marker = "O";
    		}
    		Scanner input = new Scanner(System.in);
    		String location = " ";
    		while(gameOver == false) 
    		{
    			if(playerNumber == 1) {
    				System.out.println("Player 1: ");
    				location = input.next();
    				switch(location) 
    				{
    				case "1":
    					board[0][0] = player1.marker;
    					playerNumber = 2;
    					break;
    				case "2":
    					board[0][1] = player1.marker;
    					playerNumber = 2;
    					break;
    				case "3":
    					board[0][2] = player1.marker;
    					playerNumber = 2;
    					break;
    				case "4":
    					board[1][0] = player1.marker;
    					playerNumber = 2;
    					break;
    				case "5":
    					board[1][1] = player1.marker;
    					playerNumber = 2;
    					break;
    				case "6":
    					board[1][2] = player1.marker;
    					playerNumber = 2;
    					break;
    				case "7":
    					board[2][0] = player1.marker;
    					playerNumber = 2;
    					break;
    				case "8":
    					board[2][1] = player1.marker;
    					playerNumber = 2;
    					break;
    				case "9":
    					board[2][2] = player1.marker;
    					playerNumber = 2;
    					break;
    				}
    				win = game.checkWinConditions();
    				if(win) {
    					gameOver = true;
    					System.out.println("Player 1 WINS!");
    				}
    				game.displayBoard();
    			} else if(playerNumber == 2) {
    				System.out.println("Player 2: ");
    				location = input.next();
    				player2.marker = "O";
    				switch(location) 
    				{
    				case "1":
    					board[0][0] = player2.marker;
    					playerNumber = 1;
    					break;
    				case "2":
    					board[0][1] = player2.marker;
    					playerNumber = 1;
    					break;
    				case "3":
    					board[0][2] = player2.marker;
    					playerNumber = 1;
    					break;
    				case "4":
    					board[1][0] = player2.marker;
    					playerNumber = 1;
    					break;
    				case "5":
    					board[1][1] = player2.marker;
    					playerNumber = 1;
    					break;
    				case "6":
    					board[1][2] = player2.marker;
    					playerNumber = 1;
    					break;
    				case "7":
    					board[2][0] = player2.marker;
    					playerNumber = 1;
    					break;
    				case "8":
    					board[2][1] = player2.marker;
    					playerNumber = 1;
    					break;
    				case "9":
    					board[2][2] = player2.marker;
    					playerNumber = 1;
    					break;
    				}
    				win = game.checkWinConditions();
    				if(win) {
    					gameOver = true;
    					System.out.println("Player 2 WINS!");
    				}
    				game.displayBoard();
    			}
    		}
    	}
    }
    Last edited by CoderJava; 06-30-2012 at 07:46 PM. Reason: typo

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: Java Code Critique

    Please go through the Forum Rules -- particularly the third pragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default Re: Java Code Critique

    There are sections of the code (the switch statements and the checkWinConditions) that are almost identical except for two items: marker and the values 1 or 2 and "X" and "O" for checkWin
    These should be made into methods and the different items passed to the method.
    Last edited by Norm; 06-30-2012 at 08:30 PM.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default Re: Java Code Critique

    Never compare two Strings for equality with the == operator; use the equals( ... ) method instead.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default Re: Java Code Critique

    Don't use hardcoded literals in the code. Define a final variable and use that.
    If you mistype one of those literals the compiler won't point that out:
    board[0][1] == "0" // zero vs O could be overlooked

    board[0][1] == "X"
    vs
    final String XMove = "X"; // Define one place

    board[0][1] == XMove; // use variable vs the hardcoded "X"
    or board[0][1].equals(XMove)

    Every time you type in a hardcoded value there is a chance you could mistype and the compiler will say nothing. Misspell a variable name and the compiler will give you an error message.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 16
    Last Post: 01-31-2012, 08:36 PM
  2. Critique my first Java Program!
    By Lucid15 in forum New To Java
    Replies: 3
    Last Post: 01-26-2012, 08:56 AM
  3. Replies: 0
    Last Post: 05-17-2011, 04:48 PM
  4. Please critique
    By jim01 in forum New To Java
    Replies: 4
    Last Post: 09-24-2010, 03:43 AM
  5. Critique Java Game: Help Me Improve
    By gretty in forum New To Java
    Replies: 1
    Last Post: 07-15-2010, 04:30 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
  •