HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URL's added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div id="root"></div>
<!-- #Note to self: Try adding that metallic buttons flair you bookmarked.
Things that need to be added as per suggestion written on official React documentation page which you can find on this link: https://reactjs.org/tutorial/tutorial.html#wrapping-up
1. Display the location for each move in the format (col, row) in the move history list.
2. Bold the currently selected item in the move list.
3. Rewrite Board to use two loops to make the squares instead of hardcoding them.
4. Add a toggle button that lets you sort the moves in either ascending or descending order.
5. When someone wins, highlight the three squares that caused the win.
6. When no one wins, display a message about the result being a draw.
-->
body {
font: 14px "Century Gothic", Futura, sans-serif;
margin: 20px;
}
ol,
ul {
padding-left: 30px;
}
.board-row:after {
clear: both;
content: "";
display: table;
}
.status {
margin-bottom: 10px;
}
.square1 {
background: #fff;
}
.square2 {
background: #dff0d8;
color: red;
}
.square1,
.square2 {
border: 1px solid #999;
float: left;
font-size: 24px;
font-weight: bold;
line-height: 34px;
height: 34px;
margin-right: -1px;
margin-top: -1px;
padding: 0;
text-align: center;
width: 34px;
}
.square1:focus,
.square2:focus {
outline: none;
}
.kbd-navigation .square1:focus {
background: #ddd;
}
.game {
display: flex;
flex-direction: row;
}
.game-info {
margin-left: 20px;
}
.active {
font-weight: bold;
}
.inactive {
font-weight: normal;
}
function Square(props) {
/*Funkcija koja pravi komponentu, daje joj klasu i pravi prop pod nazivom 'onClick'.*/
let klasa =
props.winningCombo &&
(props.winningCombo[1][0] == props.id ||
props.winningCombo[1][1] == props.id ||
props.winningCombo[1][2] == props.id)
? "square2"
: "square1";
return (
<button
id={props.id}
className={klasa}
onClick={() => {
props.onClick();
}}
>
{props.value}
{/*Pravi se prop pod imenom 'value'*/}
</button>
);
}
class Board extends React.Component {
renderSquare(i) {
/*Funkcija prosledjuje svoj parametar komponenti square. U handleClick funkciji se odlucuje koji ce se simbol pribeleziti u this.state.squares na osnovu parametra koji mu prosledjuje renderSquare funkcija iz koje se ova funkcija poziva. Niz square, 'i' mu je indeks, pa se posto mu se prosledi indeks na kome se zeli upisati iks ili oks zavisno od uslova, te kada se to obavi, bulijanska vrednost this.state.xIsNext se setuje na suprotno.*/
return (
<Square
key={"square-" + i.toString()}
winningCombo={this.props.winningCombo}
id={i.toString()}
value={this.props.squares[i]}
onClick={() => {
this.props.onClick(i);
}}
/>
); /*prop pod imenom 'value' iz Square komponente, dobija vrednost od stateProperty-ja zvanog squares.*/
}
/*Alternativni nacin za generisanje mreze 3x3.
squared = (arr = [0, 1, 2]) => arr.map(row => (
<div className="board-row" key={row}> {arr.map(col => this.renderSquare(row * 3 + col))} </div>
));*/
squared = () => {
const rows = [];
for (let i = 0; i < 3; i++) {
const cols = [];
for (let j = 0; j < 3; j++) {
cols.push(this.renderSquare(i * 3 + j));
}
rows.push(
<div className="board-row" key={"row-" + i.toString()}>
{cols}
</div>
);
}
return rows;
};
render() {
return <div>{this.squared()}</div>;
}
}
class Game extends React.Component {
constructor(props) {
super(props);
this.initialState = {
history: [
{
squares: Array(9).fill(null) //Puni se niz sa defoltnim vrednostima. squares sluzi da vodi racuna gde treba iks ili oks da upise.
}
],
stepNumber: 0, //Ovde se skladisti index svakog poteza, tj. njegov redni broj.
xIsnext: true, //Bulijanska vrednost koja sluzi kao prekidac kada je koji igrac na redu.
col: null,
row: null,
upDown: false
};
this.state = this.initialState;
this.reArrange = this.reArrange.bind(this);
this.calculateWinner = this.calculateWinner.bind(this);
}
handleClick(i) {
/*Setuje this.state.squares koja je niz, odnosno ubacuje jos jednu vrednost u niz.*/
/* Promenljiva 'i' je parametar koja sluzi za identifikaciju dugmeta. Zato kad se poziva funkcija, kao parametar joj se prosledjuje broj koji sluzi kao redni da bi znalo na koje se dugme kliknulo. Prosledjivanje se vrsi dole, prilikom renderovanja.*/
const history = this.state.history.slice(0, this.state.stepNumber + 1);
const current = history[history.length - 1];
const squares = current.squares.slice(); //Pravi kopiju this.state.squares niza, jer se ne sme direktno vrsiti intervencija na state-nizu.
const winner = this.calculateWinner(current.squares);
if (winner || squares[i]) {
/*Kada ove vrednost izraza postane istinite, odnosno (true || true)==true, (true || false)==true ili (false || true)==true prekida se izvrsavanje, tj. prazan return. Slicno kao sto brejk ispada iz petlje i ne vraca se u nju. Samo je (false || false)==false i tada se ne izvrsava. Odnosno, svo vreme je false tako da cim makar od jednog postane true, izraz postane istinit i kod se u njemu izvrsava.*/
return;
}
squares[i] = this.state.xIsNext ? "X" : "O"; //Ukoliko this.state.xIsNext true, onda squares[i] dobija vrednost 'X', ukoliko je false, onda dobija vrednost 'O'.
this.setState({
history: history.concat([
{
squares: squares
}
]) /*Ovde se belezi svaki potez. Dakle prilikom belezenja poteza, ne presnimava se na sledeci, vec se samo nadodaje novi potez. Stari potezi ostaju ubelezeni.*/,
stepNumber:
history.length /* duzina istorije je u stvari zadnji potez, odnosno indeks zadnjeg unesenog poteza. */,
xIsNext: !this.state.xIsNext,
col:
i === 0 || i === 3 || i === 6
? "1"
: i === 1 || i === 4 || i === 7
? "2"
: "3" /*Izracunava kolonu odigranog znaka*/,
row: i <= 2 ? "1" : i <= 5 ? "2" : "3" /*Izracunava red odigranog znaka*/
});
}
setActive(step) {
this.setState({ active: step });
}
jumpTo(step) {
/*Funkcija koja pozivom na onClick sluzi za vracanje na odredjeni (prethodni)potez. Parametar 'step' je objekat obelezenih pozicija, tj. gde je stavljen iks ili oks. */
if (step === 0) {
//Resets state if button "Go to game start/reset" is clicked.
this.setState(this.initialState);
} else {
this.setState({
stepNumber: step,
xIsNext: step % 2 === 0
});
}
}
reArrange() {
this.setState({
upDown: !this.state.upDown
});
}
calculateWinner(squares) {
const lines = [
/*Moguce kombinacije za pobedu*/
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
for (let i = 0; i < lines.length; i++) {
const [a, b, c] = lines[
i
]; /* lines[i]={0,3,6}=[a,b,c] gde je sada a=0, b=3 c=6.
Zatim se vrsi provera niza squares tako sto se vrednosti dodeljene promenljivama a, b i c postave za indekse istog. Posto ima tri promenljive(a,b,c), onda se clanovi niza squares sa ovim indeksima, medjusobno uporedjuju. Ukoliko sva tri clana niza, sa datim indeksima, sadrze istu vrednost(iks ili oks), onda se proglasava pobednik. */
if (
squares[a] &&
squares[a] === squares[b] &&
squares[a] === squares[c]
) {
return [squares[a], lines[i]];
}
}
return null;
}
render() {
const downUp = this.state.upDown;
const history = this.state.history; //Pravi se kopija objekta history.
const current = history[this.state.stepNumber]; //Ovde se stavlja zadnji(trenutni) odigran potez koji je zabelezen u objektu.
const winner = this.calculateWinner(current.squares); //Ovde se belezi simbol(iks ili oks) pobednika koji sastavi sva tri ista simbola u nizu.
const moves = history.map((step, move) => {
/*Ovde se pravi lista odigranih poteza u obliku dugmica. Klikom na nekih od tih dugmica se moze vratiti na prethodni potez.*/
const desc = move
? "Go to move #" + move
: "Go to game start/reset"; /*Ovde se postavlja tekst zavisno od rednog broja poteza. Natpis Go to move #1 na dugmetu i itd.. U else stoji po defoltu "Go to game start", a za svaki potez se dodaje tekst ali se menja broj.*/
return (
/*Renderovanje dugmica u listi. U svakom dugmetu je prisutna funkcija koja kada se aktivira, vraca unazad na zeljeni potez. Parametar 'move' koji joj se prosledjuje je indeks tog poteza, on se dobavlja od 'map' metode. 'move' ovde sluzi i kao indeks svakg pojedinacnog generisanog 'li' elementa.*/
<li
key={move}
className={this.state.active === step ? "active" : ""}
onClick={() => this.setActive(step)}
>
<button onClick={() => this.jumpTo(move)}>{desc}</button>
</li>
);
});
let movesCopy;
if (downUp === false) {
movesCopy = moves;
} else {
movesCopy = moves.reverse();
}
let status; //Promenljiva u kojoj se belezi tekst sa simbolom pobednika.
if (winner) {
// Po difoltu ova promenljiva je nedefinisana, te je false. Ona sluzi da se u njoj zabelezi pobednicki simbol. Kada se u njoj nesto zabelezi, onda nije vise nedefinisana, te je onda true i kod se izvrsava.
status = "Winner: " + winner[0]; //Bele se tekst i simbol pobednika.
} else {
//Sve dok je 'winner' promenljiva nedefinisana ovaj kod se izvrsava. Izvrsenje se sastoji od teksta i simbola igraca koji je trenutno na redu, odnosno sledeci.
status = "Next player: " + (this.state.xIsNext ? "X" : "O");
}
if (this.state.history.length === 10 && !winner) {
status = "This one is a draw!";
}
// Displaying columns and rows
let column;
let row;
if (this.state.col) {
column = "Column No. " + this.state.col;
} else {
column = "Play to show column coordinate";
}
if (this.state.row) {
row = "Row No. " + this.state.row;
} else {
row = "Play to show row coordinate";
}
return (
<div className="game">
<div className="game-board">
{/*current.squares promenljiva je u stvari niz iz objekta iz state-a. prop-u squares iz board komponente, te u njoj square komponente, dodeljuje se prop i vrednost za prop. Salje se u Board koji ga prosledjuje Square(value). handleClick(i) takodje vodi do istih komponenti, a parametar je u stvari indeks kliknutog dugmeta.*/}
<Board
squares={current.squares}
winningCombo={winner}
onClick={i => this.handleClick(i)}
/>
</div>
<div className="game-info">
<button onClick={this.reArrange}>ReArrange</button>
<div>{column}</div>
<div>{row}</div>
<div>{status}</div>
{/*Prikazivanje vrednosti prethodno definisane promenljive.*/}
<ol id="myList">{movesCopy}</ol>
{/*Prikazivanje vrednosti prethodno definisane promenljive.*/}
</div>
</div>
);
}
}
ReactDOM.render(<Game />, document.getElementById("root"));
Also see: Tab Triggers