This is my first time asking question here. Please don't be too mad if I made any kind of mistake here and just let me know what to fix. Thank you a lot :D
I am trying to write a C++ code that you can play a Tic-Tac-Toe against CPU. The CPU must win or at least tie. CPU goes first. As a player, you will be asked to enter 2 numbers, each for row and column.
The problem is that when I execute the program, it stops without any error message at the 3rd cycle of the for loop that starts at line 32. I wonder what I did wrong.
#include <iostream>
using namespace std;
void displayBoard(char []); //displays current board
bool isValidInput(char [], int, int, int); //checks the user input
bool isItAScore(char [], char); //determines if current player won or not
int getValue(char [], char, int); //get value of the board by player and number marks
int main()
{
char tBoard[9], computer = 'X', player = 'O', empty = '.';
int board[3][3], row = -1, col = -1, temp = 0;
bool didSomeoneWin = false;
//initializes the board with numbers 0 to 8
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
board[i][j] = temp;
temp++;
}
}
//initialize the actual board with '.'
for(int i= 0; i < 9; i++)
{
tBoard[i] = empty;
}
//starts the game
for(int k = 0; k < 9; k++) //it lasts only 9 turns max
{
displayBoard(tBoard);
if(k % 2 == 1) //player's turn
{
cout << "Player, row and column: ";
cin >> row >> col; //takes user input range of 1 to 3
//decreases each value by 1 to work with the array index 0 ~ 2
row--;
col--;
if(isValidInput(tBoard, row, col, board[row][col])) //iff the input is valid
{
tBoard[board[row][col]] = player; //puts the mark on the position
if(k > 4 && isItAScore(tBoard, player)) //iff the current player wins
{
displayBoard(tBoard);
cout << "Player wins.";
k = 8; //skips to the end of the loop
didSomeoneWin = true; //no tie
}
}
else //if the input is invalid
{
cout << "Invalid row or column number. Try again.\n";
k--; //redo the loop with same player
}
}
else //computer's turn
{
cout << "Computer's move\n";
if(k == 0) //first move
{
tBoard[5] = computer;
}
else
{
int value = -100, int position = -1;
for(int i = 0; i < 9; i++)
{
if(tBoard[i] == empty)
{
tBoard[i] = computer;
if(isItAScore(tBoard, computer))
{
displayBoard(tBoard);
cout << "Computer wins.";
i = 8;
k = 8;
didSomeoneWin = true;
}
else
{
int x1 = getValue(tBoard, computer, 1);
int x2 = getValue(tBoard, computer, 2);
int o1 = getValue(tBoard, player, 1);
int o2 = getValue(tBoard, player, 2);
if(value < 3 * x2 + x1 - (3 * o2 + o1))
{
value = 3 * x2 + x1 - (3 * o2 + o1);
position = i;
}
}
if(!didSomeoneWin)
{
tBoard[i] = empty;
}
}
}
tBoard[position] = computer;
}
}
}
if(!didSomeoneWin) //in case of tie
{
displayBoard(tBoard);
cout << "The cat wins";
}
return 0;
}
//display the given board
void displayBoard(char brd[])
{
for(int i = 0; i < 9; i++)
{
cout << brd[i] << " ";
if((i + 1) % 3 == 0)
{
cout << endl;
}
}
}
//checks the input
bool isValidInput(char brd[], int i, int j, int k)
{
if(((i >= 0 && i <= 2) && (j >= 0 && j <= 2)) && brd[k] == '.') //number between 0 and 2, and not taken by any players
{
return true;
}
else
{
return false;
}
}
//checks if the given player or computer won or not
bool isItAScore(char brd[], char c)
{
//chekcs rows
if((brd[0] == c && brd[1] == c && brd[2] == c) || (brd[3] == c && brd[4] == c && brd[5] == c) || (brd[6] == c && brd[7] == c && brd[8] == c))
{
return true;
}
else
{
//checks columns
if((brd[0] == c && brd[3] == c && brd[6] == c) || (brd[1] == c && brd[4] == c && brd[7] == c) || (brd[2] == c && brd[5] == c && brd[8] == c))
{
return true;
}
else
{
//checks diagonals
if((brd[0] == c && brd[4] == c && brd[8] == c) || (brd[2] == c && brd[4] == c && brd[6] == c))
{
return true;
}
//if none of them fails
else
{
return false;
}
}
}
}
int getValue(char brd[], char c, int n)
{
int temp = 0, mark = 0;
bool eligible = true;
for(int i = 0; i < 9; i + 3) //check rows
{
for(int j = i; j < i + 3; j++)
{
if(brd[j] != c)
{
j = 10;
eligible = false;
}
else
{
if(brd[j] == c)
{
temp++;
}
}
}
if(eligible && temp == n)
{
mark++;
}
eligible = true;
temp = 0;
}
for(int i = 0; i < 3; i++) //check columes
{
for(int j = i; j < i + 7; j + 3)
{
if(brd[j] != c)
{
j = 10;
eligible = false;
}
else
{
if(brd[j] == c)
{
temp++;
}
}
}
if(eligible && temp == n)
{
mark++;
}
eligible = true;
temp = 0;
}
for(int i = 0; i < 9; i + 4) //check '\' diagonal
{
if(brd[i] != c)
{
i = 10;
eligible = false;
}
else
{
if(brd[i] == c)
{
temp++;
}
}
}
if(eligible && temp == n)
{
mark++;
}
eligible = true;
temp = 0;
for(int i = 2; i < 9; i + 2) //check '/' diagonal
{
if(brd[i] != c)
{
i = 10;
eligible = false;
}
else
{
if(brd[i] == c)
{
temp++;
}
}
}
if(eligible && temp == n)
{
mark++;
}
eligible = true;
temp = 0;
return mark;
}
I tried to use something called minimax(the formula). I want it to be working properly as intended. It should play against human and make a best result(win or tie, no lose).
It just stops without any error message. The following link is online compiler I am using:
I found a lot of unusual stuff. Your first try with cpp? First in the code is an error:
int value = -100, int position = -1;
this should be
int value = -100;
int position = -1;
or
int value = -100, position = -1;
The first move of the computer set a token to the right of the middle. I think you like to set it to the middle position
tBoard[5] = computer;// should be tBorad[4]
Next problem, you have an endless loop:
for(int i = 0; i < 9; i + 3) // right would be i+=3
The variable i never change, since you only read it, and don't write it. This happens 5 times. A compiler with enabled warnings would help to find bugs like this.
Next thing, use break and continue instead of some crazy loop statements. Instead of this,
for(int i = 2; i < 9; i += 2) //check '/' diagonal
{
if(brd[i] != c)
{
i = 10;
eligible = false;
}
}
use break:
for(int i = 2; i < 9; i += 2) //check '/' diagonal
{
if(brd[i] != c)
{
eligible = false;
break;
}
}
After all this error are away, the program seems to work properly. I think you should use a real compiler, like gcc. With gcc you can use flags to see warnings.