Gomoku game

(rule)

・ Black and white alternate stones in 5x5 (changeable) squares. ・ Win when all the vertical, horizontal, and diagonal squares are filled with stones of the same color.

(Each file)

・ Stone class → Manage stone information ・ Board class → Manage board information ・ Judge class → Manage victory judgment · Constant interface → Manage constants ・ Game class → Summarize as a game

Stone.java


package Gomokunarabe;

public class Stone implements Constant{
	Board board = new Board();


	//True if the stone can be placed normally,False if not placed
	private boolean stoneSet;
	public boolean getStoneSet() {
		return stoneSet;
	}
	public void setStoneSet(boolean stoneSet) {
		this.stoneSet = stoneSet;
	}


	//Field for stone position
	private int tatePosition;
	private int yokoPosition;
	public int getTatePosition() {
		return tatePosition;
	}
	public void setTatePosition(int tatePosition) {
		this.tatePosition = tatePosition;
	}
	public int getYokoPosition() {
		return yokoPosition;
	}
	public void setYokoPosition(int yokoPosition) {
		this.yokoPosition = yokoPosition;
	}


	//Stone placement process
	@SuppressWarnings("resource")
	public void stoneConfig(boolean stoneColor) {
		//Argument stoneColor is true for black, false for white
		System.out.println("Please enter the position of the stone");
		try {
		int tatePosition = new java.util.Scanner(System.in).nextInt();
		int yokoPosition = new java.util.Scanner(System.in).nextInt();
		if((tatePosition>=0&&yokoPosition>=0)&&(tatePosition<BOARD_LENGTH && yokoPosition<BOARD_LENGTH)) {
			if(Board.getPosition(tatePosition, yokoPosition)==EMPTY_STONE) {
				setTatePosition(tatePosition);
				setYokoPosition(yokoPosition);
				board.board(getTatePosition(), getYokoPosition(), stoneColor);
				setStoneSet(true);
			}
			else {
				setStoneSet(false);
				System.out.println("Please choose a place where no stones are placed");
			}
		}
		else {
			setStoneSet(false);
			System.out.println("0~"+(BOARD_LENGTH-1)+"Please enter in one of");
		}
		}
		catch(Exception e) {
			setStoneSet(false);
			System.out.println("Please enter as an integer");
		}
	}

}

Board.java


package Gomokunarabe;

public class Board implements Constant{
	//Save the position of the stone
	private static String[][] position = new String[BOARD_LENGTH][BOARD_LENGTH];
	public static void setPosition(int tate, int yoko, String stone) {
		position[tate][yoko] = stone;
	}
	public static String getPosition(int tate, int yoko) {
		return position[tate][yoko];
	}

	//Black if true, white if false
	private Boolean stoneColor;
	public Boolean isStoneColor() {
		return stoneColor;
	}
	public void setStoneColor(Boolean stoneColor) {
		this.stoneColor = stoneColor;
	}

	public void firstShowBoard() {
		//Number display on the board
		for(int i=0; i<BOARD_LENGTH;i++) {
			if(i==(BOARD_LENGTH-1)) {
				System.out.println(i);
				}
			else if(i==0) {
				System.out.print(" "+i+",");
			}
			else {
				System.out.print(i + ",");
				}
				}
		for(int j=0; j<BOARD_LENGTH; j++) {
			System.out.println(j);
		}

		//At all coordinates of the position array"□"Store
		//Prevent null when referencing position
		for(int i=0; i<BOARD_LENGTH;i++) {
			for(int j=0; j<BOARD_LENGTH; j++) {
				setPosition(i,j,EMPTY_STONE);
				System.out.print(EMPTY_STONE);
			}
			System.out.println("");
		}
	}

	//Go board creation method
	public void board(int tate, int yoko, boolean stoneColor) {
		//stoneColor is true and black,white with false
		setStoneColor(stoneColor);

		for(int i=0; i<BOARD_LENGTH; i++) {
			//The variable i in the for statement means vertical
			for(int j=0; j<BOARD_LENGTH; j++) {
				//The variable j in the for statement means horizontal
				if((i==tate) && (j==yoko)) {
					if(isStoneColor()) {
						setPosition(tate,yoko,BLACK_STONE);
						System.out.print(getPosition(tate,yoko));
					}
					else{
						setPosition(tate,yoko,WHITE_STONE);
						System.out.print(getPosition(tate,yoko));
					}
				}
				else {
					System.out.print(getPosition(i,j));
				}
			}
			System.out.println("");
		}
	}
}

Judge.java


package Gomokunarabe;

public class Judge implements Constant{

	private int win = 0;

	public int getWin() {
		return win;
	}

	public void setWin(int win) {
		this.win += win;
	}
	public void resetWin() {
		this.win = 0;
	}


	public boolean winJudge() {
		//Is the win variable 5?-When the sum of 5 is obtained, true is returned and the victory is judged.

		//Victory judgment in row
		for(int i=0; i<BOARD_LENGTH; i++) {
			for(int j=0; j<BOARD_LENGTH; j++) {
				if(Board.getPosition(i, j)==BLACK_STONE) {
					setWin(1);
				}
				else if(Board.getPosition(i, j)==WHITE_STONE) {
					setWin(-1);
				}
				else {
					setWin(0);
				}
			}

			if((getWin()==BOARD_LENGTH) || (getWin()==(-BOARD_LENGTH))) {
				return true;
			}
			else {
				resetWin();
			}
		}

		//Victory judgment in column
		for(int j=0; j<BOARD_LENGTH; j++){
			for(int i=0; i<BOARD_LENGTH; i++)
			 {
				if(Board.getPosition(i, j)==BLACK_STONE) {
					setWin(1);
				}
				else if(Board.getPosition(i, j)==WHITE_STONE) {
					setWin(-1);
				}
				else {
					setWin(0);
				}
			}

			if((getWin()==BOARD_LENGTH) || (getWin()==(-BOARD_LENGTH))) {
				return true;
			}
			else {
				resetWin();
			}
		}

		//Victory judgment in the left diagonal column
		for(int i=0; i<BOARD_LENGTH; i++) {
			for(int j=0; j<BOARD_LENGTH; j++) {
				if((i==j) && (Board.getPosition(i, j)==BLACK_STONE)) {
					setWin(1);
				}
				else if((i==j) && (Board.getPosition(i, j)==WHITE_STONE)) {
					setWin(-1);
				}
				else {
					setWin(0);
				}
			}
			}
		if((getWin()==BOARD_LENGTH) || (getWin()==(-BOARD_LENGTH))) {
			return true;
		}
		else {
			resetWin();
		}

		//Victory judgment in the right diagonal column
		for(int i=0; i<BOARD_LENGTH; i++) {
			for(int j=0; j<BOARD_LENGTH; j++) {
				if((i+j == (BOARD_LENGTH - 1)) && (Board.getPosition(i, j)==BLACK_STONE)) {
					setWin(1);
				}
				else if((i+j == (BOARD_LENGTH - 1)) && (Board.getPosition(i, j)==WHITE_STONE)) {
					setWin(-1);
				}
				else {
					setWin(0);
				}
			}
			}
		if((getWin()==BOARD_LENGTH) || (getWin()==(-BOARD_LENGTH))) {
			return true;
		}
		else {
			resetWin();
		}

		return false;
	}

	}




Constant.java


package Gomokunarabe;

public interface Constant{
	/*
	 *By changing the constants related to the length of the board
	 *Change squares
	 */

	//Go board length
	int BOARD_LENGTH = 5;

	//Black stone
	String BLACK_STONE = "●";
	//White stone
	String WHITE_STONE = "〇";
	//Blank
	String EMPTY_STONE = "□";

}


Game.java


package Gomokunarabe;

public class Game {
	Stone stone = new Stone();
	Judge judge = new Judge();
	Board board = new Board();

	//True to continue the game, false to end the game
	private boolean gameLoop = true;
	public boolean isGameLoop() {
		return gameLoop;
	}
	public void setGameLoop(boolean gameLoop) {
		this.gameLoop = gameLoop;
	}

	//Black for odd numbers, white for even numbers
	private int loop=1;
	public int getLoop() {
		return loop;
	}
	public void setLoop(int loop) {
		this.loop = loop;
	}

	public void gameStart() {
		board.firstShowBoard();

		while(isGameLoop()) {
			//If gameLoop is true, continue the game
			if(getLoop()%2 == 1) {
				//Black when loop is odd
				System.out.println("It's black turn");
				//Black turn with true argument
				stone.stoneConfig(true);
				if(stone.getStoneSet()) {
					if(!judge.winJudge()) {
					//If winJudge is false, the game continues, and if true, the victory is judged.
					//stoneSet is true and after the next white turn
					setLoop(2);
					}
					else {
						System.out.println("Black victory!\n exit the game!");
						setGameLoop(false);
					}
				}
				else {
					setLoop(1);
				}
			}
			//White when loop is even
			else{
				System.out.println("It's white turn");
				stone.stoneConfig(false);

				if(stone.getStoneSet()) {
					if(!judge.winJudge()) {
						//If winJudge is false, the game continues, and if true, the victory is judged.
						//stoneSet is true and after the next white turn
						setLoop(1);
						}
						else {
							System.out.println("White victory!\n exit the game!");
							setGameLoop(false);
						}
					//stoneSet is true and shifts to black turn
					}
				else {
					setLoop(2);
				}
			}

		}
	}


}

Main.java


package Gomokunarabe;

public class Main {

	public static void main(String[] args) {
		Game game = new Game();
		game.gameStart();
	}
}

【Execution result】

★ Gomoku ★ 0,1,2,3,4 0 1 2 3 4 □□□□□ □□□□□ □□□□□ □□□□□ □□□□□ It's black turn Please enter the position of the stone 0 0 ●□□□□ □□□□□ □□□□□ □□□□□ □□□□□ It's white turn Please enter the position of the stone 0 4 ●□□□〇 □□□□□ □□□□□ □□□□□ □□□□□ It's black turn Please enter the position of the stone 1 1 ●□□□〇 □●□□□ □□□□□ □□□□□ □□□□□ It's white turn Please enter the position of the stone 1 4 ●□□□〇 □●□□〇 □□□□□ □□□□□ □□□□□ It's black turn Please enter the position of the stone 2 2 ●□□□〇 □●□□〇 □□●□□ □□□□□ □□□□□ It's white turn Please enter the position of the stone 2 0 ●□□□〇 □●□□〇 〇□●□□ □□□□□ □□□□□ It's black turn Please enter the position of the stone 3 3 ●□□□〇 □●□□〇 〇□●□□ □□□●□ □□□□□ It's white turn Please enter the position of the stone 2 4 ●□□□〇 □●□□〇 〇□●□〇 □□□●□ □□□□□ It's black turn Please enter the position of the stone 4 4 ●□□□〇 □●□□〇 〇□●□〇 □□□●□ □□□□● Black victory! End the game!

Recommended Posts

Gomoku game
Rock-paper-scissors game
Kinx Algorithm-Life Game