Battleship with graphical interface
mikis69
Posted messages
174
Status
Member
-
mikis69 Posted messages 174 Status Member -
mikis69 Posted messages 174 Status Member -
Hello everyone,
I am a computer science student and I have a final year project to submit by May.
I have chosen to do a battleship game with a graphical interface.. (SWING required) (if you have other project ideas, feel free to share them with me)
I just have a question regarding the game board..
I have thought about a way to create my game board and I can't achieve the result I want except by using a button array..
Let me explain: the game board will consist of squares: 10 X 10 (with numbers 0-9 along the x-axis and letters A-J along the y-axis), so each square will have coordinates like A1, B0, C5, J9 ,...
Is there a 'normal' way to create this game board (without buttons)?
Because right now, I have my grid with my squares that can be clicked, but can you imagine having to create 100 different classes for 100 different actions based on the button clicked? Because each button will need to return its own coordinates so that I can check if there is a ship or water under that square..
So there you go, I'm pretty stuck..
Thanks to those who will read and respond to my message!
It's too bad we can't attach images to show you the result, but I’m sharing the code for my board class:
Configuration: Windows 7 / Chrome 33.0.1750.149
I am a computer science student and I have a final year project to submit by May.
I have chosen to do a battleship game with a graphical interface.. (SWING required) (if you have other project ideas, feel free to share them with me)
I just have a question regarding the game board..
I have thought about a way to create my game board and I can't achieve the result I want except by using a button array..
Let me explain: the game board will consist of squares: 10 X 10 (with numbers 0-9 along the x-axis and letters A-J along the y-axis), so each square will have coordinates like A1, B0, C5, J9 ,...
Is there a 'normal' way to create this game board (without buttons)?
Because right now, I have my grid with my squares that can be clicked, but can you imagine having to create 100 different classes for 100 different actions based on the button clicked? Because each button will need to return its own coordinates so that I can check if there is a ship or water under that square..
So there you go, I'm pretty stuck..
Thanks to those who will read and respond to my message!
It's too bad we can't attach images to show you the result, but I’m sharing the code for my board class:
package bataillemoi; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JPanel; public class Plateau { private JPanel total; private JButton mesBoutons[] = new JButton[121]; public Plateau() { JPanel grille = new JPanel(); total = new JPanel(); // Create the container that will hold the JLabel total.setLayout(new BorderLayout()); // Position it! Here, default mode grille = new JPanel(); grille.setLayout(new GridLayout(11,11)); int taille = 0, lettre = 65, numero = 0; for(int i=0;i<11;i++) { for(int j=0;j<11;j++) { mesBoutons[taille] = new JButton(""); if(i==0&&j==0) mesBoutons[taille].setBackground(Color.BLACK); if(i==0&&j!=0) { mesBoutons[taille].setText(""+numero); mesBoutons[taille].setBackground(Color.ORANGE); mesBoutons[taille].setForeground(Color.red); mesBoutons[taille].setBorder(BorderFactory.createEtchedBorder(Color.blue, Color.red)); numero++; } if(j==0&&i!=0) { mesBoutons[taille].setText(""+(char)lettre); mesBoutons[taille].setBackground(Color.ORANGE); mesBoutons[taille].setBorder(BorderFactory.createEtchedBorder(Color.blue, Color.red)); lettre++; } if(i>0&&j>0) { mesBoutons[taille].setText("."); } grille.add(mesBoutons[taille]); taille++; } } total.add(grille); } public JPanel getTotal() { return total; } public JButton[] getMesBoutons() { return mesBoutons; } } Configuration: Windows 7 / Chrome 33.0.1750.149
2 answers
-
"It's a shame that we can't attach images to show you the result"
It's to prevent abuse. But I'll include it here for you. I'll also answer your question in the meantime as I write it out.
public static void main(String[] args) { Plateau plateau = new Plateau(); JFrame frame = new JFrame(); frame.setContentPane(plateau.getTotal()); frame.pack(); frame.setVisible(true); }
-
As you did, your buttons are stored in a one-dimensional array, it might already be good to put them in a two-dimensional array to maintain the coordinates.
But in your case, this will not be enough, since given the interactions you expect, you will need to create a class (not 100, right!) to define each of your cells. In this class, which we will call Cell for example, you should register the button (or other) that will represent your cell on the Swing side, the coordinates of this cell within the matrix (which I would put static in Cell rather than in Board), as well as various fields useful for your cell.
public class Cell { private static final Cell[][] cells = new Cell[10][10]; public static Cell getCell(int i, int j) { Cell c = cells[i][j]; if (c == null) { c = new Cell(i, j); cells[i][j] = c; } return c; } private final JButton button; private final int x; private final int y; private Cell(int x, int y) { if (cells[x][y] != null) throw new IllegalStateException("Cell already exists"); this.x = x; this.y = y; button = new JButton(toString()); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("I am " + toString()); } }); cells[x][y] = this; } public JButton getButton() { return button; } @Override public String toString() { return String.format("[%c%d]", 'A'+x, y); } }
To make this work, you only need to replace this piece of code:
if(i>0&&j>0) { myButtons[size].setText("."); }
With this one:
if (i>0 && j>0) { myButtons[size] = Cell.getCell(i-1, j-1).getButton(); }
Even if, as a result, it is probably no longer useful to keep the arraymyButtons
, you need to rethink the program around the grid of cells.
--
Trust does not exclude control.-
Good evening,
Thank you for your help, your information, and your time!
So, I looked at what you explained to me and I managed (I think) to express in code what you advised..
I changed a few things, like:
- I changed what is written on the buttons..
But I managed to create my computer grid (because it's on this grid that the player will click when they want to attack)
When you click, it displays: you are on -the place you clicked on- (using button.getText();) But I think that's wrong.. Because if I display a "." on the button to indicate that the button hasn't been clicked yet, when you click on it, it will display: you are on .
A problem arises: Because the Case class has a STATIC array cases, can I only create one? Can't I create two boards that will use this class?
(I'm asking you this because I tried to create a computer board and a player board with the same plate class and it doesn't work. I had to create a computer board with the Case class and another player board with my old Plateau class..)
...
I don't know what else to say because I can't visualize the next steps of my programming.. I'm waiting for your response, if you manage to understand what I've written and I will send you the rest of my ideas..
Thanks anyway!
Good night -
Good evening,
Update: I added a setActionCommand to my buttons (from the Case class you prepared for me). Now, no matter what text appears on the button, it returns its coordinate and it's quite useful ^^
So my question remains the same: how can I create two game screens (one for the player and one for the computer) that are board-type (using your Case class)?
Because of the static final, I feel like I can only create one screen, but I need two..
Thank you!
-