Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Two dimensional arrays Many problems can be done using two dimensional arrays. F

ID: 3606891 • Letter: T

Question

Two dimensional arrays

Many problems can be done using two dimensional arrays. For example, all the chess games, go games, can be implemented using two dimensional arrays. Note all the matrices you learn in linear algebra are two dimensional arrays.

Here we use the easiest one: tic-tac-toe as mentioned by Deitel, in example 10.11, on page 404.

Tic Tac Toe (15%) (as two dimensional array).

Implement the two dimensional tic-tac-toe on 3-by-3 rectangular array as Deitel stated here. Notice that the two players take turns to paly by placing 1 and 2 (or o and x) on the square. Each move must be to an empty square, and game is over if one player has 3 in a line (row, column, or diagonal). Your game must state game is over when one side wins, or at the end of the 9th move, states that it is a draw.

Show two games where one game has a winner and another game is a draw. Ignore the author’s suggestion of having computer playing against a player or develop a three-dimensional Tic-Tac-Toe.

Explanation / Answer

import java.util.Scanner; public class TicTacToe { public static void main(String[] args) { char[][] board = {{'1','2','3'}, {'4','5','6'}, {'7', '8', '9'}}; // assign player to char value of X's only int play; char player = 'X'; char cpu = 'O'; int rowNumber; int columnNumber; int playerORow; int playerOcolumn; System.out.println("Welcome to tic, tac, toe! "); System.out.println("Do you wish to play? 1 for yes, 2 for no "); Scanner input = new Scanner(System.in); play = input.nextInt(); if(play != 1) { System.out.print("Invalid input, game will now EXIT thanks for playing!"); System.exit(0); } // end if displayBoard(board); System.out.println("You are limited to X's only, good luck!"); System.out.println("Please enter row (0-3) of your move: "); rowNumber = input.nextInt(); System.out.println("Please enter column (1-3); of your move: "); columnNumber = input.nextInt(); System.out.println("Please enter row (0-3) for player O: "); playerORow = input.nextInt(); System.out.println("Please enter column (1-3); of your move: "); playerOcolumn = input.nextInt(); if(board[rowNumber][columnNumber] != 'X' && board[rowNumber][columnNumber] != 'O') { board[rowNumber][columnNumber] = player; } // end if else { } makeAMove(board, player); hasWon(board, player); boardFull(board); } // end main method // displays only the tic tac toe board public static void displayBoard(char[][] board) { // loop for each row System.out.println(board[0][0] + " | " + board[0][1] + " | " + board[0][2] + " ---------"); System.out.println(board[1][0] + " | " + board[1][1] + " | " + board[1][2] + " ---------"); System.out.println(board[2][0] + " | " + board[2][1] + " | " + board[2][2] + " "); } // takes board array of values and updates it with valid row and column selected by player..does not return anything public static void makeAMove(char[][] board, char player) { displayBoard(board); } // compare each element in board to see if the char value of 'X' exists // if exists then then return true, else return false public static boolean hasWon(char[][] board, char player) { // Check if the player has won by checking winning conditions. if (board[0][0] == player && board[0][1] == player && board[0][2] == player || // 1st row board[1][0] == player && board[1][1] == player && board[1][2] == player || // 2nd row board[2][0] == player && board[2][1] == player && board[2][2] == player || // 3rd row board[0][0] == player && board[1][0] == player && board[2][0] == player || // 1st col. board[0][1] == player && board[1][1] == player && board[2][1] == player || // 2nd col. board[0][2] == player && board[1][2] == player && board[2][2] == player || // 3rd col. board[0][0] == player && board[1][1] == player && board[2][2] == player || // Diagonal board[2][0] == player && board[1][1] == player && board[0][2] == player) // Diagonal / return true; else { return false; } } public static boolean boardFull(char [][] board) { if (board[0][0] != '1' && board[0][1] != '2' && board[0][2] != '3' && board[1][0] != '4' && board[1][1] != '5' && board[1][2] != '6' && board[2][0] != '7' && board[2][1] != '8' && board[2][2] != '9') return true; else { return false; } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote