Game tic tac toe. Extra methods removed.

public class Field { public String[][] figures = new String[3][3]; public String getFigure(final Point point) { return figures[point.x][point.y]; } } public class Point { public int x; public int y; } 

Now you need to make a check for matches on verticals, horizontals and diagonals. Letting go of all the subtleties regarding whose course, how many pieces on the field, and so on.

 public class A { public String getWinner(final Field field) { Point point1 = new Point(); point1.x = 0; point1.y = 0; Point point2 = new Point(); point1.x = 0; point1.y = 1; Point point3 = new Point(); point1.x = 0; point1.y = 2; if (field.getFigure(point1) != null && field.getFigure(point2) != null && field.getFigure(point3) != null && field.getFigure(point1).equals(field.getFigure(point2)) && field.getFigure(point2).equals(field.getFigure(point3))) { return field.getFigure(point1); } return "no winners"; } } 
  1. I understand what needs to be done through the cycle, but I don’t quite understand how. I can stupidly "lick", but I want to understand.
  2. If we have a fixed number of cells, is it possible to implement this through SWITCH?
  3. If I fill all 9 cells of the array String, am "X" or "O", I still get an exception that I can not understand - Exception in thread "main" java.lang.NoSuchMethodException:
    XOgame.model.A.main ([Ljava.lang.String;) at java.lang.Class.getMethod (Class.java:1786) at com.intellij.rt.execution.application.AppMain.main (AppMain.java: 125)
  4. I study a similar subject Java. Arrays We need a method that checks the winner in tic-tac-toe
  • Is there no constructor in the class Point (int, int) ? - vp_arth
  • No, in the Point class there are only two fields, x and y. - RattenGW
  • And in vain, add a constructor and 3 lines will turn into one. In addition, errors will automatically go away when you initialize the first point three times. - vp_arth

1 answer 1

Let us consider the general case when the field is large and the one who can build the first n rows in one row wins.

Let F be the set of all figures of a given type on the board.

  1. For each f from F, we can define a set of neighboring Nf figures of the same type.

  2. For each adjacent figure from Nf, we can determine its location relative to the source (N, NE, E, SE, S, SW, W, NW).

  3. Then, for each adjacent figure, we can check in a loop to the size of the winning sequence whether it is possible to take another step in the same direction.

At first glance, there are three cycles in different methods: enumeration of figures of a given type, enumeration of neighboring figures of a given type, enumeration of steps in a given direction.

You can optimize (if necessary, in the case of a large board) an algorithm for selecting adjacent figures, initially creating a graph in memory. You can also think of something about reducing the original set, throwing out obviously inappropriate positions from it. For example, if a connected component of a graph contains fewer elements than it should be in a winning sequence, it can be not checked. In addition, you can make calculations on each turn, processing only the changed connected component.

I hope this will help you to compile a code in which the business logic of the game is explicit and readable.