-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Julian Vianna
authored and
Julian Vianna
committed
Jan 22, 2019
1 parent
94b1d7b
commit c7ac1ae
Showing
11 changed files
with
17,725 additions
and
56 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,15 @@ | ||
.App { | ||
text-align: center; | ||
} | ||
|
||
.App-logo { | ||
animation: App-logo-spin infinite 20s linear; | ||
height: 40vmin; | ||
} | ||
|
||
.App-header { | ||
background-color: #282c34; | ||
min-height: 100vh; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: center; | ||
font-size: calc(10px + 2vmin); | ||
color: white; | ||
} | ||
|
||
.App-link { | ||
color: #61dafb; | ||
} | ||
|
||
@keyframes App-logo-spin { | ||
from { | ||
transform: rotate(0deg); | ||
} | ||
to { | ||
transform: rotate(360deg); | ||
} | ||
.app { | ||
text-align: center; | ||
} | ||
|
||
.app-header { | ||
background-color: #282c34; | ||
min-height: 100px; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: center; | ||
font-size: calc(10px + 2vmin); | ||
color: white; | ||
margin-bottom: 3rem; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import React, { Component } from "react"; | ||
import Tiles from "./Tiles/Tiles"; | ||
import WinLine from "./WinLine/WinLine"; | ||
import "./GameGrid.scss"; | ||
|
||
class GameGrid extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
gameboard: ["", "", "", "", "", "", "", "", ""], | ||
usedSquares: [], | ||
isXturn: true, | ||
endOfGame: false, | ||
isWinLineHidden: true, | ||
winLinePos: "" | ||
}; | ||
} | ||
|
||
IsEndGame = () => { | ||
let board = this.state.gameboard; | ||
let turns = this.state.usedSquares.length; | ||
console.log(this.state.usedSquares.length); | ||
|
||
for (let i = 0; i <= 6; i = i + 3) { | ||
if ( | ||
board[i] !== "" && | ||
board[i] === board[i + 1] && | ||
board[i + 1] == board[i + 2] | ||
) { | ||
this.setState({ | ||
winLinePos: "r" + i | ||
}, () => { | ||
console.log(board[i] + " " + this.state.winLinePos + " -won horizontaly"); | ||
return true; | ||
}) | ||
} | ||
} | ||
|
||
for (let i = 0; i <= 2; i = i + 1) { | ||
if ( | ||
board[i] !== "" && | ||
board[i] === board[i + 3] && | ||
board[i + 3] == board[i + 6] | ||
) { | ||
this.setState({ | ||
winLinePos: "c" + i | ||
}, () => { | ||
console.log(board[i] + " " + this.state.winLinePos + " -won vertically"); | ||
return true; | ||
}) | ||
} | ||
} | ||
|
||
for (let i = 0, j = 4; i <= 2; i = i + 2, j = j - 2) { | ||
if ( | ||
board[i] !== "" && | ||
board[i] == board[i + j] && | ||
board[i + j] === board[i + 2 * j] | ||
) { | ||
this.setState({ | ||
winLinePos: "d" + i | ||
}, () => { | ||
console.log(board[i] + " " + this.state.winLinePos + "-won diagonaly"); | ||
return true; | ||
}) | ||
} | ||
} | ||
|
||
if (turns === 9) { | ||
console.log("draw game"); | ||
return true; | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
SetPosition = pos => { | ||
if (this.state.gameboard[pos] === "" && this.state.isXturn) { | ||
let onPlaygrid = [...this.state.gameboard]; | ||
onPlaygrid[pos] = "X"; | ||
this.setState( | ||
{ | ||
gameboard: onPlaygrid, | ||
isXturn: !this.state.isXturn, | ||
usedSquares: [...this.state.usedSquares, pos] | ||
}, | ||
() => { | ||
if (!this.IsEndGame()) { | ||
console.log(this.state.usedSquares.length); | ||
this.ChooseOmove(); | ||
} | ||
} | ||
); | ||
} | ||
}; | ||
|
||
ChooseOmove = () => { | ||
let num = Math.floor(Math.random() * 8); | ||
return this.state.usedSquares.includes(num) | ||
? this.ChooseOmove() | ||
: this.botPlaying(num); | ||
}; | ||
|
||
botPlaying = Omove => { | ||
if (this.state.gameboard[Omove] === "") { | ||
let onPlaygrid = [...this.state.gameboard]; | ||
onPlaygrid[Omove] = "O"; | ||
this.setState( | ||
{ | ||
gameboard: onPlaygrid, | ||
isXturn: !this.state.isXturn, | ||
usedSquares: [...this.state.usedSquares, Omove] | ||
}, | ||
() => { | ||
this.IsEndGame(); | ||
} | ||
); | ||
} else { | ||
this.ChooseOmove(); | ||
} | ||
}; | ||
|
||
tileBlock = index => { | ||
return ( | ||
<Tiles | ||
key={index} | ||
positionClick={this.SetPosition} | ||
idx={index} | ||
value={this.state.gameboard[index]} | ||
/> | ||
); | ||
}; | ||
|
||
render() { | ||
return ( | ||
<div className="container"> | ||
<div className="grid"> | ||
{!this.state.isWinLineHidden && <WinLine position={this.state.winLinePos} isHidden={this.state.isWinLineHidden}/>} | ||
{this.state.gameboard.map((i, index) => { | ||
return this.tileBlock(index); | ||
})} | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default GameGrid; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.container { | ||
width: 70%; | ||
margin: 0 auto; | ||
min-height: 200px; | ||
} | ||
|
||
.grid { | ||
display: flex; | ||
flex-direction: row; | ||
flex-wrap: wrap; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React, { Component } from "react"; | ||
import "./Tiles.scss"; | ||
|
||
class Tiles extends Component { | ||
constructor(props) { | ||
super(props); | ||
} | ||
|
||
handleClick = () => { | ||
let index = this.props.idx; | ||
this.props.positionClick(index); | ||
}; | ||
|
||
render() { | ||
return ( | ||
<button className="tiles-bts" onClick={this.handleClick}> | ||
{this.props.value} | ||
</button> | ||
); | ||
} | ||
} | ||
|
||
export default Tiles; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.tiles-bts { | ||
display: block; | ||
width: calc(33.3% - 12px); | ||
height: 25vh; | ||
border: solid 1px #000000; | ||
padding: 4px; | ||
margin: 6px; | ||
background: #ffffffd9; | ||
font-size: 5rem; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React, { Component } from "react"; | ||
|
||
class WinLine extends Component { | ||
constructor(props) { | ||
super(props); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div className={"modal " + this.props.isHidden}>Hello, World!</div> | ||
); | ||
} | ||
} | ||
|
||
export default WinLine; |
This file was deleted.
Oops, something went wrong.