Commit ee0e7ba2 authored by liangyuetong's avatar liangyuetong

youhua

parent 1afe1566
...@@ -4,8 +4,6 @@ import styles from './square.css'; ...@@ -4,8 +4,6 @@ import styles from './square.css';
class Square extends Component { class Square extends Component {
render() { render() {
const value = this.props.value; const value = this.props.value;
// const blue = Object.assign({}, styles.blue, styles.square);
console.log(styles.blue)
return <div className={`${styles.square} ${value === 'x' ? styles.blue: value === 'o' ? styles.red : styles.default}`} onClick={this.props.onClick}><span className={styles.squareVal}>{this.props.value}</span></div>; return <div className={`${styles.square} ${value === 'x' ? styles.blue: value === 'o' ? styles.red : styles.default}`} onClick={this.props.onClick}><span className={styles.squareVal}>{this.props.value}</span></div>;
} }
} }
......
...@@ -33,9 +33,31 @@ export default { ...@@ -33,9 +33,31 @@ export default {
}, },
reducers: { reducers: {
play(state, action) { updateState(state, action) {
return { ...state, ...action.payload }; return { ...state, ...action.payload };
}, },
checkMate(state, action) {
return {
...state,
winner: action.winner
}
},
reset(state) {
return {
...state,
playerValue: 'x',// x or o
record: [],
step: 0,
winner: ''
}
},
goBack(state, action) {
return {
...state,
...action.payload
}
}
}, },
}; };
...@@ -19,6 +19,21 @@ function Container({ dispatch, con }) { ...@@ -19,6 +19,21 @@ function Container({ dispatch, con }) {
[0, 4, 8], [0, 4, 8],
[2, 4, 6] [2, 4, 6]
] ]
const calcWinner = (item) => {
let rst = '';
array.map(items => {
let [a, b, c] = items;
if (item[a] && item[b] && item[c]) {
if (item[a] === item[b] && item[c] === item[b]) {
rst = item[a];
return rst;
}
}
return rst;
})
return rst;
}
const mapSquare = (item, index, row) => { const mapSquare = (item, index, row) => {
let chess = []; let chess = [];
const play = (index, row) => { const play = (index, row) => {
...@@ -27,31 +42,27 @@ function Container({ dispatch, con }) { ...@@ -27,31 +42,27 @@ function Container({ dispatch, con }) {
let newArr = step === 0 ? new Array(9).fill(null) : Object.assign([], record[step - 1].records);//备份数组用作后续修改 let newArr = step === 0 ? new Array(9).fill(null) : Object.assign([], record[step - 1].records);//备份数组用作后续修改
const handlerChange = (arr) => { const handlerChange = (arr) => {
dispatch({ dispatch({
type: 'con/play', type: 'con/updateState',
payload: { payload: {
playerValue: next, playerValue: next,
record: record.concat({ record: step === record.length ? record.concat({
step: record.length + 1, step: step + 1,
coordinate: `[${row}, ${index}]`, coordinate: `[${row}, ${index}]`,
records: chess.concat(arr) records: chess.concat(arr)
}) : record.slice(0, step).concat({
step: step + 1,
coordinate: `[${row}, ${index}]`,
records: chess.slice(0, step).concat(arr)
}), }),
step: record.length + 1 step: step + 1
}
})
array.map(items => {
let [a, b, c] = items;
if (arr[a] && arr[b] && arr[c]) {
if (arr[a] === arr[b] && arr[c] === arr[b]) {
dispatch({
type: 'con/play',
payload: {
winner: arr[a]
}
})
}
} }
return winner;
}) })
if (calcWinner(arr) !== '') {
dispatch({
type: 'con/checkMate',
winner: calcWinner(arr)
})
}
} }
switch (row) { switch (row) {
case 0: case 0:
...@@ -94,20 +105,24 @@ function Container({ dispatch, con }) { ...@@ -94,20 +105,24 @@ function Container({ dispatch, con }) {
} }
const reset = () => { const reset = () => {
dispatch({ dispatch({
type: 'con/play', type: 'con/reset',
payload: { // payload: {
playerValue: 'x',// x or o // playerValue: 'x',// x or o
record: [], // record: [],
step: 0, // step: 0,
winner: '' // winner: ''
} // }
}) })
} }
const recall = (records, index) => { const recall = (records, index) => {
let next = index % 2 === 0 ? 'o' : 'x';
// console.log(index)
dispatch({ dispatch({
type: 'con/play', type: 'con/goBack',
payload: { payload: {
step: index + 1 winner: calcWinner(records.records),
step: index + 1,
playerValue: next
} }
}) })
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment