|
| 1 | +% Keypad Server |
| 2 | + |
| 3 | +-module(keypad). |
| 4 | +-behavior(gen_server). |
| 5 | +-export([start_link/1,stop/1,move/2,getkey/1]). |
| 6 | +-export([init/1,handle_call/3,handle_cast/2,handle_info/2,terminate/2,code_change/3]). |
| 7 | + |
| 8 | +start_link(Mode) -> gen_server:start_link(?MODULE, [Mode], []). |
| 9 | + |
| 10 | +%% Calls |
| 11 | +move(Pid, Direction) -> gen_server:cast(Pid, {move, Direction}). |
| 12 | +stop(Pid) -> gen_server:call(Pid, stop). |
| 13 | +getkey(Pid) -> gen_server:call(Pid, getkey). |
| 14 | + |
| 15 | +%%% Server Functions |
| 16 | +init([Mode]) -> {ok, {5, Mode}}. |
| 17 | + |
| 18 | +handle_call(getkey, _From, {Pos, Mode}) -> {reply, Pos, {Pos, Mode}}; |
| 19 | +handle_call(stop, _From, {Pos, Mode}) -> {stop, normal, ok, {Pos, Mode}}; |
| 20 | +handle_call(Msg, _From, State) -> io:format("Unknown Message: ~p", [Msg]), {stop, unknown_message, State}. |
| 21 | + |
| 22 | +handle_cast({move, Direction}, {Pos, Mode}) -> {noreply, {next_key(Mode, Pos, Direction), Mode}}; |
| 23 | +handle_cast(Msg, State) -> io:format("Unknown Message: ~p", [Msg]), {stop, unknown_message, State}. |
| 24 | + |
| 25 | +handle_info(Msg, State) -> io:format("Unknown Message: ~p", [Msg]), {stop, unknown_message, State}. |
| 26 | + |
| 27 | +terminate(normal, _State) -> ok. |
| 28 | + |
| 29 | +% We don't have any state migration. |
| 30 | +code_change(_OldVsn, State, _Extra) -> {ok, State}. |
| 31 | + |
| 32 | +%%% Private |
| 33 | + |
| 34 | +% Performs the move. A bit cheaty, but it's how I'd legit do it... lookup tables ftw. There's probably an algorithm but why waste the effort :) |
| 35 | +next_key(standard, 1, up) -> 1; |
| 36 | +next_key(standard, 1, right) -> 2; |
| 37 | +next_key(standard, 1, down) -> 4; |
| 38 | +next_key(standard, 1, left) -> 1; |
| 39 | +next_key(standard, 2, up) -> 2; |
| 40 | +next_key(standard, 2, right) -> 3; |
| 41 | +next_key(standard, 2, down) -> 5; |
| 42 | +next_key(standard, 2, left) -> 1; |
| 43 | +next_key(standard, 3, up) -> 3; |
| 44 | +next_key(standard, 3, right) -> 3; |
| 45 | +next_key(standard, 3, down) -> 6; |
| 46 | +next_key(standard, 3, left) -> 2; |
| 47 | +next_key(standard, 4, up) -> 1; |
| 48 | +next_key(standard, 4, right) -> 5; |
| 49 | +next_key(standard, 4, down) -> 7; |
| 50 | +next_key(standard, 4, left) -> 4; |
| 51 | +next_key(standard, 5, up) -> 2; |
| 52 | +next_key(standard, 5, right) -> 6; |
| 53 | +next_key(standard, 5, down) -> 8; |
| 54 | +next_key(standard, 5, left) -> 4; |
| 55 | +next_key(standard, 6, up) -> 3; |
| 56 | +next_key(standard, 6, right) -> 6; |
| 57 | +next_key(standard, 6, down) -> 9; |
| 58 | +next_key(standard, 6, left) -> 5; |
| 59 | +next_key(standard, 7, up) -> 4; |
| 60 | +next_key(standard, 7, right) -> 8; |
| 61 | +next_key(standard, 7, down) -> 7; |
| 62 | +next_key(standard, 7, left) -> 7; |
| 63 | +next_key(standard, 8, up) -> 5; |
| 64 | +next_key(standard, 8, right) -> 9; |
| 65 | +next_key(standard, 8, down) -> 8; |
| 66 | +next_key(standard, 8, left) -> 7; |
| 67 | +next_key(standard, 9, up) -> 6; |
| 68 | +next_key(standard, 9, right) -> 9; |
| 69 | +next_key(standard, 9, down) -> 9; |
| 70 | +next_key(standard, 9, left) -> 8; |
| 71 | + |
| 72 | +next_key(weird, 1, up) -> 1; |
| 73 | +next_key(weird, 1, right) -> 1; |
| 74 | +next_key(weird, 1, down) -> 3; |
| 75 | +next_key(weird, 1, left) -> 1; |
| 76 | + |
| 77 | +next_key(weird, 2, up) -> 2; |
| 78 | +next_key(weird, 2, right) -> 3; |
| 79 | +next_key(weird, 2, down) -> 6; |
| 80 | +next_key(weird, 2, left) -> 2; |
| 81 | + |
| 82 | +next_key(weird, 3, up) -> 1; |
| 83 | +next_key(weird, 3, right) -> 4; |
| 84 | +next_key(weird, 3, down) -> 7; |
| 85 | +next_key(weird, 3, left) -> 2; |
| 86 | + |
| 87 | +next_key(weird, 4, up) -> 4; |
| 88 | +next_key(weird, 4, right) -> 4; |
| 89 | +next_key(weird, 4, down) -> 8; |
| 90 | +next_key(weird, 4, left) -> 3; |
| 91 | + |
| 92 | +next_key(weird, 5, up) -> 5; |
| 93 | +next_key(weird, 5, right) -> 6; |
| 94 | +next_key(weird, 5, down) -> 5; |
| 95 | +next_key(weird, 5, left) -> 5; |
| 96 | + |
| 97 | +next_key(weird, 6, up) -> 2; |
| 98 | +next_key(weird, 6, right) -> 7; |
| 99 | +next_key(weird, 6, down) -> a; |
| 100 | +next_key(weird, 6, left) -> 5; |
| 101 | + |
| 102 | +next_key(weird, 7, up) -> 3; |
| 103 | +next_key(weird, 7, right) -> 8; |
| 104 | +next_key(weird, 7, down) -> b; |
| 105 | +next_key(weird, 7, left) -> 6; |
| 106 | + |
| 107 | +next_key(weird, 8, up) -> 4; |
| 108 | +next_key(weird, 8, right) -> 9; |
| 109 | +next_key(weird, 8, down) -> c; |
| 110 | +next_key(weird, 8, left) -> 7; |
| 111 | + |
| 112 | +next_key(weird, 9, up) -> 9; |
| 113 | +next_key(weird, 9, right) -> 9; |
| 114 | +next_key(weird, 9, down) -> 9; |
| 115 | +next_key(weird, 9, left) -> 8; |
| 116 | + |
| 117 | +next_key(weird, a, up) -> 6; |
| 118 | +next_key(weird, a, right) -> b; |
| 119 | +next_key(weird, a, down) -> a; |
| 120 | +next_key(weird, a, left) -> a; |
| 121 | + |
| 122 | +next_key(weird, b, up) -> 7; |
| 123 | +next_key(weird, b, right) -> c; |
| 124 | +next_key(weird, b, down) -> d; |
| 125 | +next_key(weird, b, left) -> a; |
| 126 | + |
| 127 | +next_key(weird, c, up) -> 8; |
| 128 | +next_key(weird, c, right) -> c; |
| 129 | +next_key(weird, c, down) -> c; |
| 130 | +next_key(weird, c, left) -> b; |
| 131 | + |
| 132 | +next_key(weird, d, up) -> b; |
| 133 | +next_key(weird, d, right) -> d; |
| 134 | +next_key(weird, d, down) -> d; |
| 135 | +next_key(weird, d, left) -> d. |
0 commit comments