-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
122 lines (112 loc) · 3.02 KB
/
App.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
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import MyButton from './components/MyButton';
class App extends Component {
constructor(props) {
super(props);
//every component has a state which stores the json file like a key value pa
this.state = {
cplayer:"X",
board:[".",".",".",".",".",".",".",".","."],
winner:null
}
}
draw() {
var b = this.state.board;
if(b[0]!="." && b[1]!="." &&b[2]!="." &&b[3]!="." &&b[4]!="." &&b[5]!="." &&b[6]!="." &&b[7]!="." &&b[8]!="." )
{
this.setState({winner:"No one"});
}
//console.log("draw");
}
evaluate()
{
var b = this.state.board;
var winner=null;
if((b[0]==b[1]) && b[1]==b[2] && b[0]!=".")
{
winner=b[0];
}
else if((b[3]==b[4]) && b[4]==b[5] && b[3]!=".")
{
winner=b[3];
}
else if((b[6]==b[7]) && b[7]==b[8] && b[7]!=".")
{
winner=b[6];
}
else if((b[0]==b[3]) && b[3]==b[6] && b[0]!=".")
{
winner=b[0];
}
else if((b[1]==b[4]) && b[4]==b[7] && b[1]!=".")
{
winner=b[1];
}
else if((b[2]==b[5]) && b[5]==b[8] && b[2]!=".")
{
winner=b[2];
}
else if((b[0]==b[4]) && b[4]==b[8] && b[0]!==".")
{
winner=b[0];
}
else if((b[2]==b[4]) && b[4]==b[6] && b[2]!==".")
{
winner=b[2];
}
else {
this.draw();
}
this.setState({winner:winner})
}
whenClick(buttonId) {
var oldBoard = this.state.board;
var player = this.state.cplayer;
if(oldBoard[buttonId]==".")
{
oldBoard[buttonId]=player;
player = (player=="X")?"O":"X";
this.setState({board:oldBoard,cplayer:player});
this.evaluate();
}
}
render() {
var x=this.state.board;
return (
<span>
<header className="App-header" >
<h1><center>Welcome to Tic Tac Toe</center></h1>
</header>
<div className="center-align">
<div>
<MyButton clickFunc={()=>{this.whenClick(0);}}>{x[0]}</MyButton>
<MyButton clickFunc={()=>{this.whenClick(1);}}>{x[1]}</MyButton>
<MyButton clickFunc={()=>{this.whenClick(2);}}>{x[2]}</MyButton>
</div>
<div>
<MyButton clickFunc={()=>{this.whenClick(3);}}>{x[3]}</MyButton>
<MyButton clickFunc={()=>{this.whenClick(4);}}>{x[4]}</MyButton>
<MyButton clickFunc={()=>{this.whenClick(5);}}>{x[5]}</MyButton>
</div>
<div>
<MyButton clickFunc={()=>{this.whenClick(6);}}>{x[6]}</MyButton>
<MyButton clickFunc={()=>{this.whenClick(7);}}>{x[7]}</MyButton>
<MyButton clickFunc={()=>{this.whenClick(8);}}>{x[8]}</MyButton>
</div>
<br />
<h4>
{
this.state.winner?
this.state.winner + " is the winner"
:
""
}
</h4>
</div>
</span>
);
}
}
export default App;