From 635c041dc490ded8ebac89acce2a49417be6a6e9 Mon Sep 17 00:00:00 2001 From: code_slayer3000 <80072201+Heeps10@users.noreply.github.com> Date: Tue, 5 Dec 2023 22:53:14 -0500 Subject: [PATCH] Added main, functions, and header files --- main.cpp | 67 +++++++++++++++++++++++++++++++++++++++++ ttt_functions.cpp | 77 +++++++++++++++++++++++++++++++++++++++++++++++ ttt_functions.hpp | 12 ++++++++ 3 files changed, 156 insertions(+) create mode 100644 main.cpp create mode 100644 ttt_functions.cpp create mode 100644 ttt_functions.hpp diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..d414d91 --- /dev/null +++ b/main.cpp @@ -0,0 +1,67 @@ +#include +#include +#include "ttt_functions.hpp" +using namespace std; +/* below while set inputs = to inputs_list +and change argument and parameters to accept inputs*/ +int main() { + vector input = {'_', '_', '_', '_', '_', '_', '_', '_', '_'}; + vector inputs; + int p_input; + int p_two_input; + + game_board(input); + + cout << "Input a number from 1-9 with numbers on the box going from left to right order.\n"; + + for(int i = 0;;){ + cout << "Player 1 which box do you select?\n"; + cin >> p_input; + + //check if p_input has already in input list + while(input_list(p_input, 0, inputs) == 0){ + cout << "You cannot choose that option as it has already been chosen. Choose again: \n"; + cin >> p_two_input; + } + inputs.push_back(p_input); + + input[p_input-1] = 'x'; + game_board(input); + i++; + + //result check + if(result(input) == 1){ + cout << "Player 1 wins!\n"; + break; + } + + //player 2 + if(i>8){ + break; + } + + + cout << "Player 2 which box do you select?\n"; + cin >> p_two_input; + + //check if p_two_input is already in list + while(input_list(0, p_two_input, inputs) == 0){ + cout << "You cannot choose that option as it has already been chosen. Choose again: \n"; + cin >> p_two_input; + } + inputs.push_back(p_two_input); + + input[p_two_input-1] = 'o'; + game_board(input); + i++; + + //result check + if(result(input) == 1){ + cout << "Player 2 wins!\n"; + break; + } + } + + + return 0; +} diff --git a/ttt_functions.cpp b/ttt_functions.cpp new file mode 100644 index 0000000..cb6293b --- /dev/null +++ b/ttt_functions.cpp @@ -0,0 +1,77 @@ +#include +#include +#include "ttt_functions.hpp" +//─ +using namespace std; + +void game_board(vector input){ + cout << " " << input[0] << " | " << input[1] << " | " << input[2] << endl; + cout << "───────────\n"; + cout << " " << input[3] << " | " << input[4] << " | " << input[5] << endl;; + cout << "───────────\n"; + cout << " " << input[6] << " | " << input[7] << " | " << input[8] << "\n\n"; +} +int result(vector input){ + // logic for who wins game + + if(input[0] == 'x' && input[1] == 'x' && input[2] == 'x'){ + return 1; + } else if (input[3] == 'x' && input[4] == 'x' && input[5] == 'x'){ + return 1; + } else if (input[6] == 'x' && input[7] == 'x' && input[8] == 'x'){ + return 1; + } else if (input[0] == 'x' && input[3] == 'x' && input[6] == 'x'){ + return 1; + } else if (input[1] == 'x' && input[4] == 'x' && input[7] == 'x'){ + return 1; + } else if (input[2] == 'x' && input[5] == 'x' && input[8] == 'x'){ + return 1; + } else if (input[0] == 'x' && input[4] == 'x' && input[8] == 'x'){ + return 1; + } else if (input[2] == 'x' && input[4] == 'x' && input[6] == 'x'){ + return 1; + } + + if(input[0] == 'o' && input[1] == 'o' && input[2] == 'o'){ + return 1; + } else if (input[3] == 'o' && input[4] == 'o' && input[5] == 'o'){ + return 1; + } else if (input[6] == 'o' && input[7] == 'o' && input[8] == 'o'){ + return 1; + } else if (input[0] == 'o' && input[3] == 'o' && input[6] == 'o'){ + return 1; + } else if (input[1] == 'o' && input[4] == 'o' && input[7] == 'o'){ + return 1; + } else if (input[2] == 'o' && input[5] == 'o' && input[8] == 'o'){ + return 1; + } else if (input[0] == 'o' && input[4] == 'o' && input[8] == 'o'){ + return 1; + } else if (input[2] == 'o' && input[4] == 'o' && input[6] == 'o'){ + return 1; + } + + return 0; +} + + +int input_list(int p_input, int p_two_input, vector inputs){ + + if(inputs.size() == 0){ + return p_input; + } + + for(int i = 0; i < inputs.size(); i++){ + if(p_input == inputs[i] || p_two_input == inputs[i]){ + return 0; + } + } + + if(p_input == 0){ + return p_two_input; + } else{ + return p_input; + } + + + return 1; +} diff --git a/ttt_functions.hpp b/ttt_functions.hpp new file mode 100644 index 0000000..1ef0747 --- /dev/null +++ b/ttt_functions.hpp @@ -0,0 +1,12 @@ +#ifndef ttt_functions_hpp +#define ttt_functions_hpp +#include +#include + +#endif /* ttt_functions_hpp */ +using namespace std; + + +void game_board(vector input); +int result(vector input); +int input_list(int p_input = 0, int p_two_input = 0, vector inputs = {});