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
|
//will add more to checkWin() once loop works
#include <iostream>
#include <string>
using namespace std;
void board();
int checkWin();
int findIndexI(int array[][3]);
int findIndexJ(int array[][3]);
void getUserInput();
int index_i_1 = 0;
int index_j_1 = 0;
int i = 1;
//create board array
char square[3][3] =
{
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
};
int counter = 0;
int player, gameState;
char mark;
int main()
{
while (gameState == 0) {
player = i % 2;
if (player == 0) {
mark = 'X';
} else {
mark = 'O';
}
board();
getUserInput();
i++;
gameState = checkWin();
}
if (gameState == 0) {
cout << "draw";
} else {
cout << "win";
}
return 0;
}
int findIndexI(char array[][3], int indexi)
{
int index_i = 0;
for (int j = 0; j < 3; j ++) {
for (int i = indexi; i < 3; i++) {
if (array[i][j] == 'X' | array[i][j] == 'O') {
index_i = i;
break;
}
}
}
return index_i;
}
int findIndexJ (char array[][3], int indexj)
{
int index_j = 0;
for (int i = 0; i < 3; i ++) {
for (int j = indexj; j < 3; j++) {
if (array[i][j] == 'X' | array[i][j] == 'O') {
index_j = j;
break;
}
}
}
return index_j;
}
void board()
{
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << square[i][j] << " " ;
if (j == 2) {
cout << endl;
}
}
}
}
int checkWin () {
index_i_1=findIndexI(square, 0);
index_j_1=findIndexJ(square, 0);
if (square[index_i_1][index_j_1] == square[index_i_1][index_j_1 + 1] && square[index_i_1][index_j_1 + 1] == square[index_i_1][index_j_1 + 2] == 1) {
cout << "end" << endl;
return 1;
} return 0;
}
void getUserInput()
{
cout << "Choose a square" << endl;
int input;
cin >> input;
counter++;
switch(input)
{
case 1:
if (square[0][0] != mark) {
square[0][0] = mark;
break;
} else {
i = i - 1;
break;
}
case 2:
if (square[0][1] != mark) {
square[0][1] = mark;
break;
} else {
i = i - 1;
break;
}
case 3:
if ((square[0][2] != mark)) {
square[0][2] = mark;
break;
} else {
i = i - 1;
break;
}
case 4:
if (square[1][0] != mark) {
square[1][0] = mark;
break;
} else {
i = i - 1;
break;
}
case 5:
if (square[1][1] != mark) {
square[1][1] = mark;
break;
} else {
i = i - 1;
break;
}
case 6:
if (square[1][2] != mark) {
square[1][2] = mark;
break;
} else {
i = i - 1;
break;
}
case 7:
if (square[2][0] != mark) {
square[2][0] = mark;
break;
} else {
i = i - 1;
break;
}
case 8:
if (square[2][1] != mark) {
square[2][1] = mark;
break;
} else {
i = i - 1;
break;
}
case 9:
if (square[2][2] != mark) {
square[2][2] = mark;
break;
} else {
i = i - 1;
break;
}
}
}
|