Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

I am trying to use the becker package and its code to create connect 4. This is all I have done so far. I don't

I am trying to use the becker package and its code to create connect 4. This is all I have done so far. I don't know how to finish it so that the board is constructed and so that the counters can be seen. Please finish the code, thank you!!

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 Connect4 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]; //Here we are itializing the array for (int row = 0; row < grid.length; row++){ for (int col = 0; col < grid[0].length; col++){ grid[row][col] = ' '; } } //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; 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"); } } //Public Static Main Bracket KEEP //Creating board **without use of robots** public static void display(char[][] grid){ System.out.println(" 0 1 2 3 4 5 6"); System.out.println("---------------"); for (int row = 0; row < grid.length; row++) { System.out.print("|"); for (int col = 0; col < grid[0].length; col++) { System.out.print(grid[row][col]); System.out.print("|"); } System.out.println(); System.out.println("---------------"); } System.out.println(" 0 1 2 3 4 5 6"); System.out.println(); } //Public Static Main Bracket KEEP //**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 Main Bracket KEEP public static boolean isWinner(char player, char[][] grid) { //checking for 4 counters across for(int row = 0; row

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions