← Back to ratslair.com
aboutsummaryrefslogtreecommitdiff
path: root/nships.c
blob: 667328d8099dd4db07220832bb691ca748e7bc25 (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
#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 id;
	int ships;
	char name[15];
};

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, 13);
	strncpy(ip, str, 128);
	delwin(getip);
	noecho();
}

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, 200, (LINES - 100) / 2, (COLS - 200) / 2);
	refresh();
	while(e == 0)
	{
		switch(state)
		{
			case 0:
				state = menu((LINES - 5) / 2, (COLS - 15) / 2, 5, 15, start.len, start.items, title) + 1;
				if(state == 3)
					state = 4;
				break;
			case 1:
				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;
}