[XoL]
2013-02-26 19:46:09
Hi guys back again need some more assistance… Like always please do not move out of the SRL members section.
So my assignment this time is to make the game FOURSIGHT, no AI just two player.
Right so I created a half working gui, I would like some help creating a panel in the middle and a 16 button grid in that panel..
Here is what mine looks like so far
Here is the code I have written so far, any help would be highly appreciated, I am just lost beyond lost at this point I tried everything…
So my assignment this time is to make the game FOURSIGHT, no AI just two player.
GOAL: Write a GUI-based program to provide an electronic game board for the game Foursight. This is a strategy board game sold by Lakeside games (and has recently shown up in several mobile apps). Your program should allow two people to play and make sure their moves are legal. The assignment does not include the ability of the program to play against a human.
TEAMS: You may work in teams of two persons or by yourself on this assignment.
THE GAME: The game is played by two players on a 4 by 4 game board. Each player has 8 green markers, 4 white markers, and 4 black markers. A turn consists of putting one of your markers on the game board. It is only legal to place a green marker in an empty space and only legal to place a white or black marker on a space which is currently green (i.e. "on top of" a green marker).
Although each player has both white and black markers, one player is designated the "white" player and the other the "black" player. At the end of the game (when all markers have been played) the winner is the player who has the most "three in a row"s of their color on the board.
METHOD: We'll talk about several of the details in class. In particular, you'll probably want an array of 16 JButtons arranged as the board. How these buttons react to being pressed will depend on what color the button is and whether or not the current player is holding a colored piece. Each player should have a set of three buttons (green, white, and black) that allow them to select a piece to place on the board. The program will have to keep track of how many pieces of each color the player has.
The real heart of the program lies in the ActionListeners. For each type of button, you need to figure out how it should react when pressed at various times and then code those cases into the actionPerformed method for the corresponding listener. In the case of the board buttons, you'll often want to change the background color of the button to indicate a new piece has been placed there. In order to do this, you need to call the setBackground() method on that button. So, remember that if e is an ActionEvent, e.getSource() will return the actual Java object which generated the event. In this case that will be a JButton and you'll need to cast the result of getSource to be a JButton object:
JButton b = (JButton)(e.getSource());
The game should indicate whose turn it is and only allow piece selections from that player's buttons. If a button is pressed at the wrong time, the program should play a sound and go back to waiting for button presses. For instance, clicking on a board button without selecting a piece to move, or selecting a piece when you are out of that color should produce error beeps.Right so I created a half working gui, I would like some help creating a panel in the middle and a 16 button grid in that panel..
Here is what mine looks like so far
Here is the code I have written so far, any help would be highly appreciated, I am just lost beyond lost at this point I tried everything…
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Grid extends JFrame implements ActionListener
{
private JButton panel;
public static final int WIDTH = 500;
public static final int HEIGHT = 500;
public static void main (String [] args)
{
Grid gui = new Grid( );
gui.setVisible(true);
}
public Grid( )
{
super("FourSight");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//------------------------------------
setLayout(new BorderLayout( ));
JPanel fourBoard = new JPanel( );
//fourBoard = new JPanel();
GridLayout gl = new GridLayout(4, 4);
fourBoard.setLayout(gl);
//-------------------------------------
add(fourBoard, BorderLayout.CENTER);
JButton buttonPanel = new JButton( );
buttonPanel.setBackground(Color.LIGHT_GRAY);
JButton buttonPanel2 = new JButton( );
buttonPanel2.setBackground(Color.LIGHT_GRAY);
//-------------------------------------
buttonPanel.setLayout(new BorderLayout( ));
JButton blackButton = new JButton("Black");
blackButton.setBackground(Color.BLACK);
blackButton.addActionListener(this);
buttonPanel.add(blackButton, BorderLayout.NORTH);
//-------------------------------------
JButton whiteButton = new JButton("White");
whiteButton.setBackground(Color.WHITE);
whiteButton.addActionListener(this);
buttonPanel.add(whiteButton, BorderLayout.CENTER);
//-------------------------------------
JButton greenButton = new JButton("Green");
greenButton.setBackground(Color.GREEN);
greenButton.addActionListener(this);
buttonPanel.add(greenButton, BorderLayout.SOUTH);
//-------------------------------------
//Player Two
//-------------------------------------
buttonPanel2.setLayout(new BorderLayout( ));
JButton blackButton2 = new JButton("Black");
blackButton2.setBackground(Color.BLACK);
blackButton2.addActionListener(this);
buttonPanel2.add(blackButton2, BorderLayout.NORTH);
//-------------------------------------
JButton whiteButton2 = new JButton("White");
whiteButton2.setBackground(Color.WHITE);
whiteButton2.addActionListener(this);
buttonPanel2.add(whiteButton2, BorderLayout.CENTER);
//-------------------------------------
JButton greenButton2 = new JButton("Green");
greenButton2.setBackground(Color.GREEN);
greenButton2.addActionListener(this);
buttonPanel2.add(greenButton2,BorderLayout.SOUTH);
//-------------------------------------
add(buttonPanel, BorderLayout.WEST);
add(buttonPanel2, BorderLayout.EAST);
//-------------------------------------
int i = 0;
// for (int a = 0; a > 4; a++)
// {
// for(int b = 0; b<4; b++, i++)
// {
// fourBoard.add(new JButton(), (a,b));
// }
// }
}
public void actionPerformed(ActionEvent e)
{
String buttonString = e.getActionCommand( );
if (buttonString.equals("White"))
button.setBackground(Color.WHITE);
else if (buttonString.equals("Black"))
panel.setBackground(Color.BLACK);
else if (buttonString.equals("Green"))
panel.setBackground(Color.GREEN);
else
System.out.println("We have experienced a problem");
}
}