commit d9c97eaae4147da39c460237f6fff7ce161d4d08 Author: project_one Date: Mon Apr 13 15:23:38 2026 +0330 Initial commit on develop branch diff --git a/tamam mini4.cpp b/tamam mini4.cpp new file mode 100644 index 0000000..ed8e5cc --- /dev/null +++ b/tamam mini4.cpp @@ -0,0 +1,549 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +struct game_history{ + string p1_name; + string p2_name; + int black_score; + int white_score; + string winner_name; + string time; +}; + +game_history game_h[100]; +int cu =0; +//تعداد بازی های انجام شده + +void enter_data(); +void add_data(game_history game_history); +void report_data(); +void load_data(); +void save_data(); + +const int BOARD_SIZE = 8; +const char EMPTY = '-'; +const char BLACK = 'B'; +const char WHITE = 'W'; + +char board [BOARD_SIZE][BOARD_SIZE]; + +struct coordinate{ + int r; + int c; +}; + +void Initialize_Board(){ + for (int r=0 ; r= BOARD_SIZE || c<0 || c>= BOARD_SIZE || board[r][c] != EMPTY) return false; + + int direct_r [] = {0 , 1 , 1 , 1 , 0 , -1 , -1 , -1 }; + int direct_c [] = {1 , 1 , 0 , -1 , -1 , -1 , 0 , 1 }; + + for (int i = 0; i < 8; i++){ + int next_r = r+ direct_r[i]; + int next_c = c+ direct_c[i]; + + bool found_opponent = false; + while(next_r>=0 && next_r=0 && next_r=0 && temp_r=0 && temp_c < BOARD_SIZE){ + if (board[temp_r][temp_c] == opponent){ + found_opponent = true; + } + + else if(board[temp_r][temp_c] == player){ + if(found_opponent){ + int flip_r = next_r; + int flip_c = next_c; + while(flip_r != temp_r || flip_c !=temp_c){ + if(board[flip_r][flip_c] == opponent){ + board[flip_r][flip_c] = player; + flipped_count++; + } + if(flip_rtemp_r) flip_r--; + if(flip_ctemp_c) flip_c--; + } + } + break; + } + + else if(board[temp_r][temp_c] == EMPTY){ + break; + } + + if(temp_r < temp_r +direct_r[i]) temp_r++; + else if(temp_r > temp_r +direct_r[i]) temp_r--; + + if(temp_c < temp_c +direct_c[i]) temp_c++; + else if(temp_c > temp_c +direct_c[i]) temp_c--; + } + } + return flipped_count; +} + +void run_game_flow(const string & player1_name ,const string &player2_name, bool is_single_player ){ + Initialize_Board(); + char current_player =BLACK; + string p1_name = player1_name; + string p2_name = player2_name; + string time; + + int current_row = BOARD_SIZE/2-1; + int current_col = BOARD_SIZE/2-1; + + bool valid_moves [BOARD_SIZE][BOARD_SIZE] = {false}; + get_valid_moves(current_player , (current_player == BLACK ? WHITE : BLACK) , valid_moves); + + bool initial_cursor_set = false; + for (int r= 0; r < BOARD_SIZE; r++){ + for (int c = 0; c < BOARD_SIZE; c++){ + if(valid_moves[r][c]){ + current_row = r; + current_col = c; + initial_cursor_set = true; + } + } + } + + while(true){ + char opponent =(current_player == BLACK ? WHITE : BLACK); + get_valid_moves(current_player , opponent , valid_moves); + + int black_score =0; + int white_score =0; + bool current_can_move = false; + + for (int r= 0; r < BOARD_SIZE; r++){ + for (int c = 0; c < BOARD_SIZE; c++){ + if(board[r][c]== BLACK) black_score++; + if(board[r][c]== WHITE) white_score++; + if(valid_moves[r][c]) current_can_move = true; + } + } + + bool temp_moves[BOARD_SIZE][BOARD_SIZE] ={false}; + get_valid_moves(opponent, current_player , temp_moves); + bool opponent_can_move = false; + + for (int r= 0; r < BOARD_SIZE; r++){ + for (int c = 0; c < BOARD_SIZE; c++){ + if(temp_moves[r][c]){ + opponent_can_move =true; + break; + } + } + if(opponent_can_move) break; + } + + if(!current_can_move && !opponent_can_move){ + clear_screen(); + cout << "\n ~~~~~The End of Game~~~~~"< white_score) cout <<"The winner : " << p1_name << endl; + else if(white_score > black_score) cout << "The winner : " << p2_name<white_score) ? p1_name : (black_score>white_score) ? p2_name : "Draw"; + gh.time = time; + add_data(gh); + + cout << "\n press Enter to return to meno ..."; + getch(); + break; + } + + if(!current_can_move){ + cout << current_player <<"pass!" << endl; + current_player = opponent; + continue; + } + + clear_screen(); + print_board(current_player, valid_moves ,current_row , current_col); + cout <<"current black_score: "<< black_score < 0){ + int index = rand() % move_count_found; + row = possible_moves[index].r; + col = possible_moves[index].c; + valid_input = true; + } + } + else{ + cout <<"Turn : "<< current_name < 0) temp_row--; + break; + case 'S': + if(current_row < BOARD_SIZE-1) temp_row++; + break; + case 'A': + if(current_col > 0) temp_col--; + break;; + case 'D': + if(current_col < BOARD_SIZE-1) temp_col++; + break; + } + + current_row = temp_row; + current_col = temp_col; + + clear_screen(); + print_board(current_player , valid_moves , current_row , current_col); + + cout << "current black_score : " << black_score << endl; + cout << "current white_score : " << white_score << endl; + cout << " Turn : " <> choice; + + if(choice==1){ + cout << "p1.name : "; + cin >> p1_name; + + cout << " \n p2.name : "; + cin >> p2_name; + + run_game_flow(p1_name, p2_name ,false); + } + else{ + cout << "p.name : "; + cin >> p1_name; + + run_game_flow(p1_name , "Bot" , true); + } +} + +void handle_help(){ + cout << "\n=== HOW TO PLAY OTHELLO ===" << endl; + cout << "\nOBJECTIVE:" << endl; + cout << "Have more discs of your color than your opponent at the end." << endl; + + cout << "\nRULES:" << endl; + cout << "1. Black always starts first" << endl; + cout << "2. Place your disc so that it traps opponent's discs between your discs" << endl; + cout << "3. All trapped opponent discs flip to your color" << endl; + cout << "4. You can trap discs in any direction: horizontal, vertical, diagonal" << endl; + cout << "5. If you have no valid moves, you must pass" << endl; + cout << "6. Game ends when both players have no valid moves" << endl; + + cout << "\nCONTROLS:" << endl; + cout << "W - Move cursor up" << endl; + cout << "S - Move cursor down" << endl; + cout << "A - Move cursor left" << endl; + cout << "D - Move cursor right" << endl; + cout << "P - Place disc" << endl; + cout << "Q - Quit game" << endl; + + cout << "\nBOARD SYMBOLS:" << endl; + cout << "B - Black disc" << endl; + cout << "W - White disc" << endl; + cout << "* - Valid move position" << endl; + cout << "b/w - Cursor position (empty cell)" << endl; + + cout << "\nPress any key to return to menu..."; + getch(); +} + +void load_data(){ + ifstream file("data.text"); + if(file.is_open()){ + cu=0; + + while (cu <100 && file >> game_h[cu].p1_name && file >> game_h[cu].p2_name && file >> game_h[cu].black_score && file >> game_h[cu].white_score && file >> game_h[cu].time){ + cu++; + } + } + file.close(); +} + +void save_data(){ + ofstream file("data.text"); + + if(file.is_open()){ + for (int i = 0; i < cu; i++){ + file << game_h[i].p1_name <<" "<> gh.p1_name; + + cout << "Enter White name: "; + cin >> gh.p2_name; + + cout << "Enter Black score : "; + cin >> gh.black_score; + + cout << "Enter White score : "; + cin >> gh.white_score; + + if(gh.black_score > gh.white_score) gh.winner_name = gh.p1_name; + else if(gh.white_score < gh.black_score) gh.winner_name = gh.p2_name; + else gh.winner_name = "Draw!"; + + cout << "Enter the time of the game : "; + cin >> gh.time; + + add_data(gh); + getch(); +} + +void report_data(){ + cout << "\n~~~~~~~~~~~Data~~~~~~~~~~~\n"; + + if(cu ==0) cout <<" no game history available"; + + else{ + for (int i = 0; i < cu; i++){ + game_history gah = game_h[i]; + cout << "\n ~~~~~~~~~~~~~~ The History ~~~~~~~~~~~~~~"; + cout << "p1_name : " << gah.p1_name <(time(nullptr))); + load_data(); + + int choice; + bool running = true; + + while(running){ + + display_menu(); + cin >> choice; + + switch (choice) + { + case 1: + handle_new_game(); + break; + case 2: + handle_help(); + break; + case 3: + report_data(); + break; + case 4: + enter_data(); + break; + case 5: + running = false; + break; + } + } + return 0; +} \ No newline at end of file