This is my first time using StackExchange. I did not have any idea what this website for, but I guess is a place to review the code? So, I'm just going to give it a shot.
#include <iostream>
std::string board[3][3];
void making_board();
void play_the_game();
void display_board();
void choose_position(int position, std::string* current_player);
void find_row_column(int position, int* row, int* column);
void player_turn(std::string* current_player);
std::string computer_turn(std::string* current_player);
std::string predict_future(std::string* current_player);
std::string check_the_game();
int possible_move(int* row, int* column, std::string *current_player, std::string* position);
int move_left(int row, int column, std::string* current_player);
int predict_opponent(int* row, int* col, std::string opponent, std::string* ori_position);
int possible_opponent(int* row, int* column, std::string opponent);
int main()
{
play_the_game();
return 0;
}
std::string check_the_game(){
int area_filled = 0;
for (int row = 0; row < 3; row++){
if (board[row][0] == board[row][1] && board[row][1] == board[row][2]){
return board[row][0];
}
}
for (int column = 0; column < 3; column++){
if (board[0][column] == board[1][column] && board[1][column] == board[2][column]){
return board[0][column];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2])return board[1][1];
else if (board[2][0] == board[1][1] && board[1][1] == board [0][2]) return board[1][1];
for (int rows = 0; rows < 3; rows++){
for (int columns = 0; columns < 3; columns++){
if (board[rows][columns] == "X" || board[rows][columns] == "O")
area_filled++;
if (area_filled == 9)return "neither";
}
}
return "notyet";
}
int possible_opponent(int row, int column, std::string opponent){
std::string ori_position = board[row][column];
board[row][column] = opponent;
if (check_the_game() == "X"){
board[row][column] = ori_position;
return -10;
}else {
board[row][column] = ori_position;
return 0;
}
}
int predict_opponent(int* row, int* col, std::string opponent, std::string* ori_position){
int score;
for (int r = 0; r < 3; r++){
for (int c = 0; c < 3; c++){
if (board[r][c] != "X" && board[r][c] != "O"){
if (possible_opponent(r, c, opponent) == -10){
board[*row][*col] = *ori_position;
*row = r;
*col = c;
return -10;
} else continue;
}else continue;
}
}
return 0;
}
int possible_move(int* row, int* column, std::string *current_player, std::string* position){
std::string ori_position = board[*row][*column];
board[*row][*column] = *current_player;
if (check_the_game() == "O"){
board[*row][*column] = ori_position;
return 10;
}else if (predict_opponent(row, column, "X", &ori_position) == -10){
*position = board[*row][*column];
board[*row][*column] = "O";
return -10;
}else {
board[*row][*column] = ori_position;
return 0;
}
}
std::string predict_future(std::string* current_player){
std::string position;
int score;
for (int row = 0; row < 3; row++){
for (int column = 0; column < 3; column++){
if (board[row][column] != "X" && board[row][column] != "O"){
if (possible_move(&row, &column, current_player, &position) == -10){
return position;
}
else if (possible_move(&row, &column, current_player, &position) == 10){
position = board[row][column];
board[row][column] = *current_player;
return position;
}else if (move_left(row, column, current_player) == 1){
position = board[row][column];
board[row][column] = *current_player;
return position;
}else if(possible_move(&row, &column, current_player, &position) == 0){
if (board[1][1] != "X" && board[1][1] != "O" && move_left(row, column, current_player) < 9){
position = board[1][1];
board[1][1] = *current_player;
return position;
}else continue;
}
}else continue;
}
}
}
int move_left(int row, int column, std::string* current_player){
int move_left = 0;
for (int r = row; r < 3; r++){
for (int c = column; c < 3; c++){
if (board[r][c] != "X" && board[r][c] != "O"){
move_left++;
}
}
}
return move_left;
}
void play_the_game(){
bool still_playing = true;
std::string current_player = "X";
std::string winner = "";
making_board();
while (still_playing){
display_board();
player_turn(¤t_player);
winner = check_the_game();
if (winner == "X" || winner == "O" || winner == "neither"){display_board();break;}
}
std::cout << " Winner is " << winner << std::endl;
std::cout << " Play again (1 for yes and 0 for no)? ";
std::string answer;
std::cin >> answer;
if (answer == "1"){
system("CLS");
play_the_game();
}else if(answer == "0"){
exit(0);
}else {
std::cout << " Invalid answer";
std::cout << " Play again (1 for yes and 0 for no)? ";
}
}
void player_turn(std::string *current_player){
std::string sposition;
int position;
if (*current_player == "O"){
sposition = predict_future(current_player);
std::cout << " " << *current_player << "'s turn, position chosen was: " << sposition << std::endl;
*current_player = "X";
}else {
std::cout << " " << *current_player << "'s turn, choose position: ";
std::cin >> position;
if (!std::cin >> position){
std::cin.clear();
std::cin.ignore(1000, '\n');
std::cout << " Position is not available" << std::endl;
player_turn(current_player);
}
choose_position(position, current_player);
}
}
void choose_position(int position, std::string *current_player){
int row, column;
find_row_column(position, &row, &column);
if (board[row][column] == "X" || board[row][column] == "O" || position < 0 || position > 9){
std::cout << " Position is not available" << std::endl;
player_turn(current_player);
} else {
board[row][column] = *current_player;
if (*current_player == "X") *current_player = "O";
else *current_player ="X";
}
}
void find_row_column(int position, int* row, int* column){
*row = position/3;
if (*row = 0) *column = 2;
*column = position-*row-1;
}
void display_board(){
std::cout << "\n";
for (int rows = 2; rows >= 0; rows--){
for (int columns = 0; columns < 3; columns++){
if (columns == 0){
std::cout << " " << board[rows][columns];
} else std::cout << " | " << board[rows][columns];
}
if (rows > 0)std::cout << "\n ---|---|---\n";
else std::cout << "\n\n";
}
}
void making_board(){
int i = 1;
for (int rows = 0; rows < 3; rows++){
for (int columns = 0; columns < 3; columns++){
board[rows][columns] = std::to_string(i);
i++;
}
}
}
I tried to beat my AI. Somehow I still won.