Click here to Skip to main content
15,888,984 members
Home / Discussions / JavaScript
   

JavaScript

 
QuestionBeginner, no pc Pin
Rufus1827-Apr-18 0:29
Rufus1827-Apr-18 0:29 
AnswerRe: Beginner, no pc Pin
F-ES Sitecore27-Apr-18 1:54
professionalF-ES Sitecore27-Apr-18 1:54 
GeneralRe: Beginner, no pc Pin
Anushkasharma1021-Jun-18 3:42
Anushkasharma1021-Jun-18 3:42 
AnswerRe: Beginner, no pc Pin
Richard MacCutchan27-Apr-18 2:12
mveRichard MacCutchan27-Apr-18 2:12 
GeneralRe: Beginner, no pc Pin
RICHARD LARSON5-May-18 23:39
RICHARD LARSON5-May-18 23:39 
GeneralRe: Beginner, no pc Pin
F-ES Sitecore8-May-18 6:30
professionalF-ES Sitecore8-May-18 6:30 
AnswerRe: Beginner, no pc Pin
Nitin S28-Jun-18 1:22
professionalNitin S28-Jun-18 1:22 
QuestionJavascript HEX game Pin
Member 1373553019-Apr-18 3:01
Member 1373553019-Apr-18 3:01 
This is the code of a hex board game.In this game there are two players RED(user) and BLUE(bot). The one who reaches the opposite end and creates the link without any other color interrupt that player wins.In this game every time RED(user) wins most of the times which is the problem. I want BLUE(not) to be more intelligent by developing its own intelligent decision tree and the chances of RED(user) to be reduced to 10%. Can anyone help me improving my code?


/*CODE STARTS HERE*/



<title>Hex

var r = 20;
var w = r*2*(Math.sqrt(3)/2);
var ctx;
var sel = [-1, -1];
var board = new Array(14);
var hist = [];
var player = 0;
var multiplayer = false;
var active = true;

function drawHexagon(c, x, y, r)
{
c.beginPath();
c.moveTo(x, y-r);
for(var i=0; i<6; i++)
c.lineTo(x+r*Math.cos(Math.PI*(1.5+1/3*i)), y+r*Math.sin(Math.PI*(1.5+1/3*i)));
c.closePath();
c.fill();
c.stroke();
}

function drawPath(c, p)
{
c.lineWidth = 10;
c.beginPath();
c.moveTo((p[0][0]+p[0][1])*w - (p[0][1]-4)*(w/2), (p[0][1]+2)*1.5*r);
for(var i=1; i<p.length; i++)
c.lineTo((p[i][0]+p[i][1])*w - (p[i][1]-4)*(w/2), (p[i][1]+2)*1.5*r);
c.stroke();
}

function getSel(e)
{
var color = ctx.getImageData(e.clientX-20, e.clientY, 1, 1).data;
color[0] -= color[2]==38||color[2]==178 ? 241 : 0;
color[1] -= color[2]==178 ? 220 : (color[2]==38 ? 0 : 140);
if(color[0] >= 0 && color[0] <= 13 && color[1] >= 0 && color[1] <= 13 && (color[2] == 38 || color[2] == 171 || color[2] == 178))
sel = [color[0], color[1]];
else
sel = [-1, -1];
}

function aiMove()
{
var pos;
do
pos = [Math.floor(Math.random()*14), Math.floor(Math.random()*14)];
while(board[pos[0]][pos[1]] != -1);
hist.push([pos[0],pos[1],1]);
board[pos[0]][pos[1]] = 1;
}

function findArr(a, b)
{
for(var i=0; i<a.length; i++)
if(JSON.stringify(a[i]) == JSON.stringify(b))
return i;
return -1;
}

function getConnections(x, y, c, open, closed)
{
var a = [-1, 0, 1, 0, 0, -1, 0, 1, 1, -1, -1, 1];
var ret = [];
for(var i=0; i<6; i++)
if(x+a[i*2] >= 0 && x+a[i*2] < 14 && y+a[i*2+1] >= 0 && y+a[i*2+1] < 14)
if(board[x+a[i*2]][y+a[i*2+1]] == c && findArr(open, [x+a[i*2],y+a[i*2+1]]) == -1 && findArr(closed, [x+a[i*2],y+a[i*2+1]]) == -1)
ret.push([x+a[i*2],y+a[i*2+1]]);
return ret;
}

function checkWin(c)
{
var open = [], openPrev = [], closed = [], closedPrev = [];
for(var a=0; a<14; a++)
{
if(board[c==0?aBlush | :O ][c==0?0:a] == c)
{
open.length = openPrev.length = closed.length = closedPrev.length = 0;
var pathFound = false;
open.push([c==0?aBlush | :O , c==0?0:a]);
openPrev.push(-1);
while(open.length > 0)
{
var u = open[0];
open.splice(0, 1);
var uI = openPrev.splice(0, 1);
closed.push(u);
closedPrev.push(uI);
if(u[c==0?1Blush | :O ] == 13)
{
pathFound = true;
break;
}
var connections = getConnections(u[0], u[1], c, open, closed);
for(var i=0; i<connections.length; i++)
{
open.push(connections[i]);
openPrev.push(closed.length-1);
}
}
if(pathFound)
{
var path = [];
var u = closed.length-1;
while(closedPrev[u] != -1)
{
path.push(closed[u]);
u = closedPrev[u];
}
path.push([c==0?aBlush | :O , c==0?0:a]);
path.reverse();
active = false;
return path;
}
}
}
return false;
}

function mouseDown(e)
{
getSel(e);
if(active)
{
if(sel[0] != -1 && sel[1] != -1)
{
hist.push([sel[0],sel[1],player]);
board[sel[0]][sel[1]] = player;
if(multiplayer)
player = player==0 ? 1 : 0;
else
aiMove();
draw();
var p0 = checkWin(0), p1 = checkWin(1);
if(p0 != false)
{ drawPath(ctx, p0); alert((multiplayer?"The red player":"You") + " won!"); }
else if(p1 != false)
{ drawPath(ctx, p1); alert((multiplayer?"The blue player":"The computer") + " won!"); }
}
}
}

function mouseMove(e)
{
getSel(e);
if(active)
draw();
}

function draw()
{
ctx.clearRect(0, 0, 850, 600);
ctx.lineWidth = 1;

ctx.fillStyle = "rgb(0,154,172)";
ctx.beginPath();
ctx.moveTo(w*15.65, r);
ctx.lineTo(w*23.5, 24.5*r);
ctx.lineTo(0, r);
ctx.lineTo(w*7.85, 24.5*r);
ctx.closePath();
ctx.fill();

ctx.fillStyle = "rgb(255,0,39)";
ctx.beginPath();
ctx.moveTo(0, r);
ctx.lineTo(w*15.65, r);
ctx.lineTo(w*7.85, 24.5*r);
ctx.lineTo(w*23.5, 24.5*r);
ctx.closePath();
ctx.fill();

var num = 0;
ctx.strokeStyle = "white";
for(var y=0; y<14; y++)
{
for(var x=0; x<14; x++)
{
if(board[x][y] == 0)
ctx.fillStyle = "rgb(255,0,39)";
else if(board[x][y] == 1)
ctx.fillStyle = "rgb(0,154,172)";
else if(x == sel[0] && y == sel[1])
ctx.fillStyle = "rgb(" + (x+(player==0?241Blush | :O )) + "," + (y+(player==0?0:140)) + "," + (player==0?38:171) + ")";
else
ctx.fillStyle = "rgb(" + (x+241) + "," + (y+220) + ",178)";
drawHexagon(ctx, (x+y)*w - (y-4)*(w/2), (y+2)*1.5*r, r);
num++;
}
}
}

function chgMP()
{
multiplayer = !multiplayer;
player = 0;
init();
}

function undo()
{
if(active)
{
var a;
if(hist.length > 0)
{
a = hist[hist.length-1];
board[a[0]][a[1]] = -1;
hist.pop();
}
if(!multiplayer)
{
a = hist[hist.length-1];
board[a[0]][a[1]] = -1;
hist.pop();
}
player = a[2];
draw();
}
}

function init()
{
for(var i=0; i<14; i++)
{
board[i] = new Array(14);
for(var j=0; j<14; j++)
board[i][j] = -1;
}
hist.length = 0;
active = true;
draw();
}

function load()
{
var canvas = document.getElementById("output");
ctx = canvas.getContext("2d");
document.getElementById("mp").checked = false;
canvas.onmousedown = mouseDown;
canvas.onmousemove = mouseMove;
init();
}



<canvas style="position:absolute; top:0px; left:20px" width="850" height="600" id="output">Canvas not supported...


Multiplayer:




/*CODE ENDS HERE*/
SuggestionRe: Javascript HEX game Pin
GKP199230-Apr-18 22:09
professionalGKP199230-Apr-18 22:09 
GeneralRe: Javascript HEX game Pin
Member 1373553030-Apr-18 22:22
Member 1373553030-Apr-18 22:22 
GeneralRe: Javascript HEX game Pin
Richard MacCutchan30-Apr-18 23:05
mveRichard MacCutchan30-Apr-18 23:05 
AnswerRe: Javascript HEX game Pin
Richard MacCutchan30-Apr-18 23:04
mveRichard MacCutchan30-Apr-18 23:04 
QuestionJavaScript beginner books, wich one? Pin
Member 1378482717-Apr-18 12:38
Member 1378482717-Apr-18 12:38 
SuggestionRe: JavaScript beginner books, wich one? Pin
Blikkies18-Apr-18 0:09
professionalBlikkies18-Apr-18 0:09 
AnswerRe: JavaScript beginner books, wich one? Pin
W Balboos, GHB4-May-18 7:15
W Balboos, GHB4-May-18 7:15 
QuestionJavascript on mobile application Pin
Member 1378151915-Apr-18 22:24
Member 1378151915-Apr-18 22:24 
QuestionMaking two cards match and then disappear with JQuerry Pin
Member 137670576-Apr-18 23:09
Member 137670576-Apr-18 23:09 
AnswerRe: Making two cards match and then disappear with JQuerry Pin
Christian75739-Apr-18 15:47
Christian75739-Apr-18 15:47 
QuestionNewbie: how do I analyse DOM Objects? Pin
enginestar5-Apr-18 3:05
enginestar5-Apr-18 3:05 
AnswerRe: Newbie: how do I analyse DOM Objects? Pin
Richard MacCutchan5-Apr-18 4:34
mveRichard MacCutchan5-Apr-18 4:34 
GeneralRe: Newbie: how do I analyse DOM Objects? Pin
enginestar5-Apr-18 4:50
enginestar5-Apr-18 4:50 
GeneralRe: Newbie: how do I analyse DOM Objects? Pin
Richard MacCutchan5-Apr-18 6:10
mveRichard MacCutchan5-Apr-18 6:10 
GeneralRe: Newbie: how do I analyse DOM Objects? Pin
enginestar5-Apr-18 6:57
enginestar5-Apr-18 6:57 
GeneralRe: Newbie: how do I analyse DOM Objects? Pin
Richard MacCutchan5-Apr-18 7:00
mveRichard MacCutchan5-Apr-18 7:00 
GeneralRe: Newbie: how do I analyse DOM Objects? Pin
Christian75739-Apr-18 15:51
Christian75739-Apr-18 15:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.