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

Thread: I'm a beginning Java student and really struggling with this assignment. PLEASE HELP!

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question I'm a beginning Java student and really struggling with this assignment. PLEASE HELP!

    I have to create a TicTacToe game that accepts user input of positions 1 - 9. I started with a few functions but then now I'm totally lost with taking into considerations all the possible troubleshooting: like switching turns when one player already went, how to actually print and mark the board keeping track of the progress already played, making sure nothing invalid is going to be played without an exception statement, being able to empty out the board when done, etc. I don't even know how to construct my "main". We can't use arrays, the point is to see if we know how to construct this the "long" way. I AM COMPLETELY LOST AT THIS POINT. CAN ANYONE HELP ME???


  2. #2
    Junior Member
    Join Date
    Nov 2013
    Location
    Netherlands
    Posts
    4
    My Mood
    Lurking
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I'm a beginning Java student and really struggling with this assignment. PLEASE HELP!

    Hi,

    At the moment I am creating one also.
    If you program, you need to think in parts, what am I going to do next.

    Like, I first started with the window, then add buttons etc.

    What I did for looking at turns is have a counter.
    Like if it is devided by 2 it is the players turn.
    Else it is the computers turn.

    Maybe you also have some example code?

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I'm a beginning Java student and really struggling with this assignment. PLEASE HELP!

    Here's what I have so far:
    import java.util.*; 
    /** 
    * Represents a tic tac toe board on the console, positions are shown below: 
    * ------------- 
    * | 1 | 2 | 3 | 
    * ------------- 
    * | 4 | 5 | 6 | 
    * ------------- 
    * | 7 | 8 | 9 | 
    * ------------- 
    * 
    * An example board in mid play looks like this: 
    * ------------- 
    * | X | | | 
    * ------------- 
    * | | O | | 
    * ------------- 
    * | O | | X | 
    * ------------- 
    * 
    */ 
    public class TicTacToe 
    { 
    public enum Marker 
    { 
    EMPTY, 
    X, 
    O 
    }; 
     
    private static Marker position1 = Marker.EMPTY; 
    private static Marker position2 = Marker.EMPTY; 
    private static Marker position3 = Marker.EMPTY; 
    private static Marker position4 = Marker.EMPTY; 
    private static Marker position5 = Marker.EMPTY; 
    private static Marker position6 = Marker.EMPTY; 
    private static Marker position7 = Marker.EMPTY; 
    private static Marker position8 = Marker.EMPTY; 
    private static Marker position9 = Marker.EMPTY; 
     
    private static Marker turn = Marker.X; 
     
    public static void main(String[]args) 
    { 
    Scanner console = new Scanner(System.in); 
    System.out.print("Input your position from 1 to 9. "); 
    int pos = console.nextInt(); 
    printboard(); 
    } 
    /** 
    * This function returns true if the spot is empty, false if not. 
    */ 
    public static boolean isValidMove(int pos) 
    { 
    String intPos = "position" + pos; 
    if (pos>0 && pos <10) 
    { 
    Marker test = Marker.EMPTY; 
    switch(pos) 
    { 
    case 1: test = position1; break; 
    case 2: test = position2; break; 
    case 3: test = position3; break; 
    case 4: test = position4; break; 
    case 5: test = position5; break; 
    case 6: test = position6; break; 
    case 7: test = position7; break; 
    case 8: test = position8; break; 
    case 9: test = position9; break; 
    } 
    if (test == Marker.EMPTY) 
    return true; 
    else return false; 
    } 
    else 
    { 
    System.out.println("This is an invalid move, must be an integer from 1 to 9, please try again"); 
    return false; 
    } 
    } 
    /** 
    * This method will print the board as shown in the above example. 
    */ 
     
    public static void printBoard() 
    { 
    for(int i = 0; i < 13; i++) 
    { 
    System.out.print("-"); 
    } 
    System.out.println(); 
    System.out.println("| " + position1 + " | " + position2 + " | " + position3 + " |"); 
    for(int i = 0; i < 13; i++) 
    { 
    System.out.print("-"); 
    } 
    System.out.println("| " + position4 + " | " + position5 + " | " + position6 + " |"); 
    for(int i = 0; i < 13; i++) 
    { 
    System.out.print("-"); 
    } 
    System.out.println("| " + position7 + " | " + position8 + " | " + position9 + " |"); 
    for(int i = 0; i < 13; i++) 
    { 
    System.out.print("-"); 
    } 
    } 
     
    /** 
    * Checks if a particular player has won. 
    * 
    * @param m The player to check 
    * @return true if the player won, false if not 
    */ 
    public static boolean hasWon(Marker m) 
    { 
    if(position1 == m && position2 == m && position3 == m) ||
    (position1 == m && position4 == m && position7 == m) ||
    (position1 == m && position5 == m && position9 == m) ||
    (position2 == m && position5 == m && position8 == m) ||
    (position3 == m && position5 == m && position7 == m) ||
    (position3 == m && position6 == m && position9 == m) ||
    (position4 == m && position5 == m && position6 == m) ||
    (position7 == m && position8 == m && position9 == m) 
    System.out.print("You win!"); 
    else return false; 
    } 
     
    /** 
    * Checks if the board is full with no winner 
    * @return true if the board is full with no winner, false otherwise 
    */ 
    public static boolean isTie() 
    { 
    if(hasWon = false) 
    System.out.print("This game is tied."); 
    } 
     
    /** 
    *This keeps track of whose turn it is - X or O. If X is the last marker 
    *then it will automatically be O going next. 
    */ 
    public static void turn() 
    { 
     
    } 
     
    /** 
    * Mark the given position with the given marker 
    * @param m The marker of the player given 
    * @param pos The position that we are marking 
    */ 
    public static void markTheBoard(Marker m, int pos) 
    { 
     
    } 
    }


    --- Update ---

    Can anyone help me?
    Last edited by jps; November 5th, 2013 at 02:06 PM. Reason: No spaces allowed in [highlight=java]

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,504
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: I'm a beginning Java student and really struggling with this assignment. PLEASE HELP!

    Help you with what? Please ask a specific question about what your problem is.
    Improving the world one idiot at a time!

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,712
    My Mood
    Mellow
    Thanks
    172
    Thanked 622 Times in 609 Posts

    Default Re: I'm a beginning Java student and really struggling with this assignment. PLEASE HELP!

    The code you posted has multiple compiler errors. Please fix those and keep working it or post the errors you'd like help with.

Similar Threads

  1. [SOLVED] As a self-studying student, I need moderators to mark assignment.
    By LittleCat in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2012, 08:31 AM
  2. Struggling Student seeks Guidance...
    By gabe in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 13th, 2012, 10:40 AM
  3. Struggling with Java :o
    By Jabdennel in forum Java Theory & Questions
    Replies: 1
    Last Post: May 30th, 2011, 09:08 PM
  4. [SOLVED] First assignment, clueless student, involving coordinates and distances
    By Kerrigan in forum Java Theory & Questions
    Replies: 7
    Last Post: March 10th, 2011, 03:52 PM
  5. Hi from UK, Struggling with java!
    By Sneak in forum Member Introductions
    Replies: 6
    Last Post: December 14th, 2009, 08:39 AM