tags:

views:

155

answers:

4

i've never been this frustrated in my life. cant even do the basics here.. just need to make a simple tic tac toe program. i feel so alone in this world right now.. i get the basic idea, but can't put it together logically.

Class instance variables:

  • private char[][] board; private char
  • player; // 'X' or 'O'

Methods:

  • public TicTacToe()
  • public void print()
  • public boolean play(String s)
  • public boolean won()
  • public boolean stalemate()

Here's what i've got for code:

import java.util.Scanner;

public class Six1
{
    public static void main(String[] args)
    {   
    TicTacToe ttt = new TicTacToe();
    ttt.TicTacToe();
    ttt.print();
    }

    static class TicTacToe
    {
        private char player; // 'X' or 'O'
        private char[][] board;

        // make board
        public TicTacToe()
        {
            // construct board
            board = new char[3][3];

            // initialize elements
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    board[i][j] = ' ' ;
                }
            }
        }

        // print board
        public void print()
        {  
            for ( int i = 0; i < 3; i++)
            {
                System.out.println("  ");   
                for( int j = 0; j < 3; j++)
                {
                    System.out.print(board[i][j] + " ");
                }
                System.out.println("\n------");
            }
        }
    }
}
+1  A: 

Break this problem down a bit and try not to tackle it as a whole.

For now focus on a method to test for a win. What do you want the function to return in each instances (Draw, unfinished game, player 1 won or player 2 won)?

How will you test for this win?

Once you have that method the rest should fit into place a lot more easily.

alphomega
all i'd like to do right now is just print a blank board.. i feel like i'm stuck. cant get it to compile. my brain cant make to connections between the classes, main method, creating a new board, and printing it... i feel like i have the methods in place, but i just cant call them! grrr
Bill S
@Bill: Ok well then you need to take another step back. What compiler problems are you having?
alphomega
@Bill - if it's not compiling, post the error you're getting
bemace
not sure what i need to place in my main method. i feel ike i can just call the method, then it will execute accordingly... am i missing something before i call methods? and i feel like there are issues with the public/private deal. it compiles, expect when i try to call a method in main..
Bill S
@Emil - the goal here is teaching people to fish, not handing fish out
bemace
@Bill - Okay, maybe you should try an even simpler program to start off with. Sounds like you might want to read up on some basics. http://download.oracle.com/javase/tutorial/.Do this, even if this program is an assignment due tomorrow or something.
alphomega
@Bill S first you need to learn the functionality of these two algorithms Min-Max and alpha-beta Pruning.
Suresh S
no, this is just the first portion of a two-part lab.. have to turn it in by Thurs. worth a whopping 2% of the total grade. my head gets wrapped around this stuff when i hit these walls, and it's hard to function when i cant figure it out..
Bill S
@Bill: You say it doesn't compile if you try to call the method from within main. That's probably a simple one: The main method is `static`, while the others are instance methods, so to call them, you need an instance of the class first: `TicTacToe ttt = new TicTacToe(); ttt.print();?
chiccodoro
OMG swear i tried that.. see i think that's the little guy i was missin.. lemme check quick
Bill S
@Bill: I posted it as an answer since I realized you already posted in your question what you tried to call in the main method, so when having tried it, you can post your results as a comment to my new answer.
chiccodoro
hmm nope droppin "TicTacToe ttt = new TicTacToe();" and "ttt.print();" into main doesnt compile.. gosh i'm stupid..
Bill S
@Bill: Edit your original question, add these two lines in the main method there and then write how you try to compile and run it, and add the error message you get. This will make it easier for us to help you.
chiccodoro
made the changes in my code, error message is about trying to access a non static variable from a non static method.. well i need some sleep guys. will tackle this problem in the A.M. i love all of you!
Bill S
so basically, my problem was that i failed to recognize the fact that i needed to have my board and player variables located within the TicTacToe class..
Bill S
+7  A: 

You don't have a lot done yet, but what you have seems mostly reasonable. You may be over-complicating things by using an inner class. And you'll need to put something in main if you want something to happen.

You can't write the whole program all at once. You can either start at the top and work down to the details, or work on the details and then assemble them into a working whole.

Working top-down

If you're not sure where to start, this can be a good way to get things moving. Write the main body of the code using whatever functions you wish existed. Maybe you'd start with

public static void main(String[] args) {
    printBoard();

    while (!isWinner()) {
        readMove(); // get move from stdin and mark on board
        printBoard(); // redraw board
    }

    printWinner(); // say who won
}

It's ok that these functions don't exist yet. Once you've got the main level, start implementing these made-up functions, using more made-up functions if necessary. Repeat until you get down to simple functions that you do know how to implement.

If you want to compile your code without implementing every method, you can use throw new UnsupportedOperationException("not implemented"); as the body of any methods that need to return values.

Working bottom-up

If you know you'll need certain pieces but aren't sure how they'll fit together, start with this method.

You know you'll need some way to ask the user what move they want to make. So create a function that does that and test it on it's own. You know you'll need a way to check if there's a winner. Hardcode some values into board[] and test your isWinner() function. Once you've got some working pieces you can assemble them into larger and larger chunks until you've got a functioning program.

bemace
i definitely like the top down method.. i know this is just basic 2D array stuff, with loops, if-statements, and other basics.. i understand them, but when it comes to actually combining everything i hit walls, then i get frustrated and quit, then i cant sleep, then i'm just a miserable person.. not good.
Bill S
+2  A: 

You say you don't know what to do in main:

public static void main(String[] args)
{   
// what the hell do i need here?!?!
// TicTacToe() <--???
// print() <-????
}

Simply calling the methods won't work for two reasons:

  1. main is a static method which runs without an instance of an object. The methods you want to call are instance methods, i.e. you need an object to call them on.

  2. You have defined an inner class. The methods are part of that inner class.

You can solve both of these issues, of course, although as bemace said, the 2. makes your program more complicated than necessary. You could just drop the "class TicTacToe" definition. If you want to keep it, you can create an instance of it like follows:

TicTacToe ttt = new TicTacToe();

Edit: Note that as NamshubWriter commented, this won't work unless you declare your inner class as static:

static class TicTacToe

Then you can call methods on it:

ttt.print();

The TicTacToe() you tried to call is actually a constructor. It is automatically called when you do a new TicTacToe() to create a new object.

If you put the above two lines in your main method, you should be a step further.

chiccodoro
Note that "new TicTacToe()" won't compile with the OP's code unless "static" is added before "class TicTacToe". The way it is currently written, it's an inner class, so it can only be created in an instance of class of type Six1
NamshubWriter
@NamshubWriter: Very good point!
chiccodoro
got the change made and it still doesn't work.. and this is why i question my ability to learn this.
Bill S
@Bill: Stop questioning yourself. You can learn this, but you have to investigate your problems with more patience and focus. "It doesn't work" isn't detailed enough. *What* happens? *What* kind of error message do you get? You still haven't told us that.
chiccodoro
@Bill "board" and "player" need to be defined inside of class TicTacToe. They are non-static (aka "member") variables, so they need to be accessed from an instance of an object. Do you need to have TicTacToe defined in a class named Six1? It's confusing things.
NamshubWriter
i'm just so sick of having to ask people for help for "easy" stuff like this.. seems like all that does is frustrate me further. i made the new board with TicTacToe ttt = new TicTacToe();, but then the method calls wont compile. not at home right now and i forget the error message. obviously there's a problem somewhere, but i have no idea.
Bill S
yeah Six1 is the class.. so you're saying i can just move those two variables into the TicTacToe class and i'll be good? well.. better i guess.
Bill S
@Bill: You should do that, however it'll probably not affect the error message you have, because the way how `TicTacToe` accesses them should work the other way as well.
chiccodoro
@Bill: By the way, as I stated in a comment to your question, `board[i][j] = " ";` should be `board[i][j] = ' ';` instead.
chiccodoro
cool, thanks again.
Bill S
+2  A: 

One thing that is confusing things is the inner/nested class. I'm assuming that the assignment requires you to have a class named Six1. I would make TicTacToe a top-level class. So you would have two source files in the same directory:

begin Six1.java

public class Six1 {
  public static void main(String[] args)
    TicTacToe ttt = new TicTacToe();
    ttt.play();
  }
}

end Six1.java

begin TicTacToe.java

public class TicTacToe {
  private char[][] board;
  private char player; // 'X' or 'O'

  /**
   * Constructs a new Tic Tac Toe object
   */
  public TicTacToe() {
    // initialize board
  }

  /**
   * Plays a game
   */
  public void play() {
    // play the game
    print();
  }

  /**
   * Prints the board
   */
  public void print() {
  }
}

end TicTacToe.java

NamshubWriter
yes that definitely was one of the main confusing aspects for me.. understanding the relationship between the class with thew main method, then the other class with the methods that actually do stuff. thank you for the clarification.
Bill S
Note you could put the main method in the `TicTacToe` class (the exact same code for main would work)
NamshubWriter
error i get is "not static variable this cannot be referenced from a static object"
Bill S
as you discovered, you get that error if you have a static inner class trying to reference a non-static variable in the outer class. This sort of headache is why I suggest putting the classes in different files instead of using nested or inner classes.
NamshubWriter