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
|
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
enum TicTacToeCases {
PLAYER_ONE = 'O',
PLAYER_TWO = 'X',
NEUTRAL = '*'
};
void printGame(char [][20], int);
void makeMove(char [][20], int, char);
bool checkIfGameOver(char [][20], char, int, int);
int main(){
srand(time(NULL));
int decide = rand();
bool over = false;
int boardSize = 3;
int toWin = 3;
char player;
char ticTacToe[20][20];
cout<< "Choose the size of the board: ";
cin>> boardSize;
cout<< "Choose the amount of character in a row in order to win: ";
cin>> toWin;
for (int i = 0; i < boardSize; i++){
for (int j = 0; j < boardSize; j++){
ticTacToe[i][j] = static_cast<char>(NEUTRAL);
}
}
printGame(ticTacToe, boardSize);
while (!over){
if (decide % 2 == 0){
player = static_cast<char>(PLAYER_ONE);
makeMove(ticTacToe, boardSize, player);
printGame(ticTacToe, boardSize);
over = checkIfGameOver(ticTacToe, player, boardSize, toWin);
}
else{
player = static_cast<char>(PLAYER_TWO);
makeMove(ticTacToe, boardSize, player);
printGame(ticTacToe, boardSize);
over = checkIfGameOver(ticTacToe, player, boardSize, toWin);
}
decide+=2;
}
}
void printGame(char ticTacToe[][20], int boardSize){
for (int i = 0; i < boardSize; i++){
for (int j = 0; j < boardSize; j++){
cout<< ticTacToe[i][j]<< " ";
}
cout<< "\n\n";
}
}
void makeMove(char ticTacToe[][20], int boardSize, char player){
int i;
int j;
bool valid = false;
while (!valid){
cout<< "Player one decide position\n";
cin>> i;
cin>> j;
if (ticTacToe[i-1][j-1] == '*'){
valid = true;
}
else{
cout<< "This position is already taken\n";
}
}
cout<< "\n";
ticTacToe[i-1][j-1] = static_cast<char>(player);
}
bool checkIfGameOver(char ticTacToe[][20], char player, int boardSize, int toWin){
int counter = 0;
int diagonal = 0;
int otherDiagonal = boardSize - 1;
int *forTheDiagonal = &diagonal;
int *forTheOtherDiagonal = &otherDiagonal;
for (int i = 0; i < boardSize; i++){
counter = 0;
for (int j = 0; j < boardSize; j++){
if (ticTacToe[i][j] == player){
counter++;
}
else{
counter = 0;
}
}
if (counter == toWin){
cout<< "Player one wins!\n";
return true;
}
}
counter = 0;
for (int i = 0; i < boardSize; i++){
counter = 0;
for (int j = 0; j < boardSize; j++){
if (ticTacToe[j][i] == player){
counter++;
}
else{
counter = 0;
}
}
if (counter == toWin){
cout<< "Player one wins!\n";
return true;
}
}
counter = 0;
for (int i = 0; i < boardSize; i++){
counter = 0;
if (ticTacToe[i][diagonal] == player){
counter++;
(*forTheDiagonal)++;
}
else{
*forTheDiagonal = 0;
}
if (counter == toWin){
cout<< "Player one wins!\n";
return true;
}
}
counter = 0;
for (int i = 0; i < boardSize; i++){
counter = 0;
if (ticTacToe[i][otherDiagonal] == player){
counter++;
(*forTheOtherDiagonal)--;
}
else{
*forTheOtherDiagonal = boardSize - 1;
}
if (counter == toWin){
cout<< "Player one wins!\n";
return true;
}
}
counter = 0;
return false;
}
|