← Back to ratslair.com
aboutsummaryrefslogtreecommitdiff
path: root/nships.c
diff options
context:
space:
mode:
Diffstat (limited to 'nships.c')
-rw-r--r--nships.c85
1 files changed, 70 insertions, 15 deletions
diff --git a/nships.c b/nships.c
index f2957d6..667328d 100644
--- a/nships.c
+++ b/nships.c
@@ -1,6 +1,12 @@
#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"
@@ -74,42 +80,91 @@ int menu(int y, int x, int h, int w, int aLen, char it[][15], char *title)
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, 13);
+ strncpy(ip, str, 128);
+ delwin(getip);
+ noecho();
+}
+
int main()
{
- struct player p1, p2, hostID;
+ struct player p1, p2;
struct items start = {3, {"Host Game", "Join Game", "Exit"}};
- int state = 0;
- char title[15] = "nships";
- bool exit = false;
+ 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();
- while(!exit)
+ WINDOW * g = newwin(100, 200, (LINES - 100) / 2, (COLS - 200) / 2);
+ refresh();
+ while(e == 0)
{
switch(state)
{
case 0:
- state = menu((LINES - 15) / 2, (COLS - 10) / 2, 5, 15, start.len, start.items, title);
- if(state == 0)
- state = 1;
- break;
- if(state == 1)
- state = 2;
- break;
- if(state == 2)
- exit = true;
+ state = menu((LINES - 5) / 2, (COLS - 15) / 2, 5, 15, start.len, start.items, title) + 1;
+ if(state == 3)
+ state = 4;
break;
case 1:
- hostID = fork()
+ 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");
+ state = 4;
+ }
break;
case 2:
+ 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");
+ state = 4;
+ }
+ break;
+ case 3:
+ state = 4;
+ break;
+ case 4:
+ e = 1;
break;
}
+ refresh();
}
+ if(sfd != 0)
+ close(sfd);
+
+ endwin();
+ return 0;
}