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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> matrix_options;
vector<int> box_options;
int matrix[3][3];
// still need to implement which player made the turn.
// x for player 1
// o for player 2
// 1x | 2o | 3
// [0][0] | [0][1] | [0][2]
//________|_________|_______
// 4 | 5 | 6
// [1][0] | [1][1] | [1][2]
//________|_________|_______
// 7 | 8 | 9
// [2][0] | [2][1] | [2][2]
// | |
void display_matrix()
{
cout << endl;
cout << " " << matrix[0][0] << " | " << matrix[0][1] << " | " << matrix[0][2] << endl;
cout << "------------" << endl;
cout << " " << matrix[1][0] << " | " << matrix[1][1] << " | " << matrix[1][2] << endl;
cout << "------------" << endl;
cout << " " << matrix[2][0] << " | " << matrix[2][1] << " | " << matrix[2][2] << endl;
}
void populate_selection()
{
for(int i=1; i < 10; i++)
{
box_options.push_back(i);
matrix_options.push_back(i);
}
}
void display_options(vector<int>& storage)
{
// display list of available options
for(int i=0; i < storage.size(); i++)
{
cout << storage[i] << " ";
}
cout << endl;
}
// first argument is passed by reference
// second argument is passed by value
void remove_option(vector<int>& storage, int value)
{
// removes a value from storage
auto it = std::find(storage.begin(), storage.end(), value);
if(it != storage.end())
{
storage.erase(it);
}
}
void fill_matrix(int position, int value)
{
switch(position)
{
case 1:
{
matrix[0][0] = value;
}
break;
case 2:
{
matrix[0][1] = value;
}
break;
case 3:
{
matrix[0][2] = value;
}
break;
case 4:
{
matrix[1][0] = value;
}
break;
case 5:
{
matrix[1][1] = value;
}
break;
case 6:
{
matrix[1][2] = value;
}
break;
case 7:
{
matrix[2][0] = value;
}
break;
case 8:
{
matrix[2][1] = value;
}
break;
case 9:
{
matrix[2][2] = value;
}
break;
}
system("cls");
display_matrix();
}
int main(int argc, char *argv[])
{
populate_selection() ;
cout << "Welcome to tic-tac-math" << endl;
cout << "The goal of this game is to get the sum of 15 in either a row, column, or diagonal." << endl;
cout << " You can only use numbers between 1 and 9" << endl;
bool game_active = true;
int x = 0;
int y = 0;
// start of game loop
while(game_active)
{
cout << "remaining options for matrix" << endl;
display_options(matrix_options);
cout << "Enter Matrix position: ";
while(!(cin >> x) || (x > 9) || (x < 1))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Try again: ";
}
if ( std::find(matrix_options.begin(), matrix_options.end(), x) != matrix_options.end() )
{
cout << "found " << x << endl;
remove_option(matrix_options,x);
cout << "remaining options for matrix" << endl;
display_options(matrix_options);
cout << "remaining options for box" << endl;
display_options(box_options);
cout << "Enter Box value: ";
while(!(cin >> y) || (y > 9) || (y < 1))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Try again: ";
}
if ( std::find(box_options.begin(), box_options.end(), y) != box_options.end() )
{
cout << "found " << y << endl;
remove_option(box_options,y);
cout << "remaining options for box" << endl;
display_options(box_options);
}
else
{
cout << "invalid selection " << y << endl;
display_options(box_options);
}
}
else
{
cout << "invalid selection " << x << endl;
display_options(matrix_options);
}
fill_matrix(x,y);
}
}
|