简体   繁体   English

如果矩形中没有写入任何内容,如何让 if 类什么都不做?

[英]How do I make an if class do nothing if there is nothing written in the rectangle?

So I'm making Tic Tac Toe and I made it so that when I click on the empty rectangles (tiles) it puts the label X or O on the tile, but if the tile is labeled (not empty) I want the code to not write anything.所以我正在制作 Tic Tac Toe 并且我做了它以便当我点击空矩形(瓷砖)时,它会将标签 X 或 O 放在瓷砖上,但是如果瓷砖被标记(非空)我希望代码不写任何东西。

That's how the piece of code looks currently:这就是这段代码目前的样子:

 if (!this.empty ()) {
        exit ();
    }

The issue is that when I press on a tile that is labeled the program stops entirely and I can no longer see a X or an O when clicking on an empty tile.问题是,当我按下标有标签的磁贴时,程序完全停止,并且在单击空磁贴时我再也看不到 X 或 O。 Is there an alternative for exit();是否有 exit() 的替代方案; that doesn't do that?那不这样做吗?

Here's the complete code (this code is actually not by me it's an practice from KhanAcademy): https://www.khanacademy.org/computing/computer-programming/programming-games-visualizations/memory-game/pc/challenge-tic-tac-toe这是完整的代码(这段代码实际上不是我写的,它是 KhanAcademy 的实践): https ://www.khanacademy.org/computing/computer-programming/programming-games-visualizations/memory-game/pc/challenge-tic -脚趾

var playerTurn = 0;
var NUM_COLS = 3;
var NUM_ROWS = 3;
var SYMBOLS =["X","O"];
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
    if (!this.empty ()) {
        exit();
    }
    // Put the player's symbol on the tile

        this.label = SYMBOLS[playerTurn];

    // Change the turn
    playerTurn ++;
    if (playerTurn >= 1) {
        playerTurn = 0;
    }
};

Tile.prototype.handleMouseClick = function(x, y) {
    // Check for mouse clicks inside the tile
    if (x >= this.x && x <= this.x + this.size &&
    y >= this.y && y <= this.y + this.size)
    {
        this.onClick();
    }
};

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();

};

The function exit is not a native function to javascript.函数exit不是 javascript 的本机函数。 It means nothing in this context as it has not been defined anywhere.在这种情况下它没有任何意义,因为它没有在任何地方定义。 You can use the word return to end execution of a function - it does what I think you're trying to accomplish with exit()您可以使用return一词来结束函数的执行 - 它可以完成我认为您尝试使用exit()完成的任务


Tile.prototype.onClick = function() {
    // If the tile is not empty, exit the function
    if (!this.empty ()) {
        return
    } else {
    // Put the player's symbol on the tile

        this.label = SYMBOLS[playerTurn];

    // Change the turn

    playerTurn ++;
    if (playerTurn >= 1) {
        playerTurn = 0;
    }

  }

};

Okay I solved it indirectly xD好的,我间接解决了它xD

What I did was change我所做的是改变

Tile.prototype.onClick = function() {
    // If the tile is not empty, exit the function
    if (!this.empty ()) {
        exit();
    }
    // Put the player's symbol on the tile

        this.label = SYMBOLS[playerTurn];

    // Change the turn
    playerTurn ++;
    if (playerTurn >= 1) {
        playerTurn = 0;
    }
};

to

Tile.prototype.onClick = function() {
    // If the tile is not empty, exit the function
    if (this.empty ()) {
        // Put the player's symbol on the tile
        this.label = SYMBOLS[0];
        SYMBOLS.reverse();
    }
};

So instead of making an if-class for all the not-empty tiles that should do nothing and the rest is done ouside of the if-class, I made an if class for the empty tiles.因此,我没有为所有不应该做任何事情的非空瓷砖创建一个 if 类,其余的在 if 类之外完成,我为空瓷砖创建了一个 if 类。

Yeah I know it was stupid but heyyy at least it's solved now.是的,我知道这很愚蠢,但嘿嘿,至少现在已经解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM