Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will supply several methods that implement the functionality of the Nonogram class. Add your code to the provided Nonogram.java class. The Nonogram constructor takes

You will supply several methods that implement the functionality of the Nonogram class. Add your code to the provided Nonogram.java class.

The Nonogram constructor takes a 2D array of booleans representing a desired solution, and initializes the fields to make a Nonogram for which that solution would be correct.

The findGroupLengths method is a helper method that returns a list of the group lengths in a given array of colors repesenting a single row or column of the image.

The isGuessCorrect method will test the users answer to the puzzle to see if it satisfies all of the nonogram constraints. If the solution is valid, this method returns true; otherwise, it returns false, after printing a message to standard out describing which of the constraints was violated.

The toString method should return a string representation of the nonogram, including all of the group lengths for every row and column, as well as the users current guess at the answer. This will be useful for debugging. For this method, you may assume that the puzzle is black-and-white. There are also four callback methods that you must provide for use by the GUI.

The handleMouseClickAt method flips the color of one cell of the users guess. The arguments will be a row and column in the puzzle, not a screen pixel!

The handleMousePressAt method records the location of the most recent mouse press. The arguments will be a row and column in the puzzle, not a screen pixel!

The handleMouseReleaseAt method flips a line of pixels between the most recent mouse press and mouse release locations. The arguments will be a row and column in the puzzle, not a screen pixel!

the code: what other info do you need? this was all that was given to me

import javax.swing.*;

public class Nonogram { public int height; public int width; public int maxRowGroups; public int maxColGroups; boolean[ ][ ] guess; int[ ][ ] rowGroupLength; int[ ][ ] colGroupLength;

public Nonogram(boolean[ ][ ] targetSolution) { // targetSolution must be an mxn rectangular array where m,n >= 1. // TODO: Initialize the fields correctly to make a nonogram puzzle of which// targetSolution is a valid solution. } // The next constructor is provided for you. It builds off the constructor above. public Nonogram(String s) { this(stringToBooleanArray(s));} // The next method is provided for you. It converts a string to a 2D boolean array. static boolean[][] stringToBooleanArray (String s) {

String[ ] lines = s.split(" "); boolean[][] rv = new boolean[lines.length][]; for (int i=0; i String line = lines[i]; rv[i] = new boolean[line.length()]; for (int j=0; j rv[i][j] = (line.charAt(j)!='.'); } } return rv;} int[ ] findGroupLengths (boolean[ ] data) { // figure out the groups in data, and record their // lengths in rowGroupLengths and colGroupLengths// TODO: your code here return new int[0]; } public String toString( ) { // print out the nonogram as a string. // this should include the group lengths // as well as the current guess. // this will be mainly used as an debugging tool, as // the GUI will be better for interacting with the user. // For the guess, we will represent the "EMPTY color" by the '.' char, // and the "FULL color" (black) by the 'X' char.// TODO: your code here. return null; } boolean isGuessCorrect ( ) { // return true iff the guess has all the correct row/column // group lengths. It does not have to match the "solution" field. // TODO: your code here return false; // you will replace this. } // The next 4 methods are callback methods that will be invoked by the GUI when appropriate. // You need to fill them in to provide the correct functionality. public void handleMouseClickAt(int i, int j) { // Add code here to handle a "mouseClick" event at row i, column j. // You should toggle the guessed color for the cell at the clicked location. // You will need to handle the possibility that (i,j) is out of bounds (negative or too big). // TODO: your code here } public void handleMousePressAt(int i, int j) { // Add code here to handle a "mousePress" event at row i, column j. // You should record the location of the press in an instance variable. // Don't display anything until the mouse is released. // You will need to handle the possibility that (i,j) is out of bounds (negative or too big). // TODO: your code here } public void handleMouseReleaseAt(int i, int j) { // Add code here to handle a "mouseRelease" event at row i, column j. // If there was a previous mousePress event, you should now check whether the "release" cell is // in the same row or column as the "press" cell. If it is, you should flip the color of the // "press" cell, and color all the other cells between the "press" and "release" cells to match. // You will need to handle the possibility that (i,j) is out of bounds (negative or too big). // TODO: your code here } // main method: you can put your own test code in here. public static void main(String[] args) { String pic = "..XXX.. .XX.XX. XX...XX X.....X XX...XX .XX.XX. ..XXXX.."; Nonogram nono = new Nonogram(pic); System.out.println(pic); System.out.println(); System.out.println(nono); }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Databases Organizing Information Digital And Information Literacy

Authors: Greg Roza

1st Edition

1448805929, 978-1448805921

More Books

Students also viewed these Databases questions