Introduction to programing_Chapter 8. Using Pseudocode and 2d array: Use a two-d
ID: 3568022 • Letter: I
Question
Introduction to programing_Chapter 8. Using Pseudocode and 2d array: Use a two-dimensional String array with three rows and three columns as the game board.Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following:a.Displays the contents of the board array (I have not idea).....b.Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row and column number.c.Allows player 2 to select a location on the board for an O. The program should ask the user to enter the row and column number.d.Determines whether a player has won or if a tie has occurred. If a player haswon, the program should declare that player the winner and end. If a tie has occurred, the program should say so and end.e.Player 1 wins when there are three Xs in a row on the game board. Player 2wins when there are three Os in a row on the game board. The winning Xs orOs can appear in a row, in a column, or diagonally across the board. A tie occurs when all of the locations on the board are full, but there is no winner.
Explanation / Answer
/* This is a more complex example using 2D arrays It is a simulation of a tictactoe game It makes extensive use of arrays and nested for loops */ public class tictactoe { // this member variable is a 2D array of chars, this is the playing field private char[][] grid = {{'.','.','.'}, {'.','.','.'}, {'.','.','.'}}; public static void main(String[] args) { // creates the object "myGame" which is of class "tictactoe" tictactoe myGame = new tictactoe(); // it would be trivial to prompt the user for input, but // to save time we will simply simulate a game... if(myGame.play('X',0,0) != '.') return; if(myGame.play('O',1,1) != '.') return; if(myGame.play('X',2,0) != '.') return; if(myGame.play('O',1,2) != '.') return; if(myGame.play('X',1,0) != '.') return; // should not get here. } // this method executes one play public char play(char mark, int x, int y) { grid[x][y] = mark; print(); // grid has changed, so we need to check to see if the game should end char winner = checkWin(); if(winner != '.') { System.out.println(grid[x][y]+" has won!"); } return(winner); } // this method checks for a winning condition private char checkWin() { // check all rows for(int x=0; xRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.