Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Criando um botao de reiniciar #10

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified README.md
100644 → 100755
Empty file.
65 changes: 58 additions & 7 deletions css/style.css
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,22 +1,73 @@
body {
margin: 0;
display: flex;
flex-direction: column;
align-items: center;
}

.game {
width: 600px;

width: 100vw;
height: 100vw;
margin: 0 auto;
background-color: #34495e;
color: #fff;
display: grid;
grid-template: repeat(3, 1fr) / repeat(3, 1fr)
width: 600px;
height: 600px;
margin: 0 auto;
background-color: #34495e;
color: #fff;
border: 6px solid #2c3e50;
border-radius: 10px;
background-color: #34495e;
color: #fff;
border: 6px solid #2c3e50;
border-radius: 10px;
display: grid;
grid-template: repeat(3, 1fr) / repeat(3, 1fr);

}

.game > div {
.game>div {
border: 6px solid #2c3e50;
border-radius: 2px;
font-family: Helvetica;
font-weight: bold;
font-size: 4em;
display: flex;
justify-content: center;
align-items: center;
}

.btn {
<<<<<<< HEAD
border: 5px solid #2c3e50;
background-color: #34495e;
color: white;
margin-top: 10px;
padding: 10px 20px
}

.game > div.winner {
background: yellow;
}

.game > div.winner > span {
color: blue;
}

@media (min-width: 600px) {
.game {
width: 600px;
height: 600px;
border-radius: 10px;
border: 6px solid #2c3e50;
}

.game>div {
border-radius: 2px;
}
}

@media (min-width: 1024px) {
.btn {
cursor: pointer
}
}
4 changes: 2 additions & 2 deletions index.html
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, shrink-to-fit=yes">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Tic Tac Toe</title>
<link rel="stylesheet" href="css/style.css">
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
</head>
<body>

Expand All @@ -16,6 +17,5 @@
tic_tac_toe.init( document.querySelector('.game') );
tic_tac_toe.start();
</script>

</body>
</html>
104 changes: 75 additions & 29 deletions js/tic-tac-toe.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const tic_tac_toe = {
symbols: {
options: ['O','X'],
turn_index: 0,
change: function(){
change(){
this.turn_index = ( this.turn_index === 0 ? 1:0 );
}
},
Expand All @@ -24,59 +24,105 @@ const tic_tac_toe = {
],

// FUNCTIONS
init: function(container) {
init(container) {
this.container_element = container;
},

make_play: function(position) {
if (this.gameover) return false;
if (this.board[position] === ''){
this.board[position] = this.symbols.options[this.symbols.turn_index];
this.draw();
let winning_sequences_index = this.check_winning_sequences( this.symbols.options[this.symbols.turn_index] );
if (winning_sequences_index >= 0){
this.game_is_over();
} else{
this.symbols.change();
}
return true;
make_play(position) {
if (this.gameover || this.board[position] !== '') return false;

const currentSymbol = this.symbols.options[this.symbols.turn_index];
this.board[position] = currentSymbol;
this.draw();

const winning_sequences_index = this.check_winning_sequences(currentSymbol);
if (this.is_game_over()){
this.game_is_over();
}
else {
return false;
if (winning_sequences_index >= 0) {
this.game_is_over();
this.stylize_winner_sequence(this.winning_sequences[winning_sequences_index]);
} else {
this.symbols.change();
}

return true;
},

check_winning_sequences: function(simbol) {
stylize_winner_sequence(winner_sequence) {
winner_sequence.forEach((position) => {
this
.container_element
.querySelector(`div:nth-child(${position + 1})`)
.classList.add('winner');
});
swal({
title: 'Winner',
text: 'Congratulations',
icon: 'success',
closeModal: true,
className: 'modal-winner',
button: {
text: 'Restart'
}

}).then(() => {
this.restart()
})
},

check_winning_sequences(symbol) {

for ( i in this.winning_sequences ) {
if (this.board[ this.winning_sequences[i][0] ] == simbol &&
this.board[ this.winning_sequences[i][1] ] == simbol &&
this.board[ this.winning_sequences[i][2] ] == simbol) {
if (this.board[ this.winning_sequences[i][0] ] == symbol &&
this.board[ this.winning_sequences[i][1] ] == symbol &&
this.board[ this.winning_sequences[i][2] ] == symbol) {
console.log('winning sequences INDEX:' + i);
return i;
}
};
return -1;
},

game_is_over: function() {
game_is_over() {
this.gameover = true;
swal({
title: 'TIED',
text: 'Try Again',
icon: 'warning',
closeModal: true,
className: 'modal-winner',
button: {
text: 'Restart'
}

}).then(() => {
this.restart()
})
console.log('GAME OVER');
},

start: function() {
is_game_over() {
return !this.board.includes('');
},

start() {
this.board.fill('');
this.draw();
this.gameover = false;
},

draw: function() {
let content = '';

for ( i in this.board ) {
content += '<div onclick="tic_tac_toe.make_play(' + i + ')">' + this.board[i] + '</div>';
};
restart() {
if (this.is_game_over() || this.gameover) {
this.start();
console.log('this game has been restarted!')
} else if (confirm('Are you sure you want to restart this game?')) {
this.start();
console.log('this game has been restarted!')
}
},

this.container_element.innerHTML = content;
draw() {
this.container_element.innerHTML = this.board.map((element, index) => `<div onclick="tic_tac_toe.make_play('${index}')"> ${element} </div>`).reduce((content, current) => content + current);
},
};