I am implementing a crossword puzzle backend.
Crossword is composed of slots is composed of cells.
Lets say cell at (3,14) has a value of "a"
I want crossword.changeCellValueByCoordinate(3,14,"x") to do "a"->"x"
Lets say some Slot slot has a head cell at coordinate (3,14)
Slots head can be identified by (2,across) == Cell(3,14)
So when I call slot.getCellValueByNumberAndDirection(2,across) next, it already changed from "a" to "x".
And another method (for the same cell) crossword.getSlot(2,across).getHead also changed from "a" to "x".
How do I make it so all three objects of different types (Crossword, Slot, Cell) has an attribute (perhaps of same name, lets call it "content") that is shared/referenced by all Classes?
There are lots of ways to handle this. Here's one way:
I don't care too much for the "Slot" class. This does nothing to help with the intersections between the clues.
When you look at a crossword puzzle, what do you see? I see a board of empty squares. Some have little numbers in the corners. I also see dark blocks I know I can't write in. I see a list of "down" clues, and a list of "across" clues.
No matter what I do, I write on that board of empty squares. So I'd make a grid-like class called Board that would allow me to write on it. The little number in some squares would be how specific words are indexed. The contents of each cell could be a space, a letter, or a -1 if the square isn't writable.
You might choose to implement a Square (or Cell) class. It would include a flag stating the instance's ability to be written to. It could include a number signifying the cell to which a clue applies. There are more elegant ways to do this. Implement this, and we can discuss more advanced class hierarchies.
The Board class would contain an NxN structure (probably an array) of Cell instances.
Another class would be called something like "Clues" that corresponds to the down or across list of clues. Clues includes a list of Clue instances.
Finally I'd have a Clue class where each instance includes an ID (the 15 in "15 down" for example), and the clue text to be displayed.
I think once you initialize the Board, you'll have all you need to make a nice crossword program.
Related
I need a hint, a big hint.
In a grid, say 500 by 500, I need to know the value of that square. The answer would be yes or no. Is something there or not. Think of it as piece of graph paper; has the square been colored in, or not?
How would I set up this table up in Java? An example would be; I want to see if at location, x=400, y=400; is this square occupied or not.
I am just confused by how that’s done. Is that what arrays are used for?
If you would please include an example of how I would declare this table, how I would read from this table and how I would write to this table.
Thank you for helping out.
You can use a multi-dimensional array to hold the rows and columns of a table. Then you can insert data accordingly and check whether a value is true or false using indexes. Here is a link. https://www.geeksforgeeks.org/multidimensional-arrays-in-java/
it will make sense if you make one(multidimensional array) then print it to your screen. then you can populate it and check for values by the indexes. example [x][y] returns true
I'm currently doing a course in Object Oriented programming in Java, and we need to create a game for our last lab so my lab partner an me chose to make a 2 player chess game without AI and using the model-view-controller approach.
We are a little bit lost as how and where to start so we need a simple UML diagram so we know where to start.
We have come up with the following classes, but aren't sure if they are enough or if all the data fields and methods make sense:
ChessBoard (model) class and ChessLogic class (?)
View class to present data from the model class
Controller class that updates the model data as well as the View class based on user input
An abstract Piece class or interface which is inherited or implemented by each of the 6 pieces.
This diagram is a nice start but there's still a lot to be done. I will draw your attention on some points you need to improve, but without preventing you from learning by doing.
First some formalities:
use the arrows for the relationships only to indicate navigability. If the link is bidirectional, either use 2 arrows or none. For example, here we could understand that View knows ChessBoard, but it is not clear if ChessBoard knows about Views: how can the ChessBoard then inform Views that the state of the board has changed after a move was done ?
indicate the multiplicity: for exemple, I wonder if it is one ChessBoard for one View or could there be several Views for a same Chessboard ?
avoid ambiguity between associations and properties. For example, in View, you have a model property of type ChessBoard. But you also have an association with ChessBoard. So is this all the same ChessBoard ? Or do we have two ChessBoards, one associated and one embedded ? It would be clearer to remove the model from the properties, and indicate model as the name of the asscoiated object at the end of the association.
The association starting from Controller that suddenly splits into two is not very practical, especially if you'll tell us about association ends and multiplicity. Prefer to visually have two very distinct lines .
Now to the core:
Your model should not just be a ChessBoard. The model should be a Game that has a couple of elements. ChessBoard is only one of them: the current position of the Pieces. But what about the layers ? Where are they ? How do I know that it's two of them ? How does the Controller know wo's turn it is ?
By calling something ChessBoard that is more than a chess board, you might create a confusion. For example, can the board know for sure if isGameOver(), just with the information on the position of the pieces ? Isn't it possible that a player decides to abandon ? So try to name your classes according to what they really represent.
How can I find out which Pieces are where on the board ?
How do I know the color of a Piece ?
How can I find out if a board's cell is free or occupied ?
What happens if a Piece is removed from the board because it was taken ?
Your UML diagram must evolve to clarify all this. So I think to complete the ChessBoard and the Piece you're missing at least Game, Player, BoardCell (also called "Square", and perhap's some containers of Pieces still on the board for each player.
Once you've completed, you also need to think about the relation between the Model, the View and the Controler, to be sure that the Controller knows enough to give commands to the model, but also that the model can inform the View when something has changed.
P.S: I've added some links to a the Chess Programming Wiki since this website describes well some elementary concepts for programming the game, some usual questions as well as a lot of references. But be aware however that although very informative this resource is not very object oriented.
im working on a cinema project and i want to implement a hall creation, thing is i want it to be dynamic, so i put the values i want in a JPanel and it pops me out a theatre with the rows and columns i entered, with some kind of icon that changes color when i click it, that way i can create a theatre with the seats and aisles the way i want. I have read that it can be done with a bidimensional array (or matrix), thing is, when i want to get the values from the JPanel in the matrix, it says i cant get the value from a non-static to a static reference. Is it possible to do? Any help is appreciated. Thanks a lot in advance!
Arrays are inherently static, once they are set, their length cannot be changed. So with a two dimensional array, the width and height are fixed. Try using a two dimensional ArrayList instead of an array.
ArrayList<ArrayList<Seats>> theater = new ArrayList<ArrayList<Seats>>();
Now, this functions that same way as the array's you're using, except now the ArrayLists are non static.
To navigate
theater.get(index); \\Returns the ArrayList(the row) at index
theater.get(index).get(seatNumber); \\Returns the seat at row index and seat number;
In order to be able to display a sentence on a, say, JPanel with a GridLayout(1,0) [i.e., only one line/row] and then be able to draw a syntax tree (or similar) above it, I want to display the sentence as a row of Strings, which each include one word.
The single Strings should then be either selectable (as in a JList), or I should at least be able to get their Location on the JPanel via getLocation().
Up to this point I have tried the following options, and had the following issues:
- Single Strings as JLabels: The JLabels are stretched out to fill the JPanel width, re-sizing them to fit the single String they're displaying seems complicated. I would want to be able to do this, however, to make the sentence look like a sentence and not like a badly layed out table.
- JList: All the functionality I want, but I'm unaware of an option to re-size the "cells" of a single String (cf. JLabel above). Also, I'm having difficulties restricting display of the JList to a single line/row (cf. another of my questions).
- JTextArea: I couldn't get my head round how to get the Location of the single Strings that I had appended to the JTextArea.
I'm aware that drawString() might be an option, but I'm afraid to use it since I don't want to mix AWT and Swing. Also, I would need to calculate the int values for x and y for every single String. And I'm not sure whether I'd be able to get their Locations at all (although I could of course save their ints in a Map or Vector since I have to calculate them anyway).
Thankful for any suggestions! Thanks!
I would use JTextArea and method modelToView()/viewToModel() to get x,y for position in nthe string and position in the string for coordinates x and y.
Also use Utilities class getWordStart() getWordEnd() getRowStart() getRowEnd() methods.
EDIT: As noted by camickr in the comments, setSize() is not an appropriate way to lay out Components (as this is automatically done by the respective LayoutManager, I have removed the respective code from my answer.
Triggered by StanislavL's answer, I have found a solution to do it via JTextField, albeit by using one for each String rather than just one (as suggested by StanislavL).
I can now easily getLocation() for each JTextField. Simple, really!
I'd like to thank StanislavL for his answer, without which I'd never have though about this, and camickr for his comment.
None of the answers to two previous questions (here and here) resolve my problem.
I have a multi-column jtable for which I want to display string-content of some columns over more than one line within the cell based on newline char's ("\n") within the string. The number of newlines per string is random, known only at run-time. Only the affected row must be adjusted across all columns to the new height. There may be a different number of lines per affected column, and the row-height needs to be adjusted to the maximum height of these, across the columns.
How do I do this?If possible some sample code would be very much appreciated.TIA
If I got you right, I think you need a MultilineCellRenderer . There are already plenty of examples around. Normally they are based on a JTextArea to get the line wrap functionality.
I haven't used it myself yet, but here is an example, which looks kinda good at first view:
MultilineCellRenderer