~ubuntu-branches/ubuntu/trusty/ricochet/trusty

« back to all changes in this revision

Viewing changes to ricochet-0.2/rr.5c

  • Committer: Package Import Robot
  • Author(s): Keith Packard
  • Date: 2012-05-30 15:19:02 UTC
  • Revision ID: package-import@ubuntu.com-20120530151902-z47w0cxilb0p6wmi
Tags: 0.2
* Rename 'rrclient' to 'ricochet'
* Add name/host selection dialog
* Add build dependency on 'nickle'. Closes: #674302.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id$
 
3
 *
 
4
 * Copyright © 2003 Keith Packard
 
5
 *
 
6
 * Permission to use, copy, modify, distribute, and sell this software and its
 
7
 * documentation for any purpose is hereby granted without fee, provided that
 
8
 * the above copyright notice appear in all copies and that both that
 
9
 * copyright notice and this permission notice appear in supporting
 
10
 * documentation, and that the name of Keith Packard not be used in
 
11
 * advertising or publicity pertaining to distribution of the software without
 
12
 * specific, written prior permission.  Keith Packard makes no
 
13
 * representations about the suitability of this software for any purpose.  It
 
14
 * is provided "as is" without express or implied warranty.
 
15
 *
 
16
 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 
17
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 
18
 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 
19
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 
20
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 
21
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 
22
 * PERFORMANCE OF THIS SOFTWARE.
 
23
 */
 
24
 
 
25
namespace RR {
 
26
    public typedef enum { Red, Yellow, Green, Blue, Whirl } Color;
 
27
    public typedef enum { Triangle, Square, Octagon, Circle, Whirl } Shape;
 
28
    public typedef enum { North, East, South, West } Direction;
 
29
 
 
30
    public const int    Width = 16;
 
31
    public const int    Height = 16;
 
32
    public const int    Port = 5252;
 
33
 
 
34
    public typedef struct {
 
35
        Color   color;
 
36
        bool    active;
 
37
    } Robot;
 
38
    
 
39
    public typedef union {
 
40
        void    none;
 
41
        Robot   robot;
 
42
    } RobotOrNone;
 
43
 
 
44
    public typedef struct {
 
45
        Color   color;
 
46
        Shape   shape;
 
47
        bool    active;
 
48
    } Target;
 
49
    
 
50
    public typedef union {
 
51
        void    none;
 
52
        Target  target;
 
53
    } TargetOrNone;
 
54
        
 
55
    public typedef struct {
 
56
        bool    left, right, below, above;
 
57
    } Walls;
 
58
 
 
59
    public typedef struct {
 
60
        RobotOrNone     robot;
 
61
        TargetOrNone    target;
 
62
        Walls           walls;
 
63
    } Object;
 
64
 
 
65
    public typedef enum {
 
66
        NEW,        /* no bids yet */
 
67
        BID,        /* some bids, timer running */
 
68
        SHOW,       /* timer expired, waiting for solution */
 
69
        DONE        /* turn done (solved, or bailed) */
 
70
    } GameState;
 
71
    
 
72
    public typedef Object[Width,Height] Board;
 
73
 
 
74
    public TargetOrNone active_target(&Board board) {
 
75
            for (int y = 0; y < Height; y++)
 
76
                    for (int x = 0; x < Width; x++)
 
77
                            union switch (board[x,y].target) {
 
78
                            case target t:
 
79
                                    if (t.active)
 
80
                                            return (TargetOrNone.target) t;
 
81
                                    break;
 
82
                            case none:
 
83
                                    break;
 
84
                            }
 
85
            return TargetOrNone.none;
 
86
    }
 
87
 
 
88
    public void set_target(&Board board, Target target) {
 
89
            for (int y = 0; y < Height; y++)
 
90
                    for (int x = 0; x < Width; x++)
 
91
                            union switch (board[x,y].target) {
 
92
                            case target t:
 
93
                                    board[x,y].target.target.active = t == target;
 
94
                                    break;
 
95
                            case none:
 
96
                                    break;
 
97
                            }
 
98
    }
 
99
 
 
100
    public RobotOrNone active_robot(&Board board) {
 
101
            for (int y = 0; y < Height; y++)
 
102
                    for (int x = 0; x < Width; x++)
 
103
                            union switch (board[x,y].robot) {
 
104
                            case robot r:
 
105
                                    if (r.active)
 
106
                                            return (RobotOrNone.robot) r;
 
107
                                    break;
 
108
                            case none:
 
109
                                    break;
 
110
                            }
 
111
            return RobotOrNone.none;
 
112
    }
 
113
 
 
114
    public void set_robot(&Board board, Robot robot) {
 
115
            for (int y = 0; y < Height; y++)
 
116
                    for (int x = 0; x < Width; x++)
 
117
                            union switch (board[x,y].robot) {
 
118
                            case robot r:
 
119
                                    board[x,y].robot.robot.active = r == robot;
 
120
                                    break;
 
121
                            case none:
 
122
                                    break;
 
123
                            }
 
124
    }
 
125
 
 
126
    public typedef enum {
 
127
        NOGAME,
 
128
        NOUSER,
 
129
        NOTINGAME,
 
130
        NOTPLAYING,
 
131
        NOTBIDDING,
 
132
        NOTLOWER,
 
133
        NOBID,
 
134
        NOTACTIVE,
 
135
        NOTDONE,
 
136
        NOTNUMBER,
 
137
        BLOCKED,
 
138
        NOTEMPTY,
 
139
        COMMAND,
 
140
        SYNTAX,
 
141
        NOTCOLOR,
 
142
        NOTSHAPE,
 
143
        NOTDIRECTION,
 
144
        TOOMANYMOVES,
 
145
        NONAMESET,
 
146
        INVALIDNAME,
 
147
        NOTGAMESTATE,
 
148
        NOTBOOL
 
149
    } Error;
 
150
 
 
151
    public exception rr_error (Error e);
 
152
 
 
153
    public Color color (string w) {
 
154
        switch (w[0]) {
 
155
        case 'r': case 'R': return Color.Red;
 
156
        case 'y': case 'Y': return Color.Yellow;
 
157
        case 'g': case 'G': return Color.Green;
 
158
        case 'b': case 'B': return Color.Blue;
 
159
        case 'w': case 'W': return Color.Whirl;
 
160
        default: raise rr_error (Error.NOTCOLOR);
 
161
        }
 
162
    }
 
163
    
 
164
    public bool boolean (string w) {
 
165
        switch (w[0]) {
 
166
        case 't': case 'T': case 'y': case 'Y': case '1':
 
167
            return true;
 
168
        case 'f': case 'F': case 'n': case 'N': case '0':
 
169
            return false;
 
170
        default:
 
171
            raise rr_error(Error.NOTBOOL);
 
172
        }
 
173
    }
 
174
 
 
175
    public Shape shape (string w) {
 
176
        switch (w[0]) {
 
177
        case 't': case 'T': return Shape.Triangle;
 
178
        case 's': case 'S': return Shape.Square;
 
179
        case 'o': case 'O': return Shape.Octagon;
 
180
        case 'c': case 'C': return Shape.Circle;
 
181
        case 'w': case 'W': return Shape.Whirl;
 
182
            default: raise rr_error (Error.NOTSHAPE);
 
183
        }
 
184
    }
 
185
 
 
186
    public Direction direction (string w) {
 
187
        switch (w[0]) {
 
188
        case 'n': case 'N': return Direction.North;
 
189
        case 'e': case 'E': return Direction.East;
 
190
        case 's': case 'S': return Direction.South;
 
191
        case 'w': case 'W': return Direction.West;
 
192
        default: raise rr_error (Error.NOTDIRECTION);
 
193
        }
 
194
    }
 
195
 
 
196
    public GameState game_state(string w) {
 
197
        switch (w[0]) {
 
198
        case 'n': case 'N': return GameState.NEW;
 
199
        case 'b': case 'B': return GameState.BID;
 
200
        case 's': case 'S': return GameState.SHOW;
 
201
        case 'd': case 'D': return GameState.DONE;
 
202
        default: raise rr_error (Error.NOTGAMESTATE);
 
203
        }
 
204
    }
 
205
}