Below is the game code written in cpp. This is the first time I have consciously used pointers. I can't handle the logic of winning. I have no idea how to solve this in a different, more accessible way. I know that such straining of if statements is bad and the code is unmaintable. I realize that for some reasons you can't use cls. What's the alternative?
#include <iostream>
char square[10] = { '0','1','2','3','4','5','6','7','8','9' };
int Checkwin();
void Board();
int Choice(char mark, char* square, int choice);
void showLeftNumbers(char sqiare[]);
int main()
{
int player = 1, i, choice;
char mark;
do
{
int correctChoice = 1;
Board();
player = (player % 2) ? 1 : 2;
mark = (player == 1) ? 'X' : 'O';
int triesCount = 0;
do
{
std::cout << "Player " << player << ", enter a number: ";
std::cin >> choice;
correctChoice = Choice(mark, &square[choice], choice);
triesCount++;
if (triesCount > 2) showLeftNumbers(square);
} while (correctChoice == 1);
i = Checkwin();
player++;
} while (i == -1);
Board();
if (i == 1)
std::cout << "==>\aPlayer " << --player << " win ";
else
std::cout << "==>\aGame draw";
std::cin.ignore();
std::cin.get();
return 0;
}
/*********************************************
FUNCTION TO JUDGE THE MOVE
IF MOVE IS RIGHT THEN IT RETURNS 0 AND
CHANGES THE VALUE
IF MOVE IS FORBIDEN THEN IT RETURNS 1
**********************************************/
int Choice(char mark, char* square, int choice) {
int compare = *square - '0';
if (choice < 1 || choice > 10)
{
std::cout << "Wrong choice! You have to choose number between 1 and 9. Number " << choice << " is out of scope!\n";
return 1;
}
if (compare == choice) {
*square = mark;
return 0;
}
else
{
std::cout << "Already shoot in this place\n";
return 1;
}
}
/*********************************************
FUNCTION TO SHOW PLAYERS LEFT NUMBERS
AFTER 3 FAILED ATTEMPTS
**********************************************/
void showLeftNumbers(char sqiare[])
{
std::cout << "Man, you can choose just between ";
for (int i = 1; i < 10; i++)
{
if (square[i] != 'X' && square[i] != 'O')
{
std::cout << i << " ";
}
}
std::cout << "! ";
}
/*********************************************
FUNCTION TO RETURN GAME STATUS
1 FOR GAME IS OVER WITH RESULT
-1 FOR GAME IS IN PROGRESS
O GAME IS OVER AND NO RESULT
**********************************************/
int Checkwin()
{
if (square[1] == square[2] && square[2] == square[3])
return 1;
else if (square[4] == square[5] && square[5] == square[6])
return 1;
else if (square[7] == square[8] && square[8] == square[9])
return 1;
else if (square[1] == square[4] && square[4] == square[7])
return 1;
else if (square[2] == square[5] && square[5] == square[8])
return 1;
else if (square[3] == square[6] && square[6] == square[9])
return 1;
else if (square[1] == square[5] && square[5] == square[9])
return 1;
else if (square[3] == square[5] && square[5] == square[7])
return 1;
else if (square[1] != '1' && square[2] != '2' && square[3] != '3'
&& square[4] != '4' && square[5] != '5' && square[6] != '6'
&& square[7] != '7' && square[8] != '8' && square[9] != '9')
return 0;
else
return -1;
}
/*******************************************************************
FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK
********************************************************************/
void Board()
{
system("cls");
std::cout << "\n\n\tTic Tac Toe\n\n";
std::cout << "Player 1 (X) - Player 2 (O)\n\n\n";
std::cout << " | | \n";
std::cout << " " << square[1] << " | " << square[2] << " | " << square[3] << "\n";
std::cout << "_____|_____|_____" << "\n";
std::cout << " | | " << "\n";
std::cout << " " << square[4] << " | " << square[5] << " | " << square[6] << "\n";
std::cout << "_____|_____|_____" << "\n";
std::cout << " | | " << "\n";
std::cout << " " << square[7] << " | " << square[8] << " | " << square[9] << "\n";
std::cout << " | | \n\n";
}
/*******************************************************************
END OF PROJECT
********************************************************************/