I am trying to make a Tic-tac-toe and all is good except for the fact that I want to show an image saying “X wins” until the user clicks the mouse. But somehow the mousePressed variable is true even when I don’t click and the images are also not showing up. The images will show up when I try to use a different program to display them but not in the game itself. Help!!!
Here is the code:
char[][] board =
{{' ', ' ', ' '},
{' ', ' ', ' '},
{' ', ' ', ' '}};
char[] players = {'X', 'O'};
char currentPlayer;
boolean playing = true;
char winner = ' ';
int h, w;
PImage knot, cross, draw;
void setup() {
background(255);
size(480, 480);
currentPlayer = players[0];
h = height /3;
w = width /3;
knot = loadImage("X wins.jpg");
cross = loadImage("0 wins.jpg");
draw = loadImage("draw.jpg");
imageMode(CORNER);
}
void drawXO() {
strokeWeight(2);
int xres = 30;
int yres = 30;
int radius = int(dist(0, 0, w, h) / 2);
for (int j = 0; j < 3; j++) {
for (int i = 0; i < 3; i++) {
char current = board[j][i];
if (current != ' ') {
int xr = i * w;
int yr = j * h;
if (current == 'X') {
line(0 + xr + xres, 0 + yr + yres, w + xr - xres, h + yr - yres);
line(w + xr - xres, 0 + yr + yres, 0 + xr + xres, h + yr - yres);
} else if (current == 'O') {
noFill();
ellipseMode(CENTER);
ellipse(w/2 + xr, h/2 + yr, radius, radius);
}
}
}
}
}
void drawBoard() {
strokeWeight(5);
line(w, 0, w, height);
line(w * 2, 0, w *2, height);
line(0, h, width, h);
line(0, h * 2, width, h *2);
}
void takeUserInput() {
int x = mouseX;
int y = mouseY;
int indx = 0, indy = 0;
for (int k = 0; k < 3; k++) {
for (int l = 0; l< 3; l++) {
if (x >= k * w && x <= (k + 1) * w) {
if (y >= l * h && y <= (l + 1) * h) {
indx = l;
indy = k;
}
}
}
}
if (board[indx][indy] == ' ') {
board[indx][indy] = currentPlayer;
char win = checkWinner();
if (win == ' ') {
changePlayer();
}
}
}
void changePlayer() {
if (currentPlayer == players[0]) {
currentPlayer = players[1];
} else if (currentPlayer == players[1]) {
currentPlayer = players[0];
}
}
char checkWinner() {
for (int m = 0; m < 3; m++) {
if (board[m][0] == board[m][1] && board[m][1] == board[m][2] && board[m][0] !=' ') {
winner = currentPlayer;
line(0, h/2 + m * h, width, h/2 + m * h);
}
if (board[0][m] == board[1][m] && board[1][m] == board[2][m] && board[0][m] !=' ') {
winner = currentPlayer;
line(w/2 + m * w, 0, w/2 + m * w, height);
}
}
if (board[0][0] == board[1][1] && board[2][2] == board[1][1] && board[0][0] !=' ') {
winner = currentPlayer;
line(0, 0, width, height);
}
if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] !=' ') {
winner = currentPlayer;
line(width, 0, 0, height);
}
return winner;
}
void draw() {
drawBoard();
if (mousePressed && checkWinner() == ' ' && playing) {
takeUserInput();
} else if (mousePressed && checkWinner() != ' ') {
resetBoard();
}
if (isDraw() && winner == ' ') {
winner = 'D';
resetBoard();
}
drawXO();
}
void resetBoard() {
delay(100);
playing = false;
char w = winner;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = ' ';
}
}
winner = ' ';
drawImage(w);
while (!playing) {
if (mousePressed) {
playing = true;
w = ' ';
}
}
background(255);
}
boolean isDraw() {
boolean a = true;
for (int i = 0; i < 3; i++) {
for (int j = 0; j< 3; j++) {
if (board[i][j] == ' ') {
a = false;
}
}
}
return a;
}
void drawImage(char w) {
if (w == 'X') {
image(knot, 0, 0);
} else if (w == 'O') {
image(cross, 0, 0);
} else if (w == 'D') {
image(draw, 0, 0);
}
}