Beginning Perl Third Edition PHẦN 7

của chúng tôi thông qua các câu trả lời. Một lưu ý nhanh chóng nhớ lại rằng các từ khóa và foreach là từ đồng nghĩa cho nhau. Chúng tôi làm bất cứ chuyển đổi nó đòi hỏi, và tạo ra một tài liệu tham khảo băm, ban phước cho () , và trả lại cho chúng tôi. Về điều này sau trong chương này. | CHAPTER 11 REFERENCES Then we need ref- UK to be a hash reference because we re looking for the hash key England that hash entry needs to be an array reference and so on. Perl effectively does this ref _ ref- UK ref- UK - England ref- UK - England - OXford ref- UK - England - Oxford - 1999 ref- UK - England - Oxford - 1999 - Population 500000 What this means is that we don t have to worry about creating all the entries ourselves. We can just go ahead and write my @chessboard Chessboard 0 - 0 WR This is called autovivification things springing into existence. We can use autovivification to greatly simplify the way we use references. Now that we can represent our chessboard let s set up a chess game. This will consist of two stages setting up the board and making moves. The computer will have no idea of the rules but will simply function as a board allowing us to move pieces around. Here s our program usr bin perl use warnings use strict my @chessboard my @back qw R N B Q K B N R breach chessboard 0 _ W . back _ White Back Row chessboard 1 _ WP White Pawns chessboard 6 _ BP Black Pawns chessboard 7 _ B . back _ Black Back Row while 1 Print board foreach my i reverse Row foreach my j Column if defined chessboard i j print chessboard i j elsif. . i 2 j 2 print . else print . . . . print End of cell 246 CHAPTER 11 REFERENCES print n End of row print nStarting square x y my move last unless move A s 1-8 1-8 my startx 1-1 my starty 2-1 unless defined chessboard starty startx print There s nothing on that square n next . print nEnding square x y move last unless move 1-8 1-8 my endx 1-1 my endy 2-1 Put starting square on ending square. chessboard endy endx chessboard starty startx Remove from old square undef chessboard starty startx Now let s see the first part of a game in progress perl BR BN BB BQ BK BB BN BR BP BP BP BP BP BP BP BP WP WP WP WP WP WP WP WR WN WB WQ WK WB WN WP WR Starting square x y 4 2 Ending square x y 4 4 BR BN BB BQ BK

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
34    106    2    21-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.