Beispiel #1
0
void drawgames(int pos) {
    int selected_game = 0;

    if (games[pos].status != GAME_FREE) {
        selected_game = pos;
    }
    else
        selected_game = 0;

    dispmultiframe();
    xt_par0(XT_CH_NORMAL);

    int c = 0;
    int useroffsettotal = 0;
    int ioffset = 0;

    while (2 * ioffset + useroffsettotal + (games[numgames - c].status == GAME_FREE ? 0 : games[numgames - c].slots_filled) < ROWS - 20){
        useroffsettotal += games[numgames - c].status == GAME_FREE ? 0 : games[numgames - c].slots_filled;
        c++;
    }

    int i = selected_game - c;
    int ii;

    useroffsettotal = 1;
    int useroffset = 1;
    if (i < 0) i = 0;


    for (; i < MAX_GAMES; i++) {

        if (games[i].status == GAME_FREE)
            continue;

        printgame(5 + 2 * ioffset + useroffsettotal, i == selected_game, games[i]);
        for (ii = 0; ii < games[i].slots_filled; ii++) {
            if (games[i].players[ii] == EMPTY_SLOT)
                continue;
            SETPOS(6 + 2 * ioffset + useroffsettotal, 8);
            printf("%s", users[games[i].players[ii]].username);
            useroffset++;
            useroffsettotal++;
        }
        useroffset = 1;
        i++;
        ioffset++;

    }


    SETPOS(2, 3);
    printf("Open Lobbies (Up/Down to select, %sJ%soin, %sC%sreate, %sQ%suit, %sH%selp)", XT_CH_UNDERLINE, XT_CH_NORMAL, XT_CH_UNDERLINE, XT_CH_NORMAL, XT_CH_UNDERLINE, XT_CH_NORMAL, XT_CH_UNDERLINE, XT_CH_NORMAL);

    xt_par0(XT_CH_BOLD);
    SETPOS(3, 3);
    printf("terminvaders MuLtIpLaYeR Lobby");

    xt_par0(XT_CH_DEFAULT);

}
int main(int argc, char** argv)
{
	long unsigned start,end,duration;
	printwelcome_game();
	char read_buf;
	while(1<2)
	{
		initialization_game();
		start=time();
		printgame();
		char wrong[]="Your answer is wrong!\n";
		char message1[] = "Good!\n";
		char message2[] = "Not bad!\n";
		char message3[] = "Bad!\n";
		while(1)
		{
			read(STDIN_FILENO,&read_buf,1);
			if(read_buf==hit)
			{
				break;
			}
			else
			{
				write(STDOUT_FILENO,wrong,sizeof(wrong));
			}			
		}
		end=time();
		duration=end-start;
		duration=duration*10;
		printf("\nReaction time is: %lu ms\n",duration);
		if(duration<800)
		{
			write(STDOUT_FILENO,message1,sizeof(message1));
		}
		else if(duration < 1600)
		{
			write(STDOUT_FILENO,message2,sizeof(message2));
		}
		else
		{
			write(STDOUT_FILENO,message3,sizeof(message3));
		}					
	}
	return 0;
}		
Beispiel #3
0
int main(int argc,char *argv[]){
    int i=0,j=0,turn=0,diff=3,swap=0,k,h=1,bload=0;
    char human[]="white",computer[]="black";
    char ***b;
    char n=11;
    
    for(i=1;i<argc;i++){ /*Will make changes according to the commands given when the program was called. */
        if(!(strcmp(argv[i],"-s"))) /*Enable swap rule. */
            swap=1;
        else if(!(strcmp(argv[i],"-b"))){ /* Player plays as black. */
            strcpy(human,"black");
            strcpy(computer,"white");
        }
        else if(!(strcmp(argv[i],"-n"))){ /*Changes the size if input is appropriate. */
            /*Checks if there is something after "-n" in order not to go over the limits in case of wrong input.Also if the next word is a number. */
            if((i+1>=argc)||((*argv[i+1]<'0')||(*argv[i+1]>'9'))){
		fprintf(stderr,"Not given a new size after \"-n\".\nShutdown.\n");
		return 0;
	    }
	    if((atoi(argv[i+1])>=4)&&(atoi(argv[i+1])<=26)) /*If the given size has an acceptable value. */
                n=atoi(argv[++i]);
            else {
                fprintf(stderr,"Unacceptable size input(should be 4=<size=<26),shutdown.\n");
                return 0;
            }
        }
        else if(!(strcmp(argv[i],"-d"))){ /*Change the difficulty,if given level is appropriate */
	    if((i+1>=argc)||((*argv[i+1]<'0')||(*argv[i+1]>'9'))){ /*Checks if there is something after "-d" in order not to go over the limits in case of wrong input. */
		fprintf(stderr,"Not given a new difficulty level after \"-d\".\nShutdown.\n");
		return 0;
	    }
            if(atoi(argv[i+1])>0) /*If the new difficulty is over 1. */
                diff=atoi(argv[++i]);
            else {
                fprintf(stderr,"Unacceptable difficulty level input(should be >0),shutdown.\n");
                return 0;
            }
        }
        else /*If unknown parameter */
            fprintf(stderr,"Unacceptable parameters given.Program keeps running.Feel free to \"quit\" and start again.\n");
    }
    
    if(!(addnew(&b,n))) /*Beginning the game */
        return -1; 

    printf("Difficulty:%d\n",diff);
    printf("Size:%d\n",n);
    addnext(&b,++turn,n); /*Increasing turn and adding the turn's board. */
     /*Checks if the first digit of the human string is 'w'(means that human="white")and giving the appropriate value to h. */
    if (*human=='w')h=0;
    else h=1;
    
    /*Doing it so that for turn=0 the board is full of ' '.For turn=1 board is initially the same but changes before the next one is added. */
    
    /*Loop ends when a player gives a "quit" command or the functions encounter an error. */
    while(1){
        if((turn+h)%2){ /*If its the player's/computer's turn print the appropriate message. */
            printf("%s player(human) plays now.\n",human);    
        }
        else{
            printf("%s player(computer) plays now.\n",computer);            
        }
        printf("Turn:%d\n",turn);
        printgame(n,b,turn); /*Print the game status after each move. */
        
        do{                           /*Calls stringread until it returns 2(play/cont) */
            if(done(n,b,human,computer,turn-1,h)){
                turn=-1;
            }
            
            
            printf(">");
            k=stringread(&b,&turn,&diff,&n,human,computer,&swap,&h,&bload);
            if (k==0){                       /*If it returns 0 the program has encountered an error and needs to be terminated. */
                fprintf(stderr,"Fatal error.\n");
                return -1;
            }else if(k==-1)
                break;
            
        }while(k !=2);
        if(k==-1) /*In that case player quit. */
            break;
        
        if(!((turn+h)%2)){ /*If its the opponents turn calls the AI function,plays the move and prints the appropriate message */
            
            if(AI(n,diff,turn,b,NULL,&i,&j,1,*computer,*human,-1200,swap)){ /*1 is returned if*/
                swapit(b,n,turn);
            }
            else{
                b[turn][i][j]=*computer;
                printf("Move played: %c%d\n",'A'+j,i+1);
            }
        }
        
        if(!(addnext(&b,++turn,n)))
            return -1;
    }
    for (i=0;i<turn;i++){      /*Free what needs to be freed. */      
            for(j=0;j<n;j++)
                free(b[i][j]);
            free (b[i]);
    }
    free(b);
    printf("Application shut down.\n");
    return 1;
        
}