21.1k views
5 votes
Challenge: Tic-Tac-Toe (Report a problem) Detecting mouse clicks For this step of the challenge you will complete Tile's handleMouseClick method. The handleMouseClick method has two parameters: x and y which represent the coordinates of where the user clicked the mouse. When a user clicks inside a tile, that tile's handleMouseClick method should call that tile's onClick method. To check if the mouse click is inside of the tile, you will need an if statement that checks if: - the mouse click is on, or right of, the left edge of the tile - the mouse click is on, or left of, the right edge of the tile - the mouse click is on, or below, the upper edge of the tile - the mouse click is on, or above, the lower edge of the tile

2 Answers

3 votes

Final answer:

To detect a mouse click within a tile in a Tic-Tac-Toe game, one must implement a conditional statement in the handleMouseClick method that checks if the click's coordinates fall within the tile's bounds. If the click is valid, the tile's onClick method is called.

Step-by-step explanation:

The student's question concerns a programming task involving the development of a Tic-Tac-Toe game and specifically addresses the implementation of a method to detect mouse clicks within a tile. To accomplish this, the handleMouseClick method needs an if statement to check whether the click occurred within the boundaries of the tile. The if statement must compare the x and y coordinates of the mouse click against the left, right, upper, and lower edges of the tile.

To implement this, the conditional check might look like this:

if (x >= tileLeftEdge && x <= tileRightEdge && y >= tileTopEdge && y <= tileBottomEdge) {
onClick(); // Call the onClick method for the tile
}

This code snippet checks that the mouse click's x coordinate is within the horizontal bounds of the tile and the y coordinate is within the vertical bounds. If both conditions are satisfied, it then calls the onClick method associated with the tile, indicating a valid click.

User MattMS
by
3.7k points
3 votes

Answer:

var playerTurn;

var NUM_COLS;

var NUM_ROWS;

var SYMBOLS;

var tiles = [];

var checkWin = function()

{ };

var Tile = function(x, y)

{

this.x = x;

this.y = y;

this.size = width/NUM_COLS;

this.label = "";

};

Tile.prototype.draw = function()

{

fill(214, 247, 202);

strokeWeight(2);

rect(this.x, this.y, this.size, this.size, 10);

textSize(100);

textAlign(CENTER, CENTER);

fill(0, 0, 0);

text(this.label, this.x+this.size/2, this.y+this.size/2);

};

Tile.prototype.empty = function()

{

return this.label === "";

};

Tile.prototype.onClick = function()

{

// If the tile is not empty, exit the function

// Put the player's symbol on the tile

// Change the turn

};

Tile.prototype.handleMouseClick = function(x, y)

{

// Check for mouse clicks inside the tile

};

for (var i = 0; i < NUM_COLS; i++)

{

for (var j = 0; j < NUM_ROWS; j++)

{

tiles.push(new Tile(i * (width/NUM_COLS-1), j * (height/NUM_ROWS-1)));

}

}

var drawTiles = function()

{

for (var i in tiles)

{

tiles[i].draw();

}

};

mouseReleased = function()

{

for (var i in tiles)

{

tiles[i].handleMouseClick(mouseX, mouseY);

}

};

draw = function()

{

background(143, 143, 143);

drawTiles();

};

Step-by-step explanation:

User SnNaCk
by
3.5k points