-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.js
More file actions
184 lines (163 loc) · 5.51 KB
/
Game.js
File metadata and controls
184 lines (163 loc) · 5.51 KB
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
import React from 'react';
import { StyleSheet, Text, View, TouchableOpacity, Alert, Button } from 'react-native';
import { MaterialCommunityIcons } from 'react-native-vector-icons'
const styles = StyleSheet.create({
container: {
flex: 2,
flexDirection:'row',
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
tile: {
borderWidth: 1,
width:100,
height:100,
borderColor:'green',
},
tileX: {
position: 'relative',
color: 'red',
fontSize: 90,
flex: 1,
left:5,
top:1.5,
},
tile0: {
position: 'relative',
color: 'green',
fontSize: 81,
flex: 1,
left:8,
top:5,
},
});
class Game extends React.Component
{
constructor(props){
super(props);
this.state={
gameState:[
[0,0,0],
[0,0,0],
[0,0,0]
],
currentPlayer:1,
}
this.play1_score=0;
this.play2_score=0;
}
componentDidMount(){
this.initializeGame();
}
onNewGamePress=()=>
{
this.initializeGame();
}
initializeGame=()=>{
this.setState({gameState:
[
[0,0,0],
[0,0,0],
[0,0,0]
],
currentPlayer:1,
});
}
getWinner=()=>{
const NUM_TILES=3;
var arr=this.state.gameState;
var sum;
for(var i=0;i<NUM_TILES;i++)
{
sum=arr[i][0]+arr[i][1]+arr[i][2];
if(sum==3){return 1;}
else if(sum==3){return -1;}
}
for(var i=0;i<NUM_TILES;i++)
{
sum=arr[0][i]+arr[1][i]+arr[2][i];
if(sum==3){return 1;}
else if(sum==-3){return -1;}
}
sum=arr[0][0]+arr[1][1]+arr[2][2];
if(sum==3){return 1;}
else if(sum==-3){return -1;}
sum=arr[2][0]+arr[1][1]+arr[0][2];
if(sum==3){return 1;}
else if(sum==-3){return -1;}
}
onTilePress=(row,col)=>{
var value=this.state.gameState[row][col];
if(value!=0){return;}
var currentPlayer=this.state.currentPlayer
var arr=this.state.gameState.slice();
arr[row][col]=currentPlayer;
this.setState({gameState:arr});
var nextPlayer=(currentPlayer==1)?-1:1;
this.setState({currentPlayer: nextPlayer});
var winner=this.getWinner();
if(winner==1){
this.play1_score++;
this.initializeGame();
}else if(winner==-1){
this.play2_score++;
this.initializeGame();
}
}
renderIcon=(row,col)=>{
var value=this.state.gameState[row][col];
switch(value)
{
case 1:return <MaterialCommunityIcons name="close" style={styles.tileX}/>;
case -1:return <MaterialCommunityIcons name="circle-outline" style={styles.tile0}/>;
default:return <View/>
}
}
render() {
var play1 = this.props.navigation.getParam('play1','0');
var play2 = this.props.navigation.getParam('play2','0');
return (
<view style={{position: "absolute",left:400,top:100}}>
<View style={styles.container}>
<TouchableOpacity onPress={()=>this.onTilePress(0,0)} style={styles.tile}>
{this.renderIcon(0,0)}
</TouchableOpacity>
<TouchableOpacity onPress={()=>this.onTilePress(0,1)} style={styles.tile}>
{this.renderIcon(0,1)}
</TouchableOpacity>
<TouchableOpacity onPress={()=>this.onTilePress(0,2)} style={styles.tile}>
{this.renderIcon(0,2)}
</TouchableOpacity>
</View>
<View style={styles.container}>
<TouchableOpacity onPress={()=>this.onTilePress(1,0)} style={styles.tile}>
{this.renderIcon(1,0)}
</TouchableOpacity>
<TouchableOpacity onPress={()=>this.onTilePress(1,1)} style={styles.tile}>
{this.renderIcon(1,1)}
</TouchableOpacity>
<TouchableOpacity onPress={()=>this.onTilePress(1,2)} style={styles.tile}>
{this.renderIcon(1,2)}
</TouchableOpacity>
</View>
<View style={styles.container}>
<TouchableOpacity onPress={()=>this.onTilePress(2,0)} style={styles.tile}>
{this.renderIcon(2,0)}
</TouchableOpacity>
<TouchableOpacity onPress={()=>this.onTilePress(2,1)} style={styles.tile}>
{this.renderIcon(2,1)}
</TouchableOpacity>
<TouchableOpacity onPress={()=>this.onTilePress(2,2)} style={styles.tile}>
{this.renderIcon(2,2)}
</TouchableOpacity>
</View>
<View style={{padding:10}}/>
<Button color='green' title='Start Again' onPress={this.onNewGamePress}/>
<View style={{padding:10}}/>
<Button color='green' title='Calculate Score' onPress={() => this.props.navigation.navigate('Result',{'play1':this.play1_score,'play2':this.play2_score,})}/>
</view>
);
}
}
export default Game