Hello! It's a private matter, but I moved to Tokyo from April and became a new member of society. We are currently in the middle of training. (There is 3 months of training.) In the training, all engineers are supposed to learn Java. Object-oriented thinking is important in any programming language, Java is a language designed with object orientation in mind, and I think it's a good language to learn first. It also works on Mac, Windows, and Linux in the same way, so it can be widely used without depending on the computer. Although I studied arrays, for loops, methods, and object orientation, Isn't it difficult for many people to get an idea of how these actually work together on software? Therefore, this time, I would like to create an Othello application that runs on the CLI and put the learned knowledge to practical use. I think the most efficient way to study programming is to "create and publish one application with your own power". Now, let's get into the main subject!
Hello, Othello Have you ever played Othello? There are white and black frames, and when you sandwich the opponent's frame, it changes to the color of your own frame. Ultimately, the one with the most pieces of your own color wins. Isn't it appropriate to divide the classes as follows when creating an Othello application? A frame class that holds the state of white or black, the position of the frame, etc. Field class that judges the turning over of a frame A game class that controls the entire game In order to get a feel for the atmosphere, let's first place a frame on the board and display it!
Information on each frame of Othello is handled in the frame class. The state of the frame (white, black, empty) is managed by the String type state variable. Here, black is B, white is W, and sky is E. Implement the setState () method to change the state and the getPosition () method to get the position.
Koma.java
public class Koma{
private String state; //The color of Othello is black ... B, white ... W, sky ... E
private int x;
private int y;
public Koma(int x,int y){
this.state = "E";
this.x = x;
this.y = y;
}
public String getState(){
return this.state;
}
public void setState(String state){
this.state = state;
}
public int[] getPosition(){
int[]pos = {this.x, this.y};
return pos;
}
}
In the field class, you can place pieces on the board and turn them over. The prepare () method that initializes all the frames on the board in state E, Implement the feature () method that visualizes the situation on the board and outputs it to the command line.
Field.java
import java.util.ArrayList;
import java.util.List;
public class Field{
private List<Koma> komalist;
private int ynum = 0;
private int xnum = 0;
public Field(int xnum, int ynum){
this.xnum = xnum;
this.ynum = ynum;
}
public void prepare(){
this.komalist = new ArrayList<>();
for(int y=0;y<this.ynum;y++){
for(int x=0;x<this.xnum;x++){
Koma koma = new Koma(x,y);
this.komalist.add(koma);
}
}
}
public Koma getKoma(int y, int x){
for(Koma koma : this.komalist){
int[]pos = koma.getPosition();
if(pos[0]==y && pos[1]==x){
return koma;
}
}
return null;
}
public void putKoma(int x, int y, String state){
Koma koma = this.getKoma(x,y);
koma.setState(state);
}
public void feature(){
String [][] board = new String[ynum][xnum];
for(Koma koma : this.komalist){
int[] pos = koma.getPosition();
String state = koma.getState();
board[pos[1]][pos[0]] = state;
}
System.out.println("\n\t0\t1\t2\t3\t4\t5\n");
for(int y=0;y<board.length;y++){
System.out.print(y+"\t");
for(int x=0;x<board[0].length;x++){
String b = board[y][x];
System.out.print(b+"\t");
}
System.out.println("\n");
}
}
}
Call the field class and try to put a black frame at the position of x = 2, y = 2.
Game.java
public class Game{
public static void main(String[] args){
Field field = new Field(6,6);
field.prepare();
field.putKoma(2,2,"B");
field.feature();
}
}
$ javac Game.java
$ java Game
Compile and run a Java program.
The position of x = 2, y = 2 is B, and the other places are E!
Next, we will add a process to turn over the opponent's frame when the frame is placed. Let's imagine Othello concretely. (Assuming a simple situation of 4✕4 for explanation)
In the situation shown in the figure, what happens if you put a white frame on B-1? It's that easy! All black frames in between are flipped to white frames. The black frames on B-2 and B-3 change to white frames.
But what about this case? There are many places to reverse this time! Thanks to the white frame in D-1, the black frame in C-1 is inverted. Thanks to the white frame on D-3, the black frame on C-2 is inverted. Thanks to the white frames on B-4, the black frames on B-2 and B-3 are reversed.
Now, let's put this into the algorithm. It seems a little complicated. As you can see from the two examples above, there are common rules for flipping pieces. That is, in a certain direction, the white frame and the white frame sandwich the black frame. There are eight directions (north, northeast, east, southeast, south, southwest, west, northwest) on the board, so judgment processing must be performed for all directions. The fact that the white frame and the white frame sandwich the black frame is a little sloppy. If you chew a little more so that it can be described in the flow of the program, it will be as follows. If the frame you plan to place is a white frame,
Let's consider using the previous example. If you get the frame list in 8 directions (north, northeast, east, southeast, south, southwest, west, northwest) from the position of B-1, it will be as follows.
direction | Top list |
---|---|
North | - |
Northeast | - |
east | black-White |
Southeast | black-White |
South | black-black-White |
Southwest | black |
West | White |
Northwest | - |
Since the frame color to be placed next is white, it will be turned over in three directions: east, southeast, and south.
It would be interesting if I could make network communication and play against my friends, It seems that the level is a little high, so let's play against the CPU this time. Creating a strong CPU is a little difficult, but a weak CPU can do it right away. A weak CPU can be implemented as follows.
That's it.
Othello has a rule that it should only be placed where flipping occurs (certainly) For example, if the next frame to be placed is white in the situation shown in the image below, There are four places that can be placed in orange (A-3, B-4, C-1, D-2). Randomly select one from these four places and place the frame there Let's say "the CPU placed the frame".
So far, I have explained the Othello program. I think it's important to write a program while feeling the gratitude of object orientation! This time
The code is easy for humans to read by dividing it into three parts. What if you don't have object orientation and you want to do these procedurally? I think it's very, very difficult. The important thing is to think for yourself, design and code the class. By all means, it can be Othello, it doesn't have to be Othello, it can be blackjack, it can be a web server, so when you learn a programming language, you have the experience of creating something from scratch. Let's look!
Recommended Posts