From bae11647e4d62031bd4c4023b748df0dbbb59f7c Mon Sep 17 00:00:00 2001 From: PoliEcho Date: Tue, 28 May 2024 17:03:07 +0200 Subject: [PATCH] refactoring --- Makefile | 20 +++++++++----- net.cpp | 0 src/global.hpp | 2 ++ src/input.cpp | 55 +++++++++++++++++++++++++++++++++++++++ src/input.hpp | 2 ++ main.cpp => src/main.cpp | 56 ++++++++++------------------------------ src/net.cpp | 9 +++++++ src/net.hpp | 1 + 8 files changed, 96 insertions(+), 49 deletions(-) delete mode 100644 net.cpp create mode 100644 src/global.hpp create mode 100644 src/input.cpp create mode 100644 src/input.hpp rename main.cpp => src/main.cpp (87%) create mode 100644 src/net.cpp create mode 100644 src/net.hpp diff --git a/Makefile b/Makefile index 2efceea..5f33b1a 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,17 @@ -all: main.o net.o - g++ main.o net.o -o over_the_network_tic_tac_toe -main.o: main.cpp - g++ -c main.cpp +all: build/obj/main.o build/obj/net.o build/obj/input.o + g++ build/obj/main.o build/obj/net.o build/obj/input.o -o build/bin/over_the_network_tic_tac_toe -net.o: net.cpp - g++ -c net.cpp \ No newline at end of file +build/obj/main.o: src/main.cpp + g++ -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 + +build/obj/input.o: src/input.cpp + g++ -c src/input.cpp -o build/obj/input.o + +clean: + rm build/obj/* + rm build/bin/* \ No newline at end of file diff --git a/net.cpp b/net.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/global.hpp b/src/global.hpp new file mode 100644 index 0000000..d7d4be9 --- /dev/null +++ b/src/global.hpp @@ -0,0 +1,2 @@ +extern int board_size; +extern bool ishost; \ No newline at end of file diff --git a/src/input.cpp b/src/input.cpp new file mode 100644 index 0000000..f15b1c4 --- /dev/null +++ b/src/input.cpp @@ -0,0 +1,55 @@ +#include +#include"global.hpp" +#include"input.hpp" + +using std::cout; +using std::cin; + +int board_size; +bool ishost; +bool repeat = true; +void ishost_check() { + +char charishost; +while (repeat) { + cout << "will you be host or guest of this game?\n" << "(h/g): "; + cin >> charishost; + + repeat = false; + + switch(charishost) { + case 'h': + ishost = true; + break; + case 'g': + ishost = false; + break; + default: + repeat = true; + } + +} +} +void board_size_check() { +repeat = true; + +while (repeat) { +cout << "\nplease enter board size(for example: 3 for 3x3): "; + +cin >> board_size; +if(cin.fail()) { + cout << "integer please\n"; + cin.clear(); + cin.ignore(256,'\n'); + } else { + if(board_size > 0) { + repeat = false; + } else { + cout << "positive integer please\n"; + } + + + +} +} +} \ No newline at end of file diff --git a/src/input.hpp b/src/input.hpp new file mode 100644 index 0000000..4bcc794 --- /dev/null +++ b/src/input.hpp @@ -0,0 +1,2 @@ +void ishost_check(); +void board_size_check(); \ No newline at end of file diff --git a/main.cpp b/src/main.cpp similarity index 87% rename from main.cpp rename to src/main.cpp index 304d19e..b7182fc 100644 --- a/main.cpp +++ b/src/main.cpp @@ -1,4 +1,8 @@ #include +#include"global.hpp" +#include"net.hpp" +#include"input.hpp" + using std::cout; using std::cin; @@ -34,54 +38,20 @@ cout << R"( ___ __ _______ ___ ________ ________ _____ ______ \|__| \|__|\|_______| \|__| \|__|\|__|\|_______| \|__| \|_______|\|_______| )" << '\n'; - -bool ishost; -bool repeat = true; int board_size; + +ishost_check(); +board_size_check(); + { -char charishost; -while (repeat) { - cout << "will you be host or guest of this game?\n" << "(h/g): "; - cin >> charishost; - - repeat = false; - - switch(charishost) { - case 'h': - ishost = true; - break; - case 'g': - ishost = false; - break; - default: - repeat = true; - } - -} - -repeat = true; - -while (repeat) { -cout << "\nplease enter board size(for example: 3 for 3x3): "; - -cin >> board_size; -if(cin.fail()) { - cout << "integer please\n"; - cin.clear(); - cin.ignore(256,'\n'); - } else { - if(board_size > 0) { - repeat = false; - } else { - cout << "positive integer please\n"; - } - - - -} +int init_net_status = init_net(); +if(init_net_status != 0){ + cout << "failed to initialize network\n"; + return 2; } } + char board[board_size*board_size]; return 0; diff --git a/src/net.cpp b/src/net.cpp new file mode 100644 index 0000000..5ad834f --- /dev/null +++ b/src/net.cpp @@ -0,0 +1,9 @@ +#include"net.hpp" + + +int init_net() { + + return 0; +} + + diff --git a/src/net.hpp b/src/net.hpp new file mode 100644 index 0000000..cdc0e42 --- /dev/null +++ b/src/net.hpp @@ -0,0 +1 @@ +extern int init_net(); \ No newline at end of file