概要
今回はjavaでオセロゲームのコードを書いてみました。
練習用で記述していますのでご承知ください。
参考元
以下の記事を参考元しています。
@nogitsune413様
大変勉強になりました。ご投稿感謝申し上げます。
ソースコード
Osero.java
import java.util.Scanner;
public class Osero{
private static final int L = 10,
WALL = 9,
EMPTY = 0,
BLACK = 1,
WHITE = 2,
PASS = 1,
EXIT = 2,
GIVE_UP = 3,
DRAW = 3,
SUSPENDED = 4,
X = 0,
Y = 1,
ONE_SIDE = 8;
private static final String LINE = "--------------------------\n",
TITLE = "\n" + LINE + "--- オセロ ---\n" + LINE + "\n",
USAGE = " --- 遊び方 --- \n 縦 5,横 3 のマスに置く => 「5 3」と入力。\n パス: pass \n 投了:give up\n ゲームの終了:exit\n",
URGES = "\n PUT KOMA",
RANGE = "[1-8]";
private static Scanner sc = new Scanner(System.in);
private static int[][] board = new int[L][L];
private static int turn = BLACK,
victory;
private static final int[][] dir={
{-1,-1},{0,-1},{1,-1},{1,0},
{1,1},{0,1},{-1,1},{-1,0}
};
/* オセロの盤面を座標と見做したときのx軸、y軸の値 */
private static int x,y;
static class Turn{
private static void show(){
if (turn==BLACK){System.out.println("BLACK_TURN");}
else{System.out.println("WHITE_TURN");}
}
private static void shift(){
if (turn==BLACK) turn = WHITE;
else turn = BLACK;
}
}
static class Board{
//10 × 10マスの四隅を全て壁に設定する
public static void init(){
int i;
for (i = 0;i<L;i++){
//水平方向の壁を設定
board[0][i] = board[9][i] = WALL;
//垂直方向の壁を設定
board[i][0] = board[i][9] = WALL;
}
board[4][4] = board[5][5] = WHITE;
board[4][5] = board[5][4] = BLACK;
}
//駒をオセロ盤上に表示させる
public static void show(){
int board_x,board_y;
System.out.print(LINE);
for(board_x = 0;board_x<L;board_x++){
System.out.print(" ");
System.out.print(board_x);
}
System.out.print("\n");
for(board_y = 0;board_y<L;board_y++){
System.out.print(board_y);
for(board_x = 0;board_x<L;board_x++){
switch(board[board_y][board_x]){
case WALL : {System.out.print("D"); break;}
case EMPTY : {System.out.print("-"); break;}
case WHITE : {System.out.print("o"); break;}
case BLACK : {System.out.print("x"); break;}
}
System.out.print(" ");
}
System.out.print("\n") ;
}
}
}
private static int input(){
int ret = 0;
while(true){
String[] s = sc.nextLine().split("\\s");
if (s.length==1) {
if(s[0].equals("pass")){ret = PASS;break;}
else if(s[0].equals("exit")){ret = EXIT;break;}
}else if(s.length==2){
if(s[0].equals("give") && s[1].equals("up")){ret = GIVE_UP;break;}
else if(s[0].matches(RANGE) && s[1].matches(RANGE)){
y = Integer.parseInt(s[0]);
x = Integer.parseInt(s[1]);
break;
}
}
System.out.print("put other space\n");
System.out.println(USAGE);
System.out.println(URGES);
}
return ret;
}
private static void show_result(){
String ret;
if(victory == DRAW)ret = "------- DRAW-------";//victory == 3
else if (victory == BLACK)ret = "------- LOSE-------";//victory == 1
else if (victory == WHITE)ret = "------- WIN-------";//victory == 2
else ret = "------- SUSPENDED-------";//victory == 4
System.out.println(ret);
Board.show();
}
private static boolean chk_cell(){
boolean result = false;
int i;
if(board[y][x]!=EMPTY)return result;
out:for(i=0;i<dir.length;i++){
int j=x,k=y;
j += dir[i][X];
k += dir[i][Y];
if (board[k][j] == 3 - turn) {
while (true) {
j += dir[i][X];
k += dir[i][Y];
if (board[k][j] == turn) {
result = true;
break out;
}else if(board[k][j] == 3 -turn) continue;
break;
}
}
}
return result;
}
private static void flip(){
board[y][x] = turn;
int i;
for(i = 0;i<dir.length;i++){
int j=x,k=y;
j += dir[i][X];
k += dir[i][Y];
if (board[k][j] == 3 -turn) {
out:while (true) {
j += dir[i][X];
k += dir[i][Y];
if (board[k][j] == turn) {
while (true) {
j -= dir[i][X];
k -= dir[i][Y];
if(board[k][j]==turn)break out;
board[k][j] = turn;
}
}else if(board[k][j]== 3 - turn) continue;
break;
}
}
}
}
private static boolean update(){
if(chk_cell()){
flip();
return true;
}else return false;
}
private static boolean judge(){
int black = 0;
int white = 0;
int i,j;
// 石を数える
for(i=1;i<L-1;i++){
for(j=1;j<L-1;j++){
if(board[i][j]==BLACK){
black++;
} else if(board[i][j]==WHITE){
white++;
}
}
}
String total_black = String.format("Black_koma>>>%d",black);
String total_white = String.format("white_koma>>>%d",white);
System.out.println(total_black);
System.out.println(total_white);
if(black+white==ONE_SIDE*ONE_SIDE){
if(black<white){
victory = WHITE;
} else if(black==white){
victory = DRAW;
} else {
victory = BLACK;
}
return true;
}
return false;
}
public static void main(String[] args){
Board.init();
System.out.println(TITLE);
System.out.println(USAGE);
out:while(true){
Turn.show();
Board.show();
System.out.println(URGES);
switch (input()) {
case EXIT:
System.out.print("See yout next time\n");
break out;
case GIVE_UP:
if (judge() == false)victory = SUSPENDED;
else victory = 3 - turn;
show_result();
break out;
case PASS:
Turn.shift();
continue;
}
if (update()) {
if (judge()) {
show_result();
break;
}
Turn.shift();
}else System.out.print("Not put\n");
}
}
}