int main(void) { char board[BOARD_SIZE][BOARD_SIZE]; init_board(board); printf(WELCOME_TO_DRAUGHTS); printf(ENTER_SETTINGS); char *command = input2str(stdin); int win_pos = 0; while (strcmp(command, "quit") != 0){ if (strcmp(command, "start") == 0){ if (is_valid_board(board)) break; else{ printf(WROND_BOARD_INITIALIZATION); free(command); command = input2str(stdin); continue; } } exc(command,board); free(command); command = input2str(stdin); } if (strcmp(command, "start") == 0){ // initially we printed the board at the start of the game, commented out in order to match the running examples. //if (user_color == WHITE) print_board(board); while (1){ if (user_color == WHITE){ int ret_val = user_turn(board, WHITE); if (ret_val == QUIT) break; if (ret_val == WIN_POS){ printf(BLACK_WIN); win_pos = 1; break; } if (computer_turn(board, BLACK) == WIN_POS){ printf(WHITE_WIN); win_pos = 1; break; } } else{ if (computer_turn(board, WHITE) == WIN_POS){ printf(BLACK_WIN); win_pos = 1; break; } int ret_val = user_turn(board, BLACK); if (ret_val == QUIT) break; if (ret_val == WIN_POS){ printf(WHITE_WIN); win_pos = 1; break; } } } } if (win_pos == 1){ free(command); command = input2str(stdin); } free(command); }
int print_header(int option) { #if HTML FILE *tmp_out_file; #endif #ifdef HAVE_CURSES unsigned int row=0; unsigned int col=0; unsigned int width=0; #endif switch (output_method) { #ifdef HAVE_CURSES case CURSES_OUT: erase(); mvwprintw(stdscr,1,2,"bwm-ng v" VERSION" (probing every %2.3fs), press 'h' for help",(float)delay/1000); mvwprintw(stdscr,2,2,"input: %s type: %s",input2str(),output_type2str()); wprintw(stdscr,show_all_if2str()); mvwprintw(stdscr,3,2,"%c iface Rx Tx Total",(char)IDLE_CHARS[option]); /* go to next char for next run */ option++; if (option>3) option=0; mvwprintw(stdscr,4,2,"=============================================================================="); break; case CURSES2_OUT: erase(); if (cols<48 || rows<45) mvwprintw(stdscr,1,2,"window size too small.\n it has to be at least 48x45."); else { width=(cols-3-16-4)/3; mvwprintw(stdscr,1,2,"+---{ bwm-ng v" VERSION" }"); for (col=17+sizeof(VERSION);cols >= 0 && col<(unsigned int)(32+cols-48);col++) mvwprintw(stdscr,1,col,"-"); mvwprintw(stdscr,1,32+cols-48,"+- -- - -- -->"); mvwprintw(stdscr,2,2,"|"), mvwprintw(stdscr,2,32+cols-48,"|------."); for (col=0;col<width-2;col++) { mvwaddch(stdscr,2,col+6,ACS_HLINE); attron(COLOR_PAIR(1));mvwprintw(stdscr,35,col+6," ");attroff(COLOR_PAIR(1)); mvwaddch(stdscr,2,2*width+6+col,ACS_HLINE); attron(COLOR_PAIR(2));mvwprintw(stdscr,35,2*width+6+col," ");attroff(COLOR_PAIR(2)); }; for (row=3;row<=36;row++) { mvwprintw(stdscr,row,2,"|"); mvwprintw(stdscr,row,32+cols-48,"|"); } mvwprintw(stdscr,34,33+cols-48,"<"); mvwprintw(stdscr,35,33+cols-48,"------'"); mvwprintw(stdscr,36,((width-8)/2)+5,"%c%c%cRx%c%c%c", IDLE_CHARS2[option+2], IDLE_CHARS2[option+1], IDLE_CHARS2[option], IDLE_CHARS2[9-option], IDLE_CHARS2[8-option], IDLE_CHARS2[7-option]); mvwprintw(stdscr,36,((width-8)/2)+5+2*width,"%c%c%cTx%c%c%c", IDLE_CHARS2[9-option], IDLE_CHARS2[8-option], IDLE_CHARS2[7-option], IDLE_CHARS2[option+2], IDLE_CHARS2[option+1], IDLE_CHARS2[option]); mvwprintw(stdscr,37,2,"+"); mvwprintw(stdscr,37,32+cols-48,"+"); for (col=3;cols >= 0 && col<(unsigned int)(32+cols-48);col++) mvwprintw(stdscr,37,col,"-"); mvwprintw(stdscr,38,2,"`+--> %c probing every: %2.3fs",(char)IDLE_CHARS[option],(float)delay/1000); mvwprintw(stdscr,39,2," +-----> interface: wait... "); mvwprintw(stdscr,40,2," +--------> type: %s",output_type2str()); mvwprintw(stdscr,41,2," `-----------> input: %s",input2str()); scale=max_rt/32; /* print scale */ if (max_rt>=1024) { for (row=0;row<=31;row++) mvwprintw(stdscr,row+3,34+cols-48,"%2.2fM|",(float)(max_rt-row*scale)/1024); } else { for (row=0;row<=31;row++) mvwprintw(stdscr,row+3,34+cols-48,"%4uk|",max_rt-row*scale); } }; /* go to next char for next run */ option++; if (option>3) option=0; break; #endif #ifdef HTML case HTML_OUT: tmp_out_file=out_file==NULL ? stdout : out_file; if (html_header) { fprintf(tmp_out_file,"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n<html>\n<head>\n"); fprintf(tmp_out_file,"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n"); fprintf(tmp_out_file,"<META HTTP-EQUIV=Refresh CONTENT=\"%i\">\n",html_refresh); fprintf(tmp_out_file,"<link rel=\"stylesheet\" href=\"bwm-ng.css\" type=\"text/css\" media=\"screen,projection,print\">\n"); fprintf(tmp_out_file,"<title>bwm-ng stats</title>\n</head>\n<body>\n"); } fprintf(tmp_out_file,"<div class=\"bwm-ng-header\">bwm-ng bwm-ng v" VERSION " (refresh %is); input: ",html_refresh); fprintf(tmp_out_file,"%s", input2str()); fprintf(tmp_out_file,"%s", show_all_if2str()); fprintf(tmp_out_file,"</div><table class=\"bwm-ng-output\">"); fprintf(tmp_out_file,"<tr class=\"bwm-ng-head\"><td class=\"bwm-ng-name\">Interface</td><td>Rx</td><td>Tx</td><td>Total</td></tr>"); break; #endif case PLAIN_OUT_ONCE: case PLAIN_OUT: if (output_method==PLAIN_OUT && ansi_output) printf("\033[1;2H"); printf("bwm-ng v" VERSION " (delay %2.3fs); ",(float)delay/1000); if (output_method==PLAIN_OUT) printf("press 'ctrl-c' to end this%s",(ansi_output ? "\033[2;2H" : "")); else printf("input: "); printf("%s", input2str()); printf("%s\n",show_all_if2str()); if (output_method==PLAIN_OUT) { if (ansi_output) printf("\033[3;2H"); printf("%c",(char)IDLE_CHARS[option]); } else printf(" "); printf(" iface Rx Tx Total\n"); if (output_method==PLAIN_OUT && ansi_output) printf("\033[4;2H"); printf("==============================================================================\n"); /* go to next char for next run */ option++; if (option>3) option=0; break; } return option; }
// manages the users turn, game state user input loop int user_turn(char board[BOARD_SIZE][BOARD_SIZE], COLOR color){ get_all_moves(board, color); if (moves_head == NULL) return WIN_POS; char *word1; char *command = NULL; Move* new_move = NULL; int ret_val; while (1){ printf(ENTER_YOUR_MOVE); if (new_move != NULL) clear_old_moves(new_move); new_move = malloc(sizeof(Move)); new_move->dest = malloc(sizeof(Pos) * 2 * BOARD_SIZE); new_move->next = NULL; if (command != NULL) free(command); command = input2str(stdin); word1 = strtok(command, " "); if (strcmp(word1, "quit") == 0){ ret_val = QUIT; break; } else if (strcmp(word1, "get_moves") == 0){ print_moves(moves_head); continue; } else if (strcmp(word1, "move") == 0){ char * piece_coor1 = strtok(NULL, " <,>"); char * piece_coor2 = strtok(NULL, " <,>"); new_move->piece.col = piece_coor1[0] - 'a'; new_move->piece.row = atoi(piece_coor2) - 1; if (!is_valid_pos(new_move->piece)){ printf(WRONG_POSITION); continue; } int i = 0; char * dest_coor1 = strtok(NULL, " <,>to"); char * dest_coor2 = strtok(NULL, " <,>to"); while (dest_coor1 != NULL){ new_move->dest[i].col = dest_coor1[0] - 'a'; new_move->dest[i].row = atoi(dest_coor2) - 1; if (!is_valid_pos(new_move->dest[i])){ i = -1; break; } i++; dest_coor1 = strtok(NULL, " <,>[]"); if (dest_coor1 != NULL) dest_coor2 = strtok(NULL, " <,>[]"); } if (i == -1){ printf(WRONG_POSITION); continue; } if (!is_valid_piece(board, new_move, color)){ printf(NO_DISC); continue; } new_move->dest = realloc(new_move->dest, sizeof(Pos) * i); new_move->captures = i; Move * move2do = is_valid_move(moves_head, new_move); if (move2do == NULL){ printf(ILLEGAL_MOVE); continue; } else{ exc_move(board, move2do); print_board(board); ret_val = GAME_ON; break; } } else printf(ILLEGAL_COMMAND); } free(command); clear_old_moves(new_move); clear_old_moves(moves_head); return ret_val; }