Reputation: 441
I'm trying to make simple tic-tac-toe game. I have a function that places a symbol every time I click on the grid. I have 2 variables for different symbols xSym
and circlSym
. I need to change the placed symbol from one to another, every time function mouseClicked
is called. Is it possible to do so without any return from a function?
public void mouseClicked(MouseEvent e) {
int r = e.getY() / cellH;
int c = e.getX() / cellW;
print(r, c, (char)xSym);
refresh();
}
Upvotes: 1
Views: 1053
Reputation: 727077
Make a boolean
member variable isX
to represent the current state. When isX
is set, xSym
is selected; otherwise, circleSym
is selected.
Each time end-user clicks your app, change isX
's state to the opposite. You can use the NOT !
operator for this, as follows:
boolean isX = true; // X moves first
public void mouseClicked(MouseEvent e) {
char currentSym = isX ? xSym : circleSym;
isX = !isX; // Invert isX
int r = e.getY() / cellH;
int c = e.getX() / cellW;
print(r, c, currentSym);
refresh();
}
Variable currentSym
represents the current symbol selected based on the value of isX
variable.
Upvotes: 2