/* Iam trying to finish this school project and Iam just a beginner in java prog
ID: 3818999 • Letter: #
Question
/* Iam trying to finish this school project and Iam just a beginner in java programming .
This program is about a game called Stop Gate played wih dominoes in a board.
I have done the half of the program but now i need to make the dominoes not to be puted up each other when clicking with mouse
and to and the game when there are only two horizontal or two vertical boxes left empty.
Would realy appreciate if Help.
StopGate is a two player game (in this case versus pc) which is played in a board, the game uses a checkerboard and a set of dominoes.A player sets his dominoes vertically and the other player horizontally.Each dominoe uses two boxes vertically for one player and two boxes horizontally for the other player. Players continue playing until there are only two boxes left empty either vertically or horizontally which determines the winner of the game.*
import java.awt.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.event.*;
class StopGate extends JFrame implements MouseListener{
private final int ROWS = 8;
private final int COLS = 8;
private final int GAP = 2;
private final int NUM = ROWS * COLS;
private int x;//index;
int index;
private JPanel pane = new JPanel(new GridLayout(ROWS, COLS, GAP, GAP));
private JPanel[] panel = new JPanel[NUM];
private Color color1 = Color.WHITE;
private Color color2 = Color.BLUE;
private Color tempColor;
int [] nr = new int[NUM];
public StopGate(){
super("Checkerboard & Domino"); System.out.println(nr[0]);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(pane);
for(x = 0; x < NUM; ++x){
panel[x] = new JPanel();
panel[x].addMouseListener(this);
//index=x;
pane.add(panel[x]);
if(x % COLS == 0)
{
tempColor = color1;
color1 = color2;
color2 = tempColor;
}
if(x % 2 == 0)
panel[x].setBackground(color1);
else
panel[x].setBackground(color2);
}
}
public void mouseClicked(MouseEvent e){
Object source = e.getSource();
for(x = 0; x < NUM-8; ++x)
{
if(source==panel[x])
{ panel[x].setBackground(Color.BLACK);
panel[x+8].setBackground(Color.BLACK);
nr[x]=1;
nr[x+8]=1;
do{
index = (int)(Math.random()*63) ;
}
while((index +1)%8 == 0||nr[index]==1||nr[index+1]==1);
panel[index].setBackground(Color.RED);
panel[index+1].setBackground(Color.RED);
nr[index]=1;
nr[index+1]=1;
break;
}
}
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public static void main(String[] args){
StopGate frame = new StopGate();
final int SIZE = 300;
frame.setSize(SIZE, SIZE);
frame.setVisible(true);
}
}
Explanation / Answer
package strategies;
import java.util.ArrayList;
import java.util.Random;
import clutches.Board;
import clutches.Hand;
import main.Dom;
import main.GameState;
import main.Loc;
import main.Status;
public class MattStrategy implements Strategy {
private Random rand = new Random();
@Override
public boolean playTile() {
if(GameState.getInstance().getStatus() != Status.PLAY_STATE) {
return false;
}
return playDom();
}
/**
* Plays a random domino from the hand.
* @return true if a dom was able to be played
*/
private boolean playDom() {
GameState game = GameState.getInstance();
Board board = game.getBoard();
Hand hand = game.getHand(game.getCurrentPlayer());
int left = board.getLeftEnd(),
right = board.getRightEnd();
ArrayList<Dom> doms = getPlayable(hand.getDoms(), left, right);
if(doms.isEmpty()) {
return false;
}
Dom ranDom = doms.get(rand.nextInt(doms.size()));
int randSide = getSide(ranDom, left, right);
if( (randSide == Loc.LEFT && ranDom.getRight() != left) ||
(randSide == Loc.RIGHT && ranDom.getLeft() != right) ){
ranDom.flip();
}
// Prevents issues when rendering the hand while it is being updated
synchronized(hand) {
board.addDom(randSide, hand.removeDom(getDomLocation(ranDom, hand)));
}
return true;
}
/**
* @return the location of the given domino in the given hand
*/
public int getDomLocation(Dom dom, Hand hand) {
ArrayList<Dom> hanDoms = hand.getDoms();
for(int i = 0; i < hanDoms.size(); i++) {
if(hanDoms.get(i) == dom) return i;
}
return -1;
}
/**
* @return the side of the board to place the domino on
*/
private int getSide(Dom dom, int left, int right) {
boolean canPlaceLeft = dom.getLeft() == left || dom.getRight() == left,
canPlaceRight = dom.getRight() == right || dom.getLeft() == right;
if(canPlaceLeft && !canPlaceRight) {
return Loc.LEFT;
} else if(!canPlaceLeft && canPlaceRight) {
return Loc.RIGHT;
} else {
if(rand.nextBoolean()) {
return Loc.LEFT;
}
return Loc.RIGHT;
}
}
/**
* @return all currently playable dominoes in a hand
*/
private ArrayList<Dom> getPlayable(ArrayList<Dom> hand, int left, int right) {
ArrayList<Dom> playable = new ArrayList<Dom>();
for(Dom dom : hand) {
if(canPlayDom(dom, left, right)) {
playable.add(dom);
}
}
return playable;
}
/**
* Determines if a given domino can be played on a board
* @return true if the domino can be played
*/
private boolean canPlayDom(Dom dom, int left, int right) {
int domLeft = dom.getLeft(), domRight = dom.getRight();
return domLeft == left || domLeft == right || domRight == left || domRight == right;
}
@Override
public String getName() {
return "Matt";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.