Skip to content

Commit

Permalink
tik tak toy code and core
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Vianna authored and Julian Vianna committed Jan 22, 2019
1 parent 94b1d7b commit c7ac1ae
Show file tree
Hide file tree
Showing 11 changed files with 17,725 additions and 56 deletions.
17,495 changes: 17,495 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"node-sass": "^4.11.0",
"react": "^16.7.0",
"react-dom": "^16.7.0",
"react-scripts": "2.1.3"
Expand Down
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
<title>React Julian's Tik-Tak-Toe</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand Down
45 changes: 14 additions & 31 deletions src/App.css
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;
}
24 changes: 7 additions & 17 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import React, { Component } from "react";
import GameGrid from "./GameGrid/GameGrid";
import "./App.css";

class App extends Component {
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
<div className="app">
<header className="app-header">
<h2>Tik-Tak-Toe</h2>
</header>
<GameGrid />
</div>
);
}
Expand Down
148 changes: 148 additions & 0 deletions src/GameGrid/GameGrid.jsx
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;
11 changes: 11 additions & 0 deletions src/GameGrid/GameGrid.scss
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;
}
23 changes: 23 additions & 0 deletions src/GameGrid/Tiles/Tiles.jsx
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;
10 changes: 10 additions & 0 deletions src/GameGrid/Tiles/Tiles.scss
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;
}
15 changes: 15 additions & 0 deletions src/GameGrid/WinLine/WinLine.jsx
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;
7 changes: 0 additions & 7 deletions src/logo.svg

This file was deleted.

0 comments on commit c7ac1ae

Please sign in to comment.