Data Structures and Program Design in C++ phần 4

Tham khảo tài liệu 'data structures and program design in c++ phần 4', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Section Tree-Structured Programs Look-Ahead in Games 203 Refinement 149 The outline of Section leads to the following recursive function. int look_ahead const Board game int depth Move recommended Pre Board game represents a legal game position. Post An evaluation of the game based on looking ahead depth moves is returned. The best move that can be found for the mover is recorded as Move recommended. Uses The classes Stack Board and Move together with function look_ahead recursively. if depth 0 return else Stack moves moves int value best_value while Move try_it reply try_it Board new_game game try_it value look_ahead new_game depth - 1 reply if value best_value try_it is the best move yet found best_value value recommended try_it moves. pop return best_value The reference parameter Move recommended is used to return a recommended move unless the game is over or the depth of search is 0 . The reference parameter Board game could be specified as a value parameter since we do not want to change the Board in the function. However to avoid a possibly expensive copying operation we pass game as a constant reference parameter. Observe that the compiler can guarantee that the object Board game is unchanged by the function look_ahead because the only Board methods that are applied have been declared with the modifier const. Without this earlier care in our definition of the class Board it would have been illegal to pass the parameter Board game as a constant. 204 Chapter 5 Recursion Tic-Tac-Toe Study main program We shall finish this section by giving implementations of the classes Board and Move for use in the game of tic-tac-toe noughts and crosses . Here the classes consist of little more than a formal implementation of the rules of the game. We leave the writing of a main program that harnesses these classes with the function look_ahead to play

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
192    69    1    16-06-2024
185    337    3    16-06-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.