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

A simple game involves two players choosing positions in a 4 x 4 grid. Initially

ID: 3636096 • Letter: A

Question

A simple game involves two players choosing positions in a 4 x 4 grid.
Initially every position in the grid is marked with 0. Players one and two take it in turns
to play. Player one can change any zero to be a 1. Player two can change any zero to be
a 2. A player cannot select a position if it is already occupied by a player. A player wins
if they obtain four of their values in a 2 2 position formation in the grid (i.e. a player
occupies some position, the position to its right, the position below and the position below
right). The game stops whenever either there are no zeros left in the grid (a draw) or one
player wins.
Write a Java program to repeatedly display the grid and allow each user to input a
location in turn. You should disallow input outside the required grid size and when a
position is already occupied. You should use a 2 dimensional array to represent the grid.
Do not provide a graphical user interface{just text based output to display the grid. Your
program should be able to detect when a player has won or when every place on the grid
is occupied (a draw).

Explanation / Answer

Driver.java ----------------------------------- import java.util.Scanner; public class Driver { public static void main(String[] args) { Scanner scan = new Scanner(System.in); Grid myGrid = new Grid(); int row = 0; int col = 0; int player = 0; boolean piecePlaced = false; while(!myGrid.gameover(player)) { if(player == 0) player = 1; else if(player == 1) player = 2; else if(player == 2) player = 1; do { myGrid.draw(); System.out.println("Player: " + player); System.out.print(" Row: "); row = scan.nextInt(); System.out.print(" Col: "); col = scan.nextInt(); System.out.println(); piecePlaced = myGrid.placePiece(row, col, player); if(!piecePlaced) System.out.println("Bad Input: Please try again"); }while(!piecePlaced); } } } Grid.java --------------------------- public class Grid { final int size_height = 4; final int size_width = 4; private int[][] board; public Grid() { initilizeGrid(); } private void initilizeGrid() { board = new int[size_height][size_width]; for(int c = 0; c
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