Commit d3ed529c authored by Point 郑海洋's avatar Point 郑海洋 👾

添加胜利颜色

parent b1c732b2
import styled from 'styled-components'; import styled from 'styled-components';
import WinImg from './winImg';
const Box = styled.div ` const Box = styled.div `
background: #fff; background: #fff;
border: 1px solid #999; border: 1px solid #999;
...@@ -16,7 +16,7 @@ const Box = styled.div ` ...@@ -16,7 +16,7 @@ const Box = styled.div `
function BoxMaster(props) { function BoxMaster(props) {
const handelClick = (value) => { const handelClick = (value) => {
// 判断游戏是否有胜利者 // 判断游戏是否有胜利者
if (props.winer) return if (props.winner) return
// 判断是否重复点击 // 判断是否重复点击
if (!reClick(value)) { if (!reClick(value)) {
// 更新整体 // 更新整体
...@@ -48,8 +48,13 @@ function BoxMaster(props) { ...@@ -48,8 +48,13 @@ function BoxMaster(props) {
return ( return (
<Box onClick = { () => handelClick(props)} style={{background:props.color}}> <Box onClick={ () => handelClick(props)} style={{background:props.winner && props.value === props.winner.value ?"":props.color}}>
{ props.flag } {
props.winner && props.value === props.winner.value ? (
<WinImg flag={props.flag} />
) : (<span>{props.flag}</span>)
}
</Box> </Box>
) )
} }
......
.winCss {
font-size: 14px;
animation: winAnimation 2s infinite ;
-moz-animation: winAnimation 2s infinite ; /* Firefox */
-webkit-animation: winAnimation 2s infinite ; /* Safari 和 Chrome */
-o-animation: winAnimation 2s infinite ; /* Opera */
}
@keyframes winAnimation
{
0% {background: red;}
25% {background: yellow;}
50% {background: blue;}
75% {background: green;}
100% {background: white;}
}
@-moz-keyframes winAnimation /* Firefox */
{
0% {background: red;}
25% {background: yellow;}
50% {background: blue;}
75% {background: green;}
100% {background: white;}
}
@-webkit-keyframes winAnimation /* Safari 和 Chrome */
{
0% {background: red;}
25% {background: yellow;}
50% {background: blue;}
75% {background: green;}
100% {background: white;}
}
@-o-keyframes winAnimation /* Opera */
{
0% {background: red;}
25% {background: yellow;}
50% {background: blue;}
75% {background: green;}
100% {background: white;}
}
import React,{Component} from 'react';
import style from './winImg.css';
export default class WinImg extends Component {
constructor(props) {
super(props)
}
render(){
const {flag} = this.props
return (
<div className={style.winCss}>
<span>winner is {flag} </span>
</div>
)
}
}
...@@ -6,4 +6,3 @@ html, body, #root { ...@@ -6,4 +6,3 @@ html, body, #root {
body { body {
margin: 0; margin: 0;
} }
export default { export default {
namespace: 'main', namespace: 'main',
state: { state: {
nowflag: "X", nowFlag: "X",
list: [{ list: [{
value: 1, value: 1,
flag: null, flag: null,
...@@ -31,7 +31,7 @@ export default { ...@@ -31,7 +31,7 @@ export default {
flag: null, flag: null,
}], }],
actionData: [], actionData: [],
winer: null, winner: null,
goBackKey: null goBackKey: null
}, },
reducers: { reducers: {
...@@ -39,7 +39,7 @@ export default { ...@@ -39,7 +39,7 @@ export default {
const { tempFlag } = action.payload const { tempFlag } = action.payload
return { return {
...state, ...state,
nowflag: tempFlag, nowFlag: tempFlag,
} }
}, },
updateSelectOne(state, action) { updateSelectOne(state, action) {
...@@ -63,15 +63,15 @@ export default { ...@@ -63,15 +63,15 @@ export default {
...state, ...state,
list: tempData, list: tempData,
actionData: tempActionData, actionData: tempActionData,
nowflag: tempNowFlag, nowFlag: tempNowFlag,
goBackKey: goBackKey goBackKey: goBackKey
} }
}, },
updateWiner(state, action) { updateWinner(state, action) {
const { winer } = action.payload const { winner } = action.payload
return { return {
...state, ...state,
winer: winer, winner: winner,
goBackKey: null goBackKey: null
} }
} }
...@@ -137,7 +137,7 @@ export default { ...@@ -137,7 +137,7 @@ export default {
}, },
// 回退操作 // 回退操作
* updateRenderDataFn({ * updateRenderDataFn({
flag, booleanFlag,
key, key,
actionData, actionData,
}, { }, {
...@@ -146,12 +146,13 @@ export default { ...@@ -146,12 +146,13 @@ export default {
select select
}) { }) {
const tempList = yield select(state=> state.main.list) const tempList = yield select(state=> state.main.list)
let tempNowFlag = yield select(state=> state.main.nowflag) let tempNowFlag = yield select(state=> state.main.nowFlag)
let tempActionDataList = JSON.parse(JSON.stringify(actionData)) let tempActionDataList = JSON.parse(JSON.stringify(actionData))
let tempData = [] let tempData = []
let tempActionData = [] let tempActionData = []
let goBackKey = null let goBackKey = null
if (!flag) { // 如果点击的不是重玩
if (!booleanFlag) {
let index = tempActionDataList.findIndex((item, index) => { let index = tempActionDataList.findIndex((item, index) => {
return item.key === key return item.key === key
}) })
...@@ -177,16 +178,16 @@ export default { ...@@ -177,16 +178,16 @@ export default {
}) })
}, },
// 更新获胜者 // 更新获胜者
* updateWinerFn({ * updateWinnerFn({
winer winner
}, { }, {
call, call,
put, put,
select select
}) { }) {
yield put({ yield put({
type: "updateWiner", type: "updateWinner",
payload:{ winer } payload:{ winner }
}) })
} }
}, },
......
...@@ -10,21 +10,22 @@ import { ...@@ -10,21 +10,22 @@ import {
function Main({ function Main({
dispatch, dispatch,
list, list,
nowflag, nowFlag,
actionData, actionData,
winer winner
}) { }) {
useEffect(() => { useEffect(() => {
if (winer) return // 如果有胜利者就不执行判断胜负方法 if (winner) return // 如果有胜利者就不执行判断胜负方法
let twoDArry = renderArry(list) let twoDArry = renderArry(list)
if (!twoDArry) return if (!twoDArry) return
let reData = checkWin(twoDArry) let reData = checkWin(twoDArry)
console.log("reData",reData)
if (reData && reData.status === 'win') { if (reData && reData.status === 'win') {
dispatch({ dispatch({
type: "main/updateWinerFn", type: "main/updateWinnerFn",
winer: reData.winer winner: reData.winner
}) })
} }
...@@ -48,110 +49,111 @@ function Main({ ...@@ -48,110 +49,111 @@ function Main({
if (twoDArry[0][0].flag != null && twoDArry[0][1].flag != null && twoDArry[0][2].flag != null && twoDArry[0][0].flag === twoDArry[0][1].flag && twoDArry[0][1].flag === twoDArry[0][2].flag) { if (twoDArry[0][0].flag != null && twoDArry[0][1].flag != null && twoDArry[0][2].flag != null && twoDArry[0][0].flag === twoDArry[0][1].flag && twoDArry[0][1].flag === twoDArry[0][2].flag) {
return { return {
status: 'win', status: 'win',
winer: twoDArry[0][0] winner: twoDArry[0][0]
} }
} }
// 判断第二行 // 判断第二行
if (twoDArry[1][0].flag != null && twoDArry[1][1].flag != null && twoDArry[1][2].flag != null && twoDArry[1][0].flag === twoDArry[1][1].flag && twoDArry[1][1].flag === twoDArry[1][2].flag) { if (twoDArry[1][0].flag != null && twoDArry[1][1].flag != null && twoDArry[1][2].flag != null && twoDArry[1][0].flag === twoDArry[1][1].flag && twoDArry[1][1].flag === twoDArry[1][2].flag) {
return { return {
status: 'win', status: 'win',
winer: twoDArry[1][0] winner: twoDArry[1][0]
} }
} }
// 判断第三行 // 判断第三行
if (twoDArry[2][0].flag != null && twoDArry[2][1].flag != null && twoDArry[2][2].flag != null && twoDArry[2][0].flag === twoDArry[2][1].flag && twoDArry[2][1].flag === twoDArry[2][2].flag) { if (twoDArry[2][0].flag != null && twoDArry[2][1].flag != null && twoDArry[2][2].flag != null && twoDArry[2][0].flag === twoDArry[2][1].flag && twoDArry[2][1].flag === twoDArry[2][2].flag) {
return { return {
status: 'win', status: 'win',
winer: twoDArry[2][0] winner: twoDArry[2][0]
} }
} }
// 判断第一列 // 判断第一列
if (twoDArry[0][0].flag != null && twoDArry[1][0].flag != null && twoDArry[2][0].flag != null && twoDArry[0][0].flag === twoDArry[1][0].flag && twoDArry[1][0].flag === twoDArry[2][0].flag) { if (twoDArry[0][0].flag != null && twoDArry[1][0].flag != null && twoDArry[2][0].flag != null && twoDArry[0][0].flag === twoDArry[1][0].flag && twoDArry[1][0].flag === twoDArry[2][0].flag) {
return { return {
status: 'win', status: 'win',
winer: twoDArry[0][0] winner: twoDArry[0][0]
} }
} }
// 判断第二列 // 判断第二列
if (twoDArry[0][1].flag != null && twoDArry[1][1].flag != null && twoDArry[2][1].flag != null && twoDArry[0][1].flag === twoDArry[1][1].flag && twoDArry[1][1].flag === twoDArry[2][1].flag) { if (twoDArry[0][1].flag != null && twoDArry[1][1].flag != null && twoDArry[2][1].flag != null && twoDArry[0][1].flag === twoDArry[1][1].flag && twoDArry[1][1].flag === twoDArry[2][1].flag) {
return { return {
status: 'win', status: 'win',
winer: twoDArry[0][1] winner: twoDArry[0][1]
} }
} }
// 判断第三列 // 判断第三列
if (twoDArry[0][2].flag != null && twoDArry[1][2].flag != null && twoDArry[2][2].flag != null && twoDArry[0][2].flag === twoDArry[1][2].flag && twoDArry[1][2].flag === twoDArry[2][2].flag) { if (twoDArry[0][2].flag != null && twoDArry[1][2].flag != null && twoDArry[2][2].flag != null && twoDArry[0][2].flag === twoDArry[1][2].flag && twoDArry[1][2].flag === twoDArry[2][2].flag) {
return { return {
status: 'win', status: 'win',
winer: twoDArry[0][2] winner: twoDArry[0][2]
} }
} }
// 判断交叉左上角到右下角 // 判断交叉左上角到右下角
if (twoDArry[0][0].flag != null && twoDArry[1][1].flag != null && twoDArry[2][2].flag != null && twoDArry[0][0].flag === twoDArry[1][1].flag && twoDArry[1][1].flag === twoDArry[2][2].flag) { if (twoDArry[0][0].flag != null && twoDArry[1][1].flag != null && twoDArry[2][2].flag != null && twoDArry[0][0].flag === twoDArry[1][1].flag && twoDArry[1][1].flag === twoDArry[2][2].flag) {
return { return {
status: 'win', status: 'win',
winer: twoDArry[0][0] winner: twoDArry[0][0]
} }
} }
// 判断交叉右上角到左下角 // 判断交叉右上角到左下角
if (twoDArry[0][2].flag != null && twoDArry[1][1].flag != null && twoDArry[2][0].flag != null && twoDArry[0][2].flag === twoDArry[1][1].flag && twoDArry[1][1].flag === twoDArry[2][0].flag) { if (twoDArry[0][2].flag != null && twoDArry[1][1].flag != null && twoDArry[2][0].flag != null && twoDArry[0][2].flag === twoDArry[1][1].flag && twoDArry[1][1].flag === twoDArry[2][0].flag) {
return { return {
status: 'win', status: 'win',
winer: twoDArry[0][2] winner: twoDArry[0][2]
} }
} }
} }
function handleChangeData(flag, key, actionData ) { function handleChangeData(booleanFlag, key, actionData ) {
dispatch({ dispatch({
type: 'main/updateWinerFn', type: 'main/updateWinnerFn',
winer: null winner: null
}) })
dispatch({ dispatch({
type: 'main/updateRenderDataFn', type: 'main/updateRenderDataFn',
key: key, key: key,
actionData: actionData, actionData: actionData,
flag: flag, booleanFlag: booleanFlag,
}) })
} }
return ( return (
<div > <div>
<div className = {styles.content} > <div className={styles.content}>
{ {
list.map((item, index) => { list.map((item, index) => {
let colorStr = item.flag === 'X'?'blue':item.flag==='O'?'red':'' let colorStr = item.flag === 'X'?'blue':item.flag==='O'?'red':''
return ( return (
<Box key = {item.value} <Box
value = {item.value} key={item.value}
nowFliage = {nowflag} value={item.value}
dispatch = {dispatch} nowFliage={nowFlag}
datasource = {list} dispatch={dispatch}
flag = {item.flag} datasource={list}
actionData = {actionData} flag={item.flag}
winer = {winer} actionData={actionData}
color = {colorStr} winner={winner}
color={colorStr}
/> />
) )
}) })
} }
</div> </div>
<div className = {styles.rightBox} > <div className={styles.rightBox}>
{ {
winer ? < p > Winer is {winer.flag} < /p> : <p> Next player: {nowflag }</p> winner ? <p> winner is {winner.flag} </p> : <p> Next player: {nowFlag}</p>
} }
<ul> <ul>
<li> <li>
<button onClick = {() => handleChangeData(true, false, false)} > Go to Game Start < /button> <button onClick = {() => handleChangeData(true, false, false)} > Go to Game Start </button>
</li> </li>
{ {
actionData.map((item, index) => { actionData.map((item, index) => {
return ( return (
<li key = {item.key} value = {item.key} > <li key={item.key} value={item.key}>
<button className = {styles.numLi} <button className={styles.numLi}
onClick = {() => handleChangeData(false, item.key, actionData)} > Go to move# {index+1} onClick={() => handleChangeData(false, item.key, actionData)}> Go to move# {index+1}
< /button> </button>
</li> </li>
) )
}) })
...@@ -164,16 +166,16 @@ function Main({ ...@@ -164,16 +166,16 @@ function Main({
function mapStateToProps(state) { function mapStateToProps(state) {
const { const {
nowflag, nowFlag,
list, list,
actionData, actionData,
winer winner
} = state.main; } = state.main;
return { return {
nowflag, nowFlag,
list, list,
actionData, actionData,
winer winner
}; };
} }
......
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