Question: This code is not working properly. If you could please finish the whole game for me. It should be as easy as using the mouse
This code is not working properly. If you could please finish the whole game for me. It should be as easy as using the mouse to move counters at the top of the connect 4 board and then it drops down and two players play against each other. The board and counters should have some concept of the becker robots in it. I know the explanation of how to do it but if someone could please type out the code to finish this game. Please Help
import becker.robots.City; import becker.robots.Direction; import becker.robots.Robot; import becker.robots.Thing; import becker.robots.Wall; import java.util.Scanner;
public class Game extends City {
public static void main(String[] args) { Scanner in = new Scanner(System.in);
// Creating the size of the board char[][] grid = new char[6][7];
// Initializing the array for (int row = 0; row < grid.length; row++) { for (int col = 0; col < grid[0].length; col++) { grid[row][col] = ' '; } }
// Creating the Becker City City connect4 = new City();
// Starting move int turn = 1; char player = 'R'; boolean winner = false;
// The player can take a turn while (winner == false && turn <= 42) { boolean validPlay; int play; do { display(grid);
System.out.print("Player " + player + ", choose a column: "); play = in.nextInt();
// We are checking if the play is valid validPlay = validate(play, grid); } while (validPlay == false); // Here the checker drops for (int row = grid.length - 1; row >= 0; row--) { if (grid[row][play] == ' ') { grid[row][play] = player; // Creating the Red or Blue counter robots if (player == 'R') { Robot red = new Robot(connect4, row, play, Direction.SOUTH); red.setColor(java.awt.Color.RED); } else { Robot blue = new Robot(connect4, row, play, Direction.SOUTH); blue.setColor(java.awt.Color.BLUE); } break; } }
// Checking to see if there is a winner winner = isWinner(player, grid);
// Switch players if (player == 'R') { player = 'B'; } else { player = 'R'; } turn++; } display(grid);
if (winner) { if (player == 'R') { System.out.println("Black won"); } else { System.out.println("Red won"); } } else { System.out.println("Tie game"); } }
// Creating board // We are creating the Becker City and placing the counters public static void display(char[][] grid) { City connect4 = new City(); for (int row = 0; row < grid.length; row++) { for (int col = 0; col < grid[0].length; col++) { if (grid[row][col] == 'R') { Robot red = new Robot(connect4, row, col, Direction.SOUTH); red.setColor(java.awt.Color.RED); } else if (grid[row][col] == 'B') { Robot blue = new Robot(connect4, row, col, Direction.SOUTH); blue.setColor(java.awt.Color.BLUE); } } } }
// **fixing holes in the code**//
public static boolean validate(int column, char[][] grid) { // Checking if the column is valid if (column < 0 || column > grid[0].length) { return false; }
// Checking if the column is full if (grid[0][column] != ' ') { return false; } return true; }
public static boolean isWinner(char player, char[][] grid) { // checking for 4 counters across for (int row = 0; row < grid.length; row++) { for (int col = 0; col < grid[0].length - 3; col++) { if ( grid[row][col] == player && grid[row][col + 1] == player && grid[row][col + 2] == player && grid[row][col + 3] == player ) { return true; } } }
// checking for 4 counters down for (int col = 0; col < grid[0].length; col++) { for (int row = 0; row < grid.length - 3; row++) { if ( grid[row][col] == player && grid[row + 1][col] == player && grid[row + 2][col] == player && grid[row + 3][col] == player ) { return true; } } }
// checking for 4 counters diagonally for (int row = 0; row < grid.length - 3; row++) { for (int col = 0; col < grid[0].length - 3; col++) { if ( grid[row][col] == player && grid[row + 1][col + 1] == player && grid[row + 2][col + 2] == player && grid[row + 3][col + 3] == player ) { return true; } } } for (int row = 0; row < grid.length - 3; row++) { for (int col = 3; col < grid[0].length; col++) { if ( grid[row][col] == player && grid[row + 1][col - 1] == player && grid[row + 2][col - 2] == player && grid[row + 3][col - 3] == player ) { return true; } } }
return false; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
