Winning Condition is not working properly

edited March 2014 in Questions about Code

Hi, Here is the code of a very simple and silly game where you have to join two pieces of a cube which are randomly seprated and rotated in the beginning of the game. The very first level starts with the 2X2 cube and it gets incresed to 3X3 ,4X4 and so on, as you solve it.

The problem I am facing is that the winning condition is not working properly. Sometime it works sometime it doesn't. Also the movable peice doesn't have the axis() aligned in the center ( in the cente of the complete cube).

controls:

  1. press ' l ' for light
  2. press ' o ' for outline
  3. numbers: 8,2,4,6,5,1 for rotation
  4. arrow key for moving in x and y . ' < ' & ' > ' for moving in z direction

Cheats: Press the keys for rotating the piece such that you get all cos(theta)s = 1 and then shift it to the position and it will solve.

 import peasy.*;
PeasyCam cam;
//------------------------------
int t=2;
int[][][] missbox;
int x, y, z;
int ax, ay, az;
boolean flag = false, light = false, outline =true;
int j=0, l = 100;
//------------------------------
void setup() {
  size(600, 600, P3D);
  cam = new PeasyCam(this, 100);
  cam.setMinimumDistance(50);
  cam.setMaximumDistance(3000);
  ResetGame();
}

void draw() {
  background(-1);
  frame.setTitle("LEVEL "+ (t-1) + "       Size " + t + " X " + t);
  // ResetParameters();
  if (light)lights();
  noStroke();
  pushMatrix();
  translate(25*(t-1), 25*(t-1), 25*(t-1));
  axis();
  popMatrix();

  if (outline)stroke(0);
  for (int i=0;i<t;i++) {
    for (int j=0;j<t;j++) {
      for (int k=0;k<t;k++) {
        if (missbox[i][j][k]==0) {
          pushMatrix();
          fill(255, 255, 0);
          translate(i*50, j*50, k*50);
          box(50);
          popMatrix();
        }
      }
    }
  }
  pushMatrix(); 
  translate(x, y, z);
  rotateX(radians(90*ax));
  rotateY(radians(90*ay));
  rotateZ(radians(90*az));
  pushMatrix();
  translate(25*(t-1), 25*(t-1), 25*(t-1));
  axis();
  popMatrix();
  //----create missing boxes-----
  if (outline)stroke(0);
  for (int i=0;i<t;i++) {
    for (int j=0;j<t;j++) {
      for (int k=0;k<t;k++) {
        if (missbox[i][j][k]==1) {
          pushMatrix();
          translate(i*50, j*50, k*50);
          fill(255, 0, 0);
          box(50);
          popMatrix();
        }
      }
    }
  }
  popMatrix();
  j++;
  if (dist(25*(t-1), 25*(t-1), 25*(t-1), 
  x - 25*(t-1), y-25*(t-1), z - 25*(t-1))<(t-1)*50 
    && abs((int)cos(radians(90*(ax))))== 1 
    && abs((int)cos(radians(90*(ay))))== 1 
    && abs((int)cos(radians(90*(az))))== 1 ) {  // winning condition
    println("You have solved it");
    x = 25*(t-1);
    y = 25*(t-1); 
    z = 25*(t-1);
    if (millis()%1000==0 && millis()>0) {
      t++;
      ResetGame();
    }
  }
}

void keyPressed() {
  if (key == CODED) {
    if (keyCode == LEFT) x-=2;
    else if (keyCode == RIGHT) x+=2;
    if (keyCode == UP) y+=2;
    else if (keyCode == DOWN) y-=2;
  } 

  if (key=='.' ) z+=2;
  else if (key==',') z-=2;

  if (key=='8') ax++;
  if (key=='2') ax--;
  if (key=='4') ay++;
  if (key=='6') ay--;
  if (key=='5') az++;
  if (key=='1') az--;

  if (key=='l') light= !light;
  if (key=='o') outline= !outline;

  println(" ax:" + ax + " ay:" + ay + " az:" + az);
  println(" cos(ax):" + (int)cos(radians(90*(ax))) + 
    " cos(ay):" + (int)cos(radians(90*(ay))) +
    " cos(az):" + (int)cos(radians(90*(az))));
}


void ResetGame() {
  missbox = new int[t][t][t];
  for (int i=0;i<t;i++) {
    for (int j=0;j<t;j++) {
      for (int k=0;k<t;k++) {
        if (random(0, 100)<50) {
          missbox[i][j][k] = 0;
        }
        else {
          missbox[i][j][k] = 1;
        }
      }
    }
  }
  ax = (int)random(4);
  ay = (int)random(4);
  az = (int)random(4);
  x = (int)random(t*50*3);
  y = (int)random(t*50*3);
  z = (int)random(t*50*3);
  l = t*50;
}


void axis() {
  pushStyle();
  strokeWeight(2);
  fill(255, 0, 0);
  text("-X", -l, 0, 0);
  text("+X", l, 0, 0);
  stroke(255, 0, 0);
  line(-l, 0, 0, l, 0, 0);
  fill(0, 255, 0);
  text("-Y", 0, -l, 0);
  text("+Y", 0, l, 0);
  stroke(0, 255, 0);
  line(0, -l, 0, 0, l, 0);
  fill(0, 0, 255);
  text("-Z", 0, 0, -l);
  text("+Z", 0, 0, l);
  stroke(0, 0, 255);
  line(0, 0, -l, 0, 0, l);
  popStyle();
}--

Answers

  • Your super long line is hard to read in the forum, and probably in the PDE too. Better split it over several lines...

    I see conditions like abs((int)cos(radians(90*(az))))==1
    Because of floating point precision, it can be false even if az is integer. I tried the expression with 1, 2, 3, 4 and it is not zero but a very low number. It is indeed 1 for 2 and 4, but check the other parts of the condition, too.

    With floating point numbers, it is better to check if a number is within a small range (say 0.99 to 1.01 or similar.

    Perhaps I am way off of the problem, but it is the first thought I have by glancing at your code.

  • edited March 2014

    Thanks PhiLho :) I have solved some of my problem and format the code as you have suggested. [edited above] Still I am not able to resolve the winning conition ~X(

    Here I am trying to check the winning condition if the distance between the axis of the two pieces of cube is less than by some amount (which is (number cubes in row or col -1)*size of the cube ) and simultaneously checking the orientation of the cube.

    I am pretty sure about the problem is in the checking the correct orientation of the piece. And Yes you have pointed out correctly.

Sign In or Register to comment.