Tic Tac Toe Java Program - CPU not responding - java

I've been working on this program for a couple weeks now, adding components to an applet in order to output a simple tic tac toe game. I've gotten some of it to work, but as of now, when you run, all it does is that you click it once, the CPU projects its mark in the upper left corner, and then you can click anywhere else and it automatically says you win. I can't get the CPU to keep playing. I don't expect anyone to tell me what to do, but I'm just confused as to which method I need to work on in order to get the CPU to respond. My professor has left some very helpful pseudocode, but I still don't quite understand. I've been working with the method "gameEnd," checking for winners horizontally, vertically and diagonally to see if that's the source to getting the game to continue beyond just two marks, but it's not working. Anyone got any suggestions? Thanks.
import java.awt.Color;
import java.awt.Event;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Random;
public class TicTacToe extends java.applet.Applet {
// Ignore this constant
private static final long serialVersionUID = 1942709821640345256L;
// You can change this boolean constant to control debugging log output
private static final boolean DEBUGGING = false;
// Constants
// Size of one side of the board in pixels
private static final int BOARD_SIZE_PIXELS = 600;
// Number of squares on one side of the board
private static final int SQUARES = 3;
// Diameter of the circle drawn in each square
private static final int CIRCLE_WIDTH = 90;
// Colors to be used in the game
private static final Color BACKGROUND_COLOR = Color.WHITE;
private static final Color SQUARE_BORDER_COLOR = Color.BLACK;
private static final Color GAME_OVER_MESSAGE_COLOR = Color.BLACK;
private static final Color HUMAN_COLOR = Color.RED;
private static final Color HUMAN_WINNING_COLOR = Color.MAGENTA;
private static final Color CPU_COLOR = Color.BLUE;
private static final Color CPU_WINNING_COLOR = Color.CYAN;
// Status constant values for the game board
private static final int EMPTY = 0;
private static final int HUMAN = 1;
private static final int HUMAN_WINNING = 2;
private static final int CPU = -1;
private static final int CPU_WINNING = -2;
// String displayed when the game ends
private static final String GAME_WIN_MESSAGE = "You win! Click to play again.";
private static final String GAME_LOSE_MESSAGE = "You lose......Click to play again.";
private static final String GAME_DRAW_MESSAGE = "No one wins? Click to play again...";
// Instance variables that control the game
// Whether or not the user just clicked the mouse
private boolean mouseClicked = false;
// Whether or not to start the game again
private boolean restart = false;
// Whether or not the CPU should start playing a move.
// USED ONLY WHEN THE CPU PLAYS FIRST!
private boolean onFirstMove = false;
// The column and row of the SQUARE the user clicked on
private int xMouseSquare; // column
private int yMouseSquare; // row
// The width (and height) of a single game square
private int squareWidth = BOARD_SIZE_PIXELS / SQUARES;
// An array to hold square status values on the board.
// The status values can be EMPTY, HUMAN, CPU or other values
private int[][] gameBoard;
// The column and row of the SQUARE the CPU player will move on
private int xCPUMove;
private int yCPUMove;
// Add the rest of your instance variables here, if you need any. (You won't
// need to, but you may if you want to.)
// Ignore these instance variables
// CPUinMove represents if the CPU is thinking (generating the CPU move).
// If it is true, it means the CPUMove() method is running and no new move
// should be added
private boolean CPUinMove;
// Methods that you need to write:
/*
* Pre: x and y are x-coordinate and y-coordinate where the user just
* clicks. squareWidth is the width (and height) of a single game square.
*
* Post: xMouseSquare and yMouseSquare are set to be the column and row
* where the user just clicked on (depending on x and y).
*
* Hint: You need only two statements in this method.
*/
// Setting MouseSquare equal to x and y divided by the Square Width to create a location for
// the user after clicking
private void setMouseSquare(int x, int y) {
//
xMouseSquare = x/squareWidth;
yMouseSquare = y/squareWidth;
}
/*
* Pre: SQUARES is an int that holds the number of game squares along one
* side of the game board. xSquare is an int such that 0 <= xSquare <
* SQUARES. CIRCLE_WIDTH is an int that holds the diameter of the circle to
* be drawn in the center of a square. squareWidth is an int that holds the
* width and height in pixels of a single game square.
*
* Post: Return the correct x-coordinate (in pixels) of the left side of the
* circle centered in a square in the column xSquare.
*
* Hint: This method should be very simple. What you need to do is to find
* the right equation.
*/
private int getIconDisplayXLocation(int xSquare) {
// This line is an example of using DEBUGGING variable
if (DEBUGGING) {
System.out.println("The input that getIconDisplayXLocation() receives is: " + xSquare);
}
// equation that returns the correct variable in the column xSquare
return squareWidth * xSquare + (squareWidth - CIRCLE_WIDTH)/2;
}
/*
* Pre: SQUARES is an int that holds the number of game squares along one
* side of the game board. ySquare is an int such that 0 <= ySquare <
* SQUARES. CIRCLE_WIDTH is an int that holds the diameter of the circle to
* be drawn in the center of a square. squareWidth is an int that holds the
* width and height in pixels of a single game square.
*
* Post: Return the correct y-coordinate (in pixels) of the top of the
* circle centered in a square in the row ySquare.
*
* Hint: This method should be very simple. What you need to do is to find
* the right equation.
*/
private int getIconDisplayYLocation(int ySquare) {
// This line is an example of using DEBUGGING variable
if (DEBUGGING) {
System.out.println("The input that getIconDisplayYLocation() receives is: " + ySquare);
}
// equation that returns the correct variable in the column ySquare
return squareWidth * ySquare + (squareWidth - CIRCLE_WIDTH)/2;
}
/*
* The instance variable gameBoard will be created and initialized
*
* Pre: SQUARES is set to an int. gameBoard is a 2-dimensional array type
* variable that holds the status of current game board. Each value in the
* array represents a square on the board
*
* Post: gameBoard must be assigned a new 2-dimensional array. Every square
* of gameBoard should be initialized to EMPTY.
*
* Hint: A loop.
*/
private void buildBoard() {
// Setting the two methods equal to local variables, x and y
int x = xMouseSquare;
int y = yMouseSquare;
// This line creates the gameBoard array. You must write several more
// lines to initialize all its values to EMPTY
// Write game board using the equation of three across and down for each column
// Constructs a 3 by 3 array of integers for the board
gameBoard = new int[3][3];
// Initialize variables i and j, set equal to 0; this establishes a connection for loop
for (x = 0; x < 3; x++) {
for (y = 0; y < 3; y++) {
gameBoard[x][y] = EMPTY;
}
}
}
/*
* Returns whether the most recently clicked square is a legal choice in the
* game.
*
* Pre: gameBoard is a 2-dimensional array type variable that holds the
* status of current game board. xSquare and ySquare represent the column
* and row of the most recently clicked square.
*
* Post: Returns true if and only if the square is a legal choice. If the
* square is empty on current game board, it is legal and the method shall
* return true; if it is taken by either human or CPU or it is not a square
* on current board, it is illegal and the method shall return false.
*
* Hint: Should be simple but think carefully to cover all cases.
*/
private boolean legalSquare(int xSquare, int ySquare) {
if (gameBoard[xSquare][ySquare] == EMPTY)
return true;
else return false;
}
/*
* Pre: gameBoard is an array that holds the current status of the game
* board. xSquare and ySquare represent the column and row of the most
* recently clicked square. player represent the current player (HUMAN or
* CPU). This method is always called after checking legalSquare().
*
* Post: Set the square as taken by current player on the game board if the
* square is empty.
*
* Hint: Very simple.
*/
private void setMovePosition(int xSquare, int ySquare, int player) {
player = HUMAN;
player = CPU;
this.xMouseSquare = xSquare;
this.yMouseSquare = ySquare;
}
/*
* Check if HUMAN or CPU wins the game.
*
* Pre: gameBoard is an array to hold square status values on the board. The
* status values can be EMPTY, HUMAN, CPU or other values.
*
* Post: The method will return true if and only if a player wins the game.
* Winning a game means there is at least one row, one column, or one
* diagonal that is taken by the same player. The method does not need to
* indicate who wins because if it is implemented correctly, the winner must
* be the one who just made a move.
*
* Hint: Complicated method. Use loops to shorten your code. Think about how
* to represent "a player takes 3 squares in a row/column/diagonal".
*/
private boolean gameEnd() {
// Setting local variables
int i = 0;
int x = xMouseSquare;
int y = yMouseSquare;
int sw = squareWidth;
int[][] g = gameBoard;
// Checking for a winner
// Checking columns
for (y = 0; y < 3; y++) {
for (x = 0; x < 3; x++) {
i = g[x][y] + i;
}
}
{
// Checking rows
for (x = 0; x < 3; x++) {
for (y = 0; y < 3; y++) {
i = g[x][y] + i;
}
}
}
// Checking first diagonal
{
for (x = 0; x < 3; x++) {
g[x][x] = 3;
}
}
for (x = 1; x < 3; x++) {
g[x][x] = 3;
}
for (x = 2; x < 3; x++) {
g[x][x] = 3;
}
// Checking second diagonal
for (y = 0; y < 3; y++) {
g[y][2 - y] = 3;
}
for (y = 1; y < 3; y++) {
g[y][2 - y] = 3;
}
for (y = 2; y < 3; y++) {
g[y][2 - y] = 3;
}
return true;
}
/*
* Check if the game ends as a draw.
*
* Pre: gameBoard is an array to hold square status values on the board. The
* status values can be EMPTY, HUMAN, CPU or other values. This method is
* always called after gameEnd().
*
* Post: The method will return true if and only if all squares on the game
* board are taken by HUMAN or CPU players (no EMPTY squares left).
*
* Hint: Should be simple. Use loops.
*/
// Value of...
private boolean gameDraw() {
if (squareWidth == (3^2 - 1)) {
}
return true;
}
/*
* Marks circles on the line on which a player wins.
*
* Pre: g is Graphics object that is ready to draw on. HUMAN_WINNING_COLOR
* and CPU_WINNING_COLOR are both Color objects that represent the color to
* show when HUMAN/CPU wins. gameBoard is gameBoard is an array to hold
* square status values on the board. The status values can be EMPTY, HUMAN,
* CPU or other values.
*
* Post: ALL the row(s)/column(s)/diagonal(s) on which a player wins will be
* marked as the special color.
*
* Hint: You must draw a new circle with the special color (to replace the
* old circle) on the square if the square belongs to a winning
* row/column/diagonal. You can change gameBoard because the board will be
* reset after displaying the winning line. You can use helper methods
* (existing ones or your own) to finish this method. Pay attention that
* many functions in this method is similar to gameEnd(). You should think
* about reusing code.
*
* Hint2: This method is not necessary for the game logic. You don't have to
* start it early. Start when you know everything else is correct.
*/
private void markWinningLine(Graphics g) {
// TODO
}
/*
* Generates the next square where the CPU plays a move.
*
* Pre: gameBoard is an array to hold square status values on
* the board. The status values can be EMPTY, HUMAN, CPU or other values.
*
* Post: Set xCPUMove and yCPUMove to represent the column and the row which
* CPU plans to play a move on. The respective square MUST BE EMPTY! It will
* cause a logical error if this method returns a square that is already
* taken!
*
* Hint: Don't start too early -- currently this method works (though
* naively). Make sure that everything else works before touching this
* method!
*/
private void CPUMove() {
// TODO
// The following block gives a naive solution -- it finds the first
// empty square and play a move on it. You can use this method to test
// other methods in the beginning. However, you must replace this block
// with your own algorithms eventually.
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (gameBoard[i][j] == 0) {
xCPUMove = i;
yCPUMove = j;
return;
}
}
}
}
/* Put any helper methods you wish to add here. */
/* You will not need to change anything below this line. */
/*
* DO NOT change this method.
*
* Set the game board to show a new, blank game.
*/
private void wipeBoard(Graphics g) {
g.setColor(BACKGROUND_COLOR);
g.fillRect(0, 0, BOARD_SIZE_PIXELS, BOARD_SIZE_PIXELS);
}
/*
* DO NOT change this method.
*
* Displays a circle on g, of the given color, in the center of the given
* square.
*/
private void displayHit(Graphics g, Color color, int xSquare, int ySquare) {
g.setColor(color);
g.fillOval(getIconDisplayXLocation(xSquare),
getIconDisplayYLocation(ySquare), CIRCLE_WIDTH, CIRCLE_WIDTH);
}
/*
* DO NOT change this method.
*
* This method handles mouse clicks. You will not need to call it.
*/
#Override
public boolean mouseDown(Event e, int xMouse, int yMouse) {
if (isClickable()) {
mouseClicked = true;
setMouseSquare(xMouse, yMouse);
}
repaint();
return true;
}
/*
* DO NOT change this method.
*
* This method handles drawing the board. You will not need to call it.
*/
#Override
public void update(Graphics g) {
paint(g);
}
/*
* DO NOT change this method.
*
* Draws the border between game squares onto canvas. Also, draws the moves
* that are already made.
*/
private void displayGame(Graphics canvas) {
canvas.setColor(SQUARE_BORDER_COLOR);
for (int i = 0; i < BOARD_SIZE_PIXELS; i += squareWidth) {
for (int j = 0; j < BOARD_SIZE_PIXELS; j += squareWidth) {
canvas.drawRect(i, j, squareWidth, squareWidth);
}
}
for (int i = 0; i < SQUARES; i++) {
for (int j = 0; j < SQUARES; j++) {
switch (gameBoard[i][j]) {
case HUMAN:
case HUMAN_WINNING:
displayHit(canvas, HUMAN_COLOR, i, j);
break;
case CPU:
case CPU_WINNING:
displayHit(canvas, CPU_COLOR, i, j);
break;
default:
break;
}
}
}
}
/*
* DO NOT change this method.
*
* This method relays information about the availability of mouse clicking
* in the game. You will not need to call it.
*/
private boolean isClickable() {
return !CPUinMove;
}
/*
* DO NOT change the contents this method.
*
* If this method is changed to public void paint(Graphics canvas), it will
* execute the program with the CPU-first order.
*
* This method is like the "main" method (but for applets). You will not
* need to call it. It contains most of the game logic.
*/
// #Override
public void paint(Graphics canvas) {
displayGame(canvas);
if (mouseClicked) {
if (onFirstMove) {
CPUMove();
setMovePosition(xCPUMove, yCPUMove, CPU);
displayHit(canvas, CPU_COLOR, xCPUMove, yCPUMove);
onFirstMove = false;
} else {
if (restart) {
wipeBoard(canvas);
setUpGame();
repaint();
} else if (legalSquare(xMouseSquare, yMouseSquare)) {
setMovePosition(xMouseSquare, yMouseSquare, HUMAN);
displayHit(canvas, HUMAN_COLOR, xMouseSquare, yMouseSquare);
if (gameEnd()) {
markWinningLine(canvas);
canvas.setFont(new Font("SansSerif", Font.PLAIN, 30));
canvas.setColor(GAME_OVER_MESSAGE_COLOR);
canvas.drawString(GAME_WIN_MESSAGE, squareWidth / 2,
squareWidth);
restart = true;
} else {
CPUinMove = true;
CPUMove();
setMovePosition(xCPUMove, yCPUMove, CPU);
displayHit(canvas, CPU_COLOR, xCPUMove, yCPUMove);
CPUinMove = false;
if (gameEnd()) {
markWinningLine(canvas);
canvas
.setFont(new Font("SansSerif", Font.PLAIN,
30));
canvas.setColor(GAME_OVER_MESSAGE_COLOR);
canvas.drawString(GAME_LOSE_MESSAGE,
squareWidth / 2, squareWidth);
restart = true;
} else if (gameDraw()) {
canvas
.setFont(new Font("SansSerif", Font.PLAIN,
30));
canvas.setColor(GAME_OVER_MESSAGE_COLOR);
canvas.drawString(GAME_DRAW_MESSAGE,
squareWidth / 2, squareWidth);
restart = true;
}
}
}
}
mouseClicked = false;
}
}
/*
* DO NOT change this method.
*
* This method is like the "main" method (but for applets). You will not
* need to call it. It contains most of the game logic.
*/
public void paint_game(Graphics canvas) {
// display the current game board
displayGame(canvas);
// the following block will run every time the user clicks the mouse
if (mouseClicked) { // when the user clicks the mouse
// if the game is ready to start or to be restarted
if (restart) {
// clear the window and set up the game again
wipeBoard(canvas);
setUpGame();
repaint();
}
// else, if the game is in play, check if the click is on a legal
// square
else if (legalSquare(xMouseSquare, yMouseSquare)) {
// if the square is legal, mark the corresponding position as
// taken by HUMAN
setMovePosition(xMouseSquare, yMouseSquare, HUMAN);
// display the new position with a HUMAN_COLOR circle
displayHit(canvas, HUMAN_COLOR, xMouseSquare, yMouseSquare);
// check if the game ends (if it is, HUMAN wins)
if (gameEnd()) {
// if HUMAN wins, mark the winning line as a special color.
markWinningLine(canvas);
// display the human winning message
canvas.setFont(new Font("SansSerif", Font.PLAIN, 30));
canvas.setColor(GAME_OVER_MESSAGE_COLOR);
canvas.drawString(GAME_WIN_MESSAGE, squareWidth / 2,
squareWidth);
// mark the game as ready to restart
restart = true;
} else if (gameDraw()) {
// if HUMAN doesn't win but the board is full, it is a draw
// display the draw message
canvas.setFont(new Font("SansSerif", Font.PLAIN, 30));
canvas.setColor(GAME_OVER_MESSAGE_COLOR);
canvas.drawString(GAME_DRAW_MESSAGE, squareWidth / 2,
squareWidth);
// mark the game as ready to restart
restart = true;
} else {
// if HUMAN doesn't win and the board is not full, the CPU
// is ready to move
CPUinMove = true;
// calculates the next CPU move
CPUMove();
// mark the corresponding position as taken by CPU
setMovePosition(xCPUMove, yCPUMove, CPU);
// display the new position with a CPU_COLOR circle
displayHit(canvas, CPU_COLOR, xCPUMove, yCPUMove);
CPUinMove = false;
if (gameEnd()) {
// if CPU wins, mark the winning line as a special
// color.
markWinningLine(canvas);
// display the human losing message
canvas.setFont(new Font("SansSerif", Font.PLAIN, 30));
canvas.setColor(GAME_OVER_MESSAGE_COLOR);
canvas.drawString(GAME_LOSE_MESSAGE, squareWidth / 2,
squareWidth);
// mark the game as ready to restart
restart = true;
}
// else (if the game is not ended after the CPU move), the
// game is ready to get the next HUMAN move
}
}
mouseClicked = false;
}
}
/*
* DO NOT change this method.
*
* This method initializes the applet. You will not need to call it.
*/
#Override
public void init() {
setSize(BOARD_SIZE_PIXELS, BOARD_SIZE_PIXELS);
setBackground(BACKGROUND_COLOR);
setUpGame();
}
/*
* DO NOT change this method.
*
* Creates a fresh game board and sets up the game state to get ready for a
* new game.
*/
private void setUpGame() {
buildBoard();
CPUinMove = false;
restart = false;
onFirstMove = true;
}
}

Related

How to make moving object bump a standing still object (Launching my object with mouse towards random position still object.)

Working on a program that consists of emoji's and you get an emoji that you can shoot, bit like Angry Birds but way more basic. At the moment the stand still emoji is falling down (increasing vertical velocity by gravity until it hits the ground). However this only happens when the main object has a greater horizontal position than the stand still object.
/**
* Represents an emoji that can move on a window.
*/
public class Emoji{
// Constants for all Emoji: size, position of the ground
public static final double SIZE = 40; // width/length of the emoji images
public static final double GROUND = EmojiLauncher.GROUND;
public static final double GRAVITY = 0.25; // how much to reduce the speed each step.
// Fields to store state of the Emoji:
private double horizontalPos;
private double aboveGround;
private double hSpeed;
private double vSpeed;
private String name;
// Constructor
/**
* Constructor
*/
public Emoji(double x, double h, String name){
this.horizontalPos = x;
this.aboveGround = h;
this.name = "emojis/"+name;
}
// Methods
/**
* Draws an emoji
*/
public void draw(){
UI.drawImage(this.name, this.horizontalPos, this.aboveGround , SIZE, SIZE);
}
/**
* Makes emoji take a step
*/
public void step(){
if(this.vSpeed != 0 || this.hSpeed != 0){
this.horizontalPos += hSpeed;
this.aboveGround += vSpeed;
this.vSpeed += GRAVITY;
if(this.aboveGround > GROUND - SIZE){
vSpeed = 0;
hSpeed = 0;
this.aboveGround = GROUND - SIZE;
}
}
}
/**
*
* Returns the height of the emoji above the floor.
*/
public double getHeight(){
return this.aboveGround;
}
/**
*
*
* Returns the horizontal position
*/
public double getX(){
return this.horizontalPos;
}
/**
* Returns the speed of the emoji
*/
public double getSpeed(){
return Math.hypot(hSpeed, vSpeed);
}
/**
*
* Set the horizontal and vertical speed of emoji
*/
public void launch(double xSpeed, double ySpeed){
this.hSpeed = xSpeed;
this.vSpeed = ySpeed;
}
/**
* return true if emoji is touching other emoji
*/
public boolean touching(Emoji other){
if(this.horizontalPos >= other.horizontalPos || this.aboveGround >= other.aboveGround){
return true;
}
else {
return false;
}
}
/**
* if emojis hit each other, the hit emoji starts moving
*/
public void bump(Emoji other){
if((this.hSpeed > 0 || vSpeed > 0) &&
(this.horizontalPos >= other.horizontalPos && this.aboveGround >= other.aboveGround)){
other.hSpeed = 1;
}
}
/**
* Main method
*/
public static void main(String[] args){
do {
Emoji bike = new Emoji(110, 150, "bicycle.png");
bike.draw();
Emoji moon = new Emoji(50, 200, "moon.png");
moon.draw();
moon.launch(5, 0);
UI.println("moon launched");
for (int i=0; i<=100; i++){
bike.step();
moon.step();
UI.clearGraphics();
UI.drawLine(0, GROUND, 500, GROUND);
bike.draw();
moon.draw();
if(moon.touching(bike)&& moon.getSpeed()>0){
moon.bump(bike);
UI.println(i+": moon touched and then bumped bike");
}
if(moon.getX() > 55 && moon.getX()<65){
UI.println(i+": moon has moved to the right");
}
if (moon.getHeight()==0){
UI.println(i+": moon at ground");
}
if (bike.getHeight()==0){
UI.println(i+": bike at ground");
}
UI.sleep(100);
if (bike.getHeight()==0 && moon.getHeight()==0){
break;
}
}
} while (UI.askBoolean("test again?"));
}
// END OF DRAFT CODE
}
This is the emoji class
Needing help with the touching and bump methods so that the stand still emoji gets bumped more realistically and based on the speed of the emoji controlled.
this is what it looks like when run
the moon in the bottom left was my object that I shot at the angry target. They both need to stop on the ground.
I have a UI import which makes things much easier to make.

How do I add keystroke Z to keyPressed(KeyEvent e) to jump/repaint the ball to a new random location? (KeyListener demo used.)

I have limited Java experience, especially when it relates to graphics. I've had great help on this problem so far. It's starter code (at the bottom) which works fine as I tested it in Eclipse. I'm using it from a class to teach a high school student. As per the instructions below in comments, would someone know an easy way to extend the already simple program to cause the ball to jump/move/repaint to a random new location, after pressing the Z button? I'm thinking of adding the following code to the keyPressed(KeyEvent e) method:
else if(keyCode == KeyEvent.VK_Z)
{
//Not sure what to add here. Just want to change X and Y coordinates to
//anything, so long as the program works and stays fairly simple
//for now.
//----> g.fillOval(x + radius, y + radius, 2 * radius, 2 * radius);
// this line above says "g can not be resolved".
}
.
Any tips or ideas to keep the program simple would be very much appreciated. Thank you!
.
import java.awt.*;
import java.awt.event.*; // #1
import javax.swing.*;
/******************************************************************************
*
* KeyListenerDemo.java
* Demonstrates getting keyboard input using the KeyListener interface.
*
* Program 18: Extend this program by adding a few more keystroke commands:
* z (VK_Z) - Cause the ball to jump to a random new location.
* s (VK_S) - Make the ball smaller - multiply its diameter 1/2.
* b (VK_B) - Make the ball bigger - multiply its diameter by 2.
* c (VK_C) - Change the color (in any way you'd like).
*
* In addition, modify the program to ensure the following:
* - The ball goes all the way to the edge of the screen but stays
* completely on the screen.
* - If a doubled diameter doesn't fit, make it as large as possible.
* - Be sure the ball never completely disappears.
*
*****************************************************************************/
public class KeyListenerDemo extends JFrame
implements KeyListener // #2
{
// Class Scope Finals
private static final int SCREEN_WIDTH = 1000;
private static final int SCREEN_HEIGHT = 800;
private static final int START_RADIUS = 25;
private static final int START_X = 100;
private static final int START_Y = 100;
private static final int STEP_SIZE = 10;
// Class Scope Variables
private static int x = START_X; // x at center of the ball
private static int y = START_Y; // y at center of the ball
private static int radius = START_RADIUS; // radius of the ball
// Methods
/**
* Create the window and register this as a KeyListener
*
* #param args
*/
public static void main (String[] args)
{
// Set up the JFrame window.
KeyListenerDemo gp = new KeyListenerDemo();
gp.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
gp.setVisible(true);
gp.addKeyListener(gp); // #3
// If this class had a constructor and you moved this line into
// that constructor it could not refer to gp since that variable
// is local to this method. Instead you would write::
// addKeyListener(this);
}
/**
* Called when a key is first pressed
* Required for any KeyListener
*
* #param e Contains info about the key pressed
*/
public void keyPressed(KeyEvent e) // #4A
{
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT)
{
x = x - STEP_SIZE;
}
else if (keyCode == KeyEvent.VK_RIGHT)
{
x = x + STEP_SIZE;
}
else if (keyCode == KeyEvent.VK_UP)
{
y = y - STEP_SIZE;
}
else if (keyCode == KeyEvent.VK_DOWN)
{
y = y + STEP_SIZE;
}
repaint();
}
/**
* Called when typing of a key is completed
* Required for any KeyListener
*
* #param e Contains info about the key typed
*/
public void keyTyped(KeyEvent e) // #4B
{
}
/**
* Called when a key is released
* Required for any KeyListener
*
* #param e Contains info about the key released
*/
public void keyReleased(KeyEvent e) // #4C
{
}
/**
* paint - draw the figure
*
* #param g Graphics object to draw in
*/
public void paint(Graphics g)
{
g.setColor(Color.white);
g.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
g.setColor(Color.blue);
g.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
}
}
Try doing something like this:
public void keyPressed(KeyEvent e){
if(e.getKeyCode == KeyEvent.VK_Z){
moveRand();
//Assuming that is the method for moving the ball randomly.
}
}
and than
public void moveRand(Graphics2D g){
g.drawOval(Random.nextInt() / window.getWidth() + 1, Random.nextInt() / window.getHight() + 1, ballWidth, ballHight);
}
Hope this helped.
The reason that g cannot be resolved is that it does not exist in the keyPressed() method. g is what is called a parameter of the method paint(). This means that you can only refer to it within the method paint(). You can declare another variable named g outside the method paint(), but it won't be the same g. It won't even have to have the same type.
That said, you do not need to reference g directly at all in order to solve your problem. The paint() method already handles drawing a circle. You just need to change where the circle is and tell the application to repaint it. If you assign new values to x and y and then invoke repaint(), the circle will move. Notice that repaint() is already called at the end of the keyPressed() method.
Here is an example. Replace the code on the right hand side of the equals with whatever may be appropriate to your problem.
else if(keyCode == KeyEvent.VK_Z)
{
x = x + radius;
y = y + radius;
}
If you look at the other if/else clauses, you will see that this is what they are doing as well.

Java inheritance super [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I have these classes which is the 3rd and 4th child of a base class in my game:
/*
* List of player states:
* * normal * explode */
public class LocalPlayer extends MovingObjects{
//minus 1 laps to start to accommodate going over the start line for the first time
public int lapsCompleted = -1;
public LocalPlayer(int localPlayerNumber) {
/*
* This line below and the variable localPlayerNumber will not be needed or will be different
* if later on decide to do custom rocket designs - not needed for this stage but bear in mind if the decision is
* made to go down that road this and the game object will have to be reconsidered as early as possible.
*/
super(16, "assets/images/player" + localPlayerNumber , ".jpg", new Vector(373, 450 + (55 * localPlayerNumber)));
}
//basic constructor just initialises MovingObjects Variables to zero values
public LocalPlayer(){
super();
}
///// Uploading Position Methods
public void update(){
if(objectState == "explode"){
}
if(objectState == "normal"){
super.update();
//look for edge of map and stop player leaving and reduce speed to 0
if(position.x > rightEdge - icon.getIconWidth()){
position.x = rightEdge - icon.getIconWidth();
speed = 0;
}else{
if(position.x < leftEdge){
position.x = leftEdge;
speed = 0;
}
}
if(position.y > downEdge - icon.getIconHeight()){
position.y = downEdge - icon.getIconHeight();
speed = 0;
}else{
if(position.y < upEdge){
position.y = upEdge;
speed = 0;
}
}
}
}
///// Movement Methods
//Increases speed
public void up(){
if(speed == 0){
speed = 2;// 2 is minimum speed to achieve all driving angles
}else
if(speed < 11){
speed++;
}
}
//Decreases speed
public void down(){
if(speed > 2){
speed--;
}else{
speed = 0;
}
}
//Turns image and angle 22.5 degrees to the right
public void right(){
if(angle == 337.5)
{
angle = 0;
imageNumber = 0;
}else{
angle = angle + 22.5;
imageNumber = imageNumber + 1;
}
}
//Turns image and angle 22.5 degrees to the left
public void left(){
if(angle == 0)
{
angle = 337.5;
imageNumber = 15;
}else{
angle = angle - 22.5;
imageNumber = imageNumber - 1;
}
}
// works out start grid currently only vertical lines
//add more levels either start all the same way or needs updating
public Vector getStartPos(int serverNumber, CheckPoints line)
{
int row ;
Vector vReturn;
if (serverNumber % 2 == 0) {
// even
row = serverNumber/2;
//this needs some explaining:
//vectorX = row * width of image * gap to next player
//vectorY = getline y and inset slightly(will see how goes)
vReturn = new Vector((line.pos1.x + 10 ) - row * (50 + 10), line.pos1.y + 5);
} else {
// odd
row = (serverNumber + 1)/2;
vReturn = new Vector((line.pos2.x +10) - row * (50 + 10), line.pos2.y - 55);
}
return vReturn;
}
}
and:
import java.awt.image.BufferedImage;
public class NetworkedLocalPlayer extends LocalPlayer{
RocketSpecificServer server = new RocketSpecificServer();
public NetworkedLocalPlayer(String ipAddress,int numOfImages,String filePre, String fileE, CheckPoints finishLine) {
//sign player into server
server.setUpNetwork(ipAddress);
LoadContent.gameSettings = (GameSettings) server.signIn();
//get server number (needed for initialising)
serverNumber = Integer.parseInt(((StringReturnSerial)server.getServerNumber()).s);
//this is temp and should be changed later!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//filePre = filePre + serverNumber;
filePre = filePre + 1;
//Initialise image variables
imageVariables(numOfImages,filePre,fileE);
//work out pos
position = getStartPos(serverNumber,finishLine);
//get images
initImageArray();
}
public void update(){
//updates players info table checks the state of this object
//hasn't been changed by another player
LoadContent.serverPlayerInfo =(PlayerPositionsSerial) server.getPos();
objectState = LoadContent.serverPlayerInfo.playersArray[serverNumber][4];
//usual update
super.update();
//updates server
LoadContent.serverPlayerInfo = (PlayerPositionsSerial) server.updatePos(angle, position.x, position.y,objectState);
}
}
The problem is the update method. In the NetworkedLocalPLayer player class it asks the server for a serialised class which holds all the players positions in the game (and someother bits) that is assigned to the static version in LoadContent class which implements my gameloop. the update method then checks its own position in the playertable array to update its own objectState variable which I can see when using breakpoints comes back as its supposed to as "normal". It then call the parents update method which starts executing and will stop on line in the LocalPlayer class :
if(objectState == "normal"){
the next line is :
super.update();
which should call the super update method of the MovingObjects class which i can also provide if you think it will help but bassically the I have a break opint inside the MovingObjects class and on the super.update() call in the LocalPlayer class which never get hit.
When i change a variable in the game loop to turn multiplayer off which has nothing to do with this but directly used the localPlayer class as it is this method fire perfectly fine and has done unchanged for ages. So is there a reason I cant use super like this? im now getting any errors or exceptions
Thank you
for any help John harris
Never compare Strings with ==. Do it like this (assuming objectState cannot be null - otherwise you could write "explode".equals(objectState) but I find it harder to read):
public void update(){
if(objectState.equals("explode")){
}
if(objectState.equals("normal")){
super.update();
// ...
}
}
If using Java 7, you can also switch on Strings (have a look at the changes introduced with Java 7).
You problem is that objectState == "normal" is always false.
Use "normal".equals(objectState) instead

divide and conquer algorithm

I've been given a 2^k * 2^k sized board, and one of the tiles is randomly removed making it a deficient board. The task is to fill the with "trominos" which are an L-shaped figure made of 3 tiles.
The process of the solving it isn't too difficult. If the board is 2x2, then it only takes one tromino to fill it. For any greater size, it must be divided into quarters (making four 2^(k-1) sized boards), with one tromino placed at the center point, so each quadrant has one filled in tile. After that, the board can be recursively filled until every tile is filled with a random colored tromino.
My main problem is actually implementing the code. My skills with Java programming are generally pretty weak, and I often have trouble simply finding a place to start. The only work to be done is in the tile method in the tiling class, which takes as input the deficient board to tile, the row and column to start tiling in, and the number of tiles to fill. This is a homework problem, so I'm simply looking for some guidance or a place to start - any help would be greatly appreciated.
public class BoardViewer extends JFrame {
private static final int WIDTH = 1024;
private static final int HEIGHT = 768;
private static final int OFFSET = 30;
public static final int UPVERT = 1;
public static final int DNVERT = 2;
public static final int RHORIZ = 4;
public static final int LHORIZ = 8;
public static final int FILL = 16;
private Color [][] board;
public BoardViewer(DeficientBoard db) {
super();
setSize(WIDTH + (OFFSET*2), HEIGHT + (OFFSET*2));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
board = db.getBoardColor();
}
public void paint(Graphics g) {
super.paint(g);
int width = WIDTH/board[0].length;
int height = HEIGHT/board.length;
for (int r = 0; r < board.length; r++)
for (int c = 0; c < board[r].length; c++) {
g.setColor(board[r][c]);
int x = c*width + OFFSET;
int y = r*height + OFFSET + (OFFSET/2);
g.fillRect(x+1, y+1, width-1, height-1);
}
}
}
public class DeficientBoard {
private int n;
private Color board[][];
// The four rotations of the tile.
// UL is XX
// X
// UR is XX
// X
// LL is X
// XX
// LR is X
// XX
public final int UL = 0;
public final int UR = 1;
public final int LL = 2;
public final int LR = 3;
/**
* Create a 2^k x 2^k deficient board.
*
* #param k power
*/
public DeficientBoard(int k) {
n = (int)Math.pow(2, k);
createBoard(Color.LIGHT_GRAY);
}
/**
* Actually create an n x n deficient board.
*
* #param color background color
*/
private void createBoard(Color color) {
board = new Color[n][n];
for (int r = 0; r < board.length; r++)
for (int c = 0; c < board[0].length; c++)
board[r][c] = color;
int d_row = (int)(Math.random() * n);
int d_col = (int)(Math.random() * n);
board[d_row][d_col] = Color.BLACK;
}
/**
* Given a row and column and shape based on that point
* place a tromino of the given color.
*
* #param r row
* #param c column
* #param s shape (UL, UR, LL, LR)
* #param theColor a Color
*/
public void placeTromino(int r, int c, int s, Color theColor) {
if (s == UL) {
board[r][c] = theColor;
board[r][c+1] = theColor;
board[r+1][c] = theColor;
} else if (s == UR) {
board[r][c] = theColor;
board[r][c+1] = theColor;
board[r+1][c+1] = theColor;
} else if (s == LL) {
board[r][c] = theColor;
board[r+1][c] = theColor;
board[r+1][c+1] = theColor;
} else {
board[r+1][c] = theColor;
board[r+1][c+1] = theColor;
board[r][c+1] = theColor;
}
}
/**
* Get the 2^k x 2^k board.
*
* #return the Color board.
*/
public Color[][] getBoardColor() {
return board;
}
/**
* Find and return the deficient row.
*
* #param row row
* #param col column
* #param sz size of the baord
* #return the row the deficient block is located
*/
public int getDeficientRow(int row, int col, int sz) {
for (int r = row; r < (row + sz); r++)
for (int c = col; c < (col + sz); c++)
if (board[r][c] != Color.LIGHT_GRAY)
return r;
return -1;
}
/**
* Find and return the deficient column.
*
* #param row row
* #param col column
* #param sz size of the baord
* #return the row the deficient block is located
*/
public int getDeficientCol(int row, int col, int sz) {
for (int r = row; r < (row + sz); r++)
for (int c = col; c < (col + sz); c++)
if (board[r][c] != Color.LIGHT_GRAY)
return c;
return -1;
}
/**
* Get the size of the deficient board.
*
* #return the size
*/
public int getSize() {
return n;
}
/**
* Display information about the deficient board.
*/
public String toString() {
return ("Deficient board of size "
+ n + "x" + n
+ " with position missing at ("
+ getDeficientRow(0, 0, n) + "," + getDeficientCol(0, 0, n) +").");
}
}
public class Tiling {
private static Color randColor() {
int r = (int)(Math.random() * 256);
int g = (int)(Math.random() * 256);
int b = (int)(Math.random() * 256);
return new Color(r, g, b);
}
public static void tile(DeficientBoard db, int row, int col, int n) {
}
public static void main(String[] args) {
DeficientBoard db = new DeficientBoard(3);
System.out.println(db);
tile(db, 0, 0, db.getSize());
BoardViewer bv = new BoardViewer(db);
bv.setVisible(true);
}
}
In general, when a recursive function implements a divide-and-conquer algorithm, it has to handle two basic cases:
The base case. This is the case where you're done dividing, and need to conquer a bit. In your assignment, the base case is the case where n = 2, and in that case, you just need to find which of the four tiles is missing/painted (using DefectiveBoard.getDeficientRow and DefectiveBoard.getDeficientCol) and add the appropriate triomino to cover the other three tiles.
The recursive case. This is the case where you're not done dividing, so you need to divide (i.e., recurse), and (depending on the algorithm) may need to do a bit of conquering either before or after the recursion. In your assignment, the recursive case is the case where n > 2. In that case, you need to do two things:
Find which of the four quadrants has a missing/painted tile, and add the appropriate triomino to cover one tile from each of the other three quadrants.
Recurse, calling yourself four times (one for each quadrant).
A good starting point is to write the "Is this the base case?" check, and to implement the base case.
After that, if you don't see how to write the recursive case, one approach is to temporarily write a "one above the base" case (n = 4), and see if you can generalize it. If not, you might then temporarily write a "two above the base" case (n = 8), and so on. (Once you've got your recursive algorithm working, you would then remove these special cases, since they're fully covered by the general recursive case.)
Well this is somewhat of a harder problem to solve. However, I'd say you have the skills given how much code you wrote so I wouldn't be self conscious about it.
I don't have a complete solution formulated, but I think if you start at the the removed tile and put a trominos on either side of it. Then keep putting trominos on either side of the last trominos. You're "spooning" the tromino you last placed on the board. Once you do that to the edge of the board. All that's left is tromino shaped locations. Here is an example of what I mean (X is the dropped tile ie the gap, Y are the trominos):
_ _ _ _
|_|_|_|_|
|_|Y|Y|_|
|_|Y|X|Y|
|_|_|Y|Y|
_ _ _ _
|Y|Y|_|_|
|Y|Y|Y|_|
|_|Y|X|Y|
|_|_|Y|Y|
Once the board is filled to the edges you can essentially start dropping trominos like bombs on the rest of the board. I have a feeling there is a pattern here where you fill in the diagonal trominos while filling in the 2nd part at the same time that is repeatable. But if you can't find that then create a recursive routine that spoons the gap to the edges then transitions to adding trominos in diagonal patterns. The hint there is you have to do the transition in the first stack frame only.
Good luck.

Java button inversion

There is a mysterious inversion happening when i disable buttons for users to press or not press. the project is to setup a battleship game and have one side be for a computer and the other for the user. When i have the so the user can press both buttons the ships show up properly on the buttons as hits. but when the computer shoots the buttons a mysteriously inverted 90 degrees south. here is the main part of the code and the AI class too
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
/**
* #author Alex Larson
* Project #3
* This is the main part of the BattleshipGUI.
* This class has several inner classes that are listeners.
* This class also has the main method in it.
* This class sets up the entire GUI.
*/
public class BsGui extends JFrame {
/**Array of FireButtons for the player */
FireButton play[][] = null;
/**Array of FireButtons for the player */
FireButton opp[][] = null;
/**A static constant for the player's BattleshipBoard */
public static BattleshipBoard playerB;
/**A static constant for the opponent's (or computer's) BattleshipBoard */
public static BattleshipBoard opponentB;
/**A static constant for a computer AI object */
public static ComputerAI AI;
/**A static constant that stores the computers designated AI */
public static char computer;
/**public int for passing around the shot the computer takes */
public int[][] compShot;
/**BorderLayout for the JFrame */
public BorderLayout br;
/**GridLayout for main JPanel */
public GridLayout gr;
/**GridLayout for both FireButton holding JPanels */
public GridLayout gr2;
/**GridLayout for bottom JPanel */
public GridLayout gr3;
/**FlowLayout: not used */
public FlowLayout fl;
/**JPanel that has the players FireButtons in it */
public JPanel playB;
/**JPanel that has the opponent's (or computer's) FireButtons in it */
public JPanel oppB;
/**JPanel that is not needed but hold the 2 FireButton holding JPanels */
public JPanel main;
/**JPanel that holds the title JLabel */
public JPanel top;
/**JPanel that hold shipsLeft button and ships JLabel */
public JPanel bottom;
/**Another not used, unneeded JPanel */
public JPanel left;
/**Another not used, unneeded JPanel */
public JPanel right;
/**JButton used to show user number of ships left */
public JButton shipsLeft;
/**JLabel used to show the title of the game */
public JLabel title;
/**JLabel used when game is over */
public JLabel win;
/**JLabel used to the user the number of ships left */
public JLabel ships;
/**Font used to change styles text */
public Font font = null;
/**
* Constructor for BsGui. It creates a GUI with JLabel as a center
* title and has 2 JPanels that hold arrays of FireButtons. It
* also sets up a button at the bottom that gives you the
* number of ships left for the players board, not the computers board.
*/
public BsGui(){
//Instantiating Section
play = new FireButton[10][10];
opp = new FireButton[10][10];
br = new BorderLayout(10,10);
gr = new GridLayout(1,2);
gr2 = new GridLayout(10,10);
gr3 = new GridLayout(2,2);
fl = new FlowLayout();
playB = new JPanel();
oppB = new JPanel();
main = new JPanel();
top = new JPanel();
bottom = new JPanel();
left = new JPanel();
right = new JPanel();
shipsLeft = new JButton();
title = new JLabel();
win = new JLabel();
ships = new JLabel();
font = new Font("Veranda", Font.BOLD, 20);
compShot = new int[2][1];
//Setting parameters for GUI
main.setBackground(Color.white);
main.setLayout(gr);
top.add(title);
playB.setBackground(Color.cyan);
playB.setName("Player Board");
playB.setLayout(gr2);
oppB.setBackground(Color.magenta);
oppB.setName("Computer Board");
oppB.setLayout(gr2);
shipsLeft.setText("How Many Ships Left");
shipsLeft.setVisible(true);
title.setText("Battleship");
title.setFont(font);
this.setTitle("Battlseship");
this.setSize(1100, 700);
this.setLayout(br);
//Adding Listeners
shipsLeft.addActionListener(new ShipsLeftListener());
//Adding components to GUI section
this.add(main, BorderLayout.CENTER);
this.add(top, BorderLayout.NORTH);
this.add(bottom, BorderLayout.SOUTH);
this.add(left, BorderLayout.WEST);
this.add(right, BorderLayout.EAST);
this.addFireBottonsToArray();
this.addFireButtonsPlay();
this.addFireButtonsOpp();
main.add(playB);
main.add(oppB);
bottom.add(shipsLeft);
bottom.add(ships);
}
/**
* This method adds new FireButtons to each part of the
* FireButton arrays. The first for loop adds buttons for the
* player. The second for loop adds buttons for the computer
* and makes them not click-able.
*/
private void addFireBottonsToArray(){
//Adds FireButtons to play array
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
play[j][i] = new FireButton(j, i);
}
}
//Adds FireButtons to opp array
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
opp[j][i] = new FireButton(j, i);
//opp[j][i].setEnabled(false);
}
}
}
/**
* This method adds and listener to each FireButton
* before adding it to the JPanel for the player.
*/
private void addFireButtonsPlay(){
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
play[j][i].addActionListener( new FireButtonListener());
playB.add(play[j][i]);
}
}
}
/**
* This method adds and listener to each FireButton
* before adding it to the JPanel for the computer.
*/
private void addFireButtonsOpp(){
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
opp[j][i].addActionListener( new OpponentButtonListener());
oppB.add(opp[j][i]);
}
}
}
/**
* This method hits buttons for the computer where ever
* the AI tells it to. Int x is the row of the button and
* int y is the column.
*
* #param int x
* The row coordinate.
* #param int y
* The column coordinate.
*/
public void computerButtonHitter(int x, int y){
if(opponentB.fireShot(x, y) == true){
opp[x][y].setBackground(Color.RED);
opp[x][y].setFont(font);
opp[x][y].setText("H");
}
else{
opp[x][y].setBackground(Color.BLUE);
opp[x][y].setFont(font);
opp[x][y].setText("M");
}
}
/**
* This inner class is for the FireButton Listener. This particular
* class does probably too much but it was an easy way to do the
* problem at hand.
*/
public class FireButtonListener implements ActionListener{
/**
* This method is mandatory with implementing ActionListener.
* When a FireButton is pressed it finds the place it was fired at
* and changes it to red if a ship exists at that spot and if
* not turns it blue. So red means hit and blue means miss. This
* method also adds an 'H' for hit and 'M' for miss to the button.
* This method then also calls the method to make the computer
* do its move based on a switch case for which AI to use.
*
* #param ActionEvent
* The event that triggers this method.
*/
public void actionPerformed(ActionEvent e){
if(e.getSource() instanceof FireButton){
FireButton fb = (FireButton)e.getSource();
//System.out.println("FireButton at "+(fb.getCell().getColumn()+1)+", "+(fb.getCell().getRow()+1)+" was pressed");
if(playerB.fireShot(fb.getCell().getColumn(), fb.getCell().getRow()) == true){
play[fb.getCell().getRow()][fb.getCell().getColumn()].setBackground(Color.RED);
play[fb.getCell().getRow()][fb.getCell().getColumn()].setFont(font);
play[fb.getCell().getRow()][fb.getCell().getColumn()].setText("H");
play[fb.getCell().getRow()][fb.getCell().getColumn()].setEnabled(false);
}
else{
play[fb.getCell().getRow()][fb.getCell().getColumn()].setBackground(Color.BLUE);
play[fb.getCell().getRow()][fb.getCell().getColumn()].setFont(font);
play[fb.getCell().getRow()][fb.getCell().getColumn()].setText("M");
play[fb.getCell().getRow()][fb.getCell().getColumn()].setEnabled(false);
}
switch(computer){
case 'r':
compShot = AI.randomShot();
computerButtonHitter(compShot[0][0], compShot[1][0]);
break;
case 's':
compShot = AI.systematicShot();
computerButtonHitter(compShot[0][0], compShot[1][0]);
break;
}
if(playerB.isGameOver() == true){
main.remove(playB);
main.remove(oppB);
font = new Font("Veranda", Font.BOLD, 50);
win.setFont(font);
win.setText("Congratulations u win!");
main.add(win);
}
if(opponentB.isGameOver() == true){
main.remove(playB);
main.remove(oppB);
font = new Font("Veranda", Font.BOLD, 50);
win.setFont(font);
win.setText("Awe too bad, the computer won");
main.add(win);
}
}
}
}
/**
* This Class is simple and offers the user a way finding how
* many ships he or she still needs to destroy.
*/
public class ShipsLeftListener implements ActionListener{
/**
* This method is mandatory when implementing ActionListener.
* It sets the JLabel ships to the number of ships left
* when the button is pressed.
*
* #param ActionEvent
* The event that triggers this method.
*/
public void actionPerformed(ActionEvent e){
ships.setText("" + playerB.getNumBattleshipsLeft());
}
}
/**
* This class works exactly as the first part of FireButtonListener.
* The only difference is it doesn't work now since all the
* opponent's (computer) buttons are disabled. This method is here
* for later use for 2 player mode. (Did not have time to get that far).
*/
public class OpponentButtonListener implements ActionListener{
/**
* This method is mandatory with implementing ActionListener.
* When a FireButton is pressed it finds the place it was fired at
* and changes it to red if a ship exists at that spot and if
* not turns it blue. So red means hit and blue means miss. This
* method also adds an 'H' for hit and 'M' for miss to the button.
*
* #param ActionEvent
* The event that triggers this method.
*/
public void actionPerformed(ActionEvent e){
if(e.getSource() instanceof FireButton){
FireButton fb = (FireButton)e.getSource();
//System.out.println("Opponent Button Pressed at "+(fb.getCell().getColumn()+1)+", "+(fb.getCell().getRow()+1)+" was pressed");
if(opponentB.fireShot(fb.getCell().getColumn(), fb.getCell().getRow()) == true){
opp[fb.getCell().getRow()][fb.getCell().getColumn()].setBackground(Color.RED);
opp[fb.getCell().getRow()][fb.getCell().getColumn()].setFont(font);
opp[fb.getCell().getRow()][fb.getCell().getColumn()].setText("H");
opp[fb.getCell().getRow()][fb.getCell().getColumn()].setEnabled(false);
}
else{
opp[fb.getCell().getRow()][fb.getCell().getColumn()].setBackground(Color.BLUE);
opp[fb.getCell().getRow()][fb.getCell().getColumn()].setFont(font);
opp[fb.getCell().getRow()][fb.getCell().getColumn()].setText("M");
opp[fb.getCell().getRow()][fb.getCell().getColumn()].setEnabled(false);
}
}
}
}
/**
* Main Function
*
* The main runs everything as normal. :P
* It creates a GUI, an AI, and 2 BattleshipBoards.
*
* #param args
* Command line arguments
* For program to work correctly it
* requires the first arg to be a path to
* the player's ship placements. The second
* arg requires the same as the first but for the
* opponent. The 3rd arg requires a single letter
* that represents the chosen AI for the computer.
* The only supported letters at the moment are 'r' and 's'.
* 'r' is random and 's' is systematic. Other computer AIs and
* difficulties will be added later.
*/
public static void main(String[] args) {
String player;
String opponent;
String inputAI;
File fPlay;
File fOpp;
player = args[0];
opponent = args[1];
inputAI = args[2];
computer = inputAI.charAt(0);
AI = new ComputerAI(computer);
try{
fPlay = new File(player);
fOpp = new File(opponent);
playerB = new BattleshipBoard(fPlay);
opponentB = new BattleshipBoard(fOpp);
}
catch(FileNotFoundException fne){
System.out.println("File path given must be wrong or file doesnt exist");
System.exit(0);
}
catch(Exception e){
System.exit(0);
}
JFrame gui = new BsGui();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
}
}
import java.util.Random;
/**
*
* #author Alex Larson XPS
*
*This class is how the computer gets its brains.
*Has a constructor to set up the computer and 2 brain methods.
*One method gives random shots and the other gives systematic shots.
*More brain options to be added later.
*/
public class ComputerAI {
/**This hold the last shot X coordinate */
public int lastShotX;
/**This hold the last shot Y coordinate */
public int lastShotY;
/**Not used, but will be for smarter AI
* will be for determining if last shot was a hit or not */
public boolean wasLastShotHit;
/**Not used, but will be for smarter AI,
* will be for holding last X hit */
public int lastHitX;
/**Not used, but will be for smarter AI,
* will be for holding last Y hit */
public int lastHitY;
/**Not used, but would be for keeping track of shots fired */
public int shotCounter;
/**Used for passing on the coordinates of the shot fired */
public int shotFired[][];
/**Used to keep track of all shots fired coordinates */
public int shotsFired[][];
/**The random number generator */
public Random gen;
/**Keeps track of the computer AI */
public char typeOfAI;
/**
* This is the computerAI constructor.
* It initializes all variables and
* gets the random generator ready to use.
*
* #param c
* This is the char that determines the type of AI to be used.
*/
public ComputerAI(char c){
lastShotX = -1;
lastShotY = -1;
wasLastShotHit = false;
lastHitX = -1;
lastHitY = -1;
shotCounter = 0;
//At 0,0 shotX will be stored and at 1,0 shotY will be stored
shotFired = new int[2][1];
//There are 100 total shots on a 10x10 board
//So this will hold 100 coordinates of shots
shotsFired = new int[100][2];
for(int i = 0; i < 100; i++){
int j = 0;
shotsFired[i][j] = -1;
shotsFired[i][j+1] = -1;
}
//Initializes the random number generator
gen = new Random(9);
typeOfAI = c;
}
/**
* Grabs the char representing the AI and returns it
*
* #return the type of AI being used
*/
public char getTypeOfAI(){
return typeOfAI;
}
/**
* This is the random shot brain.
* It uses a random number generator to makes random shots.
* It also keeps track of previous shots taken in shotsFired.
* After making sure the shot has been taken before it returns
* those coordinates.s
*
* #return the int[][] that represents the coordinates of the shot fired
*/
public int[][] randomShot(){
int hitX = gen.nextInt(9);
int hitY = gen.nextInt(9);
shotFired[0][0] = hitX;
shotFired[1][0] = hitY;
int i;
for(i = 0; i < 100; i++){
//This if statement is for setting that this shot has been taken
if(shotsFired[i][0] == -1){
shotsFired[i][0] = hitX;
shotsFired[i][1] = hitY;
break;
}
//This if statement is to make sure the shot being generated
//has not been taken yet.
if(shotsFired[i][0] == hitX && shotsFired[i][1] == hitY){
hitX = gen.nextInt(9);
hitY = gen.nextInt(9);
shotFired[0][0] = hitX;
shotFired[1][0] = hitY;
}
}
System.out.println(hitX +" "+ hitY);
return shotFired;
}
/**
* This is the systematic brain.
* It marches along row by row to fire shots.
* It keeps track of the last shot taken by using
* lastShotX and lastShotY. It then return the
* shot fired.
*
* #return the int[][] that represents the coordinates of the shot fired
*/
public int[][] systematicShot(){
if(lastShotX == -1 && lastShotY == -1){
lastShotX = 0;
lastShotY = 0;
shotFired[0][0] = lastShotX;
shotFired[1][0] = lastShotY;
return shotFired;
}
for(int i = 0; i < 10; i++){
if(lastShotX == -1){
lastShotX = 0;
shotFired[0][0] = lastShotX;
shotFired[1][0] = lastShotY;
return shotFired;
}
for(int j = 0; j < 10; j++){
if(lastShotX == j && lastShotY == i){
if(lastShotX == 9){
lastShotX = -1;
lastShotY++;
}
else{
lastShotX = j+1;
shotFired[0][0] = lastShotX;
shotFired[1][0] = lastShotY;
return shotFired;
}
}
}
}
return shotFired;
}
}
If you compare your method computerButtonHitter
if(opponentB.fireShot(x, y) == true){
opp[x][y].setBackground(Color.RED);
opp[x][y].setFont(font);
opp[x][y].setText("H");
}
else{
opp[x][y].setBackground(Color.BLUE);
opp[x][y].setFont(font);
opp[x][y].setText("M");
}
with the FireButtonListener
if(playerB.fireShot(fb.getCell().getColumn(), fb.getCell().getRow()) == true){
play[fb.getCell().getRow()][fb.getCell().getColumn()].setBackground(Color.RED);
play[fb.getCell().getRow()][fb.getCell().getColumn()].setFont(font);
play[fb.getCell().getRow()][fb.getCell().getColumn()].setText("H");
play[fb.getCell().getRow()][fb.getCell().getColumn()].setEnabled(false);
}
else{
play[fb.getCell().getRow()][fb.getCell().getColumn()].setBackground(Color.BLUE);
play[fb.getCell().getRow()][fb.getCell().getColumn()].setFont(font);
play[fb.getCell().getRow()][fb.getCell().getColumn()].setText("M");
play[fb.getCell().getRow()][fb.getCell().getColumn()].setEnabled(false);
}
you'll find a difference. In short form the first has opponentB.fireShot(x,y) together with opp[x][y].... while the second has playerB.fireShot(c,r) and play[r][c].... so while the first has same order in method and array the second has not. I suppose you should swith one of those in order to remain consistent with each other. So either opp[y][x] or play[c][r] but I cannot judge which one is correct (because I do not have full code).

Categories