-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.js
170 lines (162 loc) · 5.39 KB
/
tictactoe.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const http = new XMLHttpRequest()
http.open("GET", "https://localhost:2000")
http.send()
http.onload = () => console.log(http.responseText)
// var scr = document.getElementById("ttc");
// var ctx = scr.getContext("2d");
// ctx.strokeRect(0, 0, 100, 100);
// ctx.strokeRect(100, 0, 100, 100);
// ctx.strokeRect(200, 0, 100, 100);
// ctx.strokeRect(0, 100, 100, 100);
// ctx.strokeRect(100, 100, 100, 100);
// ctx.strokeRect(200, 100, 100, 100);
// ctx.strokeRect(0, 200, 100, 100);
// ctx.strokeRect(100, 200, 100, 100);
// ctx.strokeRect(200, 200, 100, 100);
// //This class shall be mostly reformulated, since it is implemented in the API
// class Board{
// constructor(){
// this.gboard = [[null, null, null],
// [null, null, null],
// [null, null, null]];
// }
// //This function shall be removed, since it is implemented in the API
// check(){
// var winner = false;
// var champion = null;
// for(var i = 0; i < 3; i++){
// if((this.gboard[i][0] == this.gboard[i][1])&&(this.gboard[i][0] == this.gboard[i][2])
// &&(this.gboard[i][0] != null)){
// winner = true;
// champion = this.gboard[i][0];
// }
// else if((this.gboard[0][i] == this.gboard[1][i])&&
// (this.gboard[0][i] == this.gboard[2][i])&&(this.gboard[0][i] != null)){
// winner = true;
// champion = this.gboard[0][i];
// }
// }
// if(!(winner)){
// if((this.gboard[0][0] == this.gboard[1][1])&&
// (this.gboard[0][0] == this.gboard[2][2])&&(this.gboard[0][0] != null)){
// winner = true;
// champion = this.gboard[0][0];
// }
// else if((this.gboard[0][2] == this.gboard[1][1])&&
// (this.gboard[2][0] == this.gboard[0][2])&&(this.gboard[0][2] != null)){
// winner = true;
// champion = this.gboard[0][2];
// }
// }
// return [winner, champion];
// }
// post_check(){
// var results = this.check()
// if(results[0] == false){
// var procceed = false;
// for(var i = 0; i < 3; i++){
// for(var j = 0; j < 3; j++){
// if(this.gboard[i][j] == null){
// procceed = true;
// }
// }
// }
// if(!procceed) {
// return false;
// } else {
// return true;
// }
// } else {
// return results[1]
// }
// }
// play(x,y,subs){
// var played = false
// if(this.gboard[x][y] == null){
// this.gboard[x][y] = subs;
// played = true;
// }
// return played;
// }
// }
// var board = new Board();
// scr.addEventListener('mousedown',onDown,false);
// var who = "";
// function transform(c){
// if(c < 100){
// var p = 0;
// }
// else if(c < 200){
// var p = 1;
// }
// else{
// var p = 2;
// }
// return p;
// }
// var who = "O";
// function whos_next(alpha){
// if(alpha == "X"){
// alpha = "O";
// }
// else{
// alpha = "X";
// }
// return alpha;
// }
// function onDown(event){
// cx = event.pageX - (scr.offsetLeft + 8); //8 is there due to the minimum distance of the
// cy = event.pageY - (scr.offsetTop + 18); //18 is there due to the header of the page, feel free to change it
// px = transform(cx);
// py = transform(cy);
// var played = window.board.play(px,py,who);
// if(played){
// draw(who, px, py);
// who = whos_next(who);
// var results = window.board.post_check();
// if(!(results === true)){
// document.getElementById("restart").style.visibility = "visible";
// if(results == "X"){
// alert("The square wins! Restart the game clicking the button bellow!");
// }
// else if(results == "O"){
// alert("The ball wins! Restart the game clicking the button bellow!");
// }
// else{
// alert("It's a tie! Restart the game clicking the button bellow!");
// }
// }
// }
// else{
// alert("You can't play here!");
// }
// }
// function drawO(px,py){
// ctx.beginPath();
// ctx.arc(px*100 + 50, py*100 + 50, 30, 0, 2 * Math.PI, false);
// ctx.stroke();
// }
// function drawX(px,py){
// ctx.strokeRect(px*100 + 25, py*100 + 25, 50, 50);
// }
// function draw(who, px, py){
// if(who == "O"){
// drawO(px,py);
// }
// else{
// drawX(px,py);
// }
// }
// function restart(){
// board = new Board();
// ctx.clearRect (0, 0, 300, 300);
// ctx.strokeRect(0, 0, 100, 100);
// ctx.strokeRect(100, 0, 100, 100);
// ctx.strokeRect(200, 0, 100, 100);
// ctx.strokeRect(0, 100, 100, 100);
// ctx.strokeRect(100, 100, 100, 100);
// ctx.strokeRect(200, 100, 100, 100);
// ctx.strokeRect(0, 200, 100, 100);
// ctx.strokeRect(100, 200, 100, 100);
// ctx.strokeRect(200, 200, 100, 100);
// }