void setZeroes(vector<vector<int>>& matrix) { if(matrix.size() == 0) return; int row = matrix.size(), col = matrix[0].size(); string str_row(row, '0'), str_col(col, '0'); // extra space: two string for(int i = 0; i < row; ++i) { for(int j = 0; j < col; ++j) { if(matrix[i][j] == 0) { str_row[i] = '1'; str_col[j] = '1'; } } } for(int i = 0; i < row; ++i) { if(str_row[i] == '1') { for(int j = 0; j < col; ++j) matrix[i][j] = 0; } } for(int j = 0; j < col; ++j) { if(str_col[j] == '1') { for(int i = 0; i < row; ++i) matrix[i][j] = 0; } } }
/** saves the current game to specified 'file_name' * returns 0 upon success, 1 otherwise. */ int save_xml(const char *file_name) { char full_path[70] = "load_save/"; char *s = strchr (file_name, '/'); if (s == NULL) strcat(full_path, file_name); else strcpy(full_path, file_name); FILE *f_out; if ( (f_out = fopen(full_path, "w")) == NULL ) return 1; char *next_turn = WHITE_TURN == 1 ? "White" : "Black"; char *game_mode = TWO_PLAYERS_MODE == 1 ? "1" : "2"; char min_depth[] = {MINIMAX_DEPTH + '0', '\0'}; //MINIMAX_DEPTH to string char *difficulty = MINIMAX_DEPTH == BEST_DEPTH_VALUE ? "best" : min_depth; char *user_color = PLAYER_WHITE == 1 ? "White" : "Black"; char row_buff[9]; if ( TWO_PLAYERS_MODE == 1 ) { user_color = ""; difficulty = ""; } fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", f_out); fputs("<game>\n", f_out); fputs("\t<next_turn>", f_out); fputs(next_turn, f_out); fputs("</next_turn>\n", f_out); fputs("\t<game_mode>", f_out); fputs(game_mode, f_out); fputs("</game_mode>\n", f_out); fputs("\t<difficulty>", f_out); fputs(difficulty, f_out); fputs("</difficulty>\n", f_out); fputs("\t<user_color>", f_out); fputs(user_color, f_out); fputs("</user_color>\n", f_out); fputs("\t<board>\n", f_out); fputs("\t\t<row_8>", f_out); fputs(str_row(row_buff, 7), f_out); fputs("</row_8>\n", f_out); fputs("\t\t<row_7>", f_out); fputs(str_row(row_buff, 6), f_out); fputs("</row_7>\n", f_out); fputs("\t\t<row_6>", f_out); fputs(str_row(row_buff, 5), f_out); fputs("</row_6>\n", f_out); fputs("\t\t<row_5>", f_out); fputs(str_row(row_buff, 4), f_out); fputs("</row_5>\n", f_out); fputs("\t\t<row_4>", f_out); fputs(str_row(row_buff, 3), f_out); fputs("</row_4>\n", f_out); fputs("\t\t<row_3>", f_out); fputs(str_row(row_buff, 2), f_out); fputs("</row_3>\n", f_out); fputs("\t\t<row_2>", f_out); fputs(str_row(row_buff, 1), f_out); fputs("</row_2>\n", f_out); fputs("\t\t<row_1>", f_out); fputs(str_row(row_buff, 0), f_out); fputs("</row_1>\n", f_out); fputs("\t</board>\n", f_out); fputs("</game>", f_out); fclose(f_out); return 0; }