actually build off of this code I have already made for it:
PlayerOne tab:
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);
}
}
}
}
void drawgrid() {
for (int r=0; r<500; r+=25) {
for (int c=0; c<500; c+=25) {
fill(100);
square(r, c, 25);
}
}
fill(25);
}
void printt() {
println();
print("player"+ turn + " board");
println();
for (int r=0; r<20; r++) {
for (int c=0; c<20; c++) {
print(grid[c][r] + ", ");
}
println();
}
}
/*
treat the second tab as a separate processing sketch.
Declare variables in the main tab and
you can use them in both the main and second window.
*/
Player2 tab:
class Player2 extends PApplet {
Player2() {
super();
PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
}
ArrayList shots = new ArrayList();
void settings() {
size(500, 500);
}
void setup() {
drawgrid();
botdrawship();
}
void draw() {
}
void shoot() {
int r, c;
do {
r = round(random(1, 18));
c = round(random(1, 18));
} while (shots.contains(new PVector(r, c)));
shots.add(new PVector(r, c));
if (grid[r][c]==1) {
grid[r][c]=2;
totalship1--;
fill(200, 0, 0);
square(b[r]+7.5, b[c]+7.5, 10);
fill(100);
gameState=GameState.PLAYER_TURN;
}
turn=1;
}
void botdrawship() {
count=0;
while (count<10) {
int r = round(random(1, 18));
int c = round(random(1, 18));
grid[r][c]=-1;
fill(100, 50, 50);
square(b[r], b[c], 25);
count++;
}
}
void grablocation() {
for (int r=0; r<20; r++) {
for (int c=0; c<20; c++) {
if (grid[r][c]==1) {
fill(50, 50, 100);
square(b[r], b[c], 25);
}
}
}
}
void drawgrid() {
for (int r=0; r<500; r+=25) {
for (int c=0; c<500; c+=25) {
fill(100);
square(r, c, 25);
}
}
fill(25);
}
}
Make any adjustments needed