/** * @author 675509- *Wes Corwin *Period 2A * *Reversi Version 1.0 created by Wes Corwin */
/*The following is a two player Chess game with an exit button. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.ArrayList;
//main method which contains everything public class Chess extends JFrame implements MouseListener { //Board of Tiles which shall be used to play the game private Tile[][] board = new Tile[8][8]; //The default white pawns for the game Pawn whitePawn1 = new Pawn("White"); Pawn whitePawn2 = new Pawn("White"); Pawn whitePawn3 = new Pawn("White"); Pawn whitePawn4 = new Pawn("White"); Pawn whitePawn5 = new Pawn("White"); Pawn whitePawn6 = new Pawn("White"); Pawn whitePawn7 = new Pawn("White"); Pawn whitePawn8 = new Pawn("White"); //The default black pawns for the game Pawn blackPawn1 = new Pawn("Black"); Pawn blackPawn2 = new Pawn("Black"); Pawn blackPawn3 = new Pawn("Black"); Pawn blackPawn4 = new Pawn("Black"); Pawn blackPawn5 = new Pawn("Black"); Pawn blackPawn6 = new Pawn("Black"); Pawn blackPawn7 = new Pawn("Black"); Pawn blackPawn8 = new Pawn("Black"); //The default white Rook for the game Rook whiteRook = new Rook("White"); //The default black rook for the game Rook blackRook = new Rook("Black"); //The default white knight for the game Knight whiteKnight = new Knight("White"); //The default black knight for the game Knight blackKnight = new Knight("Black"); //The default white bishop for the game Bishop whiteBishop = new Bishop("White"); //The default black bishop for the game Bishop blackBishop = new Bishop("Black"); //The default white king for the game King whiteKing = new King("White"); //The default black king for the game King blackKing = new King("Black"); //Deafult white queen for the game Queen whiteQueen = new Queen("White"); //The default black queen for the game Queen blackQueen = new Queen("Black"); //Arraylist that keeps track of all of white's pieces private ArrayList<Piece> whitePieces= new ArrayList<Piece>(); //Arraylist that keeps track of all of black's pieces private ArrayList<Piece> blackPieces= new ArrayList<Piece>(); //Arraylist that keeps track of all of white's captured pieces private ArrayList<Piece> whiteCaptured= new ArrayList<Piece>(); //Arraylist that keeps track of all of black's captured pieces private ArrayList<Piece> blackCaptured= new ArrayList<Piece>(); //boolean which shall keep track of whose turn it is boolean turn=true; //boolean which is used for the moves boolean blocked=false; JTextField turnKeeper= new JTextField(); //JLabel used to exit the program...The Exit Button!-"When the X in the top right just isn't convenient enough!" JLabel exit= new JLabel(); //integer to keep track of the columns of the board int x=0; //integer to keep track of the rows of the board int y=0; //int to differentiate between the piece picked and the piece's move int z=0;
//The part where the GUI is contained and given attributes public Chess() {
//The content Pane- where everything was, is, and will be Container contentPane =getContentPane(); //The layout is null because layout's don't apply for this program contentPane.setLayout(null); //The program has a height and width of 600 setSize(800,800); //The location of the program is (1,1) setLocation(1,1); //The background of the program is white...even though it looks gray setBackground(Color.white); //The title is recursive because that's the title of the program setTitle("Chess"); //The program stops when I exit the program for obvious reasons setDefaultCloseOperation(EXIT_ON_CLOSE);
//The JLabel used to exit the program JLabel exit= new JLabel("Exit"); //The bounds of the exit button. Near the bottom and out of the way exit.setBounds(250,450,100,33); //The exit button's border is black...what's it to you? exit.setBorder(BorderFactory.createLineBorder(Color.black)); //The exit button has a Mouse Listener because in order to work, its need to be able to comprehend Mouse actions exit.addMouseListener(this); //The text of the exit button is centered for a better look exit.setHorizontalAlignment(JLabel.CENTER); //And the exit button is added to the content pane contentPane.add(exit); whitePieces.add(0,whitePawn1); whitePieces.add(1,whitePawn2); whitePieces.add(2,whitePawn3); whitePieces.add(3,whitePawn4); whitePieces.add(4,whitePawn5); whitePieces.add(5,whitePawn6); whitePieces.add(6,whitePawn7); whitePieces.add(7,whitePawn8); blackPieces.add(0,blackPawn1); blackPieces.add(1,blackPawn2); blackPieces.add(2,blackPawn3); blackPieces.add(3,blackPawn4); blackPieces.add(4,blackPawn5); blackPieces.add(5,blackPawn6); blackPieces.add(6,blackPawn7); blackPieces.add(7,blackPawn8); for(x=8; x<10; x++) { whitePieces.add(x, whiteKnight); blackPieces.add(x, blackKnight); } for(x=10; x<12; x++) { whitePieces.add(x, whiteBishop); blackPieces.add(x, blackBishop); } for(x=12; x<14; x++) { whitePieces.add(x, whiteRook); blackPieces.add(x, blackRook); } whitePieces.add(14, whiteQueen); whitePieces.add(15, whiteKing); blackPieces.add(14, blackQueen); blackPieces.add(15, blackKing); //The double for loop where the board is created and given attributes. The first is for the columns... for(int x=0;x<8;x++) { //The second is for the rows for(int y=0;y<8;y++) { //The board! Each is a fifty by fifty square board[y][x] = new Tile(); //The location is fifty times the row, fifty times the column board[y][x].setBounds(50*x,50*y, 50, 50); //Yes the border on the board is black. Because blue has become boring. board[y][x].setBorder(BorderFactory.createLineBorder(Color.black)); //The board can hear the mouse, thus allowing the game to be played board[y][x].addMouseListener(this); //Now the board knows that the column is its column!!! board[y][x].setColumn(x); //Now the board knows that the row is its row!!! board[y][x].setRow(y); board[y][x].setOpaque(true); //Now the text is centered board[y][x].setHorizontalAlignment(Tile.CENTER); //The number is the value of the board's square in accordance to chess's (letter, number) board[y][x].setNumber(board[y][x].getColumn()+1);
//The letter value is assigned A because this is the first row if(board[y][x].getRow()==0) { board[y][x].setLetter("A"); } //The letter value is assigned B because this is the second row, and pawns are placed along this row else if(board[y][x].getRow()==1) { board[y][x].setLetter("B"); board[y][x].setFirstMove(true); board[y][x].setText("BP"); } //The letter value is assigned C because this is the third row else if(board[y][x].getRow()==2) { board[y][x].setLetter("C"); } //The letter value is assigned D because this is the fourth row else if(board[y][x].getRow()==3) { board[y][x].setLetter("D"); } //The letter value is assigned E because this is the fifth row else if(board[y][x].getRow()==4) { board[y][x].setLetter("E"); } //The letter value is assigned F because this is the sixth row else if(board[y][x].getRow()==5) { board[y][x].setLetter("F"); } //The letter value is assigned G because this is the seventh row, and pawns are placed along this row else if(board[y][x].getRow()==6) { board[y][x].setLetter("G"); board[y][x].setFirstMove(true); board[y][x].setText("WP"); } //The letter value is assigned H because this is the eighth row else if(board[y][x].getRow()==7) { board[y][x].setLetter("H"); } //And the board is added to the contentPane contentPane.add(board[y][x]); } } board[6][0].setPiece(whitePawn1); board[6][1].setPiece(whitePawn2); board[6][2].setPiece(whitePawn3); board[6][3].setPiece(whitePawn4); board[6][4].setPiece(whitePawn5); board[6][5].setPiece(whitePawn6); board[6][6].setPiece(whitePawn7); board[6][7].setPiece(whitePawn8); board[1][0].setPiece(blackPawn1); board[1][1].setPiece(blackPawn2); board[1][2].setPiece(blackPawn3); board[1][3].setPiece(blackPawn4); board[1][4].setPiece(blackPawn5); board[1][5].setPiece(blackPawn6); board[1][6].setPiece(blackPawn7); board[1][7].setPiece(blackPawn8); for(x=0; x<8; x=x+2) { for(y=1; y<8; y=y+2) { board[y][x].setBackground(Color.green); } }
board[0][3].setPiece(blackQueen); board[7][3].setPiece(whiteQueen); board[0][4].setPiece(blackKing); board[7][4].setPiece(whiteKing); board[0][3].setText("BQ"); board[7][3].setText("WQ"); board[0][4].setText("BK"); board[7][4].setText("WK"); board[7][4].setFirstMove(true); } //used to make GUI public static void main (String[] args) { //frame of GUI Chess frame= new Chess(); frame.setVisible(true); }
//method used when mouse is clicked public void mouseClicked(MouseEvent me) { //Sets all methods that occur when a Tile is clicked if(me.getSource() instanceof Tile) { Tile clicked = (Tile)me.getSource(); { if(turn) { if(z==0) { if(clicked.getPiece()!=null&&clicked.getPiece().getColor()=="White") { y= clicked.getRow(); x=clicked.getColumn(); System.out.println("("+board[y][x].getLetter()+","+board[y][x].getNumber()+") to...?"); z++; } } else if(z==1&&clicked.getPiece()==null||clicked.getPiece().getColor()!="White") { if(board[y][x].getPiece().pawnCheck==true) { if(clicked.getText()==""&&clicked.getRow()+2==y&&clicked.getColumn()==x&&board[y-1][x].getText()==""&&board[y][x].getFirstMove()==true||clicked.getRow()+1==y&&clicked.getColumn()==x) { clicked.setPiece(board[y][x].getPiece()); clicked.setText("WP"); clicked.setFirstMove(false); board[y][x].setPiece(null); board[y][x].setText(""); z=0; turn=!turn; } else if(clicked.getRow()+1==y&&Math.abs(clicked.getColumn()-x)==1&&clicked.getPiece()!=null) { clicked.setPiece(board[y][x].getPiece()); clicked.setText("WP"); clicked.setFirstMove(false); board[y][x].setPiece(null); board[y][x].setText(""); z=0; turn=!turn; }
public void setOccupied(boolean n) { occupied=n; }
public boolean getOccupied(boolean n) { return occupied; }
//allows player to set Tile column public void setColumn(int n) { column=n; } //allows player to get Tile column public int getColumn() { return column; } //allows player to set Tile row public void setRow(int n) { row=n; } //allows player to get Tile row public int getRow() { return row; } public void setText(String string) { // TODO Auto-generated method stub
} }
/** * */ import java.awt.Color; import javax.swing.*; /** * @author wcorwin * */ //This is the class that creates the parameters for which the Tile is created public class Tile extends JLabel { private static final Piece Pawn = null; //The int for the Tile's height int height; //The int for the Tile's width int width; //The tile's column int column=0; //The tile's row int row=0; String letter= ""; int number= 100; Piece piece= null; boolean firstMove=false; /** * */ public Tile() { //takes all JLabel attributes for Tile super(); //sets default Tile height height=0; //sets default Tile width width=0; //sets default Tile column column = 0; //sets default Tile row row=0; } //sets another Tile method public Tile(int y,int x) { //takes all JLabel attributes for Tile super(); //sets Tile height as x height=0; //sets Tile width as y width=0; //sets default Tile column column = y; //sets default Tile row row=x; piece=null; } //allows player to set Tile column public void setColumn(int n) { column=n; } //allows player to get Tile column public int getColumn() { return column; } //allows player to set Tile row public void setRow(int n) { row=n; } //allows player to get Tile row public int getRow() { return row; }
public void setLetter(String n) { letter =n; }
public String getLetter() { return letter; }
public void setNumber(int n) { number=n; }
public int getNumber() { return number; }
public void setPiece(Piece n) { piece= n; }
public Piece getPiece() { return piece; } public void setFirstMove(boolean n) { firstMove=n; }
public boolean getFirstMove() { return firstMove; }
public Pawn getPawn() { Pawn n= new Pawn(); if(getPiece()== n) { return n; }
else { return null; } } }
import java.util.ArrayList; import javax.swing.*; /** * @author wcorwin * */ public class Pawn extends Piece { private ImageIcon icon = new ImageIcon("sp001.jpg"); private Tile white = new Tile(); boolean moveHere;
public Pawn() { super(); points=1; occupied=true; pawnCheck=true; }
public Pawn(String n) { super(); points=1; occupied=true; color=n; pawnCheck=true; } public boolean doPawnCheck() { return true; } }
import java.util.ArrayList; import javax.swing.*;
/** * @author wcorwin * */ public class Rook extends Queen { private ArrayList<Tile> white = new ArrayList<Tile>(); private ImageIcon icon = new ImageIcon("sp004.jpg");
public Rook(String n) { super(n); points=3; occupied=true; color=n; }
public boolean rookCheck() { return true; }
public ArrayList<Tile> moveHere() {
return white; } }
import javax.swing.ImageIcon;
public class Queen extends Piece { private ImageIcon icon = new ImageIcon("queen.jpg"); public Queen(String n) { super(); points=9; occupied=true; color=n; }
public boolean queenCheck() { return true; } }
import javax.swing.ImageIcon;
/** * @author wcorwin * */ public class King extends Piece { private ImageIcon icon = new ImageIcon("king.jpg");
public King(String n) { super(); points=9001; occupied=true; color=n; }
public boolean kingCheck() { return true; }
public void MoveKing(Tile n) { King King = new King(getColor()); n.setPiece(King); } }
import java.util.ArrayList;
import javax.swing.ImageIcon;
/** * @author wcorwin */ public class Knight extends Piece { private ImageIcon icon = new ImageIcon("sp003.jpg");
public Knight() { super(); points=3; occupied=true; ImageIcon icon= new ImageIcon("sp003.jpg"); }
public Knight(String n) { super(); points=3; occupied=true; color=n; }
public boolean KnightCheck() { return true; } }
Locusts,
Foxes, and Heart Attacks
I. Introduction A. Lillian Hellman wrote “The
Little Foxes” to convey a message
B.
There are three types of people on the Earth: those who eat the Earth, those who watch the eaters, and those who try and
fight against them.
II. Topic 1: Person Type 1- The Greedy
A.
“Well, there are people who eat the earth and eat all the people on it.” pg 205, Addie from “The Little
Foxes” and “[Ben] shows no interest in human relations beyond the use he makes of them to achieve financial
domination [of the town] where he
was born,” from DISCovering Authors.
B.
This quote establishes the first type of person- the greedy
C.
The locusts of the people ate the people of Egypt and the land itself
endlessly. They were insatiable
and unstoppable.
D.
Addie compares the greedy Hubbards to the locusts of Egypt who destroyed everything around them without regard
for the consequences to those around.
E.
This establishes how the Hubbards are like locusts, without regard to those
around them and looking out entirely
for themselves.
F.
“All [of Lillian Hellman’s] plays are built around two consuming obsessions: family and capital,” Ellen Moers “Family
Theatre” in page 187 of American Literary Criticism.
G.
All the Hubbards desire capital and the privileges that come with it, but if
weren’t for the corrupt around them,
the cycle wouldn’t continue.
III. Topic 2: Person Type 2- The Corrupt
A.
“Then there are people who stand around and watch them eat it...”pg 205, Addie,
“Little Foxes” and “Hellman constantly
makes the point, equally applicable to private and
public affairs, that it is immoral to remain passive when evil is being done.”
From Katharine M. Rogers on page
305 of American Women Writers.
B.
This quote establishes the second type of person- the corrupt
C.
The quote suggests those that do nothing to assist those being eaten by the
locusts, the ones that look at
injustice and do nothing to stop it.
D.
The corrupt are those who can look at injustice and do nothing. Like Birdie, Addie, and originally Oscar, who watched the
Hubbards take advantage of those around
them and do nothing to stop them.
E.
This sets up the idea that both kinds of people are evil as neither take any
interest in helping those around
them.
F.
“The least attractive qualities of the Hellman people of their claustrophobic clannishness and their arrogant assumption
that the greatest battles of the outside world
are secondary to, but distantly dependent on, their family squabbles.” From EllenMoers “Family Theatre” in
page 187 of American Literary Criticism.
G.
Both the greedy and the corrupt perpetuate the cycle that eats the Earth and
the people that live on it, but the
character almost non-existent in the play is the systemic anomaly: The other.
IV. Topic 3: Person Type 3- The
Others
A.
“Sometimes I think it ain't right to stand and watch them do it." pg 205,
Addie “Little Foxes” and “[I’ll]
be fighting… some place where people don’t just stand around and watch.”
Alexandra, “The Little Foxes”
B.
This quote establishes the idea of breaking the norm, to act differently from
the two possibilities.
C.
Presents the thought that someone should stand up against injustice and greed
D.
The play presents those that try to break the mold, but are too weak to do so. Horace’s resistance poses little to Regina as all she has to
do is wait for him to die. And
Zan’s resistance is a glimmer of hope at the end, although there is no
guarantee that anything will come of
it.
E.
The fact that most of the nobler characters are too weak to defend justice
against the evil of the Hubbards
F.
“Hellman was disappointed in the frequent reception of Regina and her brothers as simple villains in a melodrama…’I had meant
the audience to recognize some part of themselves
in the money dominated Hubbards…” from the Facts on File Companion to American Drama.
G.
Hellman desired that people would recognize a piece of themselves in the Hubbards so they might try and change from
greedy and corrupt to more honest and noble.
V. Conclusion
A.
There are three types of people on the Earth: those who eat the Earth, those who watch the eaters, and those who try and
fight against them.
B.
Hellman was reported to be disappointed that the Hubbards were only seen as melodramatic villains, that her
audience didn’t see a part of themselves in each of them.
C.
The three types of people in the world as suggested by Lillian Hellman are the greedy- those who eat the earth and
those on it- the corrupt – those who watch them do it- and the others, -those who try and stop them.
D.
Hellman only introduced two kinds of people because she wanted her audience to create the third. How many will answer the
call?
import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.Random; /** * */ /** * @author wcorwin * */ public class Battleship extends JFrame implements MouseListener { //Board of Tiles which shall be used to play the game private Tile[][] playerBoard = new Tile[10][10]; private Tile[][] enemyBoard = new Tile[10][10]; //JLabel used to exit the program...The Exit Button!-"When the X in the top right just isn't convenient enough!" JLabel exit= new JLabel(); //integer to keep track of the columns of the board JLabel yourBoard = new JLabel(); JLabel hisBoard = new JLabel(); JLabel battleship = new JLabel(); JLabel carrier = new JLabel(); JLabel destroyer= new JLabel(); JLabel submarine= new JLabel(); JLabel minesweeper = new JLabel(); int x=0; //integer to keep track of the rows of the board int y=0; //Later, I'm going to have to reference two different tiles. This will be Tile one's row int a=0; //Later, I'm going to have to reference two different tiles. This will be Tile one's row int b=0; int c=0; int battleshipCheck=0; int carrierCheck=0; int destroyerCheck =0; int submarineCheck=0; int minesweeperCheck=0; boolean turn=true; int knowledge1=-1; int knowledge2=-1;
public Battleship() { //The content Pane- where everything was, is, and will be Container contentPane =getContentPane(); //The layout is null because layout's don't apply for this program contentPane.setLayout(null); //The program has a height and width of 600 setSize(800,600); //The location of the program is (1,1) setLocation(1,1); //The background of the program is white...even though it looks gray setBackground(Color.white); //The title is Battleship because that's the title of the program setTitle("Battleship"); //The program stops when I exit the program for obvious reasons setDefaultCloseOperation(EXIT_ON_CLOSE);
JLabel yourBoard = new JLabel("Your board"); //The bounds of the exit button. Near the bottom and out of the way yourBoard.setBounds(110,30,90,36); //The exit button's border is blue...don't judge me. yourBoard.setBorder(BorderFactory.createLineBorder(Color.blue)); //The exit button has a Mouse Listener because in order to work, its need to be able to comprehend Mouse actions yourBoard.addMouseListener(this); //The text of the exit button is centered for a better look yourBoard.setHorizontalAlignment(JLabel.CENTER); //And the exit button is added to the content pane contentPane.add(yourBoard);
JLabel hisBoard = new JLabel("Enemy Board"); //The bounds of the exit button. Near the bottom and out of the way hisBoard.setBounds(610,30,90,36); //The exit button's border is blue...don't judge me. hisBoard.setBorder(BorderFactory.createLineBorder(Color.blue)); //The exit button has a Mouse Listener because in order to work, its need to be able to comprehend Mouse actions hisBoard.addMouseListener(this); //The text of the exit button is centered for a better look hisBoard.setHorizontalAlignment(JLabel.CENTER); //And the exit button is added to the content pane contentPane.add(hisBoard);
if(carrierCheck==3&&battleshipCheck==3&&submarineCheck==3&&minesweeperCheck==3&&destroyerCheck==3) { JOptionPane.showMessageDialog(null, "All Right, Let's do this!"); }
//The double for loop where the board is created and given attributes. The first is for the columns... for(int x=0;x<10;x++) { //The second is for the rows for(int y=0;y<10;y++) { //The board! Each is a thirty by thirty square playerBoard[x][y] = new Tile(30,30); //The location is fifty times the row, fifty times the column playerBoard[x][y].setLocation(30*x,100+30*y); //Yes the border on the board is blue. Because black is boring. playerBoard[x][y].setBorder(BorderFactory.createLineBorder(Color.blue)); //The board can hear the mouse, thus allowing the game to be played playerBoard[x][y].addMouseListener(this); //Now the board knows that the column is its column!!! playerBoard[x][y].setColumn(x); //Now the board knows that the column is its column!!! playerBoard[x][y].setRow(y); //Now the text is centered playerBoard[x][y].setController("Player"); playerBoard[x][y].setHorizontalAlignment(Tile.CENTER); //And the board is added to the contentPane contentPane.add(playerBoard[x][y]);
//The board! Each is a thirty by thirty square enemyBoard[x][y] = new Tile(30,30); //The location is fifty times the row, fifty times the column enemyBoard[x][y].setLocation(500+30*x,100+30*y); //Yes the border on the board is blue. Because black is boring. enemyBoard[x][y].setBorder(BorderFactory.createLineBorder(Color.blue)); //The board can hear the mouse, thus allowing the game to be played enemyBoard[x][y].addMouseListener(this); //Now the board knows that the column is its column!!! enemyBoard[x][y].setColumn(x); //Now the board knows that the column is its column!!! enemyBoard[x][y].setRow(y); //Now the text is centered enemyBoard[x][y].setController("Computer"); enemyBoard[x][y].setHorizontalAlignment(Tile.CENTER); //And the board is added to the contentPane contentPane.add(enemyBoard[x][y]); } } } /** * @param args */ public static void main(String[] args) { //frame of GUI Battleship frame= new Battleship(); frame.setVisible(true); }
public void mouseClicked(MouseEvent me) { if(me.getSource() instanceof Tile) { Tile clicked = (Tile) me.getSource(); { if(carrierCheck==3&&battleshipCheck==3&&submarineCheck==3&&destroyerCheck==3&&minesweeperCheck==3) if(turn) { if(clicked.getController().equals("Computer")) { if(clicked.getType().equals("Ship")) { clicked.setText("HIT"); } else { clicked.setText("MISS"); turn=!turn; } } } else { if(knowledge1==-1&&knowledge2==-1) { Random rand= new Random(); int f= rand.nextInt(10); int g= rand.nextInt(10);
if(me.getSource() instanceof JLabel) { JLabel clicked = (JLabel) me.getSource(); { if(clicked.getText().equals("Battleship")&&battleshipCheck==0) { int width= Integer.parseInt(JOptionPane.showInputDialog(null, "What is the piece's width that you would like to place the battleship on?")); int height= Integer.parseInt(JOptionPane.showInputDialog(null, "What is the piece's height that you would like to place the battleship on?")); String direction = JOptionPane.showInputDialog(null, "Which direction would you like it to go? (Up, Down, Left, Right)");
if(playerBoard[width][height].getText().equals("")) { if(playerBoard[width][height].getColumn()>2&&direction.equals("Left")) { if(playerBoard[width-1][height].getType().equals("Ship")||playerBoard[width-2][height].getType().equals("Ship")||playerBoard[width-3][height].getType().equals("Ship")) { JOptionPane.showMessageDialog(null, "I'm afraid I can't do that, Jim..."); } else { for(int n=width; n> width-4; n--) { if(playerBoard[n][height].getText().equals("")) { playerBoard[n][height].setText("Bat"); playerBoard[n][height].setType("Ship"); battleshipCheck=1; } } } } else if(playerBoard[width][height].getColumn()<7&&direction.equals("Right")) { if(playerBoard[width+1][height].getType().equals("Ship")||playerBoard[width+2][height].getType().equals("Ship")||playerBoard[width+3][height].getType().equals("Ship")) { JOptionPane.showMessageDialog(null, "I'm afraid I can't do that, Jim..."); } else { for(int n=width; n< width+4; n++) { if(playerBoard[n][height].getText().equals("")) { playerBoard[n][height].setText("Bat"); playerBoard[n][height].setType("Ship"); battleshipCheck=1; } } } } else if(playerBoard[width][height].getRow()>2&&direction.equals("Up")) { if(playerBoard[width][height-1].getType().equals("Ship")||playerBoard[width][height-2].getType().equals("Ship")||playerBoard[width][height-3].getType().equals("Ship")) { JOptionPane.showMessageDialog(null, "I'm afraid I can't do that, Jim..."); } else { for(int n=height; n> height-4; n--) { if(playerBoard[width][n].getText().equals("")) { playerBoard[width][n].setText("Bat"); playerBoard[width][n].setType("Ship"); battleshipCheck=1; } } } } else if(playerBoard[width][height].getRow()<7&&direction.equals("Down")) { if(playerBoard[width][height+1].getType().equals("Ship")||playerBoard[width][height+2].getType().equals("Ship")||playerBoard[width][height+3].getType().equals("Ship")) { JOptionPane.showMessageDialog(null, "I'm afraid I can't do that, Jim..."); } else { for(int n=height; n< height+4; n++) { if(playerBoard[width][n].getText().equals("")) { playerBoard[width][n].setText("Bat"); playerBoard[width][n].setType("Ship"); battleshipCheck=1; } } } } } } else if(clicked.getText().equals("Carrier")&&carrierCheck==0) { int width= Integer.parseInt(JOptionPane.showInputDialog(null, "What is the piece's width that you would like to place the carrier on?")); int height= Integer.parseInt(JOptionPane.showInputDialog(null, "What is the piece's height that you would like to place the carrier on?")); String direction = JOptionPane.showInputDialog(null, "Which direction would you like it to go? (Up, Down, Left, Right)");
if(playerBoard[width][height].getText().equals("")) { if(playerBoard[width][height].getColumn()>3&&direction.equals("Left")) { if(playerBoard[width-1][height].getType().equals("Ship")||playerBoard[width-2][height].getType().equals("Ship")||playerBoard[width-3][height].getType().equals("Ship")||playerBoard[width-4][height].getType().equals("Ship")) { JOptionPane.showMessageDialog(null, "I'm afraid I can't do that, Jim..."); } else { for(int n=width; n> width-5; n--) { if(playerBoard[n][height].getText().equals("")) { playerBoard[n][height].setText("Car"); playerBoard[n][height].setType("Ship"); carrierCheck=1; } } } } else if(playerBoard[width][height].getColumn()<6&&direction.equals("Right")) { if(playerBoard[width+1][height].getType().equals("Ship")||playerBoard[width+2][height].getType().equals("Ship")||playerBoard[width+3][height].getType().equals("Ship")||playerBoard[width+4][height].getType().equals("Ship")) { JOptionPane.showMessageDialog(null, "I'm afraid I can't do that, Jim..."); } else { for(int n=width; n< width+5; n++) { if(playerBoard[n][height].getText().equals("")) { playerBoard[n][height].setText("Car"); playerBoard[n][height].setType("Ship"); carrierCheck=1; } } } } else if(playerBoard[width][height].getRow()>3&&direction.equals("Up")) { if(playerBoard[width][height-1].getType().equals("Ship")||playerBoard[width][height-2].getType().equals("Ship")||playerBoard[width][height-3].getType().equals("Ship")||playerBoard[width][height-4].getType().equals("Ship")) { JOptionPane.showMessageDialog(null, "I'm afraid I can't do that, Jim..."); } else { for(int n=height; n> height-5; n--) { if(playerBoard[width][n].getText().equals("")) { playerBoard[width][n].setText("Car"); playerBoard[width][n].setType("Ship"); carrierCheck=1; } } } } else if(playerBoard[width][height].getRow()<6&&direction.equals("Down")) { if(playerBoard[width][height+1].getType().equals("Ship")||playerBoard[width][height+2].getType().equals("Ship")||playerBoard[width][height+3].getType().equals("Ship")||playerBoard[width][height+4].getType().equals("Ship")) { JOptionPane.showMessageDialog(null, "I'm afraid I can't do that, Jim..."); } else { for(int n=height; n< height+5; n++) { if(playerBoard[width][n].getText().equals("")) { playerBoard[width][n].setText("Car"); playerBoard[width][n].setType("Ship"); carrierCheck=1; } } } } } } else if(clicked.getText().equals("Destroyer")&&destroyerCheck==0) { int width= Integer.parseInt(JOptionPane.showInputDialog(null, "What is the piece's width that you would like to place the destroyer on?")); int height= Integer.parseInt(JOptionPane.showInputDialog(null, "What is the piece's height that you would like to place the destroyer on?")); String direction = JOptionPane.showInputDialog(null, "Which direction would you like it to go? (Up, Down, Left, Right)");
else if(clicked.getText().equals("Submarine")&&submarineCheck==0) { int width= Integer.parseInt(JOptionPane.showInputDialog(null, "What is the piece's width that you would like to place the sub on?")); int height= Integer.parseInt(JOptionPane.showInputDialog(null, "What is the piece's height that you would like to place the sub on?")); String direction = JOptionPane.showInputDialog(null, "Which direction would you like it to go? (Up, Down, Left, Right)");
else if(clicked.getText().equals("Minesweeper")&&minesweeperCheck==0) { int width= Integer.parseInt(JOptionPane.showInputDialog(null, "What is the piece's width that you would like to place the sweeper on?")); int height= Integer.parseInt(JOptionPane.showInputDialog(null, "What is the piece's height that you would like to place the sweeper on?")); String direction = JOptionPane.showInputDialog(null, "Which direction would you like it to go? (Up, Down, Left, Right)");
else { for(int i=b; i>b-5; i--) { enemyBoard[a][i].setType("Ship"); enemyBoard[a][i].setText("Car"); } } } } carrierCheck++; } } } } } //method used when mouse is pressed public void mousePressed(MouseEvent me) { } //method used when mouse is entered public void mouseEntered(MouseEvent me) { } //method used when mouse is exited public void mouseExited(MouseEvent me) { } //method used when mouse is released public void mouseReleased(MouseEvent me) { } }
import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.Random; /** * */ /** * @author wcorwin * */ public class Battleship extends JFrame implements MouseListener { //Board of Tiles which shall be used to play the game private Tile[][] playerBoard = new Tile[10][10]; private Tile[][] enemyBoard = new Tile[10][10]; //JLabel used to exit the program...The Exit Button!-"When the X in the top right just isn't convenient enough!" JLabel exit= new JLabel(); //integer to keep track of the columns of the board JLabel yourBoard = new JLabel(); JLabel hisBoard = new JLabel(); JLabel battleship = new JLabel(); JLabel carrier = new JLabel(); JLabel destroyer= new JLabel(); JLabel submarine= new JLabel(); JLabel minesweeper = new JLabel(); int x=0; //integer to keep track of the rows of the board int y=0; //Later, I'm going to have to reference two different tiles. This will be Tile one's row int a=0; //Later, I'm going to have to reference two different tiles. This will be Tile one's row int b=0; int c=0; int battleshipCheck=0; int carrierCheck=0; int destroyerCheck =0; int submarineCheck=0; int minesweeperCheck=0;
public Battleship() { //The content Pane- where everything was, is, and will be Container contentPane =getContentPane(); //The layout is null because layout's don't apply for this program contentPane.setLayout(null); //The program has a height and width of 600 setSize(800,600); //The location of the program is (1,1) setLocation(1,1); //The background of the program is white...even though it looks gray setBackground(Color.white); //The title is Battleship because that's the title of the program setTitle("Battleship"); //The program stops when I exit the program for obvious reasons setDefaultCloseOperation(EXIT_ON_CLOSE);
JLabel yourBoard = new JLabel("Your board"); //The bounds of the exit button. Near the bottom and out of the way yourBoard.setBounds(110,30,90,36); //The exit button's border is blue...don't judge me. yourBoard.setBorder(BorderFactory.createLineBorder(Color.blue)); //The exit button has a Mouse Listener because in order to work, its need to be able to comprehend Mouse actions yourBoard.addMouseListener(this); //The text of the exit button is centered for a better look yourBoard.setHorizontalAlignment(JLabel.CENTER); //And the exit button is added to the content pane contentPane.add(yourBoard);
JLabel hisBoard = new JLabel("Enemy Board"); //The bounds of the exit button. Near the bottom and out of the way hisBoard.setBounds(610,30,90,36); //The exit button's border is blue...don't judge me. hisBoard.setBorder(BorderFactory.createLineBorder(Color.blue)); //The exit button has a Mouse Listener because in order to work, its need to be able to comprehend Mouse actions hisBoard.addMouseListener(this); //The text of the exit button is centered for a better look hisBoard.setHorizontalAlignment(JLabel.CENTER); //And the exit button is added to the content pane contentPane.add(hisBoard);
//The double for loop where the board is created and given attributes. The first is for the columns... for(int x=0;x<10;x++) { //The second is for the rows for(int y=0;y<10;y++) { //The board! Each is a thirty by thirty square playerBoard[x][y] = new Tile(30,30); //The location is fifty times the row, fifty times the column playerBoard[x][y].setLocation(30*x,100+30*y); //Yes the border on the board is blue. Because black is boring. playerBoard[x][y].setBorder(BorderFactory.createLineBorder(Color.blue)); //The board can hear the mouse, thus allowing the game to be played playerBoard[x][y].addMouseListener(this); //Now the board knows that the column is its column!!! playerBoard[x][y].setColumn(x); //Now the board knows that the column is its column!!! playerBoard[x][y].setRow(y); //Now the text is centered playerBoard[x][y].setController("Player"); playerBoard[x][y].setHorizontalAlignment(Tile.CENTER); //And the board is added to the contentPane contentPane.add(playerBoard[x][y]);
//The board! Each is a thirty by thirty square enemyBoard[x][y] = new Tile(30,30); //The location is fifty times the row, fifty times the column enemyBoard[x][y].setLocation(500+30*x,100+30*y); //Yes the border on the board is blue. Because black is boring. enemyBoard[x][y].setBorder(BorderFactory.createLineBorder(Color.blue)); //The board can hear the mouse, thus allowing the game to be played enemyBoard[x][y].addMouseListener(this); //Now the board knows that the column is its column!!! enemyBoard[x][y].setColumn(x); //Now the board knows that the column is its column!!! enemyBoard[x][y].setRow(y); //Now the text is centered enemyBoard[x][y].setController("Computer"); enemyBoard[x][y].setHorizontalAlignment(Tile.CENTER); //And the board is added to the contentPane contentPane.add(enemyBoard[x][y]); } } } /** * @param args */ public static void main(String[] args) { //frame of GUI Battleship frame= new Battleship(); frame.setVisible(true); }
if(me.getSource() instanceof JLabel) { JLabel clicked = (JLabel) me.getSource(); { if(clicked.getText().equals("Battleship")&&battleshipCheck==0) { int width= Integer.parseInt(JOptionPane.showInputDialog(null, "What is the piece's width that you would like to place the battleship on?")); int height= Integer.parseInt(JOptionPane.showInputDialog(null, "What is the piece's height that you would like to place the battleship on?")); String direction = JOptionPane.showInputDialog(null, "Which direction would you like it to go? (Up, Down, Left, Right)");
if(playerBoard[width][height].getText().equals("")) { if(playerBoard[width][height].getColumn()>2&&direction.equals("Left")) { if(playerBoard[width-1][height].getType().equals("Ship")||playerBoard[width-2][height].getType().equals("Ship")||playerBoard[width-3][height].getType().equals("Ship")) { JOptionPane.showMessageDialog(null, "I'm afraid I can't do that, Jim..."); } else { for(int n=width; n> width-4; n--) { if(playerBoard[n][height].getText().equals("")) { playerBoard[n][height].setText("Bat"); playerBoard[n][height].setType("Ship"); battleshipCheck=1; } } } } else if(playerBoard[width][height].getColumn()<7&&direction.equals("Right")) { if(playerBoard[width+1][height].getType().equals("Ship")||playerBoard[width+2][height].getType().equals("Ship")||playerBoard[width+3][height].getType().equals("Ship")) { JOptionPane.showMessageDialog(null, "I'm afraid I can't do that, Jim..."); } else { for(int n=width; n< width+4; n++) { if(playerBoard[n][height].getText().equals("")) { playerBoard[n][height].setText("Bat"); playerBoard[n][height].setType("Ship"); battleshipCheck=1; } } } } else if(playerBoard[width][height].getRow()>2&&direction.equals("Up")) { if(playerBoard[width][height-1].getType().equals("Ship")||playerBoard[width][height-2].getType().equals("Ship")||playerBoard[width][height-3].getType().equals("Ship")) { JOptionPane.showMessageDialog(null, "I'm afraid I can't do that, Jim..."); } else { for(int n=height; n> height-4; n--) { if(playerBoard[width][n].getText().equals("")) { playerBoard[width][n].setText("Bat"); playerBoard[width][n].setType("Ship"); battleshipCheck=1; } } } } else if(playerBoard[width][height].getRow()<7&&direction.equals("Down")) { if(playerBoard[width][height+1].getType().equals("Ship")||playerBoard[width][height+2].getType().equals("Ship")||playerBoard[width][height+3].getType().equals("Ship")) { JOptionPane.showMessageDialog(null, "I'm afraid I can't do that, Jim..."); } else { for(int n=height; n< height+4; n++) { if(playerBoard[width][n].getText().equals("")) { playerBoard[width][n].setText("Bat"); playerBoard[width][n].setType("Ship"); battleshipCheck=1; } } } } } } else if(clicked.getText().equals("Carrier")&&carrierCheck==0) { int width= Integer.parseInt(JOptionPane.showInputDialog(null, "What is the piece's width that you would like to place the carrier on?")); int height= Integer.parseInt(JOptionPane.showInputDialog(null, "What is the piece's height that you would like to place the carrier on?")); String direction = JOptionPane.showInputDialog(null, "Which direction would you like it to go? (Up, Down, Left, Right)");
if(playerBoard[width][height].getText().equals("")) { if(playerBoard[width][height].getColumn()>3&&direction.equals("Left")) { if(playerBoard[width-1][height].getType().equals("Ship")||playerBoard[width-2][height].getType().equals("Ship")||playerBoard[width-3][height].getType().equals("Ship")||playerBoard[width-4][height].getType().equals("Ship")) { JOptionPane.showMessageDialog(null, "I'm afraid I can't do that, Jim..."); } else { for(int n=width; n> width-5; n--) { if(playerBoard[n][height].getText().equals("")) { playerBoard[n][height].setText("Car"); playerBoard[n][height].setType("Ship"); carrierCheck=1; } } } } else if(playerBoard[width][height].getColumn()<6&&direction.equals("Right")) { if(playerBoard[width+1][height].getType().equals("Ship")||playerBoard[width+2][height].getType().equals("Ship")||playerBoard[width+3][height].getType().equals("Ship")||playerBoard[width+4][height].getType().equals("Ship")) { JOptionPane.showMessageDialog(null, "I'm afraid I can't do that, Jim..."); } else { for(int n=width; n< width+5; n++) { if(playerBoard[n][height].getText().equals("")) { playerBoard[n][height].setText("Car"); playerBoard[n][height].setType("Ship"); carrierCheck=1; } } } } else if(playerBoard[width][height].getRow()>3&&direction.equals("Up")) { if(playerBoard[width][height-1].getType().equals("Ship")||playerBoard[width][height-2].getType().equals("Ship")||playerBoard[width][height-3].getType().equals("Ship")||playerBoard[width][height-4].getType().equals("Ship")) { JOptionPane.showMessageDialog(null, "I'm afraid I can't do that, Jim..."); } else { for(int n=height; n> height-5; n--) { if(playerBoard[width][n].getText().equals("")) { playerBoard[width][n].setText("Car"); playerBoard[width][n].setType("Ship"); carrierCheck=1; } } } } else if(playerBoard[width][height].getRow()<6&&direction.equals("Down")) { if(playerBoard[width][height+1].getType().equals("Ship")||playerBoard[width][height+2].getType().equals("Ship")||playerBoard[width][height+3].getType().equals("Ship")||playerBoard[width][height+4].getType().equals("Ship")) { JOptionPane.showMessageDialog(null, "I'm afraid I can't do that, Jim..."); } else { for(int n=height; n< height+5; n++) { if(playerBoard[width][n].getText().equals("")) { playerBoard[width][n].setText("Car"); playerBoard[width][n].setType("Ship"); carrierCheck=1; } } } } } } else if(clicked.getText().equals("Destroyer")&&destroyerCheck==0) { int width= Integer.parseInt(JOptionPane.showInputDialog(null, "What is the piece's width that you would like to place the destroyer on?")); int height= Integer.parseInt(JOptionPane.showInputDialog(null, "What is the piece's height that you would like to place the destroyer on?")); String direction = JOptionPane.showInputDialog(null, "Which direction would you like it to go? (Up, Down, Left, Right)");
else if(clicked.getText().equals("Submarine")&&submarineCheck==0) { int width= Integer.parseInt(JOptionPane.showInputDialog(null, "What is the piece's width that you would like to place the sub on?")); int height= Integer.parseInt(JOptionPane.showInputDialog(null, "What is the piece's height that you would like to place the sub on?")); String direction = JOptionPane.showInputDialog(null, "Which direction would you like it to go? (Up, Down, Left, Right)");
else if(clicked.getText().equals("Minesweeper")&&minesweeperCheck==0) { int width= Integer.parseInt(JOptionPane.showInputDialog(null, "What is the piece's width that you would like to place the sweeper on?")); int height= Integer.parseInt(JOptionPane.showInputDialog(null, "What is the piece's height that you would like to place the sweeper on?")); String direction = JOptionPane.showInputDialog(null, "Which direction would you like it to go? (Up, Down, Left, Right)");
else { for(int i=b; i>b-5; i--) { enemyBoard[a][i].setType("Ship"); enemyBoard[a][i].setText("Car"); } } } } carrierCheck++; } } } } } //method used when mouse is pressed public void mousePressed(MouseEvent me) { } //method used when mouse is entered public void mouseEntered(MouseEvent me) { } //method used when mouse is exited public void mouseExited(MouseEvent me) { } //method used when mouse is released public void mouseReleased(MouseEvent me) { } }
/** * */ import java.awt.Color; import javax.swing.*; /** * @author wcorwin * */ public class Tile extends JLabel { int height; int width; String type; String controller; boolean movable; int column=0; int row=0; /** * */ public Tile() { // TODO Auto-generated constructor stub super(); height=0; width=0; type=""; controller=""; column = 0; row=0; }