~ubuntu-branches/ubuntu/natty/ncurses/natty

« back to all changes in this revision

Viewing changes to test/bs.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-05-17 09:00:42 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070517090042-86fgxrr6j5jzagot
Tags: 5.6-0ubuntu1
* New upstream version.
  - Remove patches applied upstream: ncurses.upstream, signed-chars.
  - Update patches: debian-backspace.
* Build-depend on g++-multilib instead of lib{32,64}c*-dev-*.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
 * Copyright (c) 1998-2005,2006 Free Software Foundation, Inc.              *
 
3
 *                                                                          *
 
4
 * Permission is hereby granted, free of charge, to any person obtaining a  *
 
5
 * copy of this software and associated documentation files (the            *
 
6
 * "Software"), to deal in the Software without restriction, including      *
 
7
 * without limitation the rights to use, copy, modify, merge, publish,      *
 
8
 * distribute, distribute with modifications, sublicense, and/or sell       *
 
9
 * copies of the Software, and to permit persons to whom the Software is    *
 
10
 * furnished to do so, subject to the following conditions:                 *
 
11
 *                                                                          *
 
12
 * The above copyright notice and this permission notice shall be included  *
 
13
 * in all copies or substantial portions of the Software.                   *
 
14
 *                                                                          *
 
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
 
16
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
 
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
 
18
 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
 
19
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
 
20
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
 
21
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
 
22
 *                                                                          *
 
23
 * Except as contained in this notice, the name(s) of the above copyright   *
 
24
 * holders shall not be used in advertising or otherwise to promote the     *
 
25
 * sale, use or other dealings in this Software without prior written       *
 
26
 * authorization.                                                           *
 
27
 ****************************************************************************/
1
28
/* 
2
29
 * bs.c - original author: Bruce Holloway
3
30
 *              salvo option by: Chuck A DeGaul
7
34
 * v2.0 featuring strict ANSI/POSIX conformance, November 1993.
8
35
 * v2.1 with ncurses mouse support, September 1995
9
36
 *
10
 
 * $Id: bs.c,v 1.41 2005/05/28 21:38:24 tom Exp $
 
37
 * $Id: bs.c,v 1.44 2006/05/20 15:38:52 tom Exp $
11
38
 */
12
39
 
13
40
#include <test.priv.h>
149
176
 
150
177
static RETSIGTYPE uninitgame(int sig) GCC_NORETURN;
151
178
 
152
 
static RETSIGTYPE uninitgame(int sig GCC_UNUSED)
 
179
static RETSIGTYPE
 
180
uninitgame(int sig GCC_UNUSED)
153
181
/* end the game, either normally or due to signal */
154
182
{
155
183
    clear();
190
218
 
191
219
    srand((unsigned) (time(0L) + getpid()));    /* Kick the random number generator */
192
220
 
193
 
    (void) signal(SIGINT, uninitgame);
194
 
    (void) signal(SIGINT, uninitgame);
195
 
    (void) signal(SIGIOT, uninitgame);  /* for assert(3) */
196
 
    if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
197
 
        (void) signal(SIGQUIT, uninitgame);
 
221
    CATCHALL(uninitgame);
198
222
 
199
223
    if ((tmpname = getlogin()) != 0) {
200
224
        (void) strcpy(name, tmpname);
839
863
        }
840
864
        (void) printw(m, ss->name);
841
865
        (void) beep();
842
 
        return (awinna() == -1);
843
866
    }
844
867
    return (hit);
845
868
}
949
972
    attrset(0);
950
973
#endif /* A_COLOR */
951
974
 
952
 
    return ((hit ? (sunk ? S_SUNK : S_HIT) : S_MISS) ? TRUE : FALSE);
 
975
    return hit ? (sunk ? S_SUNK : S_HIT) : S_MISS;
953
976
}
954
977
 
955
978
/*
956
979
 * This code implements a fairly irregular FSM, so please forgive the rampant
957
980
 * unstructuredness below. The five labels are states which need to be held
958
981
 * between computer turns.
 
982
 *
 
983
 * The FSM is not externally reset to RANDOM_FIRE if the player wins. Instead,
 
984
 * the other states check for "impossible" conditions which signify a new
 
985
 * game, then if found transition to RANDOM_FIRE.
959
986
 */
960
987
static bool
961
988
cputurn(void)
1003
1030
        if (navail == 0)        /* no valid places for shots adjacent... */
1004
1031
            goto refire;        /* ...so we must random-fire */
1005
1032
        else {
1006
 
            for (d = 0, n = rnd(navail) + 1; n; n--)
1007
 
                while (used[d])
1008
 
                    d++;
1009
 
 
1010
 
            assert(d <= 4);
1011
 
 
1012
 
            used[d] = FALSE;
 
1033
            n = rnd(navail) + 1;
 
1034
            for (d = 0; used[d]; d++) ;
 
1035
            /* used[d] is first that == 0 */
 
1036
            for (; n > 1; n--)
 
1037
                while (used[++d]) ;
 
1038
            /* used[d] is next that == 0 */
 
1039
 
 
1040
            assert(d < 4);
 
1041
            assert(used[d] == FALSE);
 
1042
 
 
1043
            used[d] = TRUE;
1013
1044
            x = ts.x + xincr[d * 2];
1014
1045
            y = ts.y + yincr[d * 2];
1015
1046
 
1040
1071
        break;
1041
1072
 
1042
1073
    case REVERSE_JUMP:          /* nail down the ship's other end */
1043
 
        d = ts.dir + 4;
 
1074
        d = (ts.dir + 4) % 8;
1044
1075
        x = ts.x + ts.hits * xincr[d];
1045
1076
        y = ts.y + ts.hits * yincr[d];
1046
1077
        if (POSSIBLE(x, y) && (hit = cpufire(x, y))) {
1053
1084
            next = RANDOM_FIRE;
1054
1085
        break;
1055
1086
 
1056
 
    case SECOND_PASS:           /* kill squares not caught on first pass */
 
1087
    case SECOND_PASS:           /* continue shooting after reversing */
1057
1088
        x = ts.x + xincr[ts.dir];
1058
1089
        y = ts.y + yincr[ts.dir];
1059
1090
        if (POSSIBLE(x, y) && (hit = cpufire(x, y))) {
1067
1098
        break;
1068
1099
    }
1069
1100
 
1070
 
    /* check for continuation and/or winner */
 
1101
    /* pause between shots in salvo */
1071
1102
    if (salvo) {
1072
1103
        (void) refresh();
1073
1104
        (void) sleep(1);
1074
1105
    }
1075
 
    if (awinna() != -1)
1076
 
        return (FALSE);
1077
 
 
1078
1106
#ifdef DEBUG
1079
1107
    (void) mvprintw(PROMPTLINE + 2, 0,
1080
1108
                    "New state %d, x=%d, y=%d, d=%d",
1213
1241
                    }
1214
1242
                }
1215
1243
            } else
1216
 
                while (turn ? cputurn() : plyturn())
 
1244
                while ((turn ? cputurn() : plyturn()) && awinna() == -1)
1217
1245
                    continue;
1218
1246
            turn = OTHER;
1219
1247
        }