othello_board.erl
来自「OTP是开放电信平台的简称」· ERL 代码 · 共 640 行 · 第 1/2 页
ERL
640 行
%% ``The contents of this file are subject to the Erlang Public License,%% Version 1.1, (the "License"); you may not use this file except in%% compliance with the License. You should have received a copy of the%% Erlang Public License along with this software. If not, it can be%% retrieved via the world wide web at http://www.erlang.org/.%% %% Software distributed under the License is distributed on an "AS IS"%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See%% the License for the specific language governing rights and limitations%% under the License.%% %% The Initial Developer of the Original Code is Ericsson Utvecklings AB.%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings%% AB. All Rights Reserved.''%% %% $Id$%%-module(othello_board).-export([start/0,stop/0,init/0]).%%----------------------------------------------------------------------%% The Othello program now uses the gs graphical package instead of the %% pxw package.%%%% Differences are, explanation why and source of change in parenthesis:%%%% - Buttons looks different (gs feature)%% - The black box around "Black to draw" have been removed. (me)%% - The Colour and Level menues have been moved directly down to the %% 'Status' box. (usability update, my addition)%% - The mouse pointer does not change into a watch when the computer %% is thinking (not supported in gs)%% - Buttons does not flash when being beeped. (not supported in gs)%%%%%% /Peter%%%%-----------------------------------------------------------------------define(BGCOL,forestgreen).start() -> spawn(othello_board,init,[]).stop() -> ok.%% This is not an application so we don't have their way of knowing%% a private data directory where the GIF files are located (this directory).%% We can find GS and makes it relative from there /kgb-define(BitmapPath,"../contribs/othello/priv").setup_path() -> GsPrivDir = code:priv_dir(gs), Path = filename:join(GsPrivDir,?BitmapPath), put(path,Path). path() -> get(path).%%%% The button area are the Quit, Rules buttons at the top of the window.%% The Status area is the black and white scores level and colour etc %% inbetween the buttons and the othello board.%% The board is the 8x8 board where othello battles are fought.%%init() -> process_flag(trap_exit,true), setup_path(), S = gs:start(), put(windowroot,S), % Ugly global store %% Shell coordinates W = 496, H = 636, %% Fix top window Shell = gs:create(window, S, [{title,"Othello"}, {width, W},{height, H}]), %% Setup window contents setup_buttons(Shell,0,0,W,40), % Fix Menubar setup_status_box(Shell,0,40,W,100), % Fix Status area setup_board(Shell,0,140,496,496), % Combat board GamePid = othello:new_game(white,black,1,first_time), %% Default settings Options = {white,black,1}, %%Wids = {Status,B,W,Dr,Le,Co}, Wids = {change,this,at,later,stage,ponto}, write_options(Options,Wids), gs:config(Shell, {map, true}), %Make win visible loop(computer,GamePid,Shell,Wids,Options).loop(User,GamePid,Shell,Wids,Options) -> receive {gs,ButtId, click,_ButtId1,[Button]} -> GamePid1 = but_pressed(Button,ButtId,User,GamePid,Shell, Wids,Options), loop(User,GamePid1,Shell,Wids,Options); {gs,_, click,_,[MenuItem,_MenuIndex]} -> Ops = menu_selected(MenuItem,User,GamePid,Wids,Options), loop(User,GamePid,Shell,Wids,Ops); {'EXIT',GamePid,_} -> loop(User,null,Shell,Wids,Options); {'EXIT',_,_} -> loop(User,GamePid,Shell,Wids,Options); GameMsg -> game_msg(GameMsg,User,GamePid,Shell,Wids,Options) end.but_pressed("Quit",_ButtId,_User,_GamePid,_Shell,_Wids,_Op) -> stop(), exit(quit);but_pressed("Rules",_ButtId,_User,GamePid,_Shell,_Wids,_Op) -> io:format("No rules, do as you wish~n",[]), GamePid;but_pressed("Help",_ButtId,_User,GamePid,_Shell,_Wids,_Op) -> io:format("Othello game~n",[]), io:format("------------~n",[]), io:format(" Put markers by clicking in squares~n",[]), io:format(" Change level by clicking on it~n",[]), io:format(" Change colour by clicking on it~n",[]), io:format("~n",[]), GamePid;but_pressed("Newgame",_ButtId,_User,GamePid,_Shell,Wids,Options) -> new_game(GamePid,Wids,Options);but_pressed([],ButtId,User,GamePid,_Shell,_Wids,_Op) when pid(GamePid),User == player -> [C,R] = atom_to_list(ButtId), GamePid ! {self(),position,othello_adt:pos(C-96,translate(R-48))}, GamePid;but_pressed([],ButtId,_User,GamePid,_Shell,_Wids,_Op) -> [C,R] = atom_to_list(ButtId), beep(othello_adt:pos(C-96,translate(R-48))), GamePid;but_pressed(Button,ButtId,_User,GamePid,_Shell,_Wids,_Op) -> io:format('Not implemented button pressed ~p, ~p!!!~n',[ButtId,Button]), GamePid.menu_selected("Black",_User,_GamePid,Wids,Options) -> Op0 = setelement(1,Options,white), Op1 = setelement(2,Op0,white), write_options(Op1,Wids), Op1;menu_selected("White",_User,_GamePid,Wids,Options) -> Op0 = setelement(1,Options,black), Op1 = setelement(2,Op0,black), write_options(Op1,Wids), Op1;menu_selected("Black (begin)",_User,_GamePid,Wids,Options) -> Op0 = setelement(1,Options,white), Op1 = setelement(2,Op0,black), write_options(Op1,Wids), Op1;menu_selected("White (begin)",_User,_GamePid,Wids,Options) -> Op0 = setelement(1,Options,black), Op1 = setelement(2,Op0,white), write_options(Op1,Wids), Op1;menu_selected("Beginner",_User,_GamePid,Wids,Options) -> Op1 = setelement(3,Options,1), write_options(Op1,Wids), Op1;menu_selected("Intermediate",_User,_GamePid,Wids,Options) -> Op1 = setelement(3,Options,2), write_options(Op1,Wids), Op1;menu_selected("Advanced",_User,_GamePid,Wids,Options) -> Op1 = setelement(3,Options,3), write_options(Op1,Wids), Op1;menu_selected("Expert",_User,_GamePid,Wids,Options) -> Op1 = setelement(3,Options,4), write_options(Op1,Wids), Op1;menu_selected(What,_User,_GamePid,_Wids,Options) -> io:format('Menu item not implemented <~s>~n',[What]), Options.game_msg(Msg,User,GamePid,Shell,Wids,Options) -> case Msg of {GamePid,new_mark,Pos,Colour} -> new_mark(Pos,Colour), loop(User,GamePid,Shell,Wids,Options); {GamePid,illegal_draw,Draw} -> beep(Draw), loop(User,GamePid,Shell,Wids,Options); {GamePid,player,Computer,Computer} -> show_player(element(1,Wids),Computer), cursor("watch"), GamePid ! {self(),go_on_play}, loop(computer,GamePid,Shell,Wids,Options); {GamePid,player,_Computer,Player} -> show_player(element(1,Wids),Player), cursor("top_left_arrow"), GamePid ! {self(),go_on_play}, loop(player,GamePid,Shell,Wids,Options); {GamePid,omit_draw,Player} -> omit_draw(GamePid,Player), loop(User,GamePid,Shell,Wids,Options); {GamePid,score,WhiteRes,BlackRes} -> write_score(Wids,WhiteRes,BlackRes), loop(User,GamePid,Shell,Wids,Options); {GamePid,draw,Draw} -> write_draw(Wids,Draw), loop(User,GamePid,Shell,Wids,Options); {GamePid,game_over,WhiteRes,BlackRes} -> game_over(WhiteRes,BlackRes), loop(User,GamePid,Shell,Wids,Options); What -> io:format('game_msg received: ~w~n',[What]), loop(User,GamePid,Shell,Wids,Options) end. new_game(GamePid,Wids,Options) when pid(GamePid) -> exit(GamePid,kill), new_game(Wids,Options);new_game(_,Wids,Options) -> new_game(Wids,Options).new_game(_Wids,Options) -> label("",lastdraw), Computer = element(1,Options), Start = element(2,Options), Depth = element(3,Options), othello:new_game(Computer,Start,Depth,restart).new_mark(Pos,Colour) -> Col = othello_adt:col(Pos), Row = othello_adt:row(Pos), Name = [Col+96,translate(Row)+48], Button = get(Name), butbit(Button,Colour).beep(Draw) -> Col = othello_adt:col(Draw), Row = othello_adt:row(Draw), Name = [Col+96,translate(Row)+48], Button = get(Name), bell(Button).show_player(_Status,white) -> label("White to draw",todraw);show_player(_Status,black) -> label("Black to draw",todraw).write_score(_Wids,WhiteRes,BlackRes) -> label(integer_to_list(BlackRes),bscore), label(integer_to_list(WhiteRes),wscore).write_draw(_Wids,Draw) -> Col = othello_adt:col(Draw), Row = othello_adt:row(Draw), label(lists:flatten(io_lib:format('{~w,~w}',[Col,Row])), lastdraw).write_options(Options,Wids) -> write_colour(Options,Wids), write_level(Options,Wids).write_colour(Options,Wids) -> write_colour(element(1,Options),element(2,Options),Wids).write_colour(black,white,_Wids) -> label("White (begin)",colour);write_colour(black,black,_Wids) -> label("White",colour);write_colour(white,black,_Wids) -> label("Black (begin)",colour);write_colour(white,white,_Wids) -> label("Black",colour). write_level(Options,_Wids) -> case element(3,Options) of 1 -> label("Beginner",level); 2 -> label("Intermediate",level); 3 -> label("Advanced",level); 4 -> label("Expert",level) end.cursor(_What) -> done.%cursor(What) -> cursor(get(),What).%cursor([{[C,R],Button}|Buts],What) ->% set_widget(Button,"cursor",What),% cursor(Buts,What);%cursor([_|Buts],What) ->% cursor(Buts,What);%cursor([],_) ->% true.translate(1) -> 8;translate(2) -> 7;translate(3) -> 6;translate(4) -> 5;translate(5) -> 4;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?