Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10 5/3/83 based; site hounx.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!mhuxt!houxm!hounx!juda From: juda@hounx.UUCP (#J.KAMINETSKY) Newsgroups: net.sources Subject: battleships Message-ID: <349@hounx.UUCP> Date: Mon, 17-Jun-85 14:19:12 EDT Article-I.D.: hounx.349 Posted: Mon Jun 17 14:19:12 1985 Date-Received: Tue, 18-Jun-85 08:00:22 EDT Organization: AT&T Bell Labs, Holmdel NJ Lines: 613 /* 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 - 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); label_x(mywin); wrefresh(mywin); label_y(mywin); wrefresh(mywin); wattroff(mywin,A_REVERSE); wattron(recwin,A_REVERSE); label_x(recwin); label_y(recwin); wattroff(recwin,A_REVERSE); 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, 55); showwin = newwin(MAX_Y, MAX_X, 5, 55); myscrwin = newwin(4, MAX_X+3, 17, 55); hisscrwin = newwin(4, MAX_X+3, 17, 15); werase(hidwin); werase(showwin); wattron(hidwin,A_REVERSE); label_x(hidwin); wrefresh(hidwin); label_y(hidwin); wrefresh(hidwin); wattroff(hidwin,A_REVERSE); wattron(showwin,A_REVERSE); label_x(showwin); label_y(showwin); wattroff(showwin,A_REVERSE); 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!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); endwin(); exit(1); } hisattack(mywin,recwin); hisrecord(hisscrwin); if(hisscore>16) { msg("YOU LOSE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); 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(1); } else { return(0); } } /*********************************************************************/ /*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; } wmove(showwin,y,x); d=winch(showwin); if(hit==YES&&d==' ')/* first hit - not repeat */ { flash(); myscore++; } wattron(showwin,A_REVERSE); 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='+'; 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!=' ')/*hit*/ { flash(); mark=c;/*mark recwin with ship character*/ hisscore++; } wattron(mywin,A_REVERSE); waddch(mywin,c); touchwin(mywin); wrefresh(mywin); waddch(recwin,mark); /*mark square as tried already and result + for blank or char for ship */ touchwin(recwin); return(0); } } } /***********************************************************************/ err(str) char *str; { WINDOW *errwin; errwin=newwin(1,COLS,23,0); werase(errwin); wattron(errwin,A_REVERSE); 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