-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
148 lines (133 loc) · 3.65 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
import React, { Component } from 'react';
import { Alert, AppRegistry, Button, StyleSheet, View, Text, TextInput, ListView, PixelRatio, TouchableWithoutFeedback, TouchableOpacity} from 'react-native';
import Sudoku from 'sudoku';
import _ from 'lodash';
export default class App extends React.Component {
constructor() {
super();
//const data = Array.apply(null, {length: 100}).map(Number.call, Number);
this.state = {
puzzle: Sudoku.makepuzzle()
};
this._newGame = this._newGame.bind(this)
this._solvePuzzle = this._solvePuzzle.bind(this)
}
_onPressButton() {
Alert.alert('You tapped the button!')
}
_onInput(key, input, puzzle) {
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)){
// Alert.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, puzzle)}
/>
</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>
);
}
}
const 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,
height: 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,
height:40,
},
blockSeperator: {
borderRightWidth: 2
},
blockText: {
fontSize: 25,
paddingTop: 4,
alignSelf: 'center'
}
})