
Javaを使ったオセロゲームの作成
1. 仕様.
・The player (yourself) uses black stones
・The CPU uses white stones
・The black stones go first and the white stones go second at the start of the game
・Stones can only be placed in places where they can flip over opponent's stones
・If there are no stones that can be flipped over, it becomes the opponent's turn
・The game ends when there are no more places to place stones or when only one player has stones on the board
・The player with the most stones at the end of the game is the winner
・Coordinates must be entered as two-digit numbers from 1 to 8 using half-width characters
・If you select a coordinate that cannot be placed, you must enter it again
2. 実装.
The implementation method will be a class that receives numerical input from the console and evaluates that number
, a class that places stones on the board based on the received number and flips or updates the board
, a class that displays the updated board
, and a class that allows the CPU to decide where to place the stones
.
3. 定数クラス.

Implement a constant class to be used when displaying Othello stones and outputting messages.
othelloConstants.java
package main.java.common;
public class othelloConstants {
//オセロ石
public static final String BLACK = "●";
public static final String WHITE = "○";
public static final String EMPTY = " ";
//盤面大きさ
public static final int STAGE_WIDTH = 10;
public static final int STAGE_HEIGHT = 10;
//説明
public static final String MESSAGE_INPUT = "縦軸横軸で石を置きたい番号をX,Y軸で入力してください(1~8)";
public static final String MESSAGE_EROOR = "指定された数字をで入力してください";
public static final String MESSAGE_ORDER_BLACK = "黒のターンです";
public static final String MESSAGE_ORDER_WHITE = "白(CPU)のターンです";
public static final String MESSAGE_ORDER_PASS = "石を置ける場所が無いので相手のターンです";
public static final String MESSAGE_BLACK_WIN = "黒の勝ちです";
public static final String MESSAGE_WHITE_WIN = "白の勝ちです";
public static final String MESSAGE_DRAW = "引き分けです";
}
4. メインクラス.

Create a main method.
Create a board and a list of positions where stones can be placed.
The board is rewritten within each method and is not returned by the return value.
The operation of Othello
is 1) to decide where to place the Othello piece,
2) to determine if a stone can be placed (if not, start again from 1),
3) to place a stone and flip it over (update the board),
and 4) to display the board.
Basically, steps 1 to 4 above are repeated, so a While statement is used for loop processing.
The While loop is exited only when the condition for the end of the game is met, and the game ends.
Each method is called from the main method to make a judgment, such as displaying the board, getting input values, updating the board, and determining whether the game has ended.
5. 盤面表示クラス.

Class for displaying the board on the console. Outputs the board to the console by looping through the board array statements.
View.java
package main.java.logic;
import main.java.common.othelloConstants;
public class View {
public void showBoard(String[][] board) {
//オセロ盤を描写する
for (int i = 0; i < othelloConstants.STAGE_HEIGHT; i++) {
for (int j = 0; j < othelloConstants.STAGE_WIDTH; j++) {
if (j == 9) {
System.out.println(board[i][j]);
break;
}
System.out.print(board[i][j]);
}
}
}
}
6. 入力値取得クラス.
This class receives the coordinates where the player places their stone from the console screen.
Although not yet implemented, a flag is used to determine the player's and CPU's turn (a numerical value is changed each time a turn changes to determine the turn). Coordinates are only accepted during the player's turn, and an input check is performed.
A While statement is also used here, and the loop can only be broken by entering a two-digit number between 1 and 8. Once
a correct input value has been received, the value is returned to the main class as an input value.
Input.java
package main.java.logic;
import java.util.List;
import java.util.Scanner;
import main.java.common.othelloConstants;
import main.java.cpu.Decision;
public class Input {
//石を置く座標を取得する
public String numberEntry(String[][] board, List putBoardList, Update update) {
//コンソールから入力された値
String inputValue;
//入力チェックのフラグ
boolean flg;
//入力した数値を受け取る
Scanner scanner = new Scanner(System.in);
//メッセージCPUの場合は自動で入力値を返す
if (Update.turnFlg % 2 == 0) {
System.out.println(othelloConstants.MESSAGE_ORDER_BLACK);
} else {
System.out.println(othelloConstants.MESSAGE_ORDER_WHITE);
update.boardPutCheck(board, putBoardList);
inputValue = Decision.cpu(putBoardList);
System.out.println(inputValue);
return inputValue;
}
System.out.println(othelloConstants.MESSAGE_INPUT);
//入力チェックがOKになるまで何度も行う。
while (true) {
inputValue = scanner.nextLine();
flg = inputCheck(inputValue);
if (flg)
break;
}
return inputValue;
}
//入力された値の入力チェック
public boolean inputCheck(S7. 盤面更新クラス.

This class mainly handles updating the board.
First, the initial display of the board is written on the walls, and black and white stones are placed in the center, with the rest of the board being left as empty space.
After that, input values are received, and when a stone is placed at the input value, it checks whether there is a stone that can be flipped up, down, left, or right, and if it can be flipped, the color is updated to the color of the placed stone.
If it cannot be placed, it asks for the input value again.
To determine whether a stone can be flipped, it looks at the next coordinate in the eight directions of the coordinate where the stone was placed, if there is a stone of a different color than the one you placed, and if your stone is placed further ahead, it flips the previous stone.
If it reaches an empty row or the end of the array during the check, it is considered that the stone cannot be flipped and the process ends.
Other processes include the same process as above, where a stone on the board is not placed at a coordinate where there is no stone placed, and if it can be flipped if it is placed on the board with the current turn's stone (if not, it goes to the opponent's turn), and it checks whether there is a place on the board, and if there is only one stone on the board, and it also investigates the conditions for the end of the game and passing.
Update.java
package main.java.logic;
import java.util.List;
import main.java.common.othelloConstants;
public class Update {
//白黒のターン判定するために使用。偶数が黒、奇数が白
public static int turnFlg = 0;
public void init(String[][] board) {
//オセロ盤の初期化
for (int i = 0; i < othelloConstants.STAGE_HEIGHT; i++) {
for (int j = 0; j < othelloConstants.STAGE_WIDTH; j++) {
if (i == 0 || i == 9) {
board[i][j] = Integer.valueOf(j).toString();
} else if (i >= 0 && i < 10 && (j == 0 || j == 9)) {8. CPUクラス.
This class determines the outcome of the game and decides the coordinates where the CPU places the stones.
The winner is the player with the most stones on the board at the end of the game.
The CPU randomly selects a coordinate from the available coordinates obtained from the Update class and places the stone.
Decision.java
package main.java.cpu;
import java.util.List;
import java.util.Random;
import main.java.common.othelloConstants;
public class Decision {
//勝敗の判定をする
public void winOrLoss(String[][] board) {
//黒白のカウント
int black = 0;
int white = 0;
//白の石と黒の石どちらが多いか確認する
for (int i = 1; i < 9; i++) {
for (int j = 1; j < 9; j++) {
if (board[i][j] == othelloConstants.BLACK) {
black++;
} else if (board[i][j] == othelloConstants.WHITE) {
white++;
}
}
}
//試合結果の出力
if (black > white) {
System.out.println(othelloConstants.MESSAGE_BLACK_WIN + "黒:" + black + "白:" + white);
} else if (black < white) {
System.out.println(othelloConstants.MESSAGE_WHITE_WIN + "黒:" + black + "白:" + white);
} else {
System.out.println(othelloConstants.MESSAGE_DRAW + "黒:" + black + "白:" + white);
}
}
//石を置ける場所を判断してランダムで配置する
public static String cpu(List putBoardList) {
Random random = new Random();
int listNumber = random.nextInt(putBoardList.size());
return putBoardList.get(listNumber);
}
}
9. まとめ.
I'm glad I was able to implement the Othello game and create a CPU.
While I was writing it, it felt like it was quite long and short, so I felt that it would be better if I could implement it with a more simplified class division.
The CPU is not very elaborate, so I would like to be able to implement something that thinks and acts as a future goal.
