Skip to content

Commit

Permalink
Added main, functions, and header files
Browse files Browse the repository at this point in the history
  • Loading branch information
tomHep10 authored Dec 6, 2023
0 parents commit 635c041
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 0 deletions.
67 changes: 67 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <iostream>
#include <vector>
#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<char> input = {'_', '_', '_', '_', '_', '_', '_', '_', '_'};
vector<int> 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;
}
77 changes: 77 additions & 0 deletions ttt_functions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <iostream>
#include <vector>
#include "ttt_functions.hpp"
//
using namespace std;

void game_board(vector<char> 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<char> 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<int> 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;
}
12 changes: 12 additions & 0 deletions ttt_functions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef ttt_functions_hpp
#define ttt_functions_hpp
#include <vector>
#include <stdio.h>

#endif /* ttt_functions_hpp */
using namespace std;


void game_board(vector<char> input);
int result(vector<char> input);
int input_list(int p_input = 0, int p_two_input = 0, vector<int> inputs = {});

0 comments on commit 635c041

Please sign in to comment.