Skip to content
This repository has been archived by the owner on Apr 12, 2021. It is now read-only.

added multiplayer options upto 8 #26

Open
wants to merge 1 commit 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
19 changes: 17 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,23 @@
</head>
<body>
<center>
<div class="undo" id="undo">
<p>UNDO</p>
<div class="select">
<div>
<span>Select No of Players</span>
<select id="noOfPlayers">
<option value="null">select</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
</select>
</div>
<div class="undo" id="undo">
<p>UNDO</p>
</div>
</div>
<canvas width="420" height="630" id="arena">
Canvas tag not supported!
Expand Down
116 changes: 71 additions & 45 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,46 @@ var undoColor = new Array(9);
var isGameOver = false;
var counterAnimate = 0;
var flag = false;

var orginalColors = ["red", "green", "blue", "orange", "yellow", "pink", "purple", "grey"];
var colors = ["red", "green", "blue", "orange", "yellow", "pink", "purple", "grey"];
var noOfPlayers = "null";
var noOfPlayersOption = document.getElementById("noOfPlayers");
var canvas = document.getElementById("arena");
var button = document.getElementById("undo");
var sound = document.getElementById("sound");
var gameArena = canvas.getContext("2d");
canvas.addEventListener("click", gameLoop);
button.addEventListener("click", undoGame);
noOfPlayersOption.addEventListener("change", selectNoOfPlayers)

// initialiseMatrix();
// initialise();

initialiseMatrix();
initialise();
function selectNoOfPlayers()
{
noOfPlayers = noOfPlayersOption.value;
if(noOfPlayers != "null")
{
noOfPlayers = parseInt(noOfPlayers)
initialiseMatrix();
initialise();
} else {
resetCanvas();
}
}

function resetCanvas()
{
gameArena.clearRect(0, 0, width, height);
clearInterval(gameTimer);
colors = orginalColors;
document.getElementById("undo").style.visibility = "hidden";
}

function initialise()
{
document.getElementById("undo").style.visibility = "visible";

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why add an empty line?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ketanhwr I think that was just by mistake, does the PR look good to merge?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've not tested it so far, can you deploy it somewhere so that I can test it? You can probably push this code to your gh-pages branch and give me the link.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ketanhwr I have pushed the changes into my repo and hosted here https://reddysridhar53.github.io/chain-reaction
Please have a look

document.getElementById("undo").style.visibility = noOfPlayers != "null" ? "visible" : "hidden";
isGameOver = false;
matrixDefault();
drawArena();
Expand Down Expand Up @@ -62,11 +88,10 @@ function matrixDefault()
function drawArena()
{
gameArena.clearRect(0, 0, width, height);

if(turnCount % 2 == 0)
gameArena.strokeStyle = "red";
if(turnCount % noOfPlayers == 0)
gameArena.strokeStyle = colors[0];
else
gameArena.strokeStyle = "green";
gameArena.strokeStyle = colors[turnCount % noOfPlayers];

for(var counter = 1; counter < 6; counter++)
{
Expand Down Expand Up @@ -116,7 +141,7 @@ function undoGame()
colorMatrix[i][j] = undoColor[i][j];
}
}

} else {
$('.undoMessage').stop().fadeIn(400).delay(2000).fadeOut(400); //fade out after 2 seconds
}
Expand Down Expand Up @@ -147,17 +172,17 @@ function gameLoop(event)
if(!isGameOver)
{
takeBackUp();
if(turnCount%2 == 0 && (colorMatrix[column][row] == "" || colorMatrix[column][row] == "red"))
if(turnCount%noOfPlayers == 0 && (colorMatrix[column][row] == "" || colorMatrix[column][row] == colors[0]))
{
countMatrix[column][row]++; //Weird graphic coordinate-system
colorMatrix[column][row] = "red";
colorMatrix[column][row] = colors[0];
turnCount++;
flag = false;
}
if(turnCount%2 == 1 && (colorMatrix[column][row] == "" || colorMatrix[column][row] == "green"))
if(turnCount%noOfPlayers >= 1 && (colorMatrix[column][row] == "" || colorMatrix[column][row] == colors[turnCount%noOfPlayers]))
{
countMatrix[column][row]++; //Weird graphic coordinate-system
colorMatrix[column][row] = "green";
colorMatrix[column][row] = colors[turnCount%noOfPlayers];
turnCount++;
flag = false;
}
Expand Down Expand Up @@ -247,14 +272,14 @@ function updateMatrix()

function checkGameOver()
{
if(gameOver() == 1 || gameOver() == 2)
if(gameOver())
{
isGameOver = true;
document.getElementById("undo").style.visibility = "hidden";
drawArena();
setTimeout(gameOverScreen.bind(null,gameOver()), 2000);
setTimeout(gameOverScreen.bind(null,gameOver()), 1000);
clearInterval(gameTimer);
setTimeout(initialise, 6000);
setTimeout(resetCanvas, 5000);
}
}

Expand Down Expand Up @@ -284,49 +309,50 @@ function notStable()

function gameOver()
{
var countRed = 0;
var countGreen = 0;
var colorCountObj={};
for(var k = 0;k < noOfPlayers; k++){
colorCountObj[colors[k]] = 0;
}
for(var i = 0; i < 9; i++)
{
for(var j = 0;j < 6; j++)
{
if(colorMatrix[i][j] == "red") countRed++;
if(colorMatrix[i][j] == "green") countGreen++;
for(var k = 0;k < noOfPlayers; k++){
if(colorMatrix[i][j] == colors[k]){
if(colorCountObj[colors[k]]){
colorCountObj[colors[k]] = colorCountObj[colors[k]] + 1;
}else{
colorCountObj[colors[k]] = 1;
}
}
}
}
}
if(turnCount > 1)
if(turnCount > noOfPlayers)
{
if(countRed == 0)
{
return 2;
var playersIn = 0;
for(var color in colorCountObj){
if(colorCountObj[color] !== 0){
playersIn++;
} else {
colors.splice(colors.indexOf(color), 1)
}
}
if(countGreen == 0)
{
return 1;
noOfPlayers = playersIn;
if(noOfPlayers == 1){
return orginalColors.indexOf(color)+1;
}
}
}

function gameOverScreen(player)
{
if(player == 2)
{
gameArena.clearRect(0, 0, width, height);
gameArena.fillStyle = "black";
gameArena.fillRect(0, 0, width, height);
gameArena.fillStyle = "white";
gameArena.font = "40px Times New Roman";
gameArena.fillText("Player 2 wins!", width/2 - 150, height/2 - 50);
}
else
{
gameArena.clearRect(0, 0, width, height);
gameArena.fillStyle = "black";
gameArena.fillRect(0, 0, width, height);
gameArena.fillStyle = "white";
gameArena.font = "40px Times New Roman";
gameArena.fillText("Player 1 wins!", width/2 - 150, height/2 - 50);
}
gameArena.clearRect(0, 0, width, height);
gameArena.fillStyle = "black";
gameArena.fillRect(0, 0, width, height);
gameArena.fillStyle = "white";
gameArena.font = "40px Times New Roman";
gameArena.fillText("Player "+player+" wins!", width/2 - 150, height/2 - 50);
}

function oneCircle(row, column, color)
Expand Down
16 changes: 15 additions & 1 deletion style.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ canvas
color: black;
font-size: 18px;
width: 130px;
visibility: hidden;
border-radius: 25px;
text-decoration: none;
height: 20px;
Expand Down Expand Up @@ -50,4 +51,17 @@ canvas
-webkit-box-shadow: 0px 0px 24px -1px rgba(56, 56, 56, 1);
-moz-box-shadow: 0px 0px 24px -1px rgba(56, 56, 56, 1);
box-shadow: 0px 0px 24px -1px rgba(56, 56, 56, 1);
}
}
.select{
margin: 2em 0;
}
.select div{
display: inline-block;
margin: 0.5em;
}
.select span{
color: #fff;
}
.select p{
margin: 0;
}