bool ChessSaver::save_chess(Chess& chess, Chess::PieceType player_type_) { if (chess_check(chess, player_type_) == false) return false; ofstream output_file; output_file.open(file_name_, ios::out); if (output_file.fail()) return false; int i, j; Chess::PieceType type; try { for (i = 0; i < Chess::SIZE; i++) { for (j = 0; j < Chess::SIZE; j++) { type = chess.get_point(i, j); if (type == Chess::BLACK) output_file << 1 << " "; else if (type == Chess::WHITE) output_file << -1 << " "; else output_file << 0 << " "; if (output_file.fail() || output_file.bad()) { output_file.close(); return false; } } output_file << endl; } if (player_type_ == Chess::BLACK) output_file << "black"; else if (player_type_ == Chess::WHITE) output_file << "white"; else { output_file.close(); return false; } output_file.close(); return true; }catch (exception e) { cout << e.what() << endl; output_file.close(); return false; } }
int chess_do(void) { int x, y; int lx, ly; other_player = curent_player == 1 ? 2 : 1; if (mx < ST_X || mx > ST_X + SPACE*(B_X-1)) { return 0; } if (my <ST_Y || my > ST_Y + SPACE*(B_Y-1)) { return 0; } x = ST_X + ((mx - ST_X) / SPACE) * SPACE + SPACE / 2; // draw circle at center y = ST_Y + ((my - ST_Y) / SPACE) * SPACE + SPACE / 2; lx = (x - ST_X) / SPACE; ly = (y - ST_Y) / SPACE; if (chess_board[lx + ly*(B_X-1)] == 0) { fb_circle_fill(x, y, C_R, curent_color); chess_board[lx + ly*(B_X-1)] = curent_player; chess_check(lx, ly); if (curent_player == 1) { curent_color = WHITE; } else { curent_color = BLACK; } curent_player = other_player; } return 0; }
bool ChessSaver::read_chess() { ifstream input_file; input_file.open(file_name_, ios::in); if (input_file.fail()) return false; int i, j; int temp_num; try { for (i = 0; i < Chess::SIZE; i++) { for (j = 0; j < Chess::SIZE; j++) { if (input_file.eof() || input_file.bad() || input_file.fail()) { input_file.close(); return false; } input_file >> temp_num; switch (temp_num) { case 0: chess_buf_.set_point(i, j, Chess::EMPTY); break; case -1: chess_buf_.set_point(i, j, Chess::WHITE); break; case 1: chess_buf_.set_point(i, j, Chess::BLACK); break; default: input_file.close(); return false; } } } if (input_file.eof()) { input_file.close(); return false; } string type; char temp[100]; input_file >> temp; type = temp; if (type == "black") player_piece_color_ = Chess::BLACK; else if (type == "white") player_piece_color_ = Chess::WHITE; else { input_file.close(); return false; } input_file.close(); return chess_check(chess_buf_, player_piece_color_); } catch (exception e) { cout << e.what() << endl; input_file.close(); return false; } }