-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnect4Dot.js
193 lines (176 loc) · 4.22 KB
/
Connect4Dot.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
function Circle(props){
var color="white"
if(props.grid == 1){
color="black"
}else if(props.grid == 2){
color="red"
}
let style = {
backgroundColor:color,
border:"1px solid black",
borderRadius:"100%",
paddingTop:"95%"
}
return(
<div style={style}></div>
)
}
function Grid(props){
let style={
height:50,
width:50,
border:"1px solid black",
backgroundColor:"#b3b8bd"
}
return(
<div style={style} onClick={()=> props.handleClick(props.row,props.col)}>
<Circle grid={props.grid}/>
</div>
)
}
function Row(props){
let style = {
display:"flex"
}
var grids =[]
for(let i=0;i<7;i++){
grids.push(<Grid key={i} grid={props.cells[i]} row={props.row} col={i} handleClick={props.handleClick}/>)
}
return(
<div style={style}>
{grids}
</div>
)
}
function Board(props){
let style={
border:"2px solid black",
width:350
}
var rows=[]
for(let i = 5; i >= 0; i--){
rows.push(<Row key={i} row={i} cells={props.cells[i]} handleClick={props.handleClick}/>)
}
return(
<div style={style}>
{rows}
</div>
)
}
class Game extends React.Component{
constructor(props){
super(props)
var cells =[]
for(let i=0;i<6;i++){
cells.push(new Array(7).fill(0))
}
//console.log(cells)
this.state={player:false,cells:cells,winner:0}
this.handleClick=this.handleClick.bind(this)
}
checkDiagonal(row,col){
//find right and left tops
var c = this.state.cells;
var val = this.state.player? 2:1;
var rR = row;
var cR = col;
while(rR < 5 && cR < 6){
rR++;
cR++;
}
while( rR >= 3 && cR >= 3){
if(c[rR][cR] == val && c[rR-1][cR-1] == val && c[rR-2][cR-2] == val && c[rR-3][cR-3] == val){
return 1
}
rR--
cR--
}
var rL = row;
var cL = col;
while(rL < 5 && cL > 0){
rL++
cL--
}
while(rL >= 3 && cL <= 3){
if(c[rL][cL] == val && c[rL-1][cL+1] == val && c[rL-2][cL+2] == val && c[rL-3][cL+3] == val){
return 1
}
rL--
cL++
}
return 0
}
checkHorizontal(row,col){
var c = this.state.cells;
var i = 6;
var val = this.state.player? 2:1;
while( i >= 3){
if(c[row][i] == val && c[row][i-1] == val && c[row][i-2] == val && c[row][i-3] == val){
return 1
}
i--
}
return 0
}
checkVertical(row,col){
var c = this.state.cells;
var i = row;
var val = this.state.player? 2: 1;
if(i >= 3){
if(c[i][col] == val && c[i - 1][col] == val && c[i - 2][col] == val && c[i - 3][col] == val){
return 1
}
}
return 0
}
checkVictory(row,col){
return this.checkVertical(row,col) || this.checkHorizontal(row,col) || this.checkDiagonal(row,col)
}
findAvailableRow(col){
for(let i=0;i<6;i++){
if(this.state.cells[i][col]==0){
return i;
}
}
return -1;
}
handleClick(row,col){
//console.log("Clicked "+ row +" "+col+" ")
//console.log(this.state.cells)
if(this.state.winner){return}
var temp = []
for(let i=0;i<6;i++){
temp.push(this.state.cells[i].slice())
}
var newRow= this.findAvailableRow(col)
temp[newRow][col]=this.state.player? 1:2
this.setState({cells:temp,player: !this.state.player},()=>{
if(this.checkVictory(newRow,col)>0){
console.log("winner")
this.setState({winner:this.state.player? 2:1})
}
})
//console.log(this.state.cells)
}
restart(){
var cells = [];
for(let i = 0; i < 6; i++ ){
cells.push(new Array(7).fill(0));
}
this.setState({ player : false, cells : cells, winner:0})
}
render(){
return (
<div>
<h1>{this.state.winner > 0 ? this.state.winner == 1? "Black Wins":"Red Wins": this.state.player? "Blacks Turn" : "Reds Turn"}</h1>
<Board cells={this.state.cells} handleClick={this.handleClick} />
<br></br>
<button onClick = { () => this.restart()}>Restart</button>
</div>
)
}
}
ReactDOM.render(
<Game/>,
document.getElementById('root')
)