I am trying to visualize a card game consisting of 20 triangles which can be placed in various ways. Each triangle has 3 sides, each of the sides has one counterpart in the remaining 19 triangles. Only if the sides match they can be put together.
the code that randomly generates possibilities is the following.( I am happy also for improvements on the code, which are, I am sure, plenty
JAVSCRIPT
function Game() {
this.cardsUnused = [];
this.cardsUsed = [];
this.possibleCards = [];
this.insert = function (obj) {
this.cardsUnused.push(obj);
};
this.useCard = function (obj) { // adds a triangle to the usedCards Array and removes it from the UnusedCards Array
this.cardsUsed.push(obj);
this.cardsUnused.remove(obj);
this.possibleCards.remove(obj);// t (array.prototype.remove)
};
this.unuseCard = function (obj) {
this.cardsUnused.push(obj);
this.cardsUsed.remove(obj);
};
}
// remove function
Array.prototype.remove = function () {
var what, a = arguments, L = a.length, ax;
while (L && this.length) {
what = a[--L];
while ((ax = this.indexOf(what)) !== -1) {
this.splice(ax, 1);
}
}
return this;
};
var game = new Game();
// Triangle wird erstellt
function Triangle(name, sideA, sideB, sideC, Direction, position) {
this.partName = name;
this.sides = [sideA, sideB, sideC];
this.sidesStatus = [0, 0, 0];
this.sideDirection = Direction;
this.stepUsed = 0;
this.angle = 0;
this.cardPosition = position;
this.turn = function (winkel) {
this.angle = this.angle + winkel;
};
this.useSide = function (side) {
this.sidesStatus[side] = 1;
};
}
var a = new Triangle("A", 1, 2, 21, 0, [0, 0]);
var b = new Triangle("B", 2, 3, 22, 0, [0, 0]);
var c = new Triangle("C", 3, 4, 23, 0, [0, 0]);
var d = new Triangle("D", 4, 5, 24, 0, [0, 0]);
var e = new Triangle("E", 5, 1, 25, 0, [0, 0]);
var f = new Triangle("F", 6, 21, 7, 180, [0, 0]);
var g = new Triangle("G", 7, 8, 26, 0, [0, 0]);
var h = new Triangle("H", 8, 22, 9, 180, [0, 0]);
var i = new Triangle("I", 9, 10, 27, 0, [0, 0]);
var j = new Triangle("J", 10, 23, 11, 180, [0, 0]);
var k = new Triangle("K", 11, 12, 28, 0, [0, 0]);
var l = new Triangle("L", 12, 24, 13, 180, [0, 0]);
var m = new Triangle("M", 13, 14, 29, 0, [0, 0]);
var n = new Triangle("N", 14, 25, 15, 180, [0, 0]);
var o = new Triangle("O", 15, 6, 30, 0, [0, 0]);
var p = new Triangle("P", 16, 26, 17, 180, [0, 0]);
var q = new Triangle("Q", 17, 27, 18, 180, [0, 0]);
var r = new Triangle("R", 18, 28, 19, 180, [0, 0]);
var s = new Triangle("S", 19, 29, 20, 180, [0, 0]);
var t = new Triangle("T", 20, 30, 16, 180, [0, 0]);
// an array with 20 triangles
var triangles = [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t];
// function to add triangles to the game
function addTriangles(arr) {
var x;
for (x = 0; x < arr.length; x += 1) {
game.insert(triangles[x]);
}
}
addTriangles(triangles);
game.useCard(a);
function toRadians (angle) {
return angle * (Math.PI / 180);
}
function findRandomCardPossibleToUsedCards() {
var isTrue = 0, randomIndexUsedCard, randomIndexUsedSide, randomIndexUnusedCard, randomIndexUnusedSide, position;
while (isTrue === 0) {
randomIndexUsedCard = Math.floor(Math.random() * game.cardsUsed.length);
randomIndexUsedSide = Math.floor(Math.random() * 3);
randomIndexUnusedCard = Math.floor(Math.random() * game.cardsUnused.length);
randomIndexUnusedSide = Math.floor(Math.random() * 3);
if (typeof game.cardsUnused[randomIndexUnusedCard] === 'undefined') {
document.getElementById("button").innerHTML = "stopped";
}
// position = game.cardsUsed[randomIndexUsedCard].cardPosition;
if (game.cardsUsed[randomIndexUsedCard].sidesStatus[randomIndexUsedSide] !== 1 && game.cardsUnused[randomIndexUnusedCard].sidesStatus[randomIndexUnusedSide] !== 1) {
if (game.cardsUsed[randomIndexUsedCard].sides[randomIndexUsedSide] === game.cardsUnused[randomIndexUnusedCard].sides[randomIndexUnusedSide]) {
isTrue = 1;
// the main problem I am having lies withing the next three " if -parts" and the translation into the css/html to depict the triangles in the correct way
if (randomIndexUsedSide === 0) {
game.cardsUnused[randomIndexUnusedCard].sideDirection = game.cardsUsed[randomIndexUsedCard].sideDirection + 60;
game.cardsUnused[randomIndexUnusedCard].cardPosition[0] = game.cardsUsed[randomIndexUsedCard].cardPosition[0] - 100 * Math.sin(toRadians(game.cardsUsed[randomIndexUsedCard].sideDirection + 90));
game.cardsUnused[randomIndexUnusedCard].cardPosition[1] = game.cardsUsed[randomIndexUsedCard].cardPosition[1] + 86 * Math.cos(toRadians(game.cardsUsed[randomIndexUsedCard].sideDirection - 90));
}
if (randomIndexUsedSide === 1) {
game.cardsUnused[randomIndexUnusedCard].sideDirection = game.cardsUsed[randomIndexUsedCard].sideDirection - 60;
game.cardsUnused[randomIndexUnusedCard].cardPosition[0] = game.cardsUsed[randomIndexUsedCard].cardPosition[0] - 100 * Math.sin(toRadians(game.cardsUsed[randomIndexUsedCard].sideDirection -90));
game.cardsUnused[randomIndexUnusedCard].cardPosition[1] = game.cardsUsed[randomIndexUsedCard].cardPosition[1] + 86 * Math.cos(toRadians(game.cardsUsed[randomIndexUsedCard].sideDirection + 90));
}
if (randomIndexUsedSide === 2) {
game.cardsUnused[randomIndexUnusedCard].sideDirection = game.cardsUsed[randomIndexUsedCard].sideDirection + 180;
game.cardsUnused[randomIndexUnusedCard].cardPosition[0] = game.cardsUsed[randomIndexUsedCard].cardPosition[0] + 100 * Math.sin(toRadians(game.cardsUsed[randomIndexUsedCard].sideDirection - 180));
game.cardsUnused[randomIndexUnusedCard].cardPosition[1] = game.cardsUsed[randomIndexUsedCard].cardPosition[1] - 86 * Math.cos(toRadians(game.cardsUsed[randomIndexUsedCard].sideDirection - 180));
}
game.cardsUsed[randomIndexUsedCard].sidesStatus[randomIndexUsedSide] = 1;
game.cardsUnused[randomIndexUnusedCard].sidesStatus[randomIndexUnusedSide] = 1;
var elem = document.getElementById(game.cardsUnused[randomIndexUnusedCard].partName);// STYLE
var elemInner = elem.getElementsByTagName('div');
for (i = 0; i < elemInner.length; i++) {
elemInner = elemInner[i];
}
elem.className = elem.className + " used"; // STYLE
//elemInner.style.transform = "rotate(" + game.cardsUnused[randomIndexUnusedCard].sideDirection + "deg)" ;
elem.style.transform =
"translate(" + game.cardsUnused[randomIndexUnusedCard].cardPosition[0] +
"px," + game.cardsUnused[randomIndexUnusedCard].cardPosition[1] +
"px)" +" rotate(" + game.cardsUnused[randomIndexUnusedCard].sideDirection + "deg)" ;
var elemA = document.getElementById(a.partName);// STYLE
elemA.className = elemA.className + " used"; // STYLE
game.useCard(game.cardsUnused[randomIndexUnusedCard]);
}
}
}
}
CSS
#game {
width: 1000px;
margin:auto;
margin-top: 300px;
}
.triangle {
display: none;
position: absolute;
width: 100px;
height:100px;
line-height: 100px;
text-align: center;
visibility: hidden;
}
.up {
background-image: url(imgs/tri_up_red.png);
}
.down {
background-image: url(imgs/tri_up_red.png);
}
.used {
display: inline-block;
visibility:visible;
}
.hidden {
visibility: hidden;
}
.inner {
width: 100px;
height: 87px;
margin-top: 6.5px;
margin-bottom: 6.5px;
}
HTML
<html>
<head>
<link href="design.css" type="text/css" rel="stylesheet">
<script language="javascript" type="text/javascript" src="game.js"></script>
</head>
<body><div id="button">
<button onclick="findRandomCardPossibleToUsedCards()">add Triangle</button>
</div>
<div id="game" class="game">
<div id="A" class="triangle">
<div class="inner up"><span>A</span></div>
</div>
<div id="B" class="triangle">
<div class="inner up"><span>B</span></div>
</div>
<div id="C" class="triangle">
<div class="inner up"><span>C</span></div>
</div>
<div id="D" class="triangle">
<div class="inner up"><span>D</span></div>
</div>
<div id="E" class="triangle">
<div class="inner up"><span>E</span></div>
</div>
<div id="F" class="triangle">
<div class="inner down"><span>F</span></div>
</div>
<div id="G" class="triangle">
<div class="inner up"><span>G</span></div>
</div>
<div id="H" class="triangle">
<div class="inner down"><span>H</span></div>
</div>
<div id="I" class="triangle">
<div class="inner up"><span>I</span></div>
</div>
<div id="J" class="triangle">
<div class="inner down"><span>J</span></div>
</div>
<div id="K" class="triangle">
<div class="inner up"><span>K</span></div>
</div>
<div id="L" class="triangle">
<div class="inner down"><span>L</span></div>
</div>
<div id="M" class="triangle">
<div class="inner up"><span>M</span></div>
</div>
<div id="N" class="triangle">
<div class="inner down"><span>N</span></div>
</div>
<div id="O" class="triangle">
<div class="inner up"><span>O</span></div>
</div>
<div id="P" class="triangle">
<div class="inner down"><span>P</span></div>
</div>
<div id="Q" class="triangle">
<div class="inner down"><span>Q</span></div>
</div>
<div id="R" class="triangle">
<div class="inner down"><span>R</span></div>
</div>
<div id="S" class="triangle">
<div class="inner down"><span>S</span></div>
</div>
<div id="T" class="triangle">
<div class="inner down"><span>T</span></div>
</div>
</div>
</body>
</html>
The code to get the possible combinations works fine for me. As I said suggestions for improvement are welcome. My real problem is the visualization. In html all the divs are squares and and rotating and translating theses Squares/triangles gives me headaches. Correct rotations works and I think am on on the right path using the sine and cosine function to determine the translation in the x and y axis. But somehow I am stuck. I am not asking anyone to solve it for me but maybe someone can show me a different approach or point out where I was wrong.