-2

I've made a Tic-Tac-Toe game, and yes some of you probably think my program is not very good. I am a new programmer and have never used swing or anything related to a JAVA GUI before this game. The program works, MOSTLY. When you compile and run it, and click player vs computer, the game gets stuck after 3-4 turns. I've been trying to figure out what the problem is for hours and can't seem to figure it out. The code is quite long... Any help is appreciated!

Here is the code: http://txt.do/6dvm

Sam Mitchell
  • 194
  • 2
  • 18
  • 6
    Nobody is going to read all that code. Your best bet: Add logging to each and every single method/within each method. Run the app again and view the logs to see where it's getting stuck, then you can't start working backwards to determine why. – Kon Jan 07 '15 at 20:36
  • Have you tried debugging with a IDE? Like Eclipse or NetBeans? – Jonathan Drapeau Jan 07 '15 at 20:37
  • @Kon I've tried that and I used almost the exact same way I created the player vs player game mode, just changed it to work with the computer, and it just gets stuck. You don't need to read all that code I provided it incase anyone wanted to run it. The problem only occurred in the last class, because that's when it showed up – Sam Mitchell Jan 07 '15 at 20:38
  • I see that boolean comparisons like `bool==true` and `bool==false` have become a plague. – Jagger Jan 07 '15 at 20:39
  • 1
    Your program ends with an exception `Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException` after typing in a user name. – Jagger Jan 07 '15 at 20:44
  • @JonathanDrapeau never used those programs before, but I will try now! – Sam Mitchell Jan 07 '15 at 20:44
  • @Jagger it works fine for me and I checked to make sure I pasted the exact same code I have. I'm currently using Dr. Java – Sam Mitchell Jan 07 '15 at 20:46
  • @Mr_Wizerman69 I have pasted your code 1 to 1 are you observing the console output after typing in the user name? – Jagger Jan 07 '15 at 20:49
  • @Mr_Wizerman69 This is what I get http://textuploader.com/6dvz – Jagger Jan 07 '15 at 20:51
  • @Jagger Yes, and it then asks if you would like to continue or not. "enter 1 to continue, 0 to cancel". I'm assuming you're talking about the player vs computer button? – Sam Mitchell Jan 07 '15 at 20:51
  • @Jagger I don't get any errors whatsoever, and I'm 100% sure this is the same code I am compiling – Sam Mitchell Jan 07 '15 at 20:52
  • In the future, no more links to code as they are absolutely no help to future users of this site. Please put in the effort to try to isolate your error and post the **relevant** code here, preferably an [mcve](http://stackoverflow.com/help/mcve). – Hovercraft Full Of Eels Jan 07 '15 at 22:50
  • Edit: And I gave you this same MCVE link in your last question -- do us all a favor and read it. – Hovercraft Full Of Eels Jan 07 '15 at 22:51
  • @HovercraftFullOfEels as I said I am a new programmer and I also said I spent hours trying to isolate my error. For you it probably would have been easier, however, my skill level in JAVA is no where near yours. It may take me longer to isolate a hard-to-find error than it would you. My past questions on this site have dealt with an isolated error because they were small programs. The only reason I posted the link was because I had trouble isolating it. I will not post links to code again. I do however believe it would help future users because answers would still show where changes were made. – Sam Mitchell Jan 07 '15 at 22:57
  • My experience here is that if you do this again, post links to code and don't post relevant code here, regardless of your expressions above, I can almost guarantee that your question will garner down-votes. And if you get enough of those you can risk being question banned. So for your benefit as well, in the future, please don't do this. At least **attempt** to post relevant code. You didn't even do that for this question. – Hovercraft Full Of Eels Jan 07 '15 at 23:32

1 Answers1

2

your computers logic is a bit... flawed:

int row = (int)(2*Math.random())+1;
int column = (int)(2*Math.random())+1;
while (button[row][column].isEnabled()==false)
{
  row = (int)(2*Math.random())+1;
  column = (int)(2*Math.random())+1;
}

in an array of size 3x3 do you see any problem with the numbers its generating? What I see here is numbers 1<=x<=2 being generated. What that means is nothing in column 1 (or array index of 0) ever being chosen. What this means is when all locations other than the first row and column are taken you get stuck in an endless loop. The logic should read:

int row = (int)(3*Math.random());
int column = (int)(3*Math.random());
while (button[row][column].isEnabled()==false)
{
  row = (int)(3*Math.random());
  column = (int)(3*Math.random());
}

or better:

int row, column;
do
{
  row = (int)(3*Math.random());
  column = (int)(3*Math.random());
} while (!button[row][column].isEnabled()); // stop with the == boolean

this still leaves you with and infinite loop if no rows are enabled and a very bad implementation of a random selection that could take a while, heres a better way:

// list containing available locations for the computer to choose
java.util.List<int[]> availableIndexes = new java.util.ArrayList<int[]>();

// go through all our buttons and make a list of ones that are enabled
for(int r=0;r<button.length;r++) {
    for(int c=0;c<button[r].length;c++) {
        if(button[r][c].isEnabled()) {
            availableIndexes.add(new int[]{r, c});
        }
    }
}

// see if we can even do anything
if(availableIndexes.isEmpty()) {
    // cats game, i dont know what you want to do there....
} else {
    // choose one of our avaible buttons at random
    int [] randomButtonIndex = availableIndexes.get((int)(Math.random()*availableIndexes.size()));
    int row = randomButtonIndex[0];
    int column = randomButtonIndex[1];


    // .... continue what you were doing
}


Some other stuff from comments I saw:
  • Learn to use an IDE, Netbeans, IntelliJ or Eclipse are good ones to look at.
  • Use data structures, loops and methods to your advantage. Things like button[0][0].addActionListener(new playerVSComputer()); button[0][1].addActionListener(new playerVSComputer());.... can be done with a loop and will help your code become more clear.

For further help try posting a question on the code review section of stack exchange.

Community
  • 1
  • 1
ug_
  • 11,267
  • 2
  • 35
  • 52
  • I tried the for loop and it almost worked like you said, I will try your other suggestions later or tomorrow, but I trust they work given the for loop almost did. Thank you very much! I will also look into what you and the others said, thanks! – Sam Mitchell Jan 07 '15 at 21:44
  • The warning I get when I run the program is the values of r and c are not used. I changed them to my variables and it still says that... So when I tried your list it actually stopped the computer from playing any O's, but I think I'm doing something wrong because you would not have suggested it if it did not work. What I did is I copied the list suggestion and changed the r and c variables to rows and columns. Then I put in my other code where it would have gone in the if statement and after it. – Sam Mitchell Jan 08 '15 at 15:24
  • Just so future users know I used: `int row, column; do { row = (int)(3*Math.random()); column = (int)(3*Math.random()); } while (!button[row][column].isEnabled());` and changed some booleans, now I'm pretty sure it works THANKS! – Sam Mitchell Jan 09 '15 at 15:03