-4

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);
        }
    }
}
Yoda
  • 17,363
  • 67
  • 204
  • 344
  • 2
    Did I misunderstand something or are you asking SO for tic-tac-toe strategy? – kviiri May 18 '14 at 12:12
  • @kviiri Right, I do, there is a tag tic-tac-toe already. Where should I ask this question? – Yoda May 18 '14 at 12:17
  • I don't know but Stack Overflow is not exactly the place for asking for game strategies. However, I can help you _generate_ strategies - see my answer. It's very easy to create a working tic-tac-toe AI using it. – kviiri May 18 '14 at 12:35

1 Answers1

0

Since Stack Overflow is a programming helpdesk and not a game strategy site, here's a way to generate the strategy programmatically:

Step 1: model the possible game states as a game tree

Step 2: label the leaf nodes of the game tree as wins, losses or ties

Step 3: whenever it's the computer's turn, use a minimax search to progress down the game tree towards options that are beneficial to the computer assuming the other player minimaxes as well (winning is better than tying, tying is better than losing).

And it's done. That's literally all it takes to create an AI for a game as simple as tic-tac-toe. Just look down the tree to see which branch is certain victory and which branch is certain loss, and go with the best one. For extra practice and efficiency, Alpha-Beta pruning makes the minimax part a bit faster, although with small boards of tic-tac-toe this won't matter in practice.

kviiri
  • 3,282
  • 1
  • 21
  • 30