From 80f557a7b78b9a8134c7b478dc940c2a9662c0b2 Mon Sep 17 00:00:00 2001 From: nanochess Date: Sat, 13 Mar 2021 21:09:33 -0600 Subject: [PATCH] Ported TICTACTOE1 to Javascript --- 89 Tic-Tac-Toe/javascript/tictactoe1.html | 9 ++ 89 Tic-Tac-Toe/javascript/tictactoe1.js | 147 ++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 89 Tic-Tac-Toe/javascript/tictactoe1.html create mode 100644 89 Tic-Tac-Toe/javascript/tictactoe1.js diff --git a/89 Tic-Tac-Toe/javascript/tictactoe1.html b/89 Tic-Tac-Toe/javascript/tictactoe1.html new file mode 100644 index 00000000..7aafc617 --- /dev/null +++ b/89 Tic-Tac-Toe/javascript/tictactoe1.html @@ -0,0 +1,9 @@ + + +TIC TAC TOE 1 + + +

+
+
+
diff --git a/89 Tic-Tac-Toe/javascript/tictactoe1.js b/89 Tic-Tac-Toe/javascript/tictactoe1.js
new file mode 100644
index 00000000..fce4b7da
--- /dev/null
+++ b/89 Tic-Tac-Toe/javascript/tictactoe1.js	
@@ -0,0 +1,147 @@
+// TIC TAC TOE 1
+//
+// Converted from BASIC to Javascript by Oscar Toledo G. (nanochess)
+//
+
+function print(str)
+{
+    document.getElementById("output").appendChild(document.createTextNode(str));
+}
+
+function input()
+{
+    var input_element;
+    var input_str;
+    
+    return new Promise(function (resolve) {
+                       input_element = document.createElement("INPUT");
+                       
+                       print("? ");
+                       input_element.setAttribute("type", "text");
+                       input_element.setAttribute("length", "50");
+                       document.getElementById("output").appendChild(input_element);
+                       input_element.focus();
+                       input_str = undefined;
+                       input_element.addEventListener("keydown", function (event) {
+                                                      if (event.keyCode == 13) {
+                                                      input_str = input_element.value;
+                                                      document.getElementById("output").removeChild(input_element);
+                                                      print(input_str);
+                                                      print("\n");
+                                                      resolve(input_str);
+                                                      }
+                                                      });
+                       });
+}
+
+function tab(space)
+{
+    var str = "";
+    while (space-- > 0)
+        str += " ";
+    return str;
+}
+
+function mf(x)
+{
+    return x - 8 * Math.floor((x - 1) / 8);
+}
+
+function computer_moves()
+{
+    print("COMPUTER MOVES " + m + "\n");
+}
+
+var m;
+
+// Main control section
+async function main()
+{
+    print(tab(30) + "TIC TAC TOE\n");
+    print(tab(15) + "CREATIVE COMPUTING  MORRISTOWN, NEW JERSEY\n");
+    print("\n");
+    print("\n");
+    print("\n");
+    //
+    // This program plays Tic Tac Toe
+    // The machine goes first
+    print("THE GAME BOARD IS NUMBERED:\n");
+    print("\n");
+    print("1  2  3\n");
+    print("8  9  4\n");
+    print("7  6  5\n");
+    print("\n");
+    //
+    // Main program
+    while (1) {
+        print("\n");
+        print("\n");
+        a = 9;
+        m = a;
+        
+        computer_moves();
+        print("YOUR MOVE");
+        m = parseInt(await input());
+        
+        p = m;
+        b = mf(p + 1);
+        m = b;
+        
+        computer_moves();
+        print("YOUR MOVE");
+        m = parseInt(await input());
+        
+        q = m;
+        if (q != mf(b + 4)) {
+            c = mf(b + 4);
+            m = c;
+            computer_moves();
+            print("AND WINS ********\n");
+            continue;
+        }
+        
+        c = mf(b + 2);
+        m = c;
+
+        computer_moves();
+        print("YOUR MOVE");
+        m = parseInt(await input());
+
+        r = m;
+        if (r != mf(c + 4)) {
+            d = mf(c + 4);
+            m = d;
+            computer_moves();
+            print("AND WINS ********\n");
+            continue;
+        }
+        
+        if (p % 2 == 0) {
+            d = mf(c + 7);
+            m = d;
+            computer_moves();
+            print("AND WINS ********\n");
+            continue;
+        }
+        
+        d = mf(c + 3);
+        m = d;
+        
+        computer_moves();
+        print("YOUR MOVE");
+        m = parseInt(await input());
+        
+        s = m;
+        if (s != mf(d + 4)) {
+            e = mf(d + 4);
+            m = e;
+            computer_moves();
+        }
+        e = mf(d + 6);
+        m = e;
+        computer_moves();
+        print("THE GAME IS A DRAW.\n");
+    }
+}
+
+main();