Results 1 to 5 of 5
  1. #1
    Sabre is offline Member
    Join Date
    Jan 2017
    Posts
    13
    Rep Power
    0

    Angry I am completely lost...

    I have been working on this for weeks now and am beyond frustrated. It is a simple tic tac toe game. My issue is with the win or tie method. I know I am not doing it right. I have looked through several threads to no avail. I have also been looking through a Java "Beginners Guide" book which isn't showing me what to do. I know it doesn't have a parameter and needs to return an integer value. I just can't seem to wrap my head around on what exactly to type in????? Every time I attempt new errors pop up and I make things more complicated.

    Any and all help is greatly appreciated.

    package tictactoegame;

    import static java.time.Clock.system;

    /**
    *
    * @author john smith
    */
    public class Tictactoegame {

    static int [] [] gameboard;
    static final int EMPTY = 0;
    static final int NOUGHT = -1;
    static final int CROSS = 1;

    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("0");
    break;
    case CROSS:
    System.out.print("X");
    break;
    default: //Empty
    System.out.print(" ");
    }
    System.out.print("|");
    }
    System.out.println("\n------\n");
    }
    }
    static void createBoard(int rows, int cols) {
    gameboard = new int [rows] [cols];
    }

    static int winOrTie() {
    if () {
    system.out.println("tie!");
    } else if (player == 1) {
    system.out.println("X won!");
    } else {
    system.out.println("0 won!")

    }
    /**
    * @param args the command line arguments
    */ /**
    * @param args the command line arguments
    */
    }
    public static void main(String[] args) {
    createBoard(3,3);
    int turn = 0;
    int playerVal;
    int outcome;
    java.util.Scanner scan = new
    java.util.Scanner(System.in);
    do {
    displayBoard();
    playerVal = (turn % 2 == 0)? NOUGHT : CROSS;
    if (playerVal == NOUGHT) {
    System.out.println ("\n-0's turn-");
    } else {
    System.out.println("\n-X's turn-");
    }
    System.out.print("Enter row and Column:");
    try {
    set(playerVal, scan.nextInt());
    } catch (IllegalArgumentException ex)
    {System.err.println(ex);}
    turn ++;
    outcome = winOrTie("tie!");
    } while ( outcome == -2 );
    displayBoard();
    switch (outcome) {
    case NOUGHT:
    System.out.println("0 wins!");
    break;
    case CROSS:
    System.out.println("X wins!");
    break;
    case 0:
    System.out.println("Tie.");
    break;
    }
    }
    // TODO code application logic here

    private static void set(int playerVal, int nextInt) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }


    }


    }


    }


    }

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

    Default Re: I am completely lost...

    First remove all the uncessary curly braces }. There are three too many at the end.

    The method winOrTie() needs to loop over your gameboard to see if any player has 3-in-a-row. My guess is that you return NOUGHT or CROSS if either player wins, or 0 if nobody wins. The algorithm on how to walk the board is all up to you. There are various ways of doing that. Just pick an easy one and make sure it works. It doesn't have to be fast. For example, there are only 8 ways to win. You can check them all separately. If it works, it ain't stupid. :)

    If a method takes no parameters, you can't write: outcome = winOrTie("tie!"); you just write: outcome = winOrTie();

    Nitpick: the javadoc for the main method is written inside the winOrTie() method. You need to move that a few lines down so it sits on top of the main method.
    Last edited by SurfMan; 01-21-2017 at 09:11 AM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    Sabre is offline Member
    Join Date
    Jan 2017
    Posts
    13
    Rep Power
    0

    Default Re: I am completely lost...

    Thanks! I am going to work on it some more later today. Once again thanks!

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: I am completely lost...

    Good, glad to know you're enthousiastic :)

    Tips for the future:
    - Include any errors if you have any
    - Put your code in [code][/code] tags. It keeps the indent and formats it nicely. Better readable for all of us.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    Sabre is offline Member
    Join Date
    Jan 2017
    Posts
    13
    Rep Power
    0

    Default Re: I am completely lost...

    Okay I have removed the unnecessary curly braces }. I also moved the javadoc a few lines down and removed "tie!" from the = winOrTie(). Do you have a visual example of doing a loop over the game board for the winortie() method? I am looking in my Java beginners guide at while loops and it's a little confusing.

    thanks again.

Similar Threads

  1. New to Java and completely lost...
    By b2271916 in forum AWT / Swing
    Replies: 1
    Last Post: 02-18-2013, 01:47 AM
  2. For Loop Help! Completely Lost
    By whileloop in forum New To Java
    Replies: 8
    Last Post: 04-17-2012, 11:01 AM
  3. Help plz: not completely working JAR
    By linkingabo in forum NetBeans
    Replies: 6
    Last Post: 01-28-2012, 04:32 AM
  4. completely new
    By battosai16 in forum New To Java
    Replies: 1
    Last Post: 08-05-2010, 07:43 PM
  5. I am completely stuck
    By jpnym15 in forum New To Java
    Replies: 2
    Last Post: 11-14-2007, 06:40 PM

Posting Permissions

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