-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattleship.c
257 lines (248 loc) · 6.95 KB
/
battleship.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
///program v1.0
//global variables
signed int rndnum(int a , int b); // random number function
int map(int a); // draw map routine
int pinput(void); // get coordinates for player input
int nogo(int x, int y, int p); // check place on map for object. Returns error=1 if space is "no-go"
char ply[11][11]; // player map
char cpu[11][11]; // cpu placement map
char gam[11][11]; // player guessing at cpu map
int shotx, shoty, error; // variables for game play and shot making
char ship[][10] = {
"","","ìÍ","ö
Í","mÍ","qóêÍ","" //names of ships
};
int main (void)
{
//initialise
int i,ii,turn,x1,y1,x2,y2;// i,ii (counters), turn (player or computer), x1/2,y1/2 (assist placing/cpu a.i)
int plcx,plcy; //placement registers
char input;
int pscore, cscore; //player/computer score
int cpuai, firstx, firsty; //cpu artificial intelligence registers
signed int cpux, cpuy;
srand(time(NULL));//randomize from clock
//title
printf("BATTLESHIPS! íDQ[I by ub`[B\n");
printf("±ÌQ[ÌÚIÍ·×ÄÌCPUÌíDð¾ßéI\n");
printf("»Ì½ßACPUÌíDÌÊuð¢¢ÄÄÝĵܵå¤I\n");
printf("Cð¯ÄBBBCPUÍ È½ÌíDð_¢Ü·BBBB\n\n");
printf("ÅÉA©ªÌDðu«Üµå¤B\n");
//place ships
for(i=5;i>1;i--) {
do {
map(0);
for(ii=0; ship[i][ii];ii++) {
printf("%c", ship[i][ii]);
}
printf("ðDZÉu«½¢Å·©H");
printf("%dubNðgpµÜ·B", i);
pinput();
x1=shotx;
y1=shoty;
do {
printf("\nã©çºÜÅAciv)A¡ih)AEÎßir)A¶Îßil)H :");
rewind(stdin);
scanf("%c", &input);
plcx=0, plcy=0;
if(input=='v') plcx=0, plcy=1;
else if(input=='h') plcx=1, plcy=0;
else if(input=='r') plcx=1, plcy=1;
else if(input=='l') plcx=-1, plcy=1;
} while((plcx==0)&&(plcy==0));
shotx=x1, shoty=y1, error=0;
for (ii=0;ii<i;ii++){
nogo(shoty,shotx,0);
shotx=shotx+plcx;
shoty=shoty+plcy;
}
if(error==0) {
shotx=x1, shoty=y1;
for (ii=0;ii<i;ii++) {
ply[shoty][shotx]='#';
shotx=shotx+plcx;
shoty=shoty+plcy;
}
}
if(error==1) {
printf("\n***»±Í¾ß¾æI***\n");
}
} while(error==1);
}
map(0);
// cpu places ships
printf("\nXÜ¿¾³¢BBBÍ©ªÌDðu«Ü·B\n");
for(i=5;i>1;i--) {
do {
x1=rndnum(9,0);
y1=rndnum(9,0);
input=rndnum(4,1);
plcx=0, plcy=0;
if(input==1) plcx=0, plcy=1;
else if(input==2) plcx=1, plcy=0;
else if(input==3) plcx=1, plcy=1;
else if(input==4) plcx=-1, plcy=1;
shotx=x1, shoty=y1, error=0;
for (ii=0;ii<i;ii++){
nogo(shoty,shotx,2);
shotx=shotx+plcx;
shoty=shoty+plcy;
}
if(error==0) {
shotx=x1, shoty=y1;
for (ii=0;ii<i;ii++) {
cpu[shoty][shotx]='#';
shotx=shotx+plcx;
shoty=shoty+plcy;
}
}
} while(error==1);
}
// who starts?
printf("ÅÍNÅ·©HvC[(p)©Rs
[^(c)? :");
rewind(stdin);
scanf("%c", &input);
if(input=='c') turn=1;
else turn=0;
// start game
pscore=0, cscore=0, cpuai=0, firsty=0, firstx=0;
do {
//player plays
if(turn==0) {
printf(" ȽÌÔÅ·B\n");
do {
map(1);
pinput();
error=0;
nogo(shoty,shotx,1);
if(error==1) printf("***»±Í¾ßÅ·I***\n");
} while(error==1);
if(cpu[shoty][shotx]=='#') {
gam[shoty][shotx]='*';
printf("ĽI*****oK°!*****\n");
pscore++;
}
else {
gam[shoty][shotx]='.';
printf("~XII\n");
}
map(1);
}
//cpu plays
else if(turn==1) {
printf("CPUÌÔÅ·B\n");
do {
if(cpuai<=0) {
shotx=rndnum(9,0); // CPUAI = 0; Computer has no idea, so makes a random guess
shoty=rndnum(9,0);
}
if((cpuai>0)&&(cpuai<=90)) {
cpuai--; // CPUAI >0 &< 90. 8 direction search around hit for more boat
input=rndnum(1,-1); // (90 chances to find free 8 direction spot before giving up)
shotx=(x1+input);
input=rndnum(1,-1);
shoty=(y1+input);
}
if(cpuai>=100) {
shotx=(x1+cpux); // CPUAI=100 sets about destoying line
shoty=(y1+cpuy); // CPUAI=200 destroys other side of boat
}
error=0;
nogo(shoty,shotx,3);
if((error==1)&&(cpuai==200)) cpuai=0;
if((error==1)&&(cpuai==100)) cpuai=200, x1=(firstx), y1=(firsty), cpux=-cpux, cpuy=-cpuy;
} while(error==1);
printf("Íi%c , %c)ÉUµÜ·B\n",(shoty+97),(shotx+97));
if(ply[shoty][shotx]=='#') {
ply[shoty][shotx]='*';
printf("ĽI*****oK°!*****\n"); // a hit!
cscore++;
if((cpuai>0)&&(cpuai<100)) {
x2=shotx, y2=shoty; // set up computer a.i. to destroy boat line
cpux=(x2-x1), cpuy=(y2-y1);
if((cpux>=-1)&&(cpux<=1)&&(cpuy>=-1)&&(cpuy<=1)) {
cpuai=100;
}
}
if(cpuai==0) {
firstx=(shotx), firsty=(shoty); //first hit on boat. Taking note for a.i.
cpuai=90;
}
x1=shotx, y1=shoty;
}
else {
ply[shoty][shotx]='.';
printf("Í~XI\n"); // computer miss
if(cpuai==200) cpuai=0;
if(cpuai==100) {
cpuai=200; // set cpu a.i. to hunt down other side of boat cpuai=200
x1=(firstx), y1=(firsty), cpux=-cpux, cpuy=-cpuy;
}
}
map(0);
}
if(turn==0) turn=1;
else turn=0;
}while((pscore<14)&&(cscore<14));
printf("\nQ[ÍIíèܵ½B\n");
if(pscore==14) map(1), printf(" ȽÍÁ½II\n");
if(cscore==14) map(1), map(2), printf("CPUÍÁ½BBB\n");
return 0;
}
signed int rndnum(int a , int b)
{
int i;
i = rand()*123456789 / (RAND_MAX + 1); // Randomize further
return (rand() * (a+1-b) / (RAND_MAX + 1)) + b; // select number
}
int map(a)
{
int i, ii;
printf("\n*|a|b|c|d|e|f|g|h|i|j\n");
printf("-+-------------------\n");
for(i=0;i<10;i++){
printf("%C|", (97+i));
for(ii=0;ii<10;ii++){
if (a==0) printf("%C ",ply[i][ii]);
else if (a==2) printf("%C ",cpu[i][ii]);
else if (a==1) printf("%C ",gam[i][ii]);
}
printf("\n");
}
printf("\n");
return 0;
}
int pinput(void)
{
char z[2];
do {
rewind(stdin);
printf("\nA-J (s-ñ) R[fBl[gðü͵ĺ³¢:");
gets(z);
shoty=("%d", z[0])-97;
shotx=("%d", z[1])-97;
} while((shotx<0)||(shotx>9)||(shoty<0)||(shoty>9));
return 0;
}
int nogo(int y, int x, int p)
{
if((x<0)||(x>9)||(y<0)||(y>9)) {
error=1;
}
if(p==0) {
if(("%d",ply[y][x])!=0) error=1;
}
if(p==1) {
if(("%d",gam[y][x])!=0) error=1;
}
if(p==2) {
if(("%d",cpu[y][x])!=0) error=1;
}
if(p==3) {
if((ply[y][x]=='.')||(ply[y][x]=='*')) error=1;
}
return 0;
}