実現したいこと
- 2人対戦オセロを完成させたい。
前提
C言語で二人対戦オセロを作っています。
main関数で手番の管理などの実装中に以下の問題が発生しました。
発生している問題・エラーメッセージ
main関数のループで白の手番のときproc==1のboard_surface()関数以降のプログラムが実行されません。
該当のソースコード
C
1#include <stdio.h> 2#include <stdlib.h> 3#include <stdbool.h> 4 5const int BLACK = 1; 6const int WHITE = -1; 7const int EMPTY = 0; 8 9int board[8][8]={ 10 {0,0,0,0,0,0,0,0}, 11 {0,0,0,0,0,0,0,0}, 12 {0,0,0,0,0,0,0,0}, 13 {0,0,0,0,0,0,0,0}, 14 {0,0,0,0,0,0,0,0}, 15 {0,0,0,0,0,0,0,0}, 16 {0,0,0,0,0,0,0,0}, 17 {0,0,0,0,0,0,0,0} 18}; 19 20void board_init(){ 21 for(int y=0;y<8;y++){ 22 for(int x=0;x<8;x++){ 23 board[y][x]=EMPTY; 24 } 25 } 26 board[3][4]=BLACK; 27 board[4][3]=BLACK; 28 board[3][3]=WHITE; 29 board[4][4]=WHITE; 30} 31 32void board_surface(){ 33 printf(" "); 34 for(int xx=0;xx<8;xx++){ 35 printf(" %d ",xx); 36 } 37 printf("\n"); 38 for(int y=0;y<8;y++){ 39 printf("%d ",y); 40 for(int x=0;x<8;x++){ 41 if(board[y][x]==BLACK){ 42 printf(" ● "); 43 } 44 else if(board[y][x]==WHITE){ 45 printf(" ○ "); 46 } 47 else{ 48 printf(" - "); 49 } 50 } 51 printf("\n"); 52 } 53} 54 55bool can_return(int x,int y,int color){ 56 int total=0; 57 if(board[y][x]>0){ 58 return false; 59 } 60 for(int dx=-1;dx<2;dx++){ 61 for(int dy=-1;dy<2;dy++){ 62 int k=0; 63 int sx=x; 64 int sy=y; 65 if(total>0){ 66 return true; 67 } 68 while(true){ 69 sx+=dx; 70 sy+=dy; 71 if(sy<0 || sy>7 || sx<0 || sx>7){ 72 break; 73 } 74 75 if(board[sy][sx]==EMPTY){ 76 break; 77 } 78 79 if(board[sy][sx]==-color){ 80 k+=1; 81 } 82 83 if(board[sy][sx]==color){ 84 total+=k; 85 break; 86 } 87 } 88 } 89 } 90 if(total>0){ 91 return true; 92 } 93 return false; 94} 95 96void put_stone(int x,int y,int color){ 97 board[y][x]=color; 98 for(int dx=-1;dx<2;dx++){ 99 for(int dy=-1;dy<2;dy++){ 100 int k=0; 101 int sx=x; 102 int sy=y; 103 while(true){ 104 sx+=dx; 105 sy+=dy; 106 if(sx<0 || sx>7 || sy<0 || sy>7){ 107 break; 108 } 109 110 if(board[sy][sx]==EMPTY){ 111 break; 112 } 113 114 if(board[sy][sx]==-color){ 115 k+=1; 116 } 117 118 if(board[sy][sx]==color){ 119 for(int i=0;i<k;i++){ 120 sx-=dx; 121 sy-=dy; 122 board[sy][sx]=color; 123 } 124 break; 125 } 126 } 127 } 128 } 129} 130 131void put_input(int*px,int*py,int color){ 132 do{ 133 printf("横 縦の形で入力:"); 134 scanf("%d %d",px,py); 135 }while(can_return(*px,*py,color)==false); 136} 137 138void can_mark(int color){ 139 printf(" "); 140 for(int xx=0;xx<8;xx++){ 141 printf(" %d ",xx); 142 } 143 printf("\n"); 144 145 for(int y=0;y<8;y++){ 146 printf("%d ",y); 147 for(int x=0;x<8;x++){ 148 if(board[y][x]==BLACK){ 149 printf(" ○ "); 150 } 151 else if(board[y][x]==WHITE){ 152 printf(" ● "); 153 } 154 else if(can_return(x,y,color)==true){ 155 printf(" * "); 156 } 157 else{ 158 printf(" - "); 159 } 160 } 161 printf("\n"); 162 } 163} 164 165bool dis_pass(int color){ 166 int cn=0; 167 for(int x=0;x<8;x++){ 168 for(int y=0;y<8;y++){ 169 cn+=can_return(x,y,color); 170 } 171 } 172 if(cn!=0){ 173 return true; 174 } 175 else{ 176 return false; 177 } 178} 179 180int win_color(){ 181 int bn=0; 182 int wn=0; 183 for(int x=0;x<8;x++){ 184 for(int y=0;y<8;y++){ 185 if(board[y][x]==BLACK){ 186 bn++; 187 } 188 else if(board[y][x]==WHITE){ 189 wn++; 190 } 191 } 192 } 193 if(bn==wn){ 194 return 3; 195 } 196 else if(bn>wn){ 197 return BLACK; 198 } 199 else{ 200 return WHITE; 201 } 202} 203 204int main(void){ 205 int proc=0; 206 int turn=0; 207 int px,py; 208 int color[2]={BLACK,WHITE}; 209 while(true){ 210 switch(proc){ 211 case 0: 212 board_init(); 213 proc=1; 214 case 1: 215 can_mark(color[turn]); 216 put_input(&px,&py,color[turn]); 217 put_stone(px,py,color[turn]); 218 board_surface(); 219 proc=2; 220 case 2: 221 turn=1-turn; 222 if(dis_pass(color[turn])==false){ 223 turn=1-turn; 224 if(dis_pass(color[turn])==false){ 225 switch(win_color()){ 226 case 3: 227 printf("引き分け"); 228 proc=0; 229 case BLACK: 230 printf("黒の勝ち"); 231 proc=0; 232 case WHITE: 233 printf("白の勝ち"); 234 proc=0; 235 } 236 }else{ 237 proc=1; 238 } 239 }else{ 240 proc=1; 241 } 242 } 243 } 244 return 0; 245}
試したこと
board_surface()関数の前にproc=2にすると、proc変数は2になりましたが相変わらずboard_surface()以降のプログラムが実行されません。
回答2件
あなたの回答
tips
プレビュー