-1

How to get winner detection? My code isn't working right now i only need the winner detection if someone can help me then do this .

$(document).ready(function() {

    var xoro = 1;

    $('#reset').on("click", function() {
        $('img').attr("src", "blank.png");
    });

    $('img').on("click", function() {

        var tmp = $(this).attr("src");

        if (tmp == "blank.png" && xoro == 1) {
            $(this).attr("src", "x.png");
            xoro = 0;
        } else if (tmp == "blank.png" && xoro == 0) {
            $(this).attr("src", "o.png");
            xoro = 1;
        }

    });
});
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
AlenB
  • 3
  • 2

2 Answers2

0

With your code you cant really determine a winner.

Options you have are : Give your images ids . like Let say we have this 3x3 field and we say its a 2dimensional array. Then the top left field is [0][0] and your bottom right is [3][3] Your HTML code should be somethig like this

<img src="x.png" id="0-0"></img><img src="blank.png" id="0-1"></img>...

And so on. After youve eine that you have to Start writing a huge if statement with all the winning Casey where you check if the SRC are all "x.PNG" or so.

EG: if ID 0-0,0-1 and 0-2 all have the SRC x.PNG x wins .

Best is to put these into a function named checkForWinner() and then just call this Funktion after every click or so

I hope i could help you out.

flx
  • 1,560
  • 13
  • 22
  • can you make a example of checkrow or so? – AlenB Mar 07 '15 at 12:28
  • https://jsfiddle.net/wcftsh6v/ somehow like this. really sloppy way to do it . but i had to make a quick example for you since i dont have the patience to make a dynamical fine one – flx Mar 07 '15 at 14:39
0

This question has already been answered here (in Java language though but the idea is the same). For that I suggest you store every move in a matrix which you can the use to determine the winner.

In addition, you have to make the images identifiable (as zeropublix suggested) so that you can actually capture the user's input and fill the matrix.

Community
  • 1
  • 1
Harijoe
  • 1,771
  • 1
  • 16
  • 27