xref: /netbsd-src/games/dab/player.cc (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: player.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  * player.C: Player base class
41  */
42 
43 #include "defs.h"
44 RCSID("$NetBSD: player.cc,v 1.1 2003/12/27 01:16:55 christos Exp $")
45 
46 #include "board.h"
47 #include "player.h"
48 
49 PLAYER::PLAYER(char who) :
50     _who(who),
51     _score(0),
52     _total(0),
53     _games(0),
54     _ties(0)
55 {
56 }
57 
58 void PLAYER::init(void)
59 {
60     _score = 0;
61 }
62 
63 void PLAYER::wl(size_t sc)
64 {
65     _total += _score;
66     _games += sc < _score;
67     _ties += sc == _score;
68 }
69 
70 int PLAYER::domove(BOARD& b)
71 {
72     size_t y, x;
73     int dir;
74     int score;
75 
76     for (;;) {
77 	if (b.full())
78 	    return 0;
79 
80 	play(b, y, x, dir);
81 
82 	switch (score = b.domove(y, x, dir, _who)) {
83 	case 0:
84 	    // No closure
85 	    return 1;
86 
87 	case -1:
88 	    // Not a valid move
89 	    b.bell();
90 	    break;
91 
92 	default:
93 	    // Closure, play again
94 	    _score += score;
95 	    break;
96 	}
97     }
98 }
99