I am new in LibGDX. For learning purpose, I am creating TicTacToe and I spent two days to figure out what went wrong with rendering the cross and circle. Whenever I click on the cell, the cross and circle blinking at the same time. It is supposed to print alternately. What I did wrong in the code?
Here is the code
package com.mytictactoe;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
public class MyTicTacToe extends ApplicationAdapter {
private static final int MAX_FINGERS = 2;
SpriteBatch batch;
Texture tile, circle, cross;
Vector3 mouse;
OrthographicCamera camera;
enum Tile { EMPTY, CROSS, CIRCLE }
final int WIDTH = 480;
final int HEIGHT = 600;
int tileSize;
int boardOffset;
Tile[][] tiles;
Tile currentPlayer = Tile.CROSS;
@Override
public void create() {
tiles = new Tile[3][3];
tileSize = WIDTH / tiles[0].length;
boardOffset = (HEIGHT - tileSize * tiles.length) / 2;
camera = new OrthographicCamera();
camera.setToOrtho(false, WIDTH, HEIGHT);
batch = new SpriteBatch();
tile = new Texture("tile.png");
circle = new Texture("circle.png");
cross = new Texture("cross.png");
}
public void update(float dt) {
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//processing user input
for (int i = 0; i < MAX_FINGERS; i++) {
if (Gdx.input.isTouched(i)) { //using polling
mouse = new Vector3();
mouse.x = Gdx.input.getX(i);
mouse.y = Gdx.input.getY(i);
camera.unproject(mouse);
if (mouse.y > boardOffset
&& mouse.y < boardOffset + tileSize * tiles.length) {
int row = (int)((mouse.y - boardOffset) / tileSize);
int col = (int)(mouse.x / tileSize);
tiles[row][col] = currentPlayer;
currentPlayer = (currentPlayer == Tile.CROSS)
? Tile.CIRCLE : Tile.CROSS;
}
}
}
batch.begin();
for (int row = 0; row < tiles.length; row++) {
for (int col = 0; col < tiles[0].length; col++) {
if (tiles[row][col] == Tile.CIRCLE) {
batch.draw(circle,col * tileSize,
row * tileSize + boardOffset,
tileSize, tileSize);
System.out.println("Circle is printed");
} else if (tiles[row][col] == Tile.CROSS) {
batch.draw(cross,col * tileSize,
row * tileSize + boardOffset,
tileSize,tileSize);
System.out.println("Cross is printed");
} else {
batch.draw(tile, col * tileSize,
row * tileSize + boardOffset,
tileSize, tileSize);
System.out.println("Tile is printed");
}
}
}
System.out.println("Outside the FOR LOOP");
batch.end();
}
@Override
public void dispose() {
batch.dispose();
tile.dispose();
cross.dispose();
circle.dispose();
}
}