Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10 5/3/83 based; site houem.UUCP
Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!mhuxt!houxm!houem!judah
From: judah@houem.UUCP (#J.KAMINETSKY)
Newsgroups: net.games
Subject: Revised Battleships
Message-ID: <347@houem.UUCP>
Date: Fri, 28-Jun-85 16:39:42 EDT
Article-I.D.: houem.347
Posted: Fri Jun 28 16:39:42 1985
Date-Received: Sat, 29-Jun-85 03:28:50 EDT
Distribution: net.sources
Organization: AT&T Bell Labs, Holmdel NJ
Lines: 622
/* battleships by Judah S. Kaminetsky */
/*cc -obs bs.c -lcurses*/
#define MAX_X 12
#define MAX_Y 12
#define NO 1
#define YES 0
#include
long r;/* for random number generator*/
int myscore = 0;
int hisscore = 0;
int hismoves = 1;
int mymoves = 1;
int nextx, nexty;/* next square to attack if adjacent to previous hit*/
main()
{
WINDOW *mywin;/*player's screen*/
WINDOW *myscrwin;/*players's score window*/
WINDOW *hisscrwin;/*computers's score window*/
WINDOW *hidwin;/* computers hidden screen */
WINDOW *showwin;/*computer's displayed screen*/
WINDOW *recwin;/*computers record of my screen */
int t;
time(&r);/*Seed the random number generator*/
srand((int)(r&0177777L));
initscr();
nonl();
noecho();
cbreak();
err("Battleships System Test 2 - comments to hounx!juda");
mywin = newwin(MAX_Y, MAX_X, 5, 15);
recwin = newwin(MAX_Y, MAX_X, 5, 0);
werase(mywin);
werase(recwin);
wattron(mywin,A_REVERSE,A_BOLD);
label_x(mywin);
wrefresh(mywin);
label_y(mywin);
wrefresh(mywin);
wattroff(mywin,A_REVERSE,A_BOLD);
wattron(recwin,A_REVERSE,A_BOLD);
label_x(recwin);
label_y(recwin);
wattroff(recwin,A_REVERSE,A_BOLD);
set(mywin,'A',5);
wrefresh(mywin);
set(mywin,'B',4);
wrefresh(mywin);
set(mywin,'S',3);
wrefresh(mywin);
set(mywin,'D',3);
wrefresh(mywin);
set(mywin,'P',2);
wrefresh(mywin);
clrtop();
hidwin = newwin(MAX_Y, MAX_X, 5, 67);
showwin = newwin(MAX_Y, MAX_X, 5, 53);
myscrwin = newwin(4, MAX_X+3, 17, 53);
hisscrwin = newwin(4, MAX_X+3, 17, 15);
werase(hidwin);
werase(showwin);
wattron(hidwin,A_REVERSE,A_BOLD);
label_x(hidwin);
label_y(hidwin);
wattroff(hidwin,A_REVERSE,A_BOLD);
wattron(showwin,A_REVERSE,A_BOLD);
label_x(showwin);
label_y(showwin);
wrefresh(showwin);
wattroff(showwin,A_REVERSE,A_BOLD);
setup(hidwin,'A',5);
setup(hidwin,'B',4);
setup(hidwin,'S',3);
setup(hidwin,'D',3);
setup(hidwin,'P',2);
clrtop();
for(t=1;t<101;t++)
{
myttack(hidwin,showwin);
myrecord(myscrwin);
if(myscore>16)
{
msg("YOU WIN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
wrefresh(recwin);
wrefresh(hidwin);
endwin();
exit(1);
}
hisattack(mywin,recwin);
hisrecord(hisscrwin);
if(hisscore>16)
{
msg("YOU LOSE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
wrefresh(recwin);
wrefresh(hidwin);
endwin();
exit(1);
}
}
endwin();
}
/*************************************************************/
/* Draw x axis labels */
label_x(win)
WINDOW *win;
{
int x,y;
int ch;
for(y=0;yhigh)
{
sprintf(msgstr,"%c is out of legal range %c to %c",c,low,high);
return(NO);
}
else
{
return(YES);
}
}
/*********************************************************************/
/*player attack computer - goes to attack coordinates on hidwin*/
/*and copies ch (+attributes?) to showwin - touchwin and refresh showwin*/
/* allows duplicate moves but does not score duplicaate hits*/
myttack(hidwin,showwin)
WINDOW *hidwin;
WINDOW *showwin;
{
int y , x;
int hit;
char c;
char d;
char msg_str[80];
msg("ATTACK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
while(verify(x=get_x(),0,10)==1)
{
}
while(verify(y=get_y(),0,10)==1)
{
}
wmove(hidwin,y,x);
c=winch(hidwin);
if(c!=' ')
{
hit=YES;
c='+';
}
else
{
c='-';
}
wmove(showwin,y,x);
d=winch(showwin);
if(hit==YES&&d==' ')/* first hit - not repeat */
{
flash();
myscore++;
}
wattron(showwin,A_REVERSE,A_BOLD);
waddch(showwin,c);
touchwin(showwin);
wrefresh(showwin);
}
/***************************************************************************/
/* clear top of screen */
clrtop()
{
WINDOW *topwin;
int i, c;
char ch;
topwin = newwin(5, COLS, 0, 0);/*top 5 lines*/
werase(topwin);
touchwin(topwin);
wrefresh(topwin);
}
/*********************************************************************/
/*computer attack player - goes to attack coordinates on recwin*/
/* checks recwin - blank means haven't attacked here yet */
/* if so go to coord on mywin - attack & record result on recwin */
hisattack(mywin,recwin)
WINDOW *mywin;
WINDOW *recwin;
{
int y , x;
char c;
char mark='-'; /*miss*/
clrtop();
while(1)
{
if(nextshot(recwin)==YES)
{
x=nextx;
y=nexty;
}
else if(nextshot(recwin)==NO)
{
x=random(11);
y=random(11);
}
wmove(recwin,y,x);/*check for repeat move*/
c=winch(recwin);
if(c!=' ')/*repeat move */
{
continue;
}
else
{
wmove(mywin,y,x);
c=winch(mywin);
if(c!='-'&&c!=' ')/*hit*/
{
flash();
mark='+';/*mark recwin with hit*/
hisscore++;
}
wattron(mywin,A_REVERSE,A_BOLD);
waddch(mywin,mark);
touchwin(mywin);
wrefresh(mywin);
waddch(recwin,mark);
/*mark square as tried already and result - for blank or + for hit */
touchwin(recwin);
return(0);
}
}
}
/***********************************************************************/
err(str)
char *str;
{
WINDOW *errwin;
errwin=newwin(1,COLS,23,0);
werase(errwin);
wattron(errwin,A_REVERSE,A_BOLD);
wprintw(errwin,"%s",str);
touchwin(stdscr);
wrefresh(errwin);
return(0);
}
/***********************************************************************/
out(c)
char c;
{
if(c=='q')
{
endwin();
exit(1);
}
}
/*********************************************************************/
myrecord(win)
WINDOW *win;
{
werase(win);
wprintw(win,"hit %d ",myscore);
wprintw(win,"move %d\n",mymoves++);
touchwin(win);
wrefresh(win);
}
/*********************************************************************/
hisrecord(win)
WINDOW *win;
{
werase(win);
wprintw(win,"hit %d ",hisscore);
wprintw(win,"move %d\n",hismoves++);
touchwin(win);
wrefresh(win);
}
/************************************************************************/
/* check win for previous hit and choose next guess at adjacent square */
nextshot(win)
WINDOW *win;
{
int y;
int x;
int ax;
int by;
char c;
for(by=1;by