Skip to content

Commit

Permalink
Your commit message here
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmmed-binas committed Mar 25, 2024
1 parent 91a11c1 commit 6b288cf
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 31 deletions.
4 changes: 1 addition & 3 deletions htmlcube.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const scene = new THREE.Scene();
const width = window.innerWidth;
const height = window.innerHeight;
const camera = new THREE.PerspectiveCamera(75, width / height, 0.1, 1000);
camera.position.z = 6;
camera.position.z = -6;


const renderer = new THREE.WebGLRenderer();
Expand Down Expand Up @@ -56,8 +56,6 @@ function animate() {
renderer.render(scene, camera);
xno();
updateFragments()


}

animate();
18 changes: 14 additions & 4 deletions sqaureobject.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
let squares = [];


function create_squares() {
const squareSize = 0.7;
const squares = [];

const squareGeometry = new THREE.PlaneGeometry(squareSize, squareSize);
const squareMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000, side: THREE.DoubleSide });

Expand Down Expand Up @@ -82,12 +83,21 @@ function rotate(){
for (var s = 18; s <= 35; s++) {
squaresArray[s].rotation.set(0, -Math.PI / 2, 0);
}

// Rotate squares from index 36 to 54
for (var s = 36; s <= 54; s++) {
squaresArray[s].rotation.set(-Math.PI / 2, 0, 0);
}
}

rotate();
create_sqaure();
create_sqaure();



// Change color of a specific square
function changeSquareColor(squareIndex, newColor) {
squaresArray[squareIndex].material.color.set(newColor);
}

// Example: Change the color of square at index 10 to green (hex: 0x00ff00)
changeSquareColor(9, 0x00ff00); // Remember, array indices start from 0, so square 10 has index 9.
97 changes: 73 additions & 24 deletions x.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ let xMatchCount = 0;
let oMatchCount = 0;
let xMatchCountText;
let oMatchCountText;
let currentPlayer = 'X';
let currentPosition=[];
let winningTexts = [];
let clickedPositions = new Set();

const textpositions= [
new THREE.Vector3(-0.94, -0.94, -1.2), // 1
Expand Down Expand Up @@ -74,9 +78,6 @@ const mouse = new THREE.Vector2();
window.addEventListener('click', mouseclick, false);




let clickedPositions = new Set();
function mouseclick(event) {

mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
Expand All @@ -97,7 +98,10 @@ function mouseclick(event) {
clickedPositions.add(i);
all_clicked_pos.push(i);
keyval(TextXO, all_clicked_pos,setsz);
updateTextContent();
keyval(TextXO, all_clicked_pos,setsnz);
console.log(addedtext)

// updateTextContent();
}
break;

Expand All @@ -110,8 +114,6 @@ function mouseclick(event) {



let currentPlayer = 'X';
let currentPosition=[];

function addXManually({ x, y, z }) {
const currentPositionn= new THREE.Vector3(x, y, z);
Expand Down Expand Up @@ -189,7 +191,7 @@ function addText(position, textValue) {
const textw = new THREE.Mesh(geometry, matLite);
textw.rotation.set(0, -Math.PI / 2, 0);
textw.position.copy(textpositions[intersectedIndex]);
textw.name=textValue;
textw.name=textValue+intersectedIndex;
scene.add(textw);
addedtext.push(textw)

Expand All @@ -198,7 +200,7 @@ function addText(position, textValue) {
} else if (intersectedIndex >=36 && intersectedIndex <=53) {
const textu = new THREE.Mesh(geometry, matLite);
textu.rotation.set(Math.PI / 2, 0, 0);
textu.name=textValue;
textu.name=textValue+intersectedIndex;
textu.position.copy(textpositions[intersectedIndex]);
scene.add(textu);
addedtext.push(textu);
Expand All @@ -209,10 +211,11 @@ function addText(position, textValue) {
} else {

const text = new THREE.Mesh(geometry, matLite);
text.name=textValue;
text.name=textValue+intersectedIndex;
text.position.copy(textpositions[intersectedIndex]);
scene.add(text);
addedtext.push(text);


}

Expand All @@ -225,6 +228,9 @@ function addText(position, textValue) {
//currentpossible contains all positions and the last posiition all postition are accessable...
//allclickedpos contains the number[location] of the clicked sqaure and clicked text,,,
// divided aray consists of allclicked positions divided by 3!!!!


//camera facing z dir
let setsz = [
[15,16,17],
[12,13,14],
Expand All @@ -236,18 +242,21 @@ let setsz = [
[17,13,9]
];


//camera facing -z dir
let setsnz = [
[15,16,17],
[12,13,14],
[9,10,11],
[15,12,9],
[16,13,10],
[17,14,11],
[15,13,11],
[17,13,9]
[8,5,2],
[7,4,1],
[6,3,0],
[8,7,6],
[5,4,3],
[2,1,0],
[8,4,0],
[6,4,2]
];




function keyval(arr1, arr2, arr3) {
let keyvalue = [];
const minLength = Math.min(arr1.length, arr2.length);
Expand Down Expand Up @@ -285,15 +294,19 @@ function keyval(arr1, arr2, arr3) {

if (foundMatch) {
matchCount++;
const winningText = keyvalue.map(obj => obj[Object.keys(obj)[0]]).join('');
winningTexts.push({ text: winningText, value: keyvalue });
}
}

console.log("Total matching sets with setsz:", matchCount);
// console.log("Total matching sets with setsz:", matchCount);
divideAndCheck(keyvalue, setsz);
console.log(keyvalue)
divideAndCheck(keyvalue, setsnz);
// console.log(keyvalue)
}
}




function divideAndCheck(keyvalue, arr3) {
let xValues = [];
Expand All @@ -309,10 +322,10 @@ function divideAndCheck(keyvalue, arr3) {
}
});


xMatchCount = 0;
oMatchCount = 0;

const winningCombinations = []; // Array to store winning combinations

for (let set of arr3) {
let xMatch = set.every(pos => xValues.includes(pos));
Expand All @@ -328,12 +341,45 @@ function divideAndCheck(keyvalue, arr3) {
}
}

console.log("Total matching sets with setsz for X:", xMatchCount);
console.log("Total matching sets with setsz for O:", oMatchCount);
console.log("Total matching sets with sets for X:", xMatchCount);
console.log("Total matching sets with sets for O:", oMatchCount);

if (xMatchCount > 0 || oMatchCount > 0) {
const winningSquares = [];

for (let set of arr3) {
let xMatch = set.every(pos => xValues.includes(pos));
let oMatch = set.every(pos => oValues.includes(pos));

if (xMatch || oMatch) {
winningSquares.push(...set);
}
}

// Push winning combinations with text names directly
for (let text of addedtext) {
const name = text.name;
for (let i = 0; i < winningTexts.length; i++) {
if (winningTexts[i].text === name) {
winningCombinations.push({ text: name, value: keyvalue });
break;
}
}
}

console.log("Winning combinations:", winningCombinations);
}

return winningCombinations; // Return the array of winning combinations
}



function getWinningTexts() {
return winningTexts;
}


let xTextMesh = null;
let oTextMesh = null;

Expand Down Expand Up @@ -388,3 +434,6 @@ function updateTextContent() {
let xMatchCountPosition = new THREE.Vector3(7, -4, -15);
let oMatchCountPosition = new THREE.Vector3(7, -5.5, -15);




0 comments on commit 6b288cf

Please sign in to comment.