xref: /netbsd-src/games/dab/human.cc (revision d20841bb642898112fe68f0ad3f7b26dddf56f07)
1 /*	$NetBSD: human.cc,v 1.1 2003/12/27 01:16:55 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * human.C: Human interface for dots, using rogue-like keys.
41  */
42 #include "defs.h"
43 RCSID("$NetBSD: human.cc,v 1.1 2003/12/27 01:16:55 christos Exp $")
44 
45 #include "human.h"
46 #include "board.h"
47 #include "box.h"
48 
49 #define CONTROL(a) ((a) & 037)
50 
51 HUMAN::HUMAN(const char c) :
52     PLAYER(c),
53     _curx(0),
54     _cury(1)
55 {
56 }
57 
58 void HUMAN::play(const BOARD& b, size_t& y, size_t& x, int& dir)
59 {
60     int mv;
61     b.setpos(_cury, _curx);
62 
63     for (;;) {
64 	switch (mv = b.getmove()) {
65 	case 'h': case 'H':
66 	    _curx -= 2;
67 	    break;
68 
69 	case 'l': case 'L':
70 	    _curx += 2;
71 	    break;
72 
73 	case 'k': case 'K':
74 	    _cury -= 2;
75 	    break;
76 
77 	case 'j': case 'J':
78 	    _cury += 2;
79 	    break;
80 
81 	case 'u': case 'U':
82 	    _curx += 1;
83 	    _cury -= 1;
84 	    break;
85 
86 	case 'y': case 'Y':
87 	    _curx -= 1;
88 	    _cury -= 1;
89 	    break;
90 
91 	case 'b': case 'B':
92 	    _curx -= 1;
93 	    _cury += 1;
94 	    break;
95 
96 	case 'n': case 'N':
97 	    _curx += 1;
98 	    _cury += 1;
99 	    break;
100 
101 	case 'q': case 'Q':
102 	    exit(0);
103 
104 	case CONTROL('L'): case CONTROL('R'):
105 	    b.clean();
106 	    b.paint();
107 	    break;
108 
109 	case ' ':
110 	    {
111 		x = _curx / 2;
112 		y = _cury / 2;
113 
114 		if (_cury & 1) {
115 		    if (_curx == 0)
116 			dir = BOX::left;
117 		    else {
118 			x--;
119 			dir = BOX::right;
120 		    }
121 		}
122 
123 		if (_curx & 1) {
124 		    if (_cury == 0)
125 			dir = BOX::top;
126 		    else {
127 			y--;
128 			dir = BOX::bottom;
129 		    }
130 		}
131 	    }
132 	    return;
133 
134 	default:
135 	    break;
136 	}
137 
138         // We add 2 before the comparison to avoid underflow
139 	if ((2 + _curx) - (_curx & 1) < 2)
140 	    _curx = (b.nx() * 2) + (_curx & 1);
141 	if (_curx >= (b.nx() * 2) + 1)
142 	    _curx = (_curx & 1);
143 
144 	if ((2 + _cury) - (_cury & 1) < 2)
145 	    _cury = (b.ny() * 2) + (_cury & 1);
146 	if (_cury >= (b.ny() * 2) + 1)
147 	    _cury = (_cury & 1);
148 
149 	b.setpos(_cury, _curx);
150     }
151 }
152