Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
othello.h
00001 #include <stdint.h> 00002 #include <stdlib.h> 00003 #include <string.h> 00004 #include <vector> 00005 #define EMPTY 0 00006 #define BLACK 1 00007 #define WHITE -1 00008 00009 class Board{ 00010 public: 00011 int8_t board[8][8]; 00012 int8_t turn; 00013 Board(){ 00014 for(int8_t i=0;i<8;i++){ 00015 for(int8_t j=0;j<8;j++)board[i][j]=EMPTY; 00016 } 00017 board[3][4]=BLACK;board[4][3]=BLACK; 00018 board[3][3]=WHITE;board[4][4]=WHITE; 00019 turn=BLACK; 00020 } 00021 void SetBoard(int8_t board[8][8]){ 00022 memcpy(this->board,board,sizeof board); 00023 } 00024 }; 00025 00026 void SetLEDBoard(Board board); 00027 00028 /*void ResetBoard(){ 00029 for(uint8_t i=0;i<8;i++){ 00030 for(uint8_t j=0;j<8;j++)board[i][j]=EMPTY; 00031 } 00032 board[3][4]=BLACK;board[4][3]=BLACK; 00033 board[3][3]=WHITE;board[4][4]=WHITE; 00034 }*/ 00035 00036 bool CanRight(Board board,int8_t x,int8_t y,int8_t color){ 00037 int8_t enemy=-color; 00038 if(x<=5&&board.board[x+1][y]==enemy){ 00039 for(int8_t i=x+2;i<8;i++){ 00040 if(board.board[i][y]==color)return true; 00041 else if(board.board[i][y]==EMPTY)break; 00042 } 00043 } 00044 return false; 00045 } 00046 00047 bool CanLeft(Board board,int8_t x,int8_t y,int8_t color){ 00048 int8_t enemy=-color; 00049 if(x>=2&&board.board[x-1][y]==enemy){ 00050 for(int8_t i=x-2;i>=0;i--){ 00051 if(board.board[i][y]==color)return true; 00052 else if(board.board[i][y]==EMPTY)break; 00053 } 00054 } 00055 return false; 00056 } 00057 00058 bool CanUp(Board board,int8_t x,int8_t y,int8_t color){ 00059 int8_t enemy=-color; 00060 if(y>=2&&board.board[x][y-1]==enemy){ 00061 for(int8_t i=y-2;i>=0;i--){ 00062 if(board.board[x][i]==color)return true; 00063 else if(board.board[x][i]==EMPTY)break; 00064 } 00065 } 00066 return false; 00067 } 00068 00069 bool CanDown(Board board,int8_t x,int8_t y,int8_t color){ 00070 int8_t enemy=-color; 00071 if(y<=5&&board.board[x][y+1]==enemy){ 00072 for(int8_t i=y+2;i<8;i++){ 00073 if(board.board[x][i]==color)return true; 00074 else if(board.board[x][i]==EMPTY)break; 00075 } 00076 } 00077 return false; 00078 } 00079 00080 bool CanRightUp(Board board,int8_t x,int8_t y,int8_t color){ 00081 int8_t enemy=-color; 00082 if(x<=5&&y>=2&&board.board[x+1][y-1]==enemy){ 00083 int8_t i=2; 00084 while(x+i<8&&y-i>=0){ 00085 if(board.board[x+i][y-i]==color)return true; 00086 else if(board.board[x+i][y-i]==EMPTY)break; 00087 i++; 00088 } 00089 } 00090 return false; 00091 } 00092 00093 bool CanRightDown(Board board,int8_t x,int8_t y,int8_t color){ 00094 int8_t enemy=-color; 00095 if(x<=5&&y<=5&&board.board[x+1][y+1]==enemy){ 00096 int8_t i=2; 00097 while(x+i<8&&y+i<8){ 00098 if(board.board[x+i][y+i]==color)return true; 00099 else if(board.board[x+i][y+i]==EMPTY)break; 00100 i++; 00101 } 00102 } 00103 return false; 00104 } 00105 00106 bool CanLeftDown(Board board,int8_t x,int8_t y,int8_t color){ 00107 int8_t enemy=-color; 00108 if(x>=2&&y<=5&&board.board[x-1][y+1]==enemy){ 00109 int8_t i=2; 00110 while(x-i>=0&&y+i<8){ 00111 if(board.board[x-i][y+i]==color)return true; 00112 else if(board.board[x-i][y+i]==EMPTY)break; 00113 i++; 00114 } 00115 } 00116 return false; 00117 } 00118 00119 bool CanLeftUp(Board board,int8_t x,int8_t y,int8_t color){ 00120 int8_t enemy=-color; 00121 if(x>=2&&y>=2&&board.board[x-1][y-1]==enemy){ 00122 int8_t i=2; 00123 while(x-i>=0&&y-i>=0){ 00124 if(board.board[x-i][y-i]==color)return true; 00125 else if(board.board[x-i][y-i]==EMPTY)break; 00126 i++; 00127 } 00128 } 00129 return false; 00130 } 00131 00132 bool CanPut(Board board,int8_t x,int8_t y,int8_t color){ 00133 return CanRight(board,x,y,color)||CanLeft(board,x,y,color)||CanUp(board,x,y,color)||CanDown(board,x,y,color)||CanRightUp(board,x,y,color)||CanRightDown(board,x,y,color)||CanLeftDown(board,x,y,color)||CanLeftUp(board,x,y,color); 00134 } 00135 00136 std::vector<uint8_t> GetPutCoords(Board board,int8_t color){ 00137 std::vector<uint8_t> coords; 00138 for(int8_t i=0;i<8;i++){ 00139 for(int8_t j=0;j<8;j++){ 00140 led_board[i+8][j+8].red=255; 00141 if(CanPut(board,i,j,color))coords.push_back(i*8+j);//ここの18回目の実行で上のCanDownからCanRightUpの間で止まる 00142 } 00143 } 00144 return coords; 00145 } 00146 00147 Board PutStone(Board board,int8_t x,int8_t y,int8_t color){ 00148 Board _dummyboard; 00149 int8_t dummyboard[8][8]; 00150 int8_t enemy=-color; 00151 uint8_t i; 00152 memcpy(dummyboard,board.board,sizeof board.board); 00153 _dummyboard.SetBoard(dummyboard); 00154 if(CanRight(_dummyboard,x,y,color)){ 00155 i=x+1; 00156 while(dummyboard[i][y]==enemy){ 00157 dummyboard[i][y]=color; 00158 i+=1; 00159 } 00160 } 00161 if(CanLeft(_dummyboard,x,y,color)){ 00162 i=x-1; 00163 while(dummyboard[i][y]==enemy){ 00164 dummyboard[i][y]=color; 00165 i-=1; 00166 } 00167 } 00168 if(CanUp(_dummyboard,x,y,color)){ 00169 i=y-1; 00170 while(dummyboard[x][i]==enemy){ 00171 dummyboard[x][i]=color; 00172 i-=1; 00173 } 00174 } 00175 if(CanDown(_dummyboard,x,y,color)){ 00176 i=y+1; 00177 while(dummyboard[x][i]==enemy){ 00178 dummyboard[x][i]=color; 00179 i+=1; 00180 } 00181 } 00182 if(CanRightUp(_dummyboard,x,y,color)){ 00183 i=1; 00184 while(dummyboard[x+i][y-i]==enemy){ 00185 dummyboard[x+i][y-i]=color; 00186 i+=1; 00187 } 00188 } 00189 if(CanRightDown(_dummyboard,x,y,color)){ 00190 i=1; 00191 while(dummyboard[x+i][y+i]==enemy){ 00192 dummyboard[x+i][y+i]=color; 00193 i+=1; 00194 } 00195 } 00196 if(CanLeftDown(_dummyboard,x,y,color)){ 00197 i=1; 00198 while(dummyboard[x-i][y+i]==enemy){ 00199 dummyboard[x-i][y+i]=color; 00200 i+=1; 00201 } 00202 } 00203 if(CanLeftUp(_dummyboard,x,y,color)){ 00204 i=1; 00205 while(dummyboard[x-i][y-i]==enemy){ 00206 dummyboard[x-i][y-i]=color; 00207 i+=1; 00208 } 00209 } 00210 dummyboard[x][y]=color; 00211 _dummyboard.SetBoard(dummyboard); 00212 return _dummyboard; 00213 } 00214 00215 uint8_t GetScore(Board board,int8_t color){ 00216 uint8_t score=0; 00217 for(int8_t i=0;i<8;i++){ 00218 for(int8_t j=0;j<8;j++){ 00219 if(board.board[i][j]==color)score++; 00220 } 00221 } 00222 return score; 00223 } 00224 00225 00226 bool GameSet(Board board){ 00227 led_board[8][0].red=255; 00228 std::vector<uint8_t> black(GetPutCoords(board,BLACK)); 00229 std::vector<uint8_t> white(GetPutCoords(board,WHITE)); 00230 led_board[0][8].red=255; 00231 return black.size()==0&&white.size()==0; 00232 }
Generated on Tue Jul 12 2022 21:43:15 by
1.7.2