C++ MyProgrammingLab problem: Define a recursive function int evaluateR( char bo
ID: 3562583 • Letter: C
Question
C++ MyProgrammingLab problem:
Define a recursive function
int evaluateR(char board[])
that returns 1, -1 or 0 depending on whether the board is a guaranteed win for X, O or an eventual cat's game.
The board array always has nine elements , representing the squares on a 3 by 3 tic tac toe board.
That means board[i] represents the square at row i / 3 + 1 and column i % 3 + 1.
Assume that each square contains one of three characters : 'X' ,'O' or '-'.
The number of X's is either equal to the number of O's or exceeds it by 1. In the first case, it's X's turn. In the second case it is O's turn.
Examples:
If board = {X,X,O,-,-,O,-,-,-} then evaluateR(board) will return -1, since this is a guaranteed win for O.
If board = {-,-,-,-,-,-,-,-,-} then evaluateR(board) will return 0, since this is an eventual cat's game.
If board = {X,O,X,-,-,-,O,-,-} then evaluateR(board) will return 1, since this is a guaranteed win for X.
Do not write the entire program.
Here is the pseudocode:
Here is my solution:
int checkforWin(char board[])
{
if((board[0] == 'X' && board[1] == 'X' && board[2] == 'X')
|| (board[3] == 'X' && board[4] == 'X' && board[5] == 'X')
|| (board[6] == 'X' && board[7] == 'X' && board[8] == 'X')
|| (board[0] == 'X' && board[3] == 'X' && board[6] == 'X')
|| (board[1] == 'X' && board[4] == 'X' && board[7] == 'X')
|| (board[2] == 'X' && board[5] == 'X' && board[8] == 'X')
|| (board[0] == 'X' && board[4] == 'X' && board[8] == 'X')
|| (board[2] == 'X' && board[4] == 'X' && board[6] == 'X'))
{
return 1;
}
if((board[0] == 'O' && board[1] == 'O' && board[2] == 'O')
|| (board[3] == 'O' && board[4] == 'O' && board[5] == 'O')
|| (board[6] == 'O' && board[7] == 'O' && board[8] == 'O')
|| (board[0] == 'O' && board[3] == 'O' && board[6] == 'O')
|| (board[1] == 'O' && board[4] == 'O' && board[7] == 'O')
|| (board[2] == 'O' && board[5] == 'O' && board[8] == 'O')
|| (board[0] == 'O' && board[4] == 'O' && board[8] == 'O')
|| (board[2] == 'O' && board[4] == 'O' && board[6] == 'O'))
{
return -1;
}
if(board[0] != '-' && board[1] != '-' && board[2] != '-'
&& board[3] != '-' && board[4] != '-' && board[5] != '-'
&& board[6] != '-' && board[7] != '-' && board[8] != '-')
{
return 0;
}
}
int evaluateR(char board[])
{
int p, q, r;
int bestofSoFar;
for(int i = 0; i < 9; i++)
{
if(checkforWin(board) == 1)
{
return 1;
}
if(checkforWin(board) == -1)
{
return -1;
}
if(checkforWin(board) == 0)
{
return 0;
}
}
bool xTurn;
for(int i = 0; i < 9; i++)
{
if(board[i] == '')
{
if(xTurn)
{
board[i] = 'X';
xTurn = true;
}
else
{
board[i] = 'O';
xTurn = false;
}
evaluateR(board);
}
if(p > q && p > r)
{
bestofSoFar = p;
}
else if(q > p && q > r)
{
bestofSoFar = q;
}
else
{
bestofSoFar = r;
}
board[i] = '';
}
return bestofSoFar;
}
MyProgrammingLab is not accepting this answer. After it didn't accept my solution, It also gave me these hints:
You almost certainly should be using: !
? You almost certainly should be using: %
? You almost certainly should be using: :
Can someone please post the correct answer for this problem?
Explanation / Answer
void print(int p) { if (p==0) return; cout<<p; print(p-1); return; } An example to print counting up: void print(int p) { if (p==0) return; print(p-1); cout<<p; return; } An example to produce the fibonacci number for a given index in the series: int Fibonacci(int n) { if (n==0) return 0; if (n==1) return 1; return( Fibonacci(n-2) + Fibonacci(n-1) ); } A recursive function to determine if an input is prime: bool isPrime(int p, int i=2) { if (i==p) return 1;//or better if (i*i>p) return 1; if (p%i == 0) return 0; return isPrime (p, i+1); } // two versions of recursive solution to adding up numbers from 1 to any given number. // the second example is tail recusion because once the total is found, the function returns and // does not need to unravel previous recursive steps int sum (int num) { if (num==0) return 0; return (sum(num-1)+(num)); } int sum (int num, int total=0) { if (num<=0) return total; sum( num-1, sum ); }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.