Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. var GameState = /** @class */ (function ()
  2. {
  3.     function GameState()
  4.     {
  5.         this.game_matrix = [[0, 0, 0],
  6.             [0, 0, 0],
  7.             [0, 0, 0]];
  8.     }
  9.     return GameState;
  10. }());
  11. var game_status = new GameState(); // luodaan ilmentymä (=olio) pelin tiedoista
  12.  
  13. function init()
  14. {
  15. ComputerShift();
  16. }
  17.  
  18. function ComputerTurn(row, col)
  19. {
  20.     // koneen siirrot
  21.     var strId; // HTML-elementin id
  22.     var imgPicture; // Viittaus sivulla olevaan kuva-elementtiin
  23.     game_status.game_matrix[row][col] = 1; // merkataan taulukossa koneen pelaamaksi
  24.     strId = "p" + row + "_" + col; // parsitaan HTML-elementin id
  25.     console.log("kone " + row + " " + col);
  26.     imgPicture = document.getElementById(strId); // haetaan viittaus elementtiin
  27.     imgPicture.src = "imgMarkO.png"; // muutetaan kuva
  28.     game_status.shift = 1; // ihmisen vuoro
  29. }
  30.  
  31. function HumanTurn(imgPicture)
  32. {
  33.     if (imgPicture.getAttribute("src") == "startingpositions.png")
  34.         {
  35.             var i, j;
  36.             i = parseInt(imgPicture.id.charAt(1)); // poimitaan rivi kuvan id:stä
  37.             j = parseInt(imgPicture.id.charAt(3)); // poimitaan sarake kuvan id:stä
  38.             console.log("ihminen " + i + " " + j);
  39.             game_status.game_matrix[i][j] = 2; // merkataan taulukossa koneen pelaamaksi
  40.             imgPicture.src = "imgMarkX.png"; // kuva nollaksi
  41.             console.log("ihminen kuva muutettu" + imgPicture.id);
  42.             game_status.shift = 2; // koneen vuoro
  43.             ComputerShift(); // kone valikoi seuraavan position
  44.             CheckWin();
  45.         }
  46.         else
  47.         {
  48.             alert ("Choose an empty square!")
  49.         }
  50. }
  51.  
  52. function ComputerShift()
  53. {
  54.     var i, j, row, col;
  55.     console.log("koneen tekoaly heraa");
  56.     i = 0;
  57.     while (i < 3)
  58.     {
  59.         j = 0;
  60.         while (j < 3)
  61.         {
  62.             if (game_status.game_matrix[i][j] === 0)
  63.             {
  64.                 row = i;
  65.                 col = j;
  66.                 i = 3;
  67.                 j = 3;
  68.             }
  69.             j = j + 1;
  70.         }
  71.         i = i + 1;
  72.     }
  73.     ComputerTurn(row, col);
  74. }
  75.  
  76. function CheckWin(imgPicture)
  77. {
  78. alert ("CheckWin activates");
  79. var winImagesX = "file:///D:/Koulu/Ohjelmointi%20II/MKLHelp/imgMarkX.png";
  80. var zeroZero = document.getElementById("p0_0").src;
  81. var zeroOne = document.getElementById("p0_1").src;
  82. var zeroTwo = document.getElementById("p0_2").src;
  83. var oneZero = document.getElementById("p1_0").src;
  84. var oneOne = document.getElementById("p1_1").src;
  85. var oneTwo = document.getElementById("p1_2").src;
  86. var twoZero = document.getElementById("p2_0").src;
  87. var twoOne = document.getElementById("p2_1").src;
  88. var twoTwo = document.getElementById("p2_2").src;
  89.  
  90. // alert("Right before the win condition checker.");
  91. alert("The value of zeroZero is " + zeroZero);
  92.  
  93. if (zeroZero == winImagesX && oneOne == winImagesX && twoTwo == winImagesX)
  94.     {
  95.         alert("Player wins!");
  96.     }
  97.     else if (zeroZero == winImagesX && zeroOne == winImagesX && zeroTwo == winImagesX)
  98.         {
  99.             alert("Player wins!");  //ja jatkuu eri vaihtoehdot else iffinä
  100.         }
  101. else if (zeroTwo == winImagesX && oneOne == winImagesX && twoZero == winImagesX)
  102. {
  103. alert("Player wins!");
  104. }
  105. else if (twoZero == winImagesX && oneOne == winImagesX && zeroTwo == winImagesX)
  106. {
  107. alert("Player wins!");
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement