How to manage the account in the game tic-tac-toe, written in js, through a cookie?
Actually, here is my code, but account management is not performed
<title>Крестики-нолики</title> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <meta name="generator" content="MS Notepad"> <style> body{background-image: url(background.jpg); background-attachment: fixed; background-size: 100%;} h1{text-align:center; font-family: 'Times New Roman' cursive;} #result { width: 200px; height: 200px; background: yellow; } </style> <script src="https://code.jquery.com/jquery-3.1.0.js"></script> </head> <body> <h1><b> Крестики-нолики</b></h1> <br> </p> <div align="center"><center> <table border="0"> <tr> <td align="center"><img src="bl.gif" name="pole0" onClick="Place(0)" width="100" height="100"></td> <td align="center"><img src="bl.gif" name="pole1" onClick="Place(1)" width="100" height="100"></td> <td align="center"><img src="bl.gif" name="pole2" onClick="Place(2)" width="100" height="100"></td> </tr> <tr> <td align="center"><img src="bl.gif" name="pole3" onClick="Place(3)" width="100" height="100"></td> <td align="center"><img src="bl.gif" name="pole4" onClick="Place(4)" width="100" height="100"></td> <td align="center"><img src="bl.gif" name="pole5" onClick="Place(5)" width="100" height="100"></td> </tr> <tr> <td align="center"><img src="bl.gif" name="pole6" onClick="Place(6)" width="100" height="100"></td> <td align="center"><img src="bl.gif" name="pole7" onClick="Place(7)" width="100" height="100"></td> <td align="center"><img src="bl.gif" name="pole8" onClick="Place(8)" width="100" height="100"></td> </tr> </table> </center></div> <div id="result"></div> <script type="text/javascript"> var c = new Array(9); for (i=0; i<9; i++) c[i] = 0; x = new Image(100,100); x.src = "x.gif"; o = new Image(100,100); o.src = "o.gif"; e = new Image(100,100); e.src = "bl.gif"; function Cross(value) { if (value == 0) document.pole0.src = x.src; if (value == 1) document.pole1.src = x.src; if (value == 2) document.pole2.src = x.src; if (value == 3) document.pole3.src = x.src; if (value == 4) document.pole4.src = x.src; if (value == 5) document.pole5.src = x.src; if (value == 6) document.pole6.src = x.src; if (value == 7) document.pole7.src = x.src; if (value == 8) document.pole8.src = x.src; } function Zero(value) { if (value == 0) document.pole0.src = o.src; if (value == 1) document.pole1.src = o.src; if (value == 2) document.pole2.src = o.src; if (value == 3) document.pole3.src = o.src; if (value == 4) document.pole4.src = o.src; if (value == 5) document.pole5.src = o.src; if (value == 6) document.pole6.src = o.src; if (value == 7) document.pole7.src = o.src; if (value == 8) document.pole8.src = o.src; } function CheckVictory() { if (c[0] == c[1] && c[1] == c[2] && c[2] > 0) return true; if (c[3] == c[4] && c[4] == c[5] && c[5] > 0) return true; if (c[6] == c[7] && c[7] == c[8] && c[8] > 0) return true; if (c[6] == c[3] && c[3] == c[0] && c[0] > 0) return true; if (c[7] == c[4] && c[4] == c[1] && c[1] > 0) return true; if (c[8] == c[5] && c[5] == c[2] && c[2] > 0) return true; if (c[6] == c[4] && c[4] == c[2] && c[2] > 0) return true; if (c[0] == c[4] && c[4] == c[8] && c[8] > 0) return true; } function Comp() { for (i=0; i<9; i++) if (c[i] == 0) ph = i; for (i=0; i<3; i++) { if (c[0] == c[1] && c[2] == 0 && c[0] == i) ph = 2; if (c[0] == c[2] && c[1] == 0 && c[0] == i) ph = 1; if (c[1] == c[2] && c[0] == 0 && c[2] == i) ph = 0; if (c[3] == c[4] && c[5] == 0 && c[3] == i) ph = 5; if (c[3] == c[5] && c[4] == 0 && c[3] == i) ph = 4; if (c[4] == c[5] && c[3] == 0 && c[5] == i) ph = 3; if (c[6] == c[7] && c[8] == 0 && c[6] == i) ph = 8; if (c[6] == c[8] && c[7] == 0 && c[6] == i) ph = 7; if (c[7] == c[8] && c[6] == 0 && c[8] == i) ph = 6; if (c[6] == c[3] && c[0] == 0 && c[6] == i) ph = 0; if (c[6] == c[0] && c[3] == 0 && c[6] == i) ph = 3; if (c[3] == c[0] && c[6] == 0 && c[3] == i) ph = 6; if (c[7] == c[4] && c[1] == 0 && c[7] == i) ph = 1; if (c[7] == c[1] && c[4] == 0 && c[7] == i) ph = 4; if (c[4] == c[1] && c[7] == 0 && c[4] == i) ph = 7; if (c[8] == c[5] && c[2] == 0 && c[8] == i) ph = 2; if (c[8] == c[2] && c[5] == 0 && c[8] == i) ph = 5; if (c[5] == c[2] && c[8] == 0 && c[5] == i) ph = 8; if (c[6] == c[4] && c[2] == 0 && c[6] == i) ph = 2; if (c[6] == c[2] && c[4] == 0 && c[6] == i) ph = 4; if (c[4] == c[2] && c[6] == 0 && c[4] == i) ph = 6; if (c[0] == c[4] && c[8] == 0 && c[0] == i) ph = 8; if (c[0] == c[8] && c[4] == 0 && c[0] == i) ph = 4; if (c[4] == c[8] && c[0] == 0 && c[4] == i) ph = 0; } Zero(ph); c[ph] = 2; if (CheckVictory() == true) { alert("Очень жаль, но Вы проиграли. Попытайтесь еще и у Вас получится!)"); GameOver(); } } function setCookie(name, value, options) { options = options || {}; var expires = options.expires; if (typeof expires == "number" && expires) { var d = new Date(); d.setTime(d.getTime() + expires * 1000); expires = options.expires = d; } if (expires && expires.toUTCString) { options.expires = expires.toUTCString(); } value = encodeURIComponent(value); var updatedCookie = name + "=" + value; for (var propName in options) { updatedCookie += "; " + propName; var propValue = options[propName]; if (propValue !== true) { updatedCookie += "=" + propValue; } } document.cookie = updatedCookie; } function GameOver() { for (i=0; i<9; i++) c[i] = 0; document.pole0.src = e.src; document.pole1.src = e.src; document.pole2.src = e.src; document.pole3.src = e.src; document.pole4.src = e.src; document.pole5.src = e.src; document.pole6.src = e.src; document.pole7.src = e.src; document.pole8.src = e.src; } function CheckNobody() { nobody = false; for (i=0; i<9; i++) if (c[i] == 0) nobody = true; if (nobody == false) { alert("Ничья. Вы были дойстойным соперником!"); var inprt = prompt("Введите ваше имя"); var f = document.cookie.split(";"); var t; if(document.cookie.length > 0) t = f.length+1; else t = 0; document.cookie = "user"+t+"="+inprt; document.getElementById("result").innerHTML = document.cookie; GameOver(); } } function Place(value) { if (c[value] == 0) { Cross(value); c[value] = 1; if (CheckVictory() == true) { alert("Вы выиграли! Поздравляем!!!"); GameOver(); } else { CheckNobody(); Comp(); CheckNobody(); } } } I don’t really know js yet, so I can’t figure it out
ru.stackoverflow.coman example so as not to leaveru.stackoverflow.comand understand - spectre_it