-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.js
147 lines (130 loc) · 3.22 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
function printCell(rowIndex,cellIndex)
{
document.write("<td id='cell" + rowIndex + cellIndex + "' onClick='cellClick("+ rowIndex+","+cellIndex+")'>"+"</td>");
}
function printTable()
{
document.write("<table>");
for (var rowCount = 0; rowCount < 3; rowCount++)
{
document.write("<tr>");
for (var cellCount = 0; cellCount < 3; cellCount++)
{
printCell(rowCount,cellCount);
}
document.write("</tr>");
}
document.write("</table>");
}
function cellClick(rowIndex, cellIndex)
{
var hinweis = document.getElementById("hinweisbox");
var cell = document.getElementById("cell"+rowIndex+cellIndex);
var spieler = getCurrentPlayerNbr();
hinweis.innerHTML="Sie haben die Zelle " + rowIndex+"/"+cellIndex+ " geklickt";
switch(spieler)
{
case 0:
cell.innerHTML = "X";
break;
case 1:
cell.innerHTML = "O";
break;
}
hasWon(rowIndex,cellIndex);
selectNextPlayer(spieler);
}
function clearCell(rowIndex,cellIndex)
{
document.getElementById("cell"+rowIndex+cellIndex).innerHTML = "";
}
function restart()
{
for(var rowIndex=0;rowIndex<3;rowIndex++)
{
for(var cellIndex=0;cellIndex<3;cellIndex++)
{
clearCell(rowIndex,cellIndex);
}
}
setPlayer(1);
}
function getCurrentPlayerNbr()
{
var player = document.getElementById("player").innerHTML;
if (player == "Spieler 1")
return 0;
if (player == "Spieler 2")
return 1;
}
function selectNextPlayer(spieler)
{
if (spieler == 0)
{
setPlayer(spieler);
}
else if (spieler == 1)
{
setPlayer(spieler);
}
}
function setPlayer(spieler)
{
if (spieler == 0)
{
document.getElementById("player").innerHTML = "Spieler 2";
}
else if (spieler == 1)
{
document.getElementById("player").innerHTML = "Spieler 1";
}
}
function hasWon(rowIndex,cellIndex)
{
isRowFilledWithSameSymbol(rowIndex,cellIndex);
isColumnFilledWithSameSymbol(rowIndex,cellIndex);
isDiagonalFilledWithSameSymbol(rowIndex,cellIndex);
if (isRowFilledWithSameSymbol == 1 || isColumnFilledWithSameSymbol == 1 || isDiagonalFilledWithSameSymbol == 1)
{
window.alert("gewonnen!");
}
}
function getSymbol(rowIndex,cellIndex)
{
var symbol = document.getElementById("cell"+rowIndex+cellIndex).innerHTML;
return symbol;
}
function isRowFilledWithSameSymbol(rowIndex,cellIndex)
{
getSymbol(rowIndex,cellIndex);
if (getSymbol(0,0) == getSymbol(0,1) && getSymbol(0,1) == getSymbol(0,2))
return 1;
else if (getSymbol(1,0) == getSymbol(1,1) && getSymbol(1,1) == getSymbol(1,2))
return 1;
else if (getSymbol(2,0) == getSymbol(2,1) && getSymbol(2,1) == getSymbol(2,2))
return 1;
else
return 0;
}
function isColumnFilledWithSameSymbol(rowIndex,cellIndex)
{
getSymbol(rowIndex,cellIndex);
if (getSymbol(0,0) == getSymbol(1,0) && getSymbol(1,0) == getSymbol(2,0))
return 1;
else if (getSymbol(0,1) == getSymbol(1,1) && getSymbol(1,1) == getSymbol(2,1))
return 1;
else if (getSymbol(0,2) == getSymbol(1,2) && getSymbol(1,2) == getSymbol(2,2))
return 1;
else
return 0;
}
function isDiagonalFilledWithSameSymbol(rowIndex,cellIndex)
{
getSymbol(rowIndex,cellIndex);
if (getSymbol(0,0) == getSymbol(1,1) && getSymbol(1,1) == getSymbol(2,2))
return 1;
else if (getSymbol(0,2) == getSymbol(1,1) && getSymbol(1,1) == getSymbol(2,0))
return 1;
else
return 0;
}