Advertisement
Guest User

Untitled

a guest
Mar 11th, 2023
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5.63 KB | Source Code | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>Document</title>
  8.     <style>
  9.         html,body{
  10.             height:100%;
  11.             width:100%;
  12.             display: flex;
  13.             flex-direction: column;
  14.             align-items: center;
  15.             justify-content: center;
  16.             padding:0px;
  17.             margin:0px;
  18.         }
  19.         .display{
  20.             height:50%;
  21.             width:50%;
  22.             display: flex;
  23.             flex-wrap: wrap;
  24.             padding:0px;
  25.             margin:0px;
  26.             border: 2px solid #000000;
  27.         }
  28.         .mapPart{
  29.             height: 33%;
  30.             width: 32.333%;
  31.             padding:0px;
  32.             margin:-1px;
  33.             display: flex;
  34.             align-items: center;
  35.             justify-content: center;
  36.             border:1px solid #000000;
  37.         }
  38.     </style>
  39. </head>
  40. <body>
  41.     <div class="display" id="display"></div>
  42.     <script>
  43.         const CircleOrCross = ["none","none","O","X"];
  44.         let currentPlayer = 0;
  45.         let MapArray = [];
  46.         for (let i = 0;i<3;i++) {
  47.            MapArray.push([]);
  48.            for (let j = 0;j<3;j++) {
  49.                MapArray[i].push(2);
  50.                let div = document.createElement('div');
  51.                div.id = "partNo"+i+"/"+j;
  52.                div.className = 'mapPart';
  53.                div.setAttribute("onclick", "set("+i+","+j+")")
  54.                document.getElementById("display").appendChild(div);
  55.                document.getElementById("partNo"+i+"/"+j).style.height = 100/3+"%";
  56.                document.getElementById("partNo"+i+"/"+j).style.width = 100/3+"%";
  57.                console.log("set MapArray property no["+i+"]["+j+"]"+MapArray);
  58.            }
  59.        }
  60.        function set(x,y) {
  61.            if (MapArray[x][y] == 2){
  62.                let img = document.createElement('img');
  63.                img.src = CircleOrCross[currentPlayer];
  64.                img.alt = CircleOrCross[currentPlayer+2];
  65.                document.getElementById("partNo"+x+"/"+y).appendChild(img);
  66.                MapArray[x][y] = currentPlayer;
  67.                let check = 0;
  68.                let j = y-2;
  69.                for (let i = x-2; i < 5-2;i++){
  70.                    try {
  71.                        if (MapArray[i][j] == currentPlayer && x+"/"+y != i+"/"+j){
  72.                            check++;
  73.                            console.log("left to right cross check ="+check);
  74.                        }
  75.                    }catch{}
  76.                    if (checkIfCheck2(check)){
  77.                        break;
  78.                    }
  79.                    j++;
  80.                }
  81.                check = 0;
  82.                j = y+2;
  83.                for (let i = x-2; i < 5-2;i++){
  84.                    try {
  85.                        if (MapArray[i][j] == currentPlayer && x+"/"+y != i+"/"+j){
  86.                            check++;
  87.                            console.log("right to left cross check ="+check);
  88.                        }
  89.                    }catch{}
  90.                    if (checkIfCheck2(check)){
  91.                        break;
  92.                    }
  93.                    j--;
  94.                }
  95.                check = 0;
  96.                j = y;
  97.                for (let i = x-2; i < 5-2;i++){
  98.                    try {
  99.                        if (MapArray[i][j] == currentPlayer && x+"/"+y != i+"/"+j){
  100.                            check++;
  101.                            console.log("vertical check="+check);
  102.                        }
  103.                    }catch{}
  104.                    if (checkIfCheck2(check)){
  105.                        break;
  106.                    }
  107.                }
  108.                check = 0;
  109.                i = x;
  110.                for (let j = y-2; j < 5-2;j++){
  111.                    try {
  112.                        if (MapArray[i][j] == currentPlayer && x+"/"+y != i+"/"+j){
  113.                            check++;
  114.                            console.log("horisontal check ="+check);
  115.                        }
  116.                    }catch{}
  117.                    if (checkIfCheck2(check)){
  118.                        break;
  119.                    }
  120.                }
  121.                check = 0;
  122.                for (i = 0; i < 3;i++){
  123.                    if (MapArray[i].includes(2)){
  124.                        break;
  125.                    } else {
  126.                        check++;
  127.                        console.log("no free spaces check="+check);
  128.                    }
  129.                }
  130.                if (check == 3){
  131.                    let div=document.createElement('div');
  132.                    div.innerHTML = "draw";
  133.                    document.body.appendChild(div);
  134.                    reset();
  135.                }
  136.                currentPlayer = (currentPlayer - 1)*(currentPlayer - 1);
  137.            }
  138.        }
  139.        function checkIfCheck2(check){
  140.            if (check >= 2){
  141.                 let div=document.createElement('div');
  142.                 div.innerHTML = CircleOrCross[currentPlayer+2]+"'s won";
  143.                 document.body.appendChild(div);
  144.                 reset();
  145.                 return true;
  146.             }
  147.         }
  148.         function reset() {
  149.             for (let i = 0; i < 3; i++){
  150.                for (let j = 0; j < 3; j++){
  151.                    document.getElementById("partNo"+i+"/"+j).textContent = '';
  152.                    MapArray[i][j] = 2;
  153.                    console.log("set MapArray property no["+i+"]["+j+"]");
  154.                }
  155.            }
  156.        }
  157.    </script>
  158. </body>
  159. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement