-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
215 lines (188 loc) · 5.32 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React, { Component } from 'react';
import { Platform, StyleSheet, Text, View, Button, Alert, TouchableOpacity } from 'react-native';
import { tsBooleanKeyword } from '@babel/types';
import { MaterialCommunityIcons as Icons } from 'react-native-vector-icons';
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
android:
'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});
type Props = {};
export default class App extends Component<Props> {
render() {
return (
<View style={styles.container}>
<Board />
</View>
);
}
}
class Board extends React.Component {
constructor(props) {
super(props);
this.state = {
squareArray: Array(9).fill(null),
xTurn: true,
drawn: false,
count:0,
}
}
componentDidMount() {
this.arrayInitilaization();
}
arrayInitilaization = () => {
this.setState({ squareArray: Array(9).fill(null) });
}
renderSquare = (i) => {
sArray = this.state.squareArray.slice();
if (winningPosition(sArray) ) {
Alert.alert("Game won by "+(this.state.xTurn?"X":"O"));
this.setState({
sArray:sArray.fill(null),
})
this.arrayInitilaization();
return;
}
if(sArray[i] !=null){return}
sArray[i] = this.state.xTurn?"X":"O";
this.setState({ squareArray: sArray })
// this.setState({ count: count+1 })
// Alert.alert(this.state.count);
}
markerTOShow = (i) => {
var value = this.state.squareArray[i];
this.state.xTurn = !this.state.xTurn;
if (value == "X") {
return (
<Text style={styles.textcolorX}>X</Text>
);
}else if(value == "O"){
return (
<Text style={styles.textcolorO}>O</Text>
);
}
}
render() {
const winner = winningPosition(this.state.squareArray)
var winnerIs = "Game is on"
if(winner){
winnerIs = "Game is won by "+(this.state.xTurn?"X":"O");
Alert.alert("Game won by "+(this.state.xTurn?"X":"O"));
this.arrayInitilaization();
// this.setState({squareArray:squareArray.fill(null)});
}else if(this.state.count == 8){
winnerIs = "Game is Drawn"
Alert.alert("Game is Drawn");
this.arrayInitilaization();
// this.setState({squareArray:squareArray.fill(null)});
}else{
winnerIs = "Next turn is of "+(this.state.xTurn?"O":"X");
}
return (
<View style={{ flexDirection: 'column' }}>
<Text >{winnerIs}</Text>
<View style={{ flexDirection: 'row' }}>
<TouchableOpacity style={styles.tile} onPress={() => this.renderSquare(0)}>
{this.markerTOShow(0)}
</TouchableOpacity>
<TouchableOpacity style={styles.tile} onPress={() => this.renderSquare(1)}>
{this.markerTOShow(1)}
</TouchableOpacity>
<TouchableOpacity style={styles.tile} onPress={() => this.renderSquare(2)} >
{this.markerTOShow(2)}
</TouchableOpacity>
</View>
<View style={{ flexDirection: 'row' }}>
<TouchableOpacity style={styles.tile} onPress={() => this.renderSquare(3)}>
{this.markerTOShow(3)}
</TouchableOpacity>
<TouchableOpacity style={styles.tile} onPress={() => this.renderSquare(4)}>
{this.markerTOShow(4)}
</TouchableOpacity>
<TouchableOpacity style={styles.tile} onPress={() => this.renderSquare(5)}>
{this.markerTOShow(5)}
</TouchableOpacity>
</View>
<View style={{ flexDirection: 'row' }}>
<TouchableOpacity style={styles.tile} onPress={() => this.renderSquare(6)}>
{this.markerTOShow(6)}
</TouchableOpacity>
<TouchableOpacity style={styles.tile} onPress={() => this.renderSquare(7)}>
{this.markerTOShow(7)}
</TouchableOpacity>
<TouchableOpacity style={styles.tile} onPress={() => this.renderSquare(8)}>
{this.markerTOShow(8)}
</TouchableOpacity>
</View>
</View>
);
}
}
winningPosition = (square) =>{
const lines= [
[0,1,2],
[3,4,5],
[6,7,8],
[0,3,6],
[1,4,7],
[2,5,8],
[0,4,8],
[2,4,6],
];
for(let i=0;i<lines.length;i++){
const [a,b,c] = lines[i];
if(square[a] && square[a] === square[b] && square[c] === square[a]){
return square[a]
}
}
return null
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
tile: {
width: 100,
height: 100,
borderWidth: 3,
},
textcolorX:{
fontWeight:'bold',
color: 'red',
fontSize: 40,
alignItems:'center',
justifyContent:'flex-end',
textAlign:'center',
marginTop:20,
},
textcolorO:{
fontWeight:'bold',
color: 'blue',
fontSize: 40,
alignItems:'center',
justifyContent:'flex-end',
textAlign:'center',
marginTop:20,
}
});