-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
166 lines (152 loc) · 4.66 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
/**
* React Native Sudoku App
* https://github.com/AhmedBM/SudokuReact
*/
import React, { Component } from 'react';
import {
Text,
Button,
View,
Picker,
Item,
StyleSheet,
Dimensions
} from 'react-native';
import SudokuBoard from './src/SudokuBoard'
// NOTE: You may add additional Sudoku Boards here
var SudokuBoards = [
[
0, 0, 5, 0, 0, 0, 0, 0, 7,
0, 0, 0, 0, 0, 9, 0, 0, 5,
9, 0, 0, 5, 0, 6, 0, 0, 0,
0, 0, 4, 0, 0, 8, 0, 0, 0,
0, 0, 7, 0, 9, 0, 2, 4, 0,
1, 8, 0, 0, 0, 0, 3, 0, 0,
6, 0, 0, 0, 3, 1, 4, 0, 0,
3, 0, 0, 2, 0, 0, 0, 0, 0,
4, 0, 2, 7, 0, 0, 0, 0, 3
],
[
8, 0, 2, 6, 0, 4, 0, 0, 5,
0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 7, 9, 3, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 3,
2, 0, 0, 0, 0, 0, 0, 0, 0,
3, 0, 0, 0, 2, 5, 0, 8, 9,
0, 0, 0, 8, 3, 0, 7, 0, 0,
0, 6, 0, 0, 0, 7, 0, 9, 0,
0, 0, 9, 0, 6, 0, 0, 1, 8,
],
[
0, 4, 0, 0, 0, 0, 0, 0, 0,
2, 3, 0, 1, 0, 0, 0, 4, 0,
0, 9, 6, 2, 0, 0, 0, 7, 0,
8, 0, 0, 0, 6, 0, 0, 0, 4,
0, 0, 9, 0, 1, 0, 7, 5, 0,
0, 0, 0, 0, 5, 0, 6, 0, 3,
3, 8, 0, 0, 0, 9, 0, 0, 0,
0, 0, 5, 7, 3, 0, 0, 0, 0,
0, 2, 0, 0, 4, 0, 0, 0, 0,
],
[
0, 0, 5, 4, 0, 0, 0, 2, 0,
7, 9, 0, 0, 0, 0, 0, 0, 4,
0, 0, 0, 7, 0, 0, 8, 0, 0,
0, 0, 9, 0, 0, 0, 2, 0, 6,
0, 0, 6, 8, 2, 0, 0, 5, 0,
4, 0, 0, 0, 3, 0, 0, 0, 0,
0, 2, 8, 0, 0, 0, 7, 0, 0,
0, 4, 7, 0, 0, 0, 5, 6, 0,
0, 3, 0, 0, 5, 0, 0, 0, 2,
],
[
0, 0, 5, 1, 9, 0, 0, 0, 0,
0, 0, 0, 0, 3, 0, 0, 0, 0,
0, 0, 0, 0, 2, 4, 9, 0, 0,
1, 2, 0, 5, 0, 0, 0, 0, 9,
0, 0, 0, 6, 0, 9, 3, 0, 0,
0, 4, 0, 0, 0, 3, 1, 0, 8,
0, 0, 9, 0, 0, 0, 0, 7, 0,
6, 0, 7, 0, 0, 1, 4, 0, 0,
0, 0, 2, 9, 5, 0, 0, 1, 0,
]
];
//
var styles = StyleSheet.create({
innerContainer: {
flex: 1,
justifyContent: 'center',
padding: 15
},
labelPicker: {
flexDirection: 'row',
justifyContent: 'center'
},
picker: {
height: 50,
width: 50
},
pickerLabel: {
alignSelf: 'center'
}
});
export default class SudokuApp extends Component {
constructor(props) {
super(props);
this.state = {
selected: 0,
// Set intial width/height
window: Dimensions.get("window")
};
this.onPressSolve = this.onPressSolve.bind(this);
this.isLandscape = this.isLandscape.bind(this);
}
onPressSolve() {
// TODO: wire up function in SudokuBoard
this.sudokuBoard.solveBoard();
}
// Create handler to update the state accordingly on screen size change
handler = dims => this.setState(dims);
isLandscape() {
return this.state.window.width > this.state.window.height;
}
componentWillMount() {
Dimensions.addEventListener("change", this.handler);
}
componentWillUnmount() {
// Important to stop updating state after unmount
Dimensions.removeEventListener("change", this.handler);
}
render() {
// Change the style according to device orientation
var rootViewStyle = {
flex: 1,
flexDirection: this.isLandscape() ? 'row' : 'column',
justifyContent: 'space-evenly'
};
return (
<View style={rootViewStyle}>
<SudokuBoard onRef={ref => (this.sudokuBoard = ref)} board={SudokuBoards[this.state.selected].slice()} />
<View style={styles.innerContainer}>
<View style={styles.labelPicker}>
<Text style={styles.pickerLabel}>Selected Board: </Text>
<Picker
style={styles.picker}
mode="dropdown"
selectedValue={this.state.selected}
onValueChange={(itemValue, itemIndex) => this.setState({ selected: itemIndex })}>
{SudokuBoards.map((item, index) => {
return (<Picker.Item label={(index + 1).toString()} value={index} key={index} />)
})}
</Picker>
</View>
<Button
onPress={this.onPressSolve}
title="Solve Puzzle!"
color="green"
accessibilityLabel="Solve the Sudoku puzzle" />
</View>
</View>
);
}
}