-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
109 lines (82 loc) · 2.94 KB
/
Main.cpp
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
/* Run command: g++ *.cpp -o snake.out && ./snake.out */
#include <iostream>
#include "utilities.h"
#include <cstring>
#include <unistd.h>
#include "Snake.h"
#include "map.h"
#include "input.h"
#include "network.h"
#include "networkTypes.h"
using namespace std;
bool devTesting = true;
bool running = true;
bool ready = false;
int server_port = 25565;
std::string server_ip = "minecraft.athaun.tech";
std::string buffer;
Snake snake;
Snake otherSnake;
Network network;
int testY = 0;
int main() {
/*
if user enters value 1, then they become a server. Else if, they enter a value of 2, then they become a client
*/
while (!ready) {
printf("Please select either server or client by typing either \"1\" or \"2\"\nType \"i\" to edit the IP address of the host or \"p\" to select the port\nCurrent address: %s:%d\n[1] Server\n[2] Client\n>>> ", server_ip.c_str(), server_port);
int character = getchar();
if (character == '1') {
printf("\nYou has now become a server");
network.isServer = true;
ready = true;
} else if (character == '2') {
printf("\nYou has now become a client");
network.isServer = false;
ready = true;
} else if (character == 'i') {
printf("\nInput the server IP address: ");
std::getline(std::cin, buffer); // Pipping the unwanted RETURN line to a dummy variable
std::getline(std::cin, server_ip);
cout << "IP address set to: " + server_ip << endl;
} else if (character == 'p') {
printf("\nInput the server port: ");
std::getline(std::cin, buffer); // Pipping the unwanted RETURN line to a dummy variable
std::getline(std::cin, buffer);
server_port = strtol(buffer.c_str(), NULL, 0); // Setting the input string to an int, then to server_port
cout << "Port set to: " + server_port << endl;
}
}
network.init();
seedRandom();
// Set input nonBlocking
int fd = fileno(stdin);
char buf[1];
bool done = false;
setNonBlocking(fd);
// Creating a random spawn with at least 5 tile buffer on every side
snake.initSnake(random(5, ARRAY_SIZE(map) - 5), random(5, ARRAY_SIZE(map) - 5));
otherSnake.initSnake(random(5, ARRAY_SIZE(map) - 5), random(5, ARRAY_SIZE(map) - 5));
// Game Loop
while (running) {
network.update();
keyInput();
// Clears the terminal
clearScreen();
// Creates the solid map of "." periods
initMap();
// Displays the snake's head
snake.display();
otherSnake.display();
printMap();
printStat();
// Sleep for 50 milliseconds
usleep(100 * 1000);
if (!snake.isAlive) {
printf("You died.");
running = false;
}
}
printf("\nThe game has closed.\n");
return 0;
}