Part A: _____> Modify the function display() so that instead of printing a board
ID: 645384 • Letter: P
Question
Part A: _____> Modify the function display() so that instead of printing a board like this,
it prints a board like this:
Do not change the size or type of the array (keep it as a 3x3 2D array of char).
Part B: _____> Now, modify main() so that it begins with a blank board, then alternately prompts two players for a row and column, then places X and O characters appropriately, updating the two-dimensional array tictac[][] and calling display() to display the tic-tac-toe board. The session should look like:
and so on (your program need not check for legal moves or winning positions)
Part C: _____> Modify your program to also print out when one player has won the game. Indicate whether there are three X's (or O's) in a row (or column or diagonal).
Explanation / Answer
PART A: for (int row = 0; row < 3; row++) {
Part C:
private boolean checkColumnsForWin() {
for (int i = 0; i < 3; i++) {
if (checkRowCol(board[0][i], board[1][i], board[2][i]) == true) {
return true;
}
}
return false;
}
// Check the two diagonals to see if either is a win. Return true if either wins.
private boolean checkDiagonalsForWin() {
return ((checkRowCol(board[0][0], board[1][1], board[2][2]) == true) || (checkRowCol(board[0][2], board[1][1], board[2][0]) == true));
// Check to see if all three values are the same (and not empty) indicating a win.
private boolean checkRowCol(char c1, char c2, char c3) {
return ((c1 != '-') && (c1 == c2) && (c2 == c3));
}