forked from christopherdro/react-native-sudoku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ios.js
155 lines (140 loc) · 3.66 KB
/
index.ios.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
'use strict';
var React = require('react-native');
var Sudoku = require('sudoku');
var Dimensions = require('Dimensions');
var _ = require('lodash');
// var puzzle = _.chunk(Sudoku.makepuzzle(), 9);
// var solved = Sudoku.solvepuzzle(_.flatten(puzzle));
var {
AlertIOS,
AppRegistry,
StyleSheet,
PixelRatio,
Text,
TextInput,
TouchableWithoutFeedback,
TouchableOpacity,
View,
} = React;
var SudokuGame = React.createClass({
getInitialState() {
return {
puzzle: Sudoku.makepuzzle()
}
},
_onInput(key, input) {
var solved = Sudoku.solvepuzzle(_.flatten(this.state.puzzle));
var gridpoint = key.split('-');
var x = gridpoint[0];
var y = gridpoint[1];
puzzle[x][y] = parseInt(--input);
if(Sudoku.boardmatches(_.flatten(puzzle), solved)){
AlertIOS.alert('Game Solved');
}
},
newGame() {
this.setState({puzzle: Sudoku.makepuzzle()});
},
solvePuzzle() {
var solved = Sudoku.solvepuzzle(_.flatten(this.state.puzzle));
this.setState({puzzle: solved});
},
generateBoard() {
var rows = [];
var blocks = [];
var puzzle = _.chunk(this.state.puzzle, 9);
puzzle.map((row) => {
var rowSeperator = ((rows.length == 2 || rows.length == 5)) ? true : false;
row.map((block) => {
var key = rows.length + '-' + blocks.length;
var blockSeperator = ((blocks.length == 2 || blocks.length == 5)) ? true : false;
if(block === null) {
blocks.push(
<View key={key} style={[styles.block, blockSeperator && styles.blockSeperator]}>
<TextInput
clearTextOnFocus={true}
keyboardType={'number-pad'}
style={styles.textInput}
onChangeText={(input) => this._onInput(key, input)}
/>
</View>
);
} else {
blocks.push(
<View key={key} style={[styles.block, blockSeperator && styles.blockSeperator]}>
<Text style={styles.blockText}>{++block}</Text>
</View>
);
}
});
rows.push(<View key={rows.length} style={[styles.row, rowSeperator && styles.rowSeperator]}>{blocks}</View>);
blocks = [];
});
return (<View key={rows.length} style={styles.container}>{rows}</View>);
},
render() {
return (
<View>
<View style={styles.header}>
<TouchableOpacity onPress={this.solvePuzzle}><Text>Solve Puzzle</Text></TouchableOpacity>
<Text style={styles.headerText}>Sudoku</Text>
<TouchableOpacity onPress={this.newGame}><Text>New Game</Text></TouchableOpacity>
</View>
<View style={styles.container}>
{this.generateBoard()}
</View>
</View>
);
}
});
var styles = StyleSheet.create({
header: {
flexDirection: 'row',
justifyContent: 'space-between',
marginTop: 25,
marginBottom: 10,
paddingLeft: 25,
paddingRight: 25,
},
headerText:{
textAlign: 'center',
fontSize: 20,
},
container: {
alignSelf: 'center',
width:320,
borderWidth: 3,
borderTopWidth: 2,
borderBottomWidth: 2
},
row: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
},
rowSeperator: {
borderBottomWidth: 3
},
textInput: {
paddingBottom: 2,
paddingLeft: 10,
height: 40,
fontSize: 25,
backgroundColor: 'rgba(0, 0, 255, .1)'
},
block: {
flex: 1,
justifyContent: 'flex-start',
borderWidth: 1 / PixelRatio.get(),
height:40,
},
blockSeperator: {
borderRightWidth: 2
},
blockText: {
fontSize: 25,
paddingTop: 4,
alignSelf: 'center'
},
});
AppRegistry.registerComponent('Sudoku', () => SudokuGame);