2017-05-20 5 views
0

Ich habe gerade angefangen, in JS zu programmieren, also versuche ich einige klassische Übungen. Jetzt mache ich eine einfache Tris Spiel, aber ich habe ein Problem mit der Sieg-Check-Funktion, die ich hier gepostet habe. Irgendwelche Ideen?Tris-Spiel in JavaScript

function controllo() { 
 
    var con = 0; 
 

 
    if (tab['usx'] == tab['uc'] == tab['udx'] == 'X' || 
 
     tab['usx'] == tab['uc'] == tab['udx'] == 'O') //Orizzontali 
 
    con = 1; 
 
    else if (tab['csx'] == tab['cc'] == tab['cdx'] == 'X' || 
 
      tab['csx'] == tab['cc'] == tab['cdx'] == 'O') 
 
    con = 1; 
 
    else if (tab['dsx'] == tab['dc'] == tab['ddx'] == 'X' || 
 
      tab['dsx'] == tab['dc'] == tab['ddx'] == 'O') 
 
    con = 1; 
 
    else if (tab['usx'] == tab['cc'] == tab['ddx'] == "X" || 
 
      tab['usx'] == tab['cc'] == tab['ddx'] == "O") //Diagonali 
 
    con = 1; 
 
    else if (tab['udx'] == tab['cc'] == tab['dsx'] == 'X' || 
 
      tab['udx'] == tab['cc'] == tab['dsx'] == 'O') 
 
    con = 1; 
 
    else if (tab['udx'] == tab['cdx'] == tab['ddx'] == 'X' || 
 
      tab['udx'] == tab['cdx'] == tab['ddx'] == 'O') //Verticali 
 
    con = 1; 
 
    else if (tab['uc'] == tab['cc'] == tab['dc'] == 'X' || 
 
      tab['uc'] == tab['cc'] == tab['dc'] == 'O') 
 
    con = 1; 
 
    else if (tab['udx'] == tab['cdx'] == tab['ddx'] == 'X' || 
 
      tab['udx'] == tab['cdx'] == tab['ddx'] == 'O') 
 
    con = 1; 
 

 
    return (con); 
 
}

tab ist ein globales Array und con ist eine lokale Flagge. "X" und "O" bedeuten die 2 verschiedenen Spieler. Das Problem ist, dass alle Bedingungen immer falsch sind.

+0

Ein Problem ist, dass Tab ['usx'] == tab ['uc'] == tab ['udx'] == 'X'' funktioniert nicht so, wie Sie erwarten. Es sollte "tab ['usx'] == 'X' && tab ['uc'] == 'X' && tab ['udx'] == 'X'' sein. –

+0

Um zu demonstrieren, was @ GOTO0 gesagt hat: '" x "==" x "==" x "' ergibt 'falsch' aber '" x "==" x "== wahr' ergibt 'wahr' - das ist es nicht was du willst. –

+0

Zu viel falsch damit, und diese Art von Frage gehört nicht in dieses Forum – vol7ron

Antwort

0

this implementation In erster Linie sehen erstellen Sie eine Konstante mit den möglichen Siegen

var LINES = [ 
    [0, 1, 2], 
    [3, 4, 5], 
    [6, 7, 8], 
    [0, 3, 6], 
    [1, 4, 7], 
    [2, 5, 8], 
    [0, 4, 8], 
    [2, 4, 6] 
]; 

, die Sie als bei der Implementierung können Sie die X- oder O-zählen und wenn es drei sind es gewonnen hat. Der Rest der Umsetzung überlasse ich Ihnen. Ist wie this