- チェッカーボードゲームは11x11のマトリックスです。
- ピースは白、黒、(1)ホワイトキング
- 相手を挟んで駒を「捕まえる」ことができる
- 例)黒い部分は、その左右または上下に白い部分/白い王があります
- ボードの端または4つのコーナーピースのいずれかを使用してサンドイッチおよびキャプチャすることもできます
- 例)ボードの左端にある白い部分、次に黒い部分が白い部分の右側に直接移動します。これはキャプチャになります
これまでのところ私は
- 11x11マトリックス
- (int)0 =空、1 =白、2 =黒、3=ホワイトキング
これまでの私のアルゴリズムは基本的なものです。上/下/左/右をチェックし、反対の場合は、その隣のピースが友好的なピースであるかどうかを確認し、そうである場合はキャプチャします。
しかし、それを単純に行うことはできません。ピースが2つの外縁の行または列にある場合、上記のアルゴリズムを使用すると、ArrayOutofBoundsExceptionエラーが発生するためです。
それから私は作品が白か黒かについての巨大なifステートメントを持っています。
これを最適化するもっと簡単な方法があるように感じます。そして、初心者のプログラマーとして、私はそれを見ることができません。誰かが何かを提案できますか?
以下の私のコードを見ると、これは移動が外側の端にある場合にのみ発生することがわかります...それが「1」列/行にある場合は、ほとんどすべてを再コピーして貼り付ける必要があります。 。その後、ArrayOutofBoundsExceptionを気にすることなく、最終的に2つのスペースを上/左/右/下にチェックできます。
次に、Black Piecesに対してすべてをやり直す必要があります。私のコードは本当にずさんなように見え、これを行う簡単な方法があると感じています。助言がありますか?
void makeMove(int typePiece, int fromRow, int fromCol, int toRow, int toCol) {
board[toRow][toCol] = board[fromRow][fromCol];
board[fromRow][fromCol] = EMPTY;
//CAPTURE
if(typePiece == WHITE) {
if(toRow==0) { //top row
//check right
if(toCol!=9 && board[toRow][toCol+1]==BLACK &&
(toCol==10 || board[toRow][toCol+2]==WHITE || board[toRow][toCol+2]==WHITEKING)) {
board[toRow][toCol+1]=EMPTY;
}
//check left
if(toCol!=1 && board[toRow][toCol-1]==BLACK &&
(toCol==0 || board[toRow][toCol-2]==WHITE || board[toRow][toCol-2]==WHITEKING)) {
board[toRow][toCol-1]=EMPTY;
}
//check bottom
if(board[toRow-1][toCol]==BLACK && (board[toRow-2][toCol]==WHITE || board[toRow-2][toCol]==WHITEKING)) {
board[toRow-1][toCol]=EMPTY;
}
}
else if(toRow == 10) { //bottom row
//check right
if(toCol!=9 && board[toRow][toCol+1]==BLACK && (toCol==10 || board[toRow][toCol+2]==WHITE || board[toRow][toCol+2]==WHITEKING)) {
board[toRow][toCol+1]=EMPTY;
}
//check left
if(toCol!=1 && board[toRow][toCol-1]==BLACK && (toCol==0 || board[toRow][toCol-2]==WHITE || board[toRow][toCol-2]==WHITEKING)) {
board[toRow][toCol-1]=EMPTY;
}
//check top
if(board[toRow+1][toCol]==BLACK && (board[toRow+2][toCol]==WHITE || board[toRow+2][toCol]==WHITEKING)) {
board[toRow+1][toCol]=EMPTY;
}
}
else if(toCol == 0) { //left column
//check right
if(board[toRow][toCol+1]==BLACK && (board[toRow][toCol+2]==WHITE || board[toRow][toCol+2]==WHITEKING)) {
board[toRow][toCol+1]=EMPTY;
}
//check top
if(toRow!=1 && board[toRow+1][toCol]==BLACK && (board[toRow+2][toCol]==WHITE || board[toRow+2][toCol]==WHITEKING)) {
board[toRow+1][toCol]=EMPTY;
}
//check bottom
if(toRow != 9 && board[toRow-1][toCol]==BLACK && (board[toRow-2][toCol]==WHITE || board[toRow-2][toCol]==WHITEKING)) {
board[toRow-1][toCol]=EMPTY;
}
}
else if(toCol == 10) { //right column
//check left
if(board[toRow][toCol-1]==BLACK && (toCol==0 || board[toRow][toCol-2]==WHITE || board[toRow][toCol-2]==WHITEKING)) {
board[toRow][toCol-1]=EMPTY;
//check top
if(toRow!=1 && board[toRow+1][toCol]==BLACK && (board[toRow+2][toCol]==WHITE || board[toRow+2][toCol]==WHITEKING)) {
board[toRow+1][toCol]=EMPTY;
}
//check bottom
if(toRow != 9 && board[toRow-1][toCol]==BLACK && (board[toRow-2][toCol]==WHITE || board[toRow-2][toCol]==WHITEKING)) {
board[toRow-1][toCol]=EMPTY;
}
}