Skip to content

Commit

Permalink
Completed with no GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
ad3ldev committed Dec 16, 2021
1 parent 53861d5 commit b1014db
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 12 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# TicTacToe

A basic TicTacToe game.
A basic TicTacToe game, 2 players or against an impossible to win against AI.

## Technologies

Expand All @@ -13,10 +13,12 @@ For the CSS, I am using Sass because it extends the functionalities and capabili

## Algorithms

Loops to check which player won
MiniMax to make the AI choose which move will it choose.

Loops to check which player won.

---

### What comes now?

I wanna change the look and make the win button become a pop-up
Better UI
14 changes: 13 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@
</head>
<body>
<main>
<button id="end" hidden></button>
<div class="start">
<h1>TicTacToe</h1>
<button id="single">Single Player</button>
<button id="coop">Mutli Player</button>
</div>
<div class="choice">
<h2>Difficulity</h2>
<button id="easy">Easy</button>
<button id="medium">Medium</button>
<button id="hard">Hard</button>
<button id="impossible">Impossible</button>
</div>
<div class="board">
<button id="btn00" class="btn"></button>
<button id="btn01" class="btn"></button>
Expand All @@ -20,6 +31,7 @@
<button id="btn21" class="btn"></button>
<button id="btn22" class="btn"></button>
</div>
<button id="end"></button>
</main>
<script type="module" src="src/index.js"></script>
</body>
Expand Down
95 changes: 89 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,88 @@
import "./style.scss";

let level;
let COOP = false;
let aiFirst = false;

let board = [
["", "", ""],
["", "", ""],
["", "", ""],
];

const levels = {
easy: 1,
medium: 3,
hard: 6,
impossible: 9,
};

let player = true;
let HUMAN = player ? "X" : "O";
let AI = player ? "O" : "X";
const buttons = document.querySelectorAll(".btn");
const end = document.querySelector("#end");
const grid = document.querySelector(".board");
const start = document.querySelector(".start");
const choice = document.querySelector(".choice");

let board = [
["", "", ""],
["", "", ""],
["", "", ""],
];
const single = document.querySelector("#single");
const multi = document.querySelector("#coop");

single.addEventListener("click", () => {
COOP = false;
start.setAttribute("style", "display:none");
choice.setAttribute("style", "display: grid");
});
const easyBtn = document.querySelector("#easy");
easyBtn.addEventListener("click", () => {
choice.setAttribute("style", "display:none");
grid.setAttribute("style", "display: grid");
level = levels.easy;
});

const mediumBtn = document.querySelector("#medium");
mediumBtn.addEventListener("click", () => {
choice.setAttribute("style", "display:none");
grid.setAttribute("style", "display: grid");
level = levels.medium;
});

const hardBtn = document.querySelector("#hard");
hardBtn.addEventListener("click", () => {
choice.setAttribute("style", "display:none");
grid.setAttribute("style", "display: grid");
level = levels.hard;
});

const impossibleBtn = document.querySelector("#impossible");
impossibleBtn.addEventListener("click", () => {
choice.setAttribute("style", "display:none");
grid.setAttribute("style", "display: grid");
level = levels.impossible;
});

multi.addEventListener("click", () => {
COOP = true;
start.setAttribute("style", "display:none");
grid.setAttribute("style", "display: grid");
});

end.addEventListener("click", () => {
board = [
["", "", ""],
["", "", ""],
["", "", ""],
];
buttons.forEach((button) => {
button.innerHTML = "";
button.disabled = false;
});
grid.setAttribute("style", "display: none");
start.setAttribute("style", "display:grid");
end.setAttribute("style", "display: none");
end.innerHTML = "";
});

function emptyPlaces(state) {
let places = [];
Expand Down Expand Up @@ -57,6 +126,16 @@ buttons.forEach((button) => {
drawOnBoard();
if (gameOverAll(board)) {
disableAll();
if (evaluate(board) > 0) {
end.innerHTML = `${HUMAN} WON`;
} else {
end.innerHTML = `${AI} WON`;
}
end.setAttribute("style", "display:grid");
}
if (emptyPlaces(board).length == 0) {
end.setAttribute("style", "display:grid");
end.innerHTML = "DRAW";
}
});
});
Expand Down Expand Up @@ -139,7 +218,11 @@ function aiTurn() {
x = Math.trunc(Math.random() * 3);
y = Math.trunc(Math.random() * 3);
} else {
move = minimax(board, emptyPlaces(board).length, AI);
let depth = emptyPlaces(board).length;
if (depth > level) {
depth = level;
}
move = minimax(board, depth, AI);
x = move[0];
y = move[1];
}
Expand Down
29 changes: 27 additions & 2 deletions src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ main {
align-items: center;
}
.board {
position: relative;
display: grid;
display: none;
grid-template-columns: repeat(3, 1fr);
aspect-ratio: 1/1;
.btn {
Expand All @@ -25,7 +24,33 @@ main {
}
}

.start {
display: grid;
place-items: center;
gap: 1rem;
grid-template-columns: repeat(2, 1fr);
}
.start h1 {
grid-column: span 2;
}
.start button {
padding: 1rem;
}
.choice {
display: none;
grid-template-columns: repeat(4, 1fr);
gap: 1rem;
place-items: center;
}
.choice h2 {
grid-column: span 4;
}
.choice button {
padding: 1em;
}

#end {
display: none;
padding: 1rem;
font-size: clamp(16px, 5vmin, 100vw);
}

0 comments on commit b1014db

Please sign in to comment.