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
|
#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];
};
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();
}
void gameWin(int x, struct player p)
{
WINDOW * gWin = newwin(30, 45, 0, x);
refresh();
box(gWin, 0, 0);
mvwprintw(gWin, 0, 1, p.name);
wrefresh(gWin);
}
int main()
{
struct player p1, p2;
struct items start = {3, {"Host Game", "Join Game", "Exit"}};
int state = 0, e = 0, sfd, host;
char title[15] = "nships", nameTitle[20] = "Enter Name", ipTitle[20] = "Enter IP", ip[128];
initscr();
curs_set(0);
noecho();
WINDOW * g = newwin(100, 300, (LINES - 100) / 2, (COLS - 200) / 2);
refresh();
while(e == 0)
{
switch(state)
{
case 0: //Start menu
state = menu((LINES - 5) / 2, (COLS - 15) / 2, 5, 15, start.len, start.items, title) + 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, ipTitle);
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();
gameWin(45, p2);
gameWin(0, p1);
refresh();
getch();
state = 4;
break;
case 4: //Exit state
e = 1;
break;
}
refresh();
}
close(sfd);
endwin();
return 0;
}
|