fixed seg fault and addded basic board displaying

This commit is contained in:
PoliEcho 2024-06-04 16:44:55 +02:00
parent f81ac6ea19
commit 79fd1f6c7b
8 changed files with 68 additions and 24 deletions

View File

@ -1,22 +1,24 @@
CC = g++
CC_FLAGS =
all: build/bin/over_the_network_tic_tac_toe
build/bin/over_the_network_tic_tac_toe: build/obj/main.o build/obj/net.o build/obj/input.o build/obj/gameplay.o
g++ build/obj/main.o build/obj/net.o build/obj/input.o build/obj/gameplay.o -o build/bin/over_the_network_tic_tac_toe
$(CC) $(CC_FLAGS) build/obj/main.o build/obj/net.o build/obj/input.o build/obj/gameplay.o -o build/bin/over_the_network_tic_tac_toe
build/obj/main.o: src/main.cpp
mkdir -p build/obj
mkdir -p build/bin
g++ -c src/main.cpp -o build/obj/main.o
$(CC) $(CC_FLAGS) -c src/main.cpp -o build/obj/main.o
build/obj/net.o: src/net.cpp
g++ -c src/net.cpp -o build/obj/net.o
$(CC) $(CC_FLAGS) -c src/net.cpp -o build/obj/net.o
build/obj/input.o: src/input.cpp
g++ -c src/input.cpp -o build/obj/input.o
$(CC) $(CC_FLAGS) -c src/input.cpp -o build/obj/input.o
build/obj/gameplay.o: src/gameplay.cpp
g++ -c src/gameplay.cpp -o build/obj/gameplay.o
$(CC) $(CC_FLAGS) -c src/gameplay.cpp -o build/obj/gameplay.o
clean:
rm -fr build

View File

@ -0,0 +1,41 @@
#include <iostream>
#include "gameplay.hpp"
#include "global.hpp"
using std::cout;
using std::cin;
using std::cerr;
void printBoard(const std::vector<std::vector<char>>& board) {
cout << "\n";
for (int i = 0; i < board_size; i++) {
if(i != board_size-1) {
cout << i << "";
} else {
cout << i << "";
}
}
cout << "\n";
for (int y = 0; y < board_size; y++) {
cout << y << "";
for (int x = 0; x < board_size; x++) {
cout << board[x][y] << "";
}
cout << "\n";
}
cout << "";
for (int i = 0; i < board_size; i++) {
if(i != board_size-1) {
cout << "─┴";
} else {
cout << "─┘";
}
}
}

View File

@ -0,0 +1,3 @@
#include <vector>
void printBoard(const std::vector<std::vector<char>>& board);

View File

@ -1,5 +1,6 @@
#include <string>
extern int board_size;
extern bool ishost;
extern unsigned short port;
extern char *serverIp;
extern int length;
extern int length;
extern std::string serverIp_str;

View File

@ -10,6 +10,7 @@ int length;
int board_size;
bool ishost;
unsigned short port;
std::string serverIp_str;
bool repeat = true;
void ishost_check() {
@ -102,17 +103,15 @@ void port_check() {
void ip_check() {
repeat = true;
std::string serverIp_temp;
while(repeat) {
cout << "enter host ip or hostname: ";
cin >> serverIp_temp;
if(serverIp_temp == "") {
cin >> serverIp_str;
if(serverIp_str == "") {
cerr << "enter something!\n";
} else {
repeat = false;
const char *serverIp = serverIp_temp.c_str();
}
}
}

View File

@ -1,8 +1,10 @@
#include <cstddef>
#include<iostream>
#include <vector>
#include"global.hpp"
#include"net.hpp"
#include"input.hpp"
#include"gameplay.hpp"
using std::cout;
@ -40,7 +42,7 @@ int main() {
\|__| \|__|\|_______| \|__| \|__|\|__|\|_______| \|__| \|_______|\|_______|
)" << '\n';
int board_size;
ishost_check();
@ -62,18 +64,12 @@ int main() {
}
char board[board_size*board_size];
int ncord = 0;
for (int i = 0; i < sizeof(board); i++) {
board[ncord] = '_';
ncord++;
}
//alocate board
std::vector<std::vector<char>> board(board_size, std::vector<char>(board_size, '_'));
if(ishost) {
}
printBoard(board);
return 0;

View File

@ -22,6 +22,8 @@ using std::cin;
using std::cerr;
using std::string;
void handshake_failed() {
cerr << "handshake failed";
exit(3);
@ -107,8 +109,7 @@ int init_net() {
} else {
// code for client
char *serverIp;
const char *serverIp = serverIp_str.c_str();
//setup a socket and connection tools
struct hostent* host = gethostbyname(serverIp);
sockaddr_in sendSockAddr;

View File

@ -1 +1,2 @@
extern int init_net();
extern int init_net();
void init_var();