Heres Processing 4 java code with me attempting to do battleship, please use as many…
- User
Heres Processing 4 java code with me attempting to do battleship, please use as many messages as needed to fix the code or improve it:
Player2 myPlayer2; int [] b = {0, 25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 450, 475, 500}; /* values: -2 player2's ship shot. -1 means player2's ships 0 you don't know what is in that cell. 1 means player1's ship, 2 player1's ship shot. 3 is ?? */ int [][] grid = new int [20][20]; int lcx, lcy; boolean gameWin, placedallships, shipguess; boolean isPlayer2Initialized = false; int count; int ships1 = 0, totalship1=10; int ships2 = 0, totalship2=10; int turn = 1;
enum GameState { SETUP, PLAYER_TURN, AI_TURN, GAME_OVER }
GameState gameState = GameState.SETUP;
void checkGameState() { // Implement logic to check for win conditions and change gameState accordingly if (totalship1 == 0 || totalship2 == 0 && ships1 == 0 || ships2 == 0) { gameState = GameState.GAME_OVER; println("Game Over! Winner: " + (totalship1 == 0 ? "Player 2" : "Player 1")); } if (mousePressed && placedallships == true) { gameState = GameState.PLAYER_TURN; } }
void checkForWinAndSwitchTurn() { checkGameState(); if (gameState != GameState.GAME_OVER) { turn = (turn == 1) ? 2 : 1; gameState = (gameState == GameState.PLAYER_TURN) ? GameState.AI_TURN : GameState.PLAYER_TURN; } }
void turns() { checkGameState(); if (gameState == GameState.PLAYER_TURN) { if (placedallships==true) { gameState = GameState.AI_TURN; } } if (gameState == GameState.AI_TURN) { myPlayer2.shoot(); } }
void setup() { background(250); size(500, 500); gameState = GameState.SETUP; drawgrid(); if (!isPlayer2Initialized) { myPlayer2 = new Player2(); isPlayer2Initialized = true; } }
void draw() { if (gameState == GameState.GAME_OVER && key == 'r') { setup(); } if (gameState == GameState.PLAYER_TURN) { turns(); } if (gameState == GameState.AI_TURN) { turns(); } if (key == 'p' && keyPressed) { printt(); } }
void mousePressed() { if (mouseButton == LEFT && turn==1 && placedallships == false) { for (int r=0; r<20; r++) { for (int c=0; c<20; c++) { if (mouseX>b[r] && mouseX<b[r+1] && mouseY>b[c] && mouseY<b[c+1] && ships1<10 && grid[r][c]!=1 && grid[r][c]!=2) { fill(50, 50, 100); ships1++; square(b[r], b[c], 25); grid[r][c]=1; if (ships1==10) { placedallships = true; } } } } } if (mouseButton == RIGHT && gameState == GameState.PLAYER_TURN) { for (int r=0; r<20; r++) { for (int c=0; c<20; c++) { checkGuess(r, c); } } } }
void checkGuess(int r, int c) { if (mouseX>b[r] && mouseX<b[r+1] && mouseY>b[c] && mouseY<b[c+1] && ships1<10 && grid[r][c]==-1) { if (grid[r][c] == -1) { // Hit println("Hit at " + r + ", " + c); grid[r][c] = -2; //Player's 2 ship is shot fill(250, 70, 50); square(b[r], b[c], 15); } else if (grid[r][c] == 0) { // Miss println("Miss at " + r + ", " + c); grid[r][c] = 3; // Miss fill(250, 170, 150); square(b[r], b[c], 15); gameState = GameState.AI_TURN; } } }
void grabshots() { for (int r=0; r<20; r++) { for (int c=0; c<20; c++) { if (grid[r][c]==2) { fill(200, 0, 0); square(b[r]+7.5, b[c]+7.5, 10); fill(100); } } } }
- Assistant