As I know TicTacToe is a solved game. I want to create a strategy for computer player on which basis he will decide where(on which) put cross.
In my game the game board is:
Field[][] fields; //which has size n x n, definition on fields is at the bottom:
The n could be 3,4,5. I found some strategies only for 3x3 version.
What strategy should computer use to win?
Field can have state: EMPTY, CROSS or CIRCLE.
import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JButton;
public class Field extends JButton {
public final static int PREFERRED_SIZE = 100;
public final static int EMPTY = 1, CROSS = 2, CIRCLE = 4;
private int state;
public Field() {
state = EMPTY;
}
public void setState(int state) {
if(state != EMPTY) {
this.state = state;
setEnabled(false);
}
}
public void getState(){return state;}
public Dimension getPreferredSize() {
return new Dimension(PREFERRED_SIZE, PREFERRED_SIZE);
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setStroke(new BasicStroke(4));
int shorterEdge = (int) (Math.min(getWidth(), getHeight()));
int startXY = (int) (1d / 5d * shorterEdge);
if(state == CIRCLE) {
int ovalSize = (int) (3d / 5d * shorterEdge);
g2d.drawOval(startXY, startXY, ovalSize, ovalSize);
System.out.println(getWidth() + " x " + getHeight());
} else if(state == CROSS) {
g2d.drawLine(startXY, startXY, shorterEdge - startXY, shorterEdge - startXY);
g2d.drawLine(shorterEdge - startXY, startXY, startXY, shorterEdge - startXY);
}
}
}