Exemplo n.º 1
0
void RubiksCube::UpdatePositions() {
    if(m_moving) {
        struct timeval m_tv;
        gettimeofday(&m_tv, NULL);
        int new_time = m_tv.tv_usec;
        // time ranges from 0 -> 1000000 us
        int diff;
        if(new_time < m_start_time) {
            diff = (1000000 - m_start_time) + new_time;
        } else {
            diff = new_time - m_start_time;
        }
        int turns = floor(diff*90/(1000000*m_duration));
        if(turns > 0) {
            
            if((m_angle + turns) >= 90*m_times) {
                turns = (90*m_times) - m_angle;
            }
            
            RotateSide(m_side, m_dir*turns);
            m_angle+= turns;
            // update start time
            // include diff so there is no accumilated error
            m_start_time = m_start_time + turns*(1000000*m_duration/90);
            if(m_start_time > 1000000) {
                m_start_time = 1000000 - m_start_time;
            }
            
            if(m_angle >= 90*m_times) {
                //printf("finished move on m_angle = %d\n", m_angle);
                m_moving = false;               
            }
        }
    }
}
Exemplo n.º 2
0
//---------------------------------------------------------------------------------
// Interprets the final position of a twist and puts the data back onto the cube
// Also sends twist data to thecubex solver, and manages scrambling/solving.
//---------------------------------------------------------------------------------
void RubiksCube::FinishTwist()
{
	// Count up rotations (quarter-twists)
	int rotation=Twist.rotation;
	rotation %= 360;
	if(rotation<0)
		rotation+=360;
	int rotations=rotation/90;//find the number of quarter-turns (moves)
	if(rotation % 90 > 45) rotations++;
	
	if(Twist.position==0 || Twist.position==3)
	{
		Twist.left.side=RotateSide(Twist.left.side, rotations);
		for(int i=0;i<rotations;i++)
		{
			RubikLine tmpline=Twist.left.line[3];
			Twist.left.line[3]=Twist.left.line[2];
			Twist.left.line[2]=Twist.left.line[1];
			Twist.left.line[1]=Twist.left.line[0];
			Twist.left.line[0]=tmpline;
		}
	}
	if(Twist.position==1 || Twist.position==3)
	{
		
		for(int i=0;i<rotations;i++)
		{
			RubikLine tmpline=Twist.middle.line[3];
			Twist.middle.line[3]=Twist.middle.line[2];
			Twist.middle.line[2]=Twist.middle.line[1];
			Twist.middle.line[1]=Twist.middle.line[0];
			Twist.middle.line[0]=tmpline;
		}
	}
	if(Twist.position==2 || Twist.position==3)
	{
		Twist.right.side=RotateSide(Twist.right.side, 4-rotations);
		for(int i=0;i<rotations;i++)
		{
			RubikLine tmpline=Twist.right.line[3];
			Twist.right.line[3]=Twist.right.line[2];
			Twist.right.line[2]=Twist.right.line[1];
			Twist.right.line[1]=Twist.right.line[0];
			Twist.right.line[0]=tmpline;
		}
	}
	
	Side[Twist.axis]=Twist.left.side;
	Side[Twist.axis + 3]=Twist.right.side;
	
	int one=((Twist.axis+1)%3)+3;
	int two=((Twist.axis+2)%3)+3;
	int three=(Twist.axis+1)%3;
	int four=(Twist.axis+2)%3;
	for(int i=0;i<3;i++)
	{
		Side[one].tile[2][2-i].color=Twist.left.line[0].tile[i].color;
		Side[one].tile[1][2-i].color=Twist.middle.line[0].tile[i].color;
		Side[one].tile[0][2-i].color=Twist.right.line[0].tile[i].color;
		
		Side[two].tile[i][2].color=Twist.left.line[1].tile[i].color;
		Side[two].tile[i][1].color=Twist.middle.line[1].tile[i].color;
		Side[two].tile[i][0].color=Twist.right.line[1].tile[i].color;
		
		Side[three].tile[0][i].color=Twist.left.line[2].tile[i].color;
		Side[three].tile[1][i].color=Twist.middle.line[2].tile[i].color;
		Side[three].tile[2][i].color=Twist.right.line[2].tile[i].color;
		
		Side[four].tile[i][2].color=Twist.left.line[3].tile[i].color;
		Side[four].tile[i][1].color=Twist.middle.line[3].tile[i].color;
		Side[four].tile[i][0].color=Twist.right.line[3].tile[i].color;
	}
	
	// Send the twist to cubex.
	switch(Twist.axis)
	{
	case 0:
		switch(Twist.position)
		{
		case 0:
			for(int i=0; i<rotations; i++)
			{
				solver.LD();
				printf("LD\n");
			}
			break;
		case 1:
			for(int i=0; i<rotations; i++)
			{
				solver.MD();
				printf("MD\n");
			}
			break;
		case 2:
			for(int i=0; i<rotations; i++)
			{
				solver.RD();
				printf("RD\n");
			}
			break;
		case 3:
			for(int i=0; i<rotations; i++)
			{
				solver.CD();
				printf("CD\n");
			}
			break;
		}
	break;
	case 1:
		switch(Twist.position)
		{
		case 0:
			for(int i=0; i<rotations; i++)
			{
				solver.DR();
				printf("DR\n");
			}
			break;
		case 1:
			for(int i=0; i<rotations; i++)
			{
				solver.MR();
				printf("MR\n");
			}
			break;
		case 2:
			for(int i=0; i<rotations; i++)
			{
				solver.UR();
				printf("UR\n");
			}
			break;
		case 3:
			for(int i=0; i<rotations; i++)
			{
				solver.CR();
				printf("CR\n");
			}
			break;
		}
	break;
	case 2:
		switch(Twist.position)
		{
		case 0:
			for(int i=0; i<rotations; i++)
			{
				solver.BA();
				printf("BA\n");
			}
			break;
		case 1:
			for(int i=0; i<rotations; i++)
			{
				solver.MA();
				printf("MA\n");
			}
			break;
		case 2:
			for(int i=0; i<rotations; i++)
			{
				solver.FA();
				printf("FA\n");
			}
			break;
		case 3:
			for(int i=0; i<rotations; i++)
			{
				solver.CA();
				printf("CA\n");
			}
			break;
		}
	break;
	}

	Twist.rotation=0;
	AutoTwisting=false;
	
	// handle scrambling
	if(Scrambling)
	{
		scramblecount--;
		if(scramblecount==0)
			Scrambling=false;
	}
	// or solving
	if(Solving)
	{
		solvecount--;
		if(solvecount==0)
			Solving = false;
	}
}