Tic Tac Toe Glitching

Tic Tac Toe Glitching
0.0 0

#1

I am trying to get my tic tac toe game to place a random O for each X. (If you test it choose to play x because the o doesn’t work right now.) It works about 50 percent of the time, if you test it out a few times you will see what I mean. If someone is brave enough to look through my over complicated and mess of a code to help me figure out this glitch I would very much appreciate it.


#2

Can you explain what you mean by it works a few times? What works a few times? Try to be as detailed as possible about what your project is doing that does not result in the way you expect.

One problem I currently see is that when I make 3 X’s in a row or column, the game keeps playing and does not tell me that I have one when it should. Is that the problem you are talking about?

Another problem is even if I do not have 3 X’s in a row or column sometimes, the game says I win.

Also, before I would attempt to look at your very non-DRY (do not repeat yourself) code, you would need to make an effort to make it DRY. You have so many cases where you could create a single function that would easily eliminate 60-70 lines of code. Case in point is the following Switch statement:

    switch (current) {
      case 1:
        $("#XTL").html("O");
        topLefto = true;
        xWin();
        var index = AIList.indexOf(1);
        AIList.splice(index, 1);
        break;

      case 2:
        $("#XTM").html("O");
        topMiddleo = true;
        xWin();
        var index = AIList.indexOf(2);
        AIList.splice(index, 1);
        break;

      case 3:
        $("#XTR").html("O");
        topRighto = true;
        xWin();
        var index = AIList.indexOf(3);
        AIList.splice(index, 1);
        break;

      case 4:
        $("#XML").html("O");
        middleLefto = true;
        xWin();
        var index = AIList.indexOf(4);
        AIList.splice(index, 1);
        break;

      case 5:
        $("#XMM").html("O");
        middleMiddleo = true;
        xWin();
        var index = AIList.indexOf(5);
        AIList.splice(index, 1);
        break;

      case 6:
        $("#XMR").html("O");
        middleRighto = true;
        xWin();
        var index = AIList.indexOf(6);
        AIList.splice(index, 1);
        break;

      case 7:
        $("#XBL").html("O");
        bottomLefto = true;
        xWin();
        var index = AIList.indexOf(7);
        AIList.splice(index, 1);
        break;

      case 8:
        $("#XBM").html("O");
        bottomMiddleo = true;
        xWin();
        var index = AIList.indexOf(8);
        AIList.splice(index, 1);
        break;

      case 9:
        $("#XBR").html("O");
        bottomRighto = true;
        xWin();
        var index = AIList.indexOf(9);
        AIList.splice(index, 1);
        break;
    }

Just look at all those lines of repeated code. It is a great opportunity for you to write a resealable function to reduce lines of repeat code.


#3

Ok I will start with that. When I run it, it will tell me when I have won, but somtimes the O will no happen.
Do you have any advice for making a repetitive function when every case has different variables in it? For instance, how could I have repetition when for case 2 the index is different as well as the variable that is coming true. For instance, in case 2, topMiddleo is becoming true, but for case 3, it’s topRighto.

Also thank you so much for your help. you’ve been such a great help throughout all of my projects.