kubowania / connect-four Goto Github PK
View Code? Open in Web Editor NEWA Pure JavaScript grid-based game
A Pure JavaScript grid-based game
In the winningArrays
, we have:
[11, 7, 23, 29]
But that should be:
[11, 17, 23, 29]
All the best,
Please provide the website link so that we can check the output.
Hi Ania, thank you very much for the JS course!
In this game though I think that rather than using a fixed array to find winning combinations it would be better to calculate the position of the selected cell, then look for the three adjacent cells in the available directions for matching cells dynamically.
These functions do that with basic arithmetics. You'll need to invoke checkWinner()
with the position of the clicked cell (its index
in the squares
array) and the player that is performing the play (so invoke the function before changing currentPlayer
, or store it for later use).
Best,
Proteo.
function checkCandidates(index, cssName, callbackId) {
// Look for the three adjacent cells, as defined by callbackId().
for (let i = 1; i <= 3; i++) {
let id = callbackId(index, i);
if (!squares[id].classList.contains(cssName)) {
return false;
}
}
return true;
}
function checkWinner(index, player) {
const cssName = (player == 2) ? 'player-two' : 'player-one';
const cellPos = (index + 1);
const cellRow = Math.ceil(cellPos / 7);
const cellCol = Math.ceil(cellPos - ((cellRow - 1) * 7));
// Check bottom cells (after, in the same column).
if (cellRow < 4 && checkCandidates(index, cssName, (index, i) => index + (7 * i))) {
return true;
}
// Check left cells (before, in the same row).
if (cellCol >= 4 && checkCandidates(index, cssName, (index, i) => index - i)) {
return true;
}
// Check top cells (before, in the same column).
if (cellRow >= 4 && checkCandidates(index, cssName, (index, i) => index - (7 * i))) {
return true;
}
// Check right cells (after, in the same column).
if (cellCol <= 4 && checkCandidates(index, cssName, (index, i) => index + i)) {
return true;
}
// No winner yet!
return false;
}
i think a good addition would be to center it in the screen and making it a bit bigger so its easier to see
@kubowania
Hi, I would like to point out that the link which you have given in the README.MD
here is not redirecting.
Tried playing this yesterday with my son Noah.
But we immediately noticed some hard to fix bugs in this implementation.
Wrote a new version of this game from scratch. Hope you like it (we certainly enjoyed ourselves playing it yesterday):
https://github.com/w-A-L-L-e/connect-four
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.