University of Wisconsin-Milwaukee College of Business Administration Course: Obj
ID: 3704356 • Letter: U
Question
University of Wisconsin-Milwaukee
College of Business Administration
Course: Object-Oriented Programming (BUSADM 432)
Professor: Dr. Kurt A. Pflughoeft
Homework: #5 – Intelligent Tic Tac Toe
Date Assigned: April 10, 2018
Date Due: April 24, 2018
Modify the Tic Tac Toe program that I presented in class. I got the original procedural version from this web site:
https://rosettacode.org/wiki/Tic-tac-toe#C.23
I modified the code slightly to place it in a class. (i.e. converted the procedural code to an OO paradigm)
In the procedural code, there are some alternatives for selectedMove which are commented out. I chose the getRandomMove option for the computer. I would like you to choose a more intelligent option such as getBestMove - see below.
/*================================================================
*Move Logic
*================================================================*/
static int getMoveFor(int player)
{
if (player == Human)
return getManualMove(player);
else
{
//int selectedMove = getManualMove(player);
//int selectedMove = getRandomMove(player);
int selectedMove = getSemiRandomMove(player);
//int selectedMove = getBestMove(player);
Console.WriteLine("{0} selects position {1}.", playerName(player), selectedMove + 1);
return selectedMove;
}
}
Once you got the more advanced logic working, I would like you to convert it to a GUI program (i.e. Windows Form program). Thus, you will remove Console.WriteLines and Console.Readlines and rely on the control objects in the GUI for input and output.
Happy computing.
Check the syllabus under homework policy as to what format is required for assignments to turn in for D2L.
Please put the actual code and screenshouts of output
Explanation / Answer
void InitializeBoard(char board[ROWS][COLS])
{
char index = '1';
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
board[i][j] = index;
index++;
}
}
}
void DisplayBoard(char board[ROWS][COLS])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%c", board[i][j]);
if (j != 2)
printf("|");
}
if (i != 2)
printf(" ---------");
printf(" ");
}
}
//Calculate the position after the moves
int PositionCalculation(char board[3][3], int selection, char input)
{
int column = selection % 3;
int row = selection / 3;
if (column == 0)
{
column += 3;
row -= 1;
}
if (board[row][column - 1] == 'o' || board[row][column - 1] == 'x')
return -1;
board[row][column - 1] = input;
return 0;
}
// Condition check for a win
bool ROWSCheck(char board[ROWS][COLS])
{
for (int i = 0; i < ROWS; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2])
return true;
}
}
bool COLSCheck(char board[ROWS][COLS])
{
for (int i = 0; i < COLS; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i])
return true;
}
}
bool DiagonalCheck(char board[ROWS][COLS])
{
if ((board[0][0] == board[1][1] && board[1][1] == board[2][2]) || (board[0][2] == board[1][1] && board[1][1] == board[2][0]))
return true;
}
// Main Program
#define ROWS 3
#define COLS 3
void TicTacToe()
{
int selection = 0;
int counter = 0;
int checker = 0;
char board[ROWS][COLS];
BoardInitializer(board);
DrawBoard(board);
while (counter != 9)
{
if (counter % 2 == 0)
{
printf(" Please input where you want to place X: ");
scanf(" %d", &selection);
SCANX:
while (selection < 0 || selection > 10)
{
printf("Invalid input, please try again: ");
scanf(" %d", &selection);
}
printf(" ");
checker = PositionCalculation(board, selection, 'x');
if (checker == -1)
{
selection = -1;
goto SCANX;
}
DrawBoard(board);
counter++;
if (counter > 3)
{
if (ROWSCheck(board) == true || COLSCheck(board) == true || DiagonalCheck(board) == true)
goto XWINS;
}
}
else
{
printf(" Please input where you want to place O: ");
scanf(" %d", &selection);
SCANO:
while (selection < 0 || selection > 10)
{
printf("Invalid input, please try again: ");
scanf(" %d", &selection);
}
printf(" ");
checker = PositionCalculation(board, selection, 'o');
if (checker == -1)
{
selection = -1;
goto SCANO;
}
DrawBoard(board);
counter++;
if (counter > 3)
{
if (ROWSCheck(board) == true || COLSCheck(board) == true || DiagonalCheck(board) == true)
goto OWINS;
}
}
}
printf("Its a draw ");
goto ENDPROGRAM;
XWINS:
printf(" X wins! ");
goto ENDPROGRAM;
OWINS:
printf(" O wins ");
goto ENDPROGRAM;
ENDPROGRAM:
return;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.