← Back to ratslair.com
aboutsummaryrefslogtreecommitdiff
path: root/nships.c
blob: 7084bc780af93cdc44882e2b94a47619d1f2ad50 (plain)
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include <ncurses.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>

#include "server.c"

#define height 	80
#define width	200

struct p { int x;
	int y;
};

struct p rP() {
	struct p p;
	p.x = (rand() % (width - 2)) + 1;
	if (p.x%2 == 0) {
	} else {
		p.x++;
	}
	p.y = (rand() % (height - 2)) + 1;
	return p;
}

struct player {
	struct p p;
	int ships;
	char name[1024];
};

struct items {
        int len;
        char items[3][15];
};

struct ship {
	struct p p;
	int dir;
	int len;
	int live;
};

int menu(int y, int x, int h, int w, int aLen, char it[][15], char *title)
{
        int key, i, c = 0, o = 0;
        WINDOW * menu = newwin(h, w, y, x);
        refresh();
        box(menu, 0, 0);
	if(title != 0)
		mvwprintw(menu, 0, 1, "%s", title);
        while(o == 0)
        {
                for(i = 0; i < aLen; ++i) {
                        if(i == c) {
                                wattron(menu, A_STANDOUT);
                                mvwprintw(menu, i + 1, 1, "%s", it[i]);
                                wattroff(menu, A_STANDOUT);
                        } else {
                                mvwprintw(menu, i + 1, 1, "%s", it[i]);
                        }
                }
                switch(key = wgetch(menu))
                {
                        case 119:
                                if(c > 0)
                                        --c;
                                break;
                        case 115:
                                if(c < aLen)
                                        ++c;
                                break;
                        case 10:
                                o = 1;
                                break;
                        case 27:
                                o = 2;
                                break;
                }
                wrefresh(menu);
        }
        delwin(menu);
	clear();
        if(o == 1)
                return c;
        else
                return -1;
}

void getstrwin(char * ip, char *title)
{
	char str[80];
	echo();
	
	WINDOW * getip = newwin(3, 20, (LINES - 3) / 2, (COLS - 20) / 2);
	box(getip, 0, 0);
	refresh();
	mvwprintw(getip, 0, 1, title);
	wmove(getip, 1, 1);
	wgetnstr(getip, str, 18);
	strncpy(ip, str, 128);
	delwin(getip);
	noecho();
}

struct p getPlace(struct p p, WINDOW * w)
{
	p.x = 1;
	p.y = 1;
	int e = 0, wh, ww;
	curs_set(1);
	getmaxyx(w, wh, ww);
	wmove(w, p.y, p.x);
	wrefresh(w);
	for(;;)
	{
		switch(getch())
		{
			case 119: // up
				if(p.y > 1)
					--p.y;
				break;
			case 115: // down
				if(p.y < wh - 2)
					++p.y;
				break;
			case 97: // left
				if(p.x > 1)
					--p.x;
				break;
			case 100: // right
				if(p.x < ww - 2)
					++p.x;
				break;
			case 10:
				e = 1;
				break;
		}
		wmove(w, p.y, p.x);
		wrefresh(w);
		if(e == 1)
			break;
	}
	curs_set(0);
	return p;
}

struct ship setPlace(WINDOW * w, int len)
{
	struct ship s = {{1, 1}, 0, len, 1};
	int e = 0, wh, ww;
	char character[64];
	curs_set(0);
	getmaxyx(w, wh, ww);
	for(;;)
	{
		switch(getch())
		{
			case 119:
				if(s.p.y > 1)
					s.p.y--;
				break;
			case 115:
				if(s.dir == 0 && s.p.y < wh - 2)
					s.p.y++;
				else if(s.dir == 1 && s.p.y + 1 < wh - s.len)
					s.p.y++;
				break;
			case 97:
				if(s.p.x > 1)
					s.p.x-=2;
				break;
			case 100:
				if(s.dir == 0 && s.p.x < ww - s.len - 5)
					s.p.x+=2;
				else if(s.dir == 1 && s.p.x < ww - 2)
					s.p.x+=2;
				break;
			case 114:
				if(s.dir == 0) {
					s.dir = 1;
					if(s.p.y + s.len > wh)
						s.p.y = s.p.y - (s.p.y + s.len - wh) - 1;
				}
				else {
					s.dir = 0;
					if(s.p.x + s.len > ww - 5)
						s.p.x = s.p.x - (s.p.x + s.len - ww) - ;
				}
				break;
			case 10:
				e = 1;
				break;
		}
		wclear(w);
		box(w, 0, 0);
		mvwprintw(w, 0, 0, "%i, %i", s.p.x, ww - s.len - 2);
		for(int l=0;l<s.len;++l)
		{
			if(s.dir == 0) {
				if(l == 0)
					strcpy(character, "<");
				else if(l == s.len - 1)
					strcpy(character, ">");
				else
					strcpy(character, "-");
				mvwprintw(w, s.p.y, s.p.x + (l * 2), "%s", character);
			}
			else {
				if(l == 0)
					strcpy(character, "^");
				else if(l == s.len - 1)
					strcpy(character, "v");
				else
					strcpy(character, "|");
				mvwprintw(w, s.p.y + l, s.p.x, "%s", character);
			}
		}
		wrefresh(w);
		if(e == 1)
			break;
	}
}

int main() 
{
	struct player p1, p2;
	struct p p1p[256], p2p[256];
	struct items start = {3, {"Host Game", "Join Game", "Exit"}};
	struct ship p1s[5], p2s[5];
	int state = 3, e = 0, sfd, round = 0, host, p1i = 0, p2i = 0, phase = 0, l;
	char ip[128];

	initscr();
	curs_set(0);
	noecho();
	WINDOW * p1w = newwin(12, 22, 1, 0);
	WINDOW * p2w = newwin(12, 22, 1, 22);
	refresh();
	while(e == 0)
	{
		switch(state)
		{
			case 0:	//Start menu
				state = menu((LINES - 5) / 2, (COLS - 15) / 2, 5, 15, start.len, start.items, "nships") + 1;
				if(state == 3)
					state = 4;
				break;
			case 1:	//Host menu
				clear();
				printw("Waiting for client to connect");
				refresh();
				sfd = hostGame();
				clear();
				if(sfd == 0) {
					printw("Failed to host");
					state = 0;
				}
				else {
					host = 1;
					getstrwin(p1.name, "Enter your name");
					clear();
					printw("Waiting for response from client");
					refresh();
					send(sfd, p1.name, sizeof(p1.name), 0);
					recv(sfd, p2.name, sizeof(p2.name), 0);
					state = 3;
				}
				break;
			case 2:	//Client menu
				getstrwin(ip, "Enter IP");
				sfd = joinGame(ip);
				clear();
				if(sfd == 0) {
					printw("Failed to connect");
					state = 0;
				}
				else {
					host = 0;
					getstrwin(p2.name, "Enter your name");
					clear();
					printw("Waiting for response from host");
					refresh();
					recv(sfd, p1.name, sizeof(p1.name), 0);
					send(sfd, p2.name, sizeof(p2.name), 0);
					state = 3;
				}
				break;
			case 3: //Game loop
				clear();
				refresh();
				box(p1w, 0, 0);
				box(p2w, 0, 0);
				mvwprintw(p1w, 0, 1, p1.name);
				mvwprintw(p2w, 0, 1, p2.name);
				if(host == 0)
					wmove(p1w, 0, 0);
				else
					wmove(p2w, 0, 0);
				wrefresh(p1w);
				wrefresh(p2w);
				switch(phase)
				{
					case 0: // Choose ship locations
						for(int s=0;s<6;++s)
						{
							if(s < 3)
								l = 3;
							else if(s < 4)
								l = 4;
							else
								l = 5;
							switch(host)
							{
								case 0:
									setPlace(p2w, l);
									break;
								case 1:
									setPlace(p1w, l);
									break;
							}
						}
						phase = 1;
						break;
					case 1: // Play the game
						switch(round)
						{
							case 0:
								if(host == 0) {
									p2p[p2i] = getPlace(p2.p, p2w);
									//send(sfd, p2p[p2i], sizeof(p2p[p2i]), 0);
								}
								else {
									printw("%s's turn", p2.name);
									//recv(sfd, p2p[p2i], sizeof(p2p[p2i]), 0);
								}
								++p2i;
								round = 1;
								break;
							case 1:
								if(host == 1) {
									p1p[p1i] = getPlace(p1.p, p1w);
									//send(sfd, p1p[p1i], sizeof(p1p[p1i]), 0);
								}
								else {
									printw("%s's turn", p1.name);
									//recv(sfd, p1p[p1i], sizeof(p1p[p1i]), 0);
								}
								++p1i;
								round = 0;
								break;
						}
						break;
				}
				break;
			case 4:	//Exit state
				e = 1;
				break;
		}
		refresh();
	}
	close(sfd);
	endwin();
	return 0;
}