Results 1 to 3 of 3
  1. #1
    Pyrexkidd is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default First TicTacToe Attempt

    Here is my first attempt at TicTacToe.

    1) Any thoughts, comments, suggestions, or recommendations, on how I can improve this code?

    2) Any thoughts on how I can add a computer player that does not just randomly pick a location. I have googled this, and think I am not understanding the logic behind it.

    Kinda looks to me like it belongs on the dailyWTF. But it is "Student" code if you will.

    Thanks in advance.

    Java Code:
    package TicTacToe;
    import java.util.*;
    
    public class TicTacToe {
    
        public static void main(String[] args) {
    
            Scanner console = new Scanner(System.in);
            int[][] playField = new int[3][3];
            int players = 0;
            //player 1 = true, player 2 = false;
            boolean player = false, gameWon = false;
            players = startGame(console);
            initilizeField(playField);
            do{
                drawField(playField);
                player = !player;
                if (player == true && players == 1 || player == true && players == 2 || player == false && players == 2){
                    playerChoice(player, console, playField);
                }else if (player == false && players != 2){
                    //computerChoice(player, playField);
                    System.out.print("This would be Player 2's Turn\n");
                }
                gameWon = isGameWon(playField);
    
            } while(!gameWon);
            winningMessage(player);
        }
        //displays message at end of game to notify user game is over
        public static void winningMessage(boolean player){
            int playerInt = (player == true) ? 1 : 2;
            System.out.print("You Won Player " + playerInt + "\n");
            System.out.print("Thanks for Playing\n");
        }
        //validates the game has not been won
        public static boolean isGameWon(int[][] playField){
            if ( //Horizontal Rows top to bottom
                 playField[0][0] == playField[0][1] && playField[0][0] == playField[0][2] && playField[0][0] != 0||
                 playField[1][0] == playField[1][1] && playField[1][0] == playField[1][2] && playField[1][0] != 0||
                 playField[2][0] == playField[2][1] && playField[2][0] == playField[2][2] && playField[2][0] != 0||
                 //Vertical Rows left to right
                 playField[0][0] == playField[1][0] && playField[0][0] == playField[2][0] && playField[0][0] != 0||
                 playField[0][1] == playField[1][1] && playField[0][1] == playField[1][2] && playField[0][1] != 0||
                 playField[0][2] == playField[1][2] && playField[0][2] == playField[2][2] && playField[0][2] != 0||
                 //Diagonal Rows)
                 playField[0][0] == playField[1][1] && playField[0][0] == playField[2][2] && playField[0][0] != 0||
                 playField[0][2] == playField[1][1] && playField[0][2] == playField[2][0] && playField[0][2] != 0){
                return true;
            }
    
    
            return false;
        }
        //takes input from user, sets choice in playField[][]
        public static void playerChoice(boolean player, Scanner console, int[][] playField){
            int choice = (player == true) ? 1 : 2;
            int location;
            boolean doLoop = true;
            do {
                System.out.print("Please Make Your Move player " + choice + "\n:");
                location = console.nextInt();
                doLoop = setChoice(location, choice, playField);
                if (doLoop == true) {
                    System.out.print("That location is taken.  Please Choose again.\n");
                }
            } while (doLoop);
        }
        //takes input from user if one or two users.  displays key map.
        public static int startGame(Scanner console){
           int choice = 0;
           System.out.print("This is a simple Java TicTacToe Game\n");
           System.out.print("Use the Number Pad to make your Selection\n");
           System.out.print(" 7 | 8 | 9\n" +
                            "---|---|---\n" +
                            " 4 | 5 | 6\n" +
                            "---|---|---\n" +
                            " 1 | 2 | 3\n\n");
           System.out.print("Please Choose One or Two Players\n:");
           do{
              choice = console.nextInt();
              if (choice == 1 || choice == 2){
                  return choice;
              }
              System.out.print("Please enter 1 or 2\n:");
           } while(true);
        }
        //initilizes all play spaces to 0
        public static void initilizeField(int[][] playField){
            for( int i= 0; i < 3; i++){
                for ( int j = 0; j < 3; j++){
                    playField[i][j]= 0;
                }
            }
        }
        // Draws the Play Field
        public static void drawField (int[][] playField){
            for( int i = 0; i < 3; i++){
                for( int j = 0; j < 3; j++){
                    System.out.print(checkField(playField[i][j]));
                    if ( j != 2){
                        System.out.print("|");
                    }
                    else{
                        System.out.print("\n");
                    }
    
                }
                    if( i != 2){
                        for( int j = 0; j < 3; j++){
                            System.out.print("---");
                            if ( j != 2){
                                System.out.print("|");
                            }
                        }
                    }
                    System.out.print("\n");
            }
        }
            // Checks if a blnk X or O should be played
            public static String checkField(int field){
                switch (field){
                        case 0:
                            return "   ";
                        case 1:
                            return " X ";
                        case 2:
                            return " O ";
                    }
                return null;
            }
    
         //validates no existing move in location.
         //changes the value of the playField array based on player choice
         public static boolean setChoice(int location, int choice, int[][] playField){
             switch(location){
                 case 7:
                     if (playField[0][0] == 0){
                         playField[0][0] = choice;
                         return false;
                     }
                     return true;
                 case 8:
                     if (playField[0][1] == 0){
                         playField[0][1] = choice;
                         return false;
                     }
                     return true;
                 case 9:
                     if (playField[0][2] == 0){
                         playField[0][2] = choice;
                         return false;
                     }
                     return true;
                 case 4:
                     if (playField[1][0] == 0){
                         playField[1][0] = choice;
                         return false;
                     }
                     return true;
                 case 5:
                     if (playField[1][1] == 0){
                         playField[1][1] = choice;
                         return false;
                     }
                     return true;
                 case 6:
                     if (playField[1][2] == 0){
                         playField[1][2] = choice;
                         return false;
                     }
                     return true;
                 case 1:
                     if (playField[2][0] == 0){
                         playField[2][0] = choice;
                         return false;
                     }
                     return true;
                 case 2:
                     if (playField[2][1] == 0){
                         playField[2][1] = choice;
                         return false;
                     }
                     return true;
                 case 3:
                     if (playField[2][2] == 0){
                         playField[2][2] = choice;
                         return false;
                     }
                     return true;
                 default:
                     return false;
             }
         }
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    I wrote a Tic Tac Toe game in javascript when I was learning javascript. You could look at its logic to get some ideas for the computer's play.
    TicTacToe using JavaScript

    The basic idea was to build tables to hold combinations of 3 in a row and use that.

  3. #3
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    if ur using the netbeans IDE , there is an example for tictactoe in the demos applet folder
    Teaching myself java so that i can eventually join the industry! Started in June 2010

Similar Threads

  1. Switching JPanels inside JFrame attempt
    By frenk_castle in forum AWT / Swing
    Replies: 7
    Last Post: 03-31-2010, 08:39 AM
  2. my Quicksort attempt has failed
    By Jeremy8 in forum New To Java
    Replies: 4
    Last Post: 11-16-2009, 02:56 AM
  3. Replies: 0
    Last Post: 02-18-2009, 05:20 AM
  4. TicTacToe
    By Joe3161 in forum New To Java
    Replies: 4
    Last Post: 11-25-2007, 06:47 PM
  5. Replies: 1
    Last Post: 07-14-2007, 05:59 PM

Tags for this Thread

Posting Permissions

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