//オセロゲーム //安澤出海 import java.io.*; import java.util.*; public class hatten2{ private static int othello[][];//フィールド変数。黒:1 白:2 無:0 private static boolean shoriok; //全体的にみてこの入力はOK? private static boolean up; //上方向で覆せるか? private static boolean up_right; //右上方向で private static boolean right; //右方向で覆せるか? private static boolean down_right; //右下方向で覆せるか? private static boolean down; //下方向で覆せるか? private static boolean down_left; //左下方向で覆せるか? private static boolean left; //左方向で覆せるか? private static boolean up_left; //左上方向で覆せるか? private static final int BLACK=1; private static final int WHITE=2; private static final int NOKORI=0; public static void main(String[] args) throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); //ローカル変数の準備 int gyou=0,retsu=0,gamemode=0;//0は暫定的に入れておく。 String s1="プレイヤー1",s2="プレイヤー2";//デフォルト Random r=new Random(); timewait t=new timewait(); //ローカル変数の準備完了 printstart();//タイトルを表示 shokika();//フィールド変数を初期化。 init_boolean();//判定を初期化。 gamemode=gamemode();//対戦モードを選択。 switch(gamemode){//対戦モードに基づき名前設定。 case 1: s1=setYou(); s2=setCp1(); break; case 2: s1=setYou(); s2=setCp2(); break; case 3: s1=setCp1(); s2=setYou(); break; case 4: s1=setCp2(); s2=setYou(); break; case 6: s1=setCp1(); s2=setCp2(); break; default: break; } for(;;){ //先攻・黒 printboard();//盤面表示 System.out.println("黒…"+keisan(BLACK)+" 白…"+keisan(WHITE)+" 残…"+keisan(NOKORI));//枚数。 if(JudgeWay(BLACK,WHITE)==false){ System.out.println(s1+"の打つ手がなくなりましたので試合終了!"); win_lose(s1,s2); } System.out.println(s1+"(黒)のターンです。"); if(gamemode==1 || gamemode==2 || gamemode==5){//手入力 do{ init_boolean();//判定を初期化。 gyou=gyou();//行入力メソッド retsu=retsu();//列入力メソッド search_possible(gyou,retsu,BLACK,WHITE);//判定メソッド if(shoriok!=true){ //display_boolean();//チェック用。削除可能。 System.out.println("正しく入力してください。"); } }while(shoriok!=true);//ルール上可能か } else if(gamemode==3 || gamemode==6){//cp1 do{ init_boolean();//判定を初期化。 gyou=r.nextInt(8); retsu=r.nextInt(8); search_possible(gyou,retsu,BLACK,WHITE);//判定メソッド }while(shoriok!=true);//ルール上可能か System.out.print(s1+"の思考中"); t.ten(3); System.out.println("行>"+(gyou+1) ); t.run(1); display_retsu(retsu);//列表示 t.run(1); } else{//cp2 for(int i=0;i<8;i++){ for(int j=0;j<8;j++){ init_boolean();//判定を初期化。 search_possible(i,j,BLACK,WHITE);//判定メソッド if(shoriok==true){ gyou=i; retsu=j; break; } } if(shoriok==true){break;} } System.out.print(s1+"の思考中"); t.ten(3); System.out.println("行>"+(gyou+1) ); t.run(1); display_retsu(retsu);//列表示 t.run(1); } //display_boolean();//チェック用。削除可能。 reflection(gyou,retsu,BLACK,WHITE);//実行&反映メソッド if(keisan(2)==0){//白が全滅になったらゲーム終了 break; } //後攻・白 printboard();//盤面表示 System.out.println("黒…"+keisan(BLACK)+" 白…"+keisan(WHITE)+" 残…"+keisan(NOKORI));//枚数。 if(JudgeWay(WHITE,BLACK)==false){ System.out.println(s2+"の打つ手がなくなりましたので試合終了!"); win_lose(s1,s2); } System.out.println(s2+"(白)のターンです。"); if(gamemode==3 || gamemode==4 || gamemode==5){//手入力 do{ init_boolean();//判定を初期化。 gyou=gyou();//行入力メソッド retsu=retsu();//列入力メソッド search_possible(gyou,retsu,WHITE,BLACK);//判定メソッド if(shoriok!=true){ // display_boolean();//チェック用。削除可能。 System.out.println("正しく入力してください。"); } }while(shoriok!=true);//ルール上可能か } else if(gamemode==1){//cp1 do{ init_boolean();//判定を初期化。 gyou=r.nextInt(8); retsu=r.nextInt(8); search_possible(gyou,retsu,WHITE,BLACK);//判定メソッド }while(shoriok!=true);//ルール上可能か System.out.print(s2+"の思考中"); t.ten(3); //t.run(2); //System.out.println(); System.out.println("行>"+(gyou+1)); t.run(1); display_retsu(retsu);//列表示 t.run(1); } else{ for(int i=0;i<8;i++){ for(int j=0;j<8;j++){ init_boolean();//判定を初期化。 search_possible(i,j,WHITE,BLACK);//判定メソッド if(shoriok==true){ gyou=i; retsu=j; break; } } if(shoriok==true){break;} } System.out.print(s2+"の思考中"); t.ten(3); System.out.println("行>"+(gyou+1) ); t.run(1); display_retsu(retsu);//列表示 t.run(1); } //display_boolean();//チェック用。削除可能。 reflection(gyou,retsu,WHITE,BLACK);//実行&反映メソッド if( keisan(BLACK)==0 || keisan(NOKORI)==0){//黒が全滅もしくは残りゼロなら終了 break; } } printboard();//盤面表示 System.out.println("黒…"+keisan(BLACK)+" 白…"+keisan(WHITE)+" 残…"+keisan(NOKORI));//枚数を数える。 win_lose(s1,s2);//勝敗判定メソッド } //ここまでがメインメソッド。 private static void display_retsu(int a){ System.out.print("列>"); switch(a){ case 0: System.out.println("A"); break; case 1: System.out.println("B"); break; case 2: System.out.println("C"); break; case 3: System.out.println("D"); break; case 4: System.out.println("E"); break; case 5: System.out.println("F"); break; case 6: System.out.println("G"); break; case 7: System.out.println("H"); break; default: break; } } private static void display_boolean(){//チェック用。削除可能。 System.out.println("up\t\t="+up); System.out.println("up_right\t="+up_right); System.out.println("right\t\t="+right); System.out.println("down_right\t="+down_right); System.out.println("down\t\t="+down); System.out.println("down_left\t="+down_left); System.out.println("left\t\t="+left); System.out.println("up_left\t\t="+up_left); } private static void reflection(int x,int y,int a,int b){ //実行・反映メソッド //(1)up if(up==true){ for(int i=1;;i++){ if(othello[x-i][y]==b){ othello[x-i][y]=a; } else{ break; } } } else{} //(2) if(up_right==true){ for(int i=1;;i++){ if(othello[x-i][y+i]==b){ othello[x-i][y+i]=a; } else{ break; } } } else{} //(3) if(right==true){ for(int i=1;;i++){ if(othello[x][y+i]==b){ othello[x][y+i]=a; } else{ break; } } } else{} //(4) if(down_right==true){ for(int i=1;;i++){ if(othello[x+i][y+i]==b){ othello[x+i][y+i]=a; } else{ break; } } } else{} //(5) if(down==true){ for(int i=1;;i++){ if(othello[x+i][y]==b){ othello[x+i][y]=a; } else{ break; } } } else{} //(6)down_left if(down_left==true){ for(int i=1;;i++){ if(othello[x+i][y-i]==b){ othello[x+i][y-i]=a; } else{ break; } } } else{} //(7)left if(left==true){ for(int i=1;;i++){ if(othello[x][y-i]==b){ othello[x][y-i]=a; } else{ break; } } } else{} //(8)up_left if(up_left==true){ for(int i=1;;i++){ if(othello[x-i][y-i]==b){ othello[x-i][y-i]=a; } else{ break; } } } else{} othello[x][y]=a; } private static void search_possible(int x,int y,int a,int b){ //(入力値x,入力値y,自分の駒の番号a,相手の駒の番号b)黒:1。白:2。 if(othello[x][y]==0){//そこに何もないか? //8方向判定 //(1)up if(x<2){up=false;} else if(othello[x-1][y]==a || othello[x-1][y]==0){ up=false; } else if(othello[x-1][y]==b){ for(int i=2;;i++){ if((x-i)<0){break;} else if(othello[x-i][y]==a){ up=true; break; } //else{} else if(othello[x-i][y]==0){break;} } } //(2)up_right if(x<2 || y>5){up_right=false;} else if(othello[x-1][y+1]==a || othello[x-1][y+1]==0){ up_right=false; } else if(othello[x-1][y+1]==b){ for(int i=2;;i++){ if((y+i)>7 || (x-i)<0){break;} //else{} else if(othello[x-i][y+i]==a){ up_right=true; break; } else if(othello[x-i][y+i]==0){break;} } } //(3)right if(y>5){right=false;} else if(othello[x][y+1]==a || othello[x][y+1]==0){ right=false; } else if(othello[x][y+1]==b){ for(int i=2;;i++){ if((y+i)>7){break;} else if(othello[x][y+i]==a){ right=true; break; } else if(othello[x][y+i]==0){break;} } } //(4)down_right if(x==6||x==7||y==6||y==7){down_right=false;} else if(othello[x+1][y+1]==a || othello[x+1][y+1]==0){ down_right=false; } else if(othello[x+1][y+1]==b){ for(int i=2;;i++){ if((x+i)>7 || (y+i)>7){break;} else if(othello[x+i][y+i]==a){ down_right=true; break; } else if(othello[x+i][y+i]==0){break;} } } //(5)down if(x==6||x==7){down=false;} else if(othello[x+1][y]==a || othello[x+1][y]==0){ down=false; } else if(othello[x+1][y]==b){ for(int i=2;;i++){ if((x+i)>7){break;} else if(othello[x+i][y]==a){ down=true; break; } else if(othello[x+i][y]==0){break;} } } //(6)down_left if(x==6||x==7||y==0||y==1){down_left=false;} else if(othello[x+1][y-1]==a || othello[x+1][y-1]==0){ down_left=false; } else if(othello[x+1][y-1]==b){ for(int i=2;;i++){ if((x+i)>7 || (y-i)<0){break;} else if(othello[x+i][y-i]==a){ down_left=true; break; } else if(othello[x+i][y-i]==0){break;} } } //(7)left if(y==0||y==1){left=false;} else if(othello[x][y-1]==a || othello[x][y-1]==0){ left=false; } else if(othello[x][y-1]==b){ for(int i=2;;i++){ if((y-i)<0){break;} else if(othello[x][y-i]==a){ left=true; break; } else if(othello[x][y-i]==0){break;} } } //else{} //(8)up_left if(x==0||x==1||y==0||y==1){up_left=false;} else if(othello[x-1][y-1]==a || othello[x-1][y-1]==0){ up_left=false; } else if(othello[x-1][y-1]==b){ for(int i=2;;i++){ if((x-i)<0 || (y-i)<0){break;} //else{} else if(othello[x-i][y-i]==a){ up_left=true; break; } else if(othello[x-i][y-i]==0){break;} } } //(1)-(8)の中から一つでもtrueがあるか? if(up==true || up_right==true || right==true || down_right==true || down==true || down_left==true || left==true || up_left==true){ shoriok=true; } } else if(othello[x][y]==a || othello[x][y]==b){ shoriok=false; } } private static int gyou() throws IOException{//行入力&判定メソッド BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int a=0; for(;;){ System.out.print("行を入力してください。(1〜8)>"); try{ a=Integer.parseInt(br.readLine() ); if(a>=1 && a<=8){ a=a-1; break; } else{ System.out.println("正しく入力してください。"); } }catch(Exception e){ System.out.println("正しく入力してください。"); } } return a; } private static int retsu() throws IOException{//列入力&判定メソッド BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int a=0; String s1; for(;;){ System.out.print("列を入力してください。(A〜H)>"); s1=br.readLine(); if(s1.equals("A") || s1.equals("a") ){ a=0; break; } else if(s1.equals("B") || s1.equals("b") ){ a=1; break; } else if(s1.equals("C") || s1.equals("c") ){ a=2; break; } else if(s1.equals("D") || s1.equals("d") ){ a=3; break; } else if(s1.equals("E") || s1.equals("e") ){ a=4; break; } else if(s1.equals("F") || s1.equals("f") ){ a=5; break; } else if(s1.equals("G") || s1.equals("g") ){ a=6; break; } else if(s1.equals("H") || s1.equals("h") ){ a=7; break; } else{ System.out.println("正しく入力してください。"); } } return a; } private static int keisan(int a){//枚数計算(白・黒・残) int b=0; for(int i=0;i<8;i++){ for(int j=0;j<8;j++){ if(othello[i][j]==a){ b++; } } } return b; } private static void printstart(){ System.out.println("┏━┳━━━━━━━━━━━━━━━━━━━━━┳━┓"); System.out.println("┃○┃    ┏┓  ┏┓           ┃●┃"); System.out.println("┣━┫ ┏━━┛┗┓┏┛┗━━━┓┏━━━━┓ ┣━┫"); System.out.println("┃●┃ ┗━┳ ┏┛┗┓┏┳━ ┃┃┏━━┓┃ ┃○┃"); System.out.println("┣━┫  ┏┛┃┃  ┃┃┗━━┛┃┃  ┃┃ ┣━┫"); System.out.println("┃○┃ ┏┛┏┫┃  ┃┃ ┏┓ ┃┃  ┃┃ ┃●┃"); System.out.println("┣━┫ ┗━┛┃┃  ┃┗━┛┃ ┃┗━━┛┃ ┣━┫"); System.out.println("┃●┃    ┗┛  ┗━━━┛ ┗━━━━┛ ┃○┃"); System.out.println("┗━┻━━━━━━━━━━━━━━━━━━━━━┻━┛"); } private static void shokika(){//フィールド変数othelloを初期化。 othello=new int[8][8];//8*8の升目を用意。 othello[3][3]=2; othello[3][4]=1; othello[4][3]=1; othello[4][4]=2; } private static void printboard(){//盤面表示 System.out.println(" ABCDEFGH"); for(int i=0;i<8;i++){ System.out.print(" "+(i+1) ); for(int j=0;j<8;j++){ switch(othello[i][j]){ case 1: System.out.print("○");//黒。黒い画面では反転表示されるから黒。 break; case 2: System.out.print("●");//白。黒い画面では反転表示されるから白。 break; default: System.out.print("・"); break; } } System.out.println(); } } private static int gamemode() throws IOException{//対戦相手選択モード BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int a=0; System.out.println("\t【対戦モード一覧】"); System.out.println("(1)対"+setCp1()+"戦(先攻)\t(2)対"+setCp2()+"戦(先攻)"); System.out.println("(3)対"+setCp1()+"戦(後攻)\t(4)対"+setCp2()+"戦(後攻)"); System.out.println("(5)対人戦\t\t(6)テスト用(cp×cp)"); for(;;){ System.out.print("対戦モードを入力してください。(1〜6)>"); try{ a=Integer.parseInt(br.readLine() ); if(a>=1 || a<=6){ break; } else{ System.out.println("正しく入力してください。"); } }catch(Exception e){ System.out.println("正しく入力してください。"); } } return a; } private static void win_lose(String s1,String s2){ if(keisan(1)>keisan(2) ){ System.out.println(s1+"の勝利!"); } else if(keisan(1)