/* * restores balance at n after insertion, assuming that the right subtree of n is too high */ void balanceright (avltree ** n, short adjust) { short dif; dif = difference ((*n)->right); if (dif == 0) { rotateleft (n); /* both subtrees of right child of n have same height */ ((*n)->height) -= adjust; /* 'decrease' height of current node */ ((*n)->left->height) += adjust; /* 'increase' height of left subtree */ } else { if (dif < 0) { rotateleft (n); /* right subtree of right child of n is higher */ (*n)->left->height -= 2; } else { /* left subtree of right child of n is higher */ rotateright (&(*n)->right); /* pointer to n->right */ rotateleft (n); ++((*n)->height); /* increase height of current node */ (*n)->left->height -= 2; --((*n)->right->height); /* decrease height of right subtree */ } } }
static void insert_recolour(struct opr_rbtree *head, struct opr_rbtree_node *node) { struct opr_rbtree_node *parent, *gramps; while ((parent = node->parent) && parent->red) { gramps = parent->parent; if (parent == gramps->left) { struct opr_rbtree_node *uncle = gramps->right; if (uncle && uncle->red) { uncle->red = 0; parent->red = 0; gramps->red = 1; node = gramps; continue; } if (parent->right == node) { rotateleft(head, parent); swapnode(&parent, &node); } parent->red = 0; gramps->red = 1; rotateright(head, gramps); } else { struct opr_rbtree_node *uncle = gramps->left; if (uncle && uncle->red) { uncle->red = 0; parent->red = 0; gramps->red = 1; node = gramps; continue; } if (parent->left == node) { rotateright(head, parent); swapnode(&parent, &node); } parent->red = 0; gramps->red = 1; rotateleft(head, gramps); } } head->root->red = 0; }
tree_t *balance(tree_t *p) { fixheight(p); if (bfactor(p) == 2) { if (bfactor(p->right) < 0) p->right = rotateright(p->right); return rotateleft(p); } if (bfactor(p) == -2) { if (bfactor(p->left) > 0) p->left = rotateleft(p->left); return rotateright(p); } return p; }
node *RR(node *T) { T=rotateleft(T); printf("after the left rotation : \n"); prettyPrint(T,0); return(T); }
void rotate90left() { start_request = 1; TransmitComm(update); //stå still och kontrollera väggar runt roboten for(long i = 0; i < 80000; i ++) { stopp(); } storedValues[6] = 0; while(turnisDone == 0) { start_request = 1; TransmitSensor(turn); // Start gyro hos sensormodul if (storedValues[6] != 1) { rotateleft(); } else { start_request = 1; TransmitSensor(turnstop); // kolla med sensormodul om vi roterat 90 stopp(); turnisDone = 1; } } // uppdatera riktning på roboten if(mydirection == 4) { mydirection = 1; } else { mydirection += 1; } turnisDone = 0; storedValues[8] = mydirection; //stå still och kontrollera väggar runt roboten for(long i = 0; i < 80000; i ++) { stopp(); } start_request = 1; TransmitSensor(0); TransmitComm(update); posdistance = 0; n = 0; // Tållåt kontrol efter ö controlisland = true; }
unsigned int murmurhash3_32(int key,int size) //MurmurHash 3: key=input value { unsigned int c1=0xcc9e2d51,c2=0x1b873593,r1=15,r2=13,m=5,n=0xe6546b64,hash,k; //these values are constant for 32bit hashing hash=100; //100 used as a random seed value, can use any other also k=key; k*=c1; k=rotateleft(k,r1); k*=c2; hash^=k; hash=rotateleft(hash,r2); hash=hash*m+n; hash^=hash>>16; //HASH FINALIZER STARTS hash*=0x85ebca6b; hash^=hash>>13; hash*=0xc2b2ae35; hash^=hash>>16; //HASH FINALIZER ENDS hash=hash%size; return hash; }
node * LR(node *T) { T->left=rotateleft(T->left); printf("after the left rotation : \n"); prettyPrint(T,0); T=rotateright(T); printf("after the right rotation : \n"); prettyPrint(T,0); return(T); }
void * keylistener(void *arg){ noecho(); int key; while(!over){ key = getch(); if(key == 'q' || key =='Q'){ over = true; } switch(key){ case KEY_UP:rotateleft();break; case KEY_LEFT:moveleft();break; case KEY_RIGHT:moveright();break; case KEY_DOWN:movedown();break; } printpanel(PANELSTARTX,PANELSTARTY); } pthread_exit((void *) 0); }
node * RR(node *T) { T=rotateleft(T); return(T); }
static void remove_recolour(struct opr_rbtree *head, struct opr_rbtree_node *parent, struct opr_rbtree_node *node) { struct opr_rbtree_node *other; while ((node == NULL || !node->red) && node != head->root) { if (parent->left == node) { other = parent->right; if (other->red) { other->red = 0; parent->red = 1; rotateleft(head, parent); other = parent->right; } if ((other->left == NULL || !other->left->red) && (other->right == NULL || !other->right->red)) { other->red = 1; node = parent; parent = node->parent; } else { if ((other->right == NULL) || (!other->right->red)) { other->left->red = 0; other->red = 1; rotateright(head, other); other = parent->right; } other->red = parent->red; parent->red = 0; other->right->red = 0; rotateleft(head, parent); node = head->root; break; } } else { other = parent->left; if (other->red) { other->red = 0; parent->red = 1; rotateright(head, parent); other = parent->left; } if ((other->left == NULL || !other->left->red) && (other->right == NULL || !other->right->red)) { other->red = 1; node = parent; parent = node->parent; } else { if (other->left == NULL || !other->left->red) { other->right->red = 0; other->red = 1; rotateleft(head, other); other = parent->left; } other->red = parent->red; parent->red = 0; other->left->red = 0; rotateright(head, parent); node = head->root; break; } } } if (node) node->red = 0; }
AVLTree_Node * RL(AVLTree_Node *T) { T->right=rotateright(T->right); T=rotateleft(T); return(T); }
AVLTree_Node * LR(AVLTree_Node *T) { T->left=rotateleft(T->left); T=rotateright(T); return(T); }
void f_sha1(void) { unsigned long int h0,h1,h2,h3,h4,a,b,c,d,e,f,k,temp; int i, j, m; h0 = 0x67452301; h1 = 0xEFCDAB89; h2 = 0x98BADCFE; h3 = 0x10325476; h4 = 0xC3D2E1F0; unsigned char *str; str = (unsigned char *)malloc(SVALUE_STRLEN(sp)+100); strcpy((char *)str, (const char *)sp->u.string); int current_length = strlen((const char *)str); int original_length = current_length; str[current_length] = 0x80; str[current_length + 1] = '\0'; char ic = str[current_length]; current_length++; int ib = current_length % 64; if (ib < 56) ib = 56 - ib; else ib = 120 - ib; for (i=0; i<ib; i++) { str[current_length] = 0x00; current_length++; } str[current_length + 1] = '\0'; for (i=0; i<6; i++) { str[current_length] = 0x0; current_length++; } str[current_length] = (original_length * 8) / 0x100; current_length++; str[current_length] = (original_length * 8) % 0x100; current_length++; str[current_length + i] = '\0'; int number_of_chunks = current_length / 64; unsigned long int word[80]; for (i=0; i<number_of_chunks; i++) { for (j=0; j<16; j++) { word[j] = str[i*64+j*4+0] * 0x1000000 + str[i*64+j*4+1] * 0x10000 + str[i*64+j*4+2] * 0x100 + str[i*64+j*4+3]; } for (j=16; j<80; j++) { word[j] = rotateleft((word[j-3] ^ word[j-8] ^ word[j-14] ^ word[j-16]),1); } a = h0; b = h1; c = h2; d = h3; e = h4; for (m=0; m<80; m++) { if (m <= 19) { f = (b & c) | ((~b) & d); k = 0x5A827999; } else if (m <= 39) { f = b ^ c ^ d; k = 0x6ED9EBA1; } else if (m <= 59) { f = (b & c) | (b & d) | (c & d); k = 0x8F1BBCDC; } else { f = b ^ c ^ d; k = 0xCA62C1D6; } temp = (rotateleft(a, 5) + f + e + k + word[m]) & 0xFFFFFFFF; e = d; d = c; c = rotateleft(b, 30); b = a; a = temp; } h0 = h0 + a; h1 = h1 + b; h2 = h2 + c; h3 = h3 + d; h4 = h4 + e; } sprintf((char *)str, "%x%x%x%x%x", h0, h1, h2, h3, h4); pop_stack(); push_malloced_string(string_copy((char *)str, "f_sha1")); free(str); }
int twosides (float a, float b, float c, float A, float B, float C) { int rotation = 0; /* always rotate LEFT, and unrotate RIGHT */ float tmp; int solutions = 0; if (A) rotation = 0; if (B) rotation = 1; if (C) rotation = 2; rotateleft(&a, &b, &c, &A, &B, &C, rotation); if (a && b) { /* a/sin(A)=b/sin(B) */ B = (b*sin(A))/a; if (B > 1 || B < -1) goto FAILURE; B = asin(B); tmp = B; if (deg2rad(180)-B+A < deg2rad(180)) { /* two solutions */ solutions++; B = deg2rad(180) - B; C = deg2rad(180-rad2deg(A)-rad2deg(B)); c = a*sin(C)/sin(A); rotateright(&a, &b, &c, &A, &B, &C, rotation); solution(a,b,c,A,B,C,solutions); rotateleft(&a, &b, &c, &A, &B, &C, rotation); solutions++; } B = tmp; C = deg2rad(180-rad2deg(A)-rad2deg(B)); c = a*sin(C)/sin(A); rotateright(&a, &b, &c, &A, &B, &C, rotation); solution(a,b,c,A,B,C,solutions); rotateleft(&a, &b, &c, &A, &B, &C, rotation); return 0; } if (a && c) { /* a/sin(A)=c/sin(C) */ C = (c*sin(A))/a; if (C > 1 || C < -1) goto FAILURE; C = asin(C); tmp = C; if (deg2rad(180)-C+A < deg2rad(180)) { /* two solutions*/ solutions++; C = deg2rad(180) - C; B = deg2rad(180-rad2deg(A)-rad2deg(C)); b=a*sin(B)/sin(A); rotateright(&a, &b, &c, &A, &B, &C, rotation); solution(a,b,c,A,B,C,solutions); rotateleft(&a, &b, &c, &A, &B, &C, rotation); solutions++; } C = tmp; B = deg2rad(180-rad2deg(A)-rad2deg(C)); b = a*sin(B)/sin(A); rotateright(&a, &b, &c, &A, &B, &C, rotation); solution(a,b,c,A,B,C,solutions); rotateleft(&a, &b, &c, &A, &B, &C, rotation); return 0; } if (b && c) { /* a^2 = b^2+c^2-2bc*cos(A) */ a = sqrt(pow(b,2)+pow(c,2)-2*b*c*cos(A)); B = (pow(a,2) + pow(c,2) - pow(b,2)) / (2*a*c); if (B > 1 || B < -1) goto FAILURE; B = acos(B); C = deg2rad(180) - A - B; rotateright(&a, &b, &c, &A, &B, &C, rotation); solution(a,b,c,A,B,C,solutions); rotateleft(&a, &b, &c, &A, &B, &C, rotation); return 0; } FAILURE: printf("Impossible triangle!\n"); return 1; }
node * LR(node *T) { T->left=rotateleft(T->left); T=rotateright(T); return(T); }
AVLTree_Node * RR(AVLTree_Node *T) { T=rotateleft(T); return(T); }
node * RL(node *T) { T->right=rotateright(T->right); T=rotateleft(T); return(T); }
int main(int argc,char** argv) { SDL_Init(SDL_INIT_EVERYTHING); //init the SDL SDL_Surface* screen=SDL_SetVideoMode(BLOCK_SIZE*WIDTH+BLOCK_SIZE*10,BLOCK_SIZE*HEIGHT,32,SDL_SWSURFACE); //and our screen TTF_Init(); //the TTF as well, cus we want to write out stuff TTF_Font* font=TTF_OpenFont("air.ttf",12); Uint32 start; //the start time, to limit FPS bool running=true; //is the program still running? SDL_Event event; //What event has happened? srand(time(0)); //seed the random number generator int db=0; //how many blocks do we have (init 0)? int elements[8][4][4]; //we store all of the blocks in this 3D array int table[HEIGHT][WIDTH]; //This is our whole game-table, all of the blocks, which already put down is stored here int currentblock[4][4]; //our current falling block fillelement(elements,db); //load the blocks SDL_Surface* blocks=SDL_LoadBMP("blocks.bmp"); //load the image, which stores, the part of the images SDL_SetColorKey(blocks,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255)); //The purple color should be invisible int blockx; //the init position of the falling block int blocky; bool h; //if it's the 1. or 2. block, we need 4x4, else 3x3 Uint32 lastmove=SDL_GetTicks(); //how often should the block come down one Uint32 lastkey=SDL_GetTicks(); //how often should react the block, when the key is pressed (without release) bool keys[2]={0,0}; //if left arrow pressed, than keys[0]=1 if right arrow is pressed keys[1]=1 int points; int normalspeed; //how quick the tetris blocks fall (in this case once every 0.5 seconds) int speed=500; //the speed, the game currently running (the speed increase, when you press the down arrow) const int FPS=30; //how many FPS the game will run (this effect minimally, how quick the blocks fall) int nextblock; //what is the next block? int deletedlines; //how much lines we already deleted int movingspeed=400; //if we hold down the left/right arrow, how often (150ms) we want to move the block int quickmovingspeed=30; bool mousepointing=false; //do we pointing to the toplist text? int tmpx,tmpy; //the location of the mouse cursor //we initialize the game (set every parameter to default) initGame(&blockx,&blocky,&h,elements,table,currentblock,&normalspeed,&points,&deletedlines,&nextblock,&speed,db); int moved=0; while(running) //while the game running { start=SDL_GetTicks(); //get the current time (for FPS limitation) while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_KEYDOWN: switch(event.key.keysym.sym) { case SDLK_ESCAPE: //if escape is pressed, escape running=false; break; case SDLK_UP: //if up arrow is pressed rotateleft(currentblock,h); //rotate the block if(checkCollision(currentblock,blockx,blocky,table)) //and check if there is a collision for(int i=0;i<3;i++) rotateleft(currentblock,h); //if there was a collision, rotate back (rotate 4 times, is like if you haven't done anything) break; case SDLK_LEFT: keys[0]=1; break; case SDLK_RIGHT: keys[1]=1; break; case SDLK_DOWN: speed=10; //if down key is pressed, speed up a little bit break; } break; case SDL_KEYUP: switch(event.key.keysym.sym) { case SDLK_DOWN: speed=normalspeed; //if you released the down arrow, set back the speed break; case SDLK_LEFT: keys[0]=0; moved=0; break; case SDLK_RIGHT: keys[1]=0; moved=0; break; } break; case SDL_QUIT: running=false; break; case SDL_MOUSEMOTION: //if we moved the mouse tmpx=event.motion.x; //get the coordinates tmpy=event.motion.y; //if we are pointing to the square, which contain the toplist text if(tmpx>BLOCK_SIZE*WIDTH+2 && tmpx<BLOCK_SIZE*WIDTH+80 && tmpy>80+BLOCK_SIZE*5 && tmpy<80+BLOCK_SIZE*5+20) mousepointing=true; //make this boolean true else mousepointing=false; //else false break; case SDL_MOUSEBUTTONDOWN: //if we hit the mousebutton tmpx=event.button.x; tmpy=event.button.y; //on the toplist text if(tmpx>BLOCK_SIZE*WIDTH && tmpx<BLOCK_SIZE*WIDTH+80 && tmpy>80+BLOCK_SIZE*5 && tmpy<80+BLOCK_SIZE*5+20) displayToplist(font); //display it break; } } //LOGIC //if the collision happens, and the block is at the top of the screen, than game is over if(checkCollision(currentblock,blockx,blocky,table) && blocky==0) { if(addToplist(font,points)) //try to add out points to the toplist, if addtoplist returns 1, restart the game initGame(&blockx,&blocky,&h,elements,table,currentblock,&normalspeed,&points,&deletedlines,&nextblock,&speed,db); else running=false; //else we exit } //if we exceeded the time, how often should it go down, than move it down, and set back the time if(SDL_GetTicks()-lastmove>speed) { blocky++; lastmove=SDL_GetTicks(); } //if (left) key was pressed, and last time, when we moved the block is more than 200ms, than move it again if((keys[0] && moved>=2 && SDL_GetTicks()-lastkey>quickmovingspeed) || (keys[0] && moved==0) || (keys[0] && moved==1 && SDL_GetTicks()-lastkey>movingspeed)) { blockx--; //move moved++; lastkey=SDL_GetTicks(); //set back the time if(checkCollision(currentblock,blockx,blocky,table)==1)//if there is a collision blockx++; //move back }else if((keys[1] && moved>=2 && SDL_GetTicks()-lastkey>quickmovingspeed) || (keys[1] && moved==0) || (keys[1] && moved==1 && SDL_GetTicks()-lastkey>movingspeed)) //same with right arrow { blockx++; moved++; lastkey=SDL_GetTicks(); if(checkCollision(currentblock,blockx,blocky,table)==1) blockx--; } //RENDER SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,0,0,0)); //clear the screen for(int i=0;i<24;i++) //render out the table for(int j=0;j<12;j++) { if(!table[i][j]) //if a value=0, than don't do anything, else draw the corresponding block (1 is the first block on the images, 2 is the 2...) continue; else blitSurface(blocks,(table[i][j]-1)*BLOCK_SIZE,0,BLOCK_SIZE,BLOCK_SIZE,screen,j*BLOCK_SIZE,i*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE); } //render the falling block for(int i=0;i<4;i++) for(int j=0;j<4;j++) { if(currentblock[i][j]) //if not "empty", draw the corresponding block blitSurface(blocks,(currentblock[i][j]-1)*BLOCK_SIZE,0,BLOCK_SIZE,BLOCK_SIZE,screen,blockx*BLOCK_SIZE+j*BLOCK_SIZE,blocky*BLOCK_SIZE+i*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE); } //here I check the collision, first I move the object down blocky++; if(checkCollision(currentblock,blockx,blocky,table))//and if there is a collision { blocky--; //move back for(int i=0;i<4;i++) for(int j=0;j<4;j++) { if(currentblock[i][j]!=0) table[blocky+i][blockx+j]=currentblock[i][j]; //and draw the block to the table matrix (except the 0), so we handle it as fallen block, it's not falling anymore } blocky=0; //and generate a new block the same way, as we did in the beginning of the main blockx=4; setCurrentblock(elements,currentblock,nextblock); h=(nextblock>2); nextblock=rand()%(db+1); }else blocky--; //if there was no collision, move the object back (else it will go every 2nd line) //this while loop will go as long, as we have full lines while(1) { int k=checkful(table); if(k==-1) break; deletedlines++; //if we have full lines for(int i=k;i>0;i--) for(int j=0;j<WIDTH;j++) table[i][j]=table[i-1][j]; //delete them, by move everything above it a line down for(int i=0;i<HEIGHT;i++) table[0][i]=0; //and 0 out the most top line (usually this has no effect, except, if there is a block-part in the top row) points+=(550-normalspeed)/10; if((points%50)==0 && normalspeed>0) //if the points are dividable by 50 (50,100,150...), speed up the game { if(normalspeed>50) normalspeed-=50; else if(normalspeed>0) normalspeed-=10; } } //render the menu SDL_Rect rec={BLOCK_SIZE*WIDTH,0,screen->clip_rect.w,screen->clip_rect.h}; //the background of the tetris is black (if you want SDL_FillRect(screen,&rec,SDL_MapRGB(screen->format,50,50,50)); //you can change it to an image) rec.x=BLOCK_SIZE*WIDTH+10; //this is the part next to the tetris (the menu) rec.w=4*BLOCK_SIZE; rec.y=20; rec.h=4*BLOCK_SIZE; SDL_FillRect(screen,&rec,SDL_MapRGB(screen->format,100,100,100)); //fill it with gray for(int i=0;i<4;i++) for(int j=0;j<4;j++) { if(elements[nextblock][i][j]) //if not "empty", draw the corresponding block { blitSurface(blocks,(elements[nextblock][i][j]-1)*BLOCK_SIZE,0,BLOCK_SIZE,BLOCK_SIZE,screen,(BLOCK_SIZE*WIDTH)+j*BLOCK_SIZE+20,20+i*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE); } } //draw all of the menutext (huhh, it's good, that I have this function, else it would be a 4,5 times that amount of line) writeText(font,BLOCK_SIZE*WIDTH,0,"NEXT BLOCK",255,255,255); writeText(font,BLOCK_SIZE*WIDTH,20+BLOCK_SIZE*5,"POINTS: ",255,255,255); writeText(font,BLOCK_SIZE*WIDTH+150,20+BLOCK_SIZE*5,points,255,255,255); writeText(font,BLOCK_SIZE*WIDTH,40+BLOCK_SIZE*5,"CURRENT SPEED:",255,255,255); writeText(font,BLOCK_SIZE*WIDTH+150,40+BLOCK_SIZE*5,normalspeed,255,255,255); writeText(font,BLOCK_SIZE*WIDTH,60+BLOCK_SIZE*5,"DELETED LINES: ",255,255,255); writeText(font,BLOCK_SIZE*WIDTH+150,60+BLOCK_SIZE*5,deletedlines,255,255,255); rec.x=BLOCK_SIZE*WIDTH+2; //the toplist square rec.y=80+BLOCK_SIZE*5; rec.w=80; rec.h=20; if(!mousepointing) //if we are not pointing to the toplist square, fill it with { SDL_FillRect(screen,&rec,SDL_MapRGB(screen->format,0,0,0)); //black }else { SDL_FillRect(screen,&rec,SDL_MapRGB(screen->format,255,0,0)); //else red } writeText(font,BLOCK_SIZE*WIDTH+5,80+BLOCK_SIZE*5,"TOPLIST",255,255,255); //and write the toplist text SDL_Flip(screen); //show evrything o the real screen if(1000.0/FPS>SDL_GetTicks()-start) SDL_Delay(1000.0/FPS-(SDL_GetTicks()-start)); //regulate the FPS } SDL_FreeSurface(blocks); //delete and close everything TTF_CloseFont(font); TTF_Quit(); SDL_Quit(); return 0; }