#!/usr/bin/perl -w # 作者: 洪朝貴 http://www.cyut.edu.tw/~ckhung/ # 功能: 讓使用者下跳棋的程式. 純粹只是版面安排及棋子移動; # 沒有電腦下棋的功能. # 需求: perl-Tk 模組. # 命令列選項: # -n grid_count (每一國有多少格寬) # -s grid_size (每一格有多寬) # 版權聲明: XFree86 style. 如果您要將我的程式改寫成有用的大程式, # 建議將您的版本施以 GPL use Getopt::Std; use Tk; use Tk::Dialog; use Tk::DialogBox; use POSIX qw(floor); use Data::Dumper; use strict; use vars qw($board $pos); # 用 my 宣告的變數無法跨越檔案 my ( # $board, # 棋盤外觀 # $pos, # 棋子的位置. 只有讀寫檔案時用得到. %opts, # 選項 $main, # 主視窗 $dir, # (六個)方向 $picked, # 目前撿起來的棋子 $chess_id, # 棋子的 id @tri_id, # 六片三角形的 id $file_name, # 檔案名稱 $chess_moved, # 棋子移動過了嗎? ); # 設定棋盤外觀 getopts('n:s:', \%opts); $board = { color => ["red", "blue", "yellow"], grid_count => $opts{n} || 3, grid_size => $opts{s} || 20, }; # 建立主視窗及選單 $main = MainWindow->new(); $main->protocol("WM_DELETE_WINDOW", \&on_quit); $main->{menubar} = $main->Frame(-relief=>"raised", -bd=>2); $main->{menubar}->pack(-side=>"top", -fill=>"both"); $main->{status} = $main->Frame(-relief=>"sunken", -bd=>2); $main->{status}->pack(-side=>"bottom", -fill=>"both"); $main->{worksp} = $main->Canvas(-bg=>"white"); $main->{worksp}->pack(-side=>"top", -fill=>"both", -expand=>"yes"); $main->{menubar}{file} = $main->{menubar}->Menubutton( -text=>"File", -tearoff=>0, -menuitems=>[ ["command"=>"Open", -command=>\&on_open], ["command"=>"Save", -command=>\&on_save], ["command"=>"Print (to file as eps)", -command=>\&on_print], "-", ["command"=>"Quit", -command=>\&on_quit] ] ); $main->{menubar}{file}->pack(-side=>"left", -fill=>"both"); $file_name = $chess_moved = ""; $picked = 0; if ($ARGV[0] && -r $ARGV[0]) { &on_open($ARGV[0]); } else { &rebuild(); } $main->{worksp}->bind("chess", '