📄 ruchy.java
字号:
package JavaWarcaby;
import java.util.*;
class Ruchy
{
final static int DOZWOLONY = 1;
final static int NIEDOZWOLONY = 2;
final static int NIEKOMPLETNY = 3;
static boolean KoniecRuchow(int[][] plansza,int czyjRuch)
{
for (int i=0; i<8; i++)
for (int j=0; j<8; j++)
if ( (float)(i+j)/2 != (i+j)/2 )
{
if (czyjRuch == Gra.BIALE &&
(plansza[i][j] == Gra.BIALE ||
plansza[i][j] == Gra.B_DAMKA))
{
if (czyMozliwePosuniecie(plansza,i,j)) return false;
else if (czyMozeBic(plansza,i,j)) return false;
}
else if (czyjRuch == Gra.CZARNE &&
(plansza[i][j] == Gra.CZARNE ||
plansza[i][j] == Gra.C_DAMKA))
{
if (czyMozliwePosuniecie(plansza,i,j)) return false;
else if (czyMozeBic(plansza,i,j)) return false;
}
} // if and for
return true;
}
static int WykonajRuch(int[][] plansza,int start_i,int start_j,int end_i,int end_j)
{
int wynik = czyRuchDozwolony(plansza,start_i,start_j,end_i,end_j,kolor(plansza[start_i][start_j]));
if (wynik != NIEDOZWOLONY)
{
if ( Math.abs(end_i - start_i) == 1)
{
plansza[end_i][end_j] = plansza[start_i][start_j];
plansza[start_i][start_j] = Gra.PUSTE;
}
else // bicie
{
plansza[(start_i + end_i)/2][(start_j + end_j)/2] = Gra.PUSTE;
plansza[end_i][end_j] = plansza[start_i][start_j];
plansza[start_i][start_j] = Gra.PUSTE;
}
if (wynik == NIEKOMPLETNY)
{
if (!(czyMozeBic(plansza,end_i,end_j)))
wynik = DOZWOLONY;
}
//nowe damki
if ( plansza[end_i][end_j] == Gra.BIALE && end_j == 7)
plansza[end_i][end_j] = Gra.B_DAMKA;
else if ( plansza[end_i][end_j] == Gra.CZARNE && end_j == 0)
plansza[end_i][end_j] = Gra.C_DAMKA;
}
return wynik;
}
static int czyRuchDozwolony(int[][] plansza,int start_i,int start_j,int end_i,int end_j,int czyjRuch)
{
if (! (wPlanszy(start_i,start_j) && wPlanszy(end_i,end_j) ) )
return NIEDOZWOLONY;
if (plansza[end_i][end_j] != Gra.PUSTE)
return NIEDOZWOLONY;
int pion = plansza[start_i][start_j];
if ( Math.abs(start_i - end_i) == 1 )
{
//najpierw bicia
switch (pion)
{
case Gra.BIALE:
case Gra.B_DAMKA:
for (int i=0;i<8;i++)
for (int j=0;j<8;j++)
{
if ((plansza[i][j] == Gra.BIALE ||
plansza[i][j] == Gra.B_DAMKA)
&& czyMozeBic(plansza,i,j))
return NIEDOZWOLONY;
}
break;
case Gra.CZARNE:
case Gra.C_DAMKA:
for (int i=0;i<8;i++)
for (int j=0;j<8;j++)
{
if ((plansza[i][j] == Gra.CZARNE ||
plansza[i][j] == Gra.C_DAMKA)
&& czyMozeBic(plansza,i,j))
return NIEDOZWOLONY;
}
break;
} // switch
switch (pion)
{
case Gra.BIALE:
if (end_j - start_j == 1) return DOZWOLONY;
break;
case Gra.CZARNE:
if (end_j - start_j == -1) return DOZWOLONY;
break;
case Gra.B_DAMKA:
case Gra.C_DAMKA:
if ( Math.abs(end_j - start_j) == 1 ) return DOZWOLONY;
break;
} // switch (pion)
return NIEDOZWOLONY;
} // if ( (Math.abs(start_i - end_i) == 1 )
else if ( Math.abs(start_i - end_i) == 2 )
{
int bity_i = (start_i + end_i) / 2;
int bity_j = (start_j + end_j) / 2;
int bity_pion = plansza[bity_i][bity_j];
if (czyjRuch == Gra.BIALE)
{
if (!(bity_pion == Gra.CZARNE || bity_pion == Gra.C_DAMKA))
return NIEDOZWOLONY;
}
else if (!(bity_pion == Gra.BIALE || bity_pion == Gra.B_DAMKA))
return NIEDOZWOLONY;
switch (pion)
{
case Gra.BIALE:
if (end_j - start_j != 2)
return NIEDOZWOLONY;
break;
case Gra.CZARNE:
if (end_j - start_j != -2)
return NIEDOZWOLONY;
break;
case Gra.B_DAMKA:
case Gra.C_DAMKA:
if (Math.abs(end_j - start_j) != 2)
return NIEDOZWOLONY;
}
return NIEKOMPLETNY;
}
return NIEDOZWOLONY;
}
static int czyPosuniecieDozwolone(int[][] plansza,int start_i,int start_j,int end_i,int end_j,int czyjRuch)
{
if (! (wPlanszy(start_i,start_j) && wPlanszy(end_i,end_j) ) )
return NIEDOZWOLONY;
if (plansza[end_i][end_j] != Gra.PUSTE)
return NIEDOZWOLONY;
int pion = plansza[start_i][start_j];
if ( Math.abs(start_i - end_i) == 1 )
{
switch (pion)
{
case Gra.BIALE:
if (end_j - start_j == 1) return DOZWOLONY;
break;
case Gra.CZARNE:
if (end_j - start_j == -1) return DOZWOLONY;
break;
case Gra.B_DAMKA:
case Gra.C_DAMKA:
if ( Math.abs(end_j - start_j) == 1 ) return DOZWOLONY;
break;
} // switch (pion)
return NIEDOZWOLONY;
} // if ( (Math.abs(start_i - end_i) == 1 )
return NIEDOZWOLONY;
}
static boolean czyMozeBic(int[][] plansza, int czyjRuch)
{
for (int i=0; i<8; i++)
for (int j=0; j<8; j++)
{
if (kolor(plansza[i][j]) == czyjRuch && czyMozeBic(plansza,i,j))
return true;
}
return false;
}
static boolean czyMozeBic(int[][] plansza, int i, int j)
{
switch (plansza[i][j])
{
case Gra.BIALE:
if (i+2<8 && j+2<8)
if ( (plansza[i+1][j+1] == Gra.CZARNE ||
plansza[i+1][j+1] == Gra.C_DAMKA)
&&
(plansza[i+2][j+2] == Gra.PUSTE))
return true;
if (i-2>-1 && j+2<8)
if ( (plansza[i-1][j+1] == Gra.CZARNE ||
plansza[i-1][j+1] == Gra.C_DAMKA)
&&
plansza[i-2][j+2] == Gra.PUSTE)
return true;
break;
case Gra.CZARNE:
if (i+2<8 && j-2>-1)
if ( (plansza[i+1][j-1] == Gra.BIALE ||
plansza[i+1][j-1] == Gra.B_DAMKA)
&&
plansza[i+2][j-2] == Gra.PUSTE)
return true;
if (i-2>-1 && j-2>-1)
if ( (plansza[i-1][j-1] == Gra.BIALE ||
plansza[i-1][j-1] == Gra.B_DAMKA)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -