#include "judge.h"
#include <QDebug>
player& judge::GetCurrentPlayer(){
switch (this->m_currentplayer) {
case judge::RED:
return this->m_player_red;
break;
case judge::BLACK:
return this->m_player_black;
break;
default:
break;
}
return this->m_player_black;
}
player& judge::GetWaitPlayer(){
switch (this->m_currentplayer) {
case judge::RED:
return this->m_player_black;
break;
case judge::BLACK:
return this->m_player_red;
break;
default:
break;
}
return this->m_player_black;
}
void judge::SetRobet(player &robet){
robet.SetType(player::ROBET);
switch (this->GetGameLevel()) {
case judge::EASY:
robet.SetRobetLevel(player::EASY);
break;
case judge::MEDUIM:
robet.SetRobetLevel(player::MEDUIM);
break;
case judge::HARD:
robet.SetRobetLevel(player::HARD);
break;
default:
robet.SetRobetLevel(player::EASY);
break;
}
}
void judge::DecisionColor(int row,int col){
this->SetState(judge::START);
chess &achess=this->m_chesshander->GetChess(row,col);
switch (achess.GetColor()) {
case chess::RED:
this->m_currentplayer=judge::RED;
this->m_player_red.SetType(player::PLAYER);
this->ConvertPlayer();
break;
case chess::BLACK:
this->m_currentplayer=judge::BLACK;
this->m_player_black.SetType(player::PLAYER);
this->ConvertPlayer();
break;
default:
break;
}
player& currentplayer=this->GetCurrentPlayer();
switch (this->GetGameType()) {
case judge::PVP:
currentplayer.SetType(player::PLAYER);
break;
case judge::PVE:
this->SetRobet( currentplayer);
break;
default:
break;
}
achess.SetState(chess::LIVE);
qDebug()<<"当前玩家获得颜色:";
if(achess.GetColor()==chess::RED)
qDebug()<<"红色";
else
qDebug()<<"黑色";
}
void judge::SelectChess(int row,int col){
chess &achess=this->m_chesshander->GetChess(row,col);
if(this->m_currentplayer==judge::RED)
qDebug()<<"当前红棋操作中";
else
qDebug()<<"当前黑棋操作中";
player &player=this->GetCurrentPlayer();
switch (player.GetSelectState()) {
case player::SELECT_WAIT:
this->SelectWaitRuler(player,achess,row,col);
break;
case player::SELECT_SOLDIER:
this->SelectSoldierRuler(player,achess,row,col);
break;
default:
break;
}
}
void judge::SelectSoldierRuler(player &player,chess &chess,int row,int col){
switch (chess.GetState()) {
case chess::LIVE:
if(this->IsSoliderChess(player,chess)){
qDebug()<<"soldier:重新选择自己棋子";
player.SetSolider(row,col);
player.SetSelectState(player::SELECT_SOLDIER);
}else{
qDebug()<<"soldier:攻击";
this->AttackRuler(player,chess,row,col);
// player.SetSelectState(player::SELECT_ENEMY);
}
break;
case chess::DOWN:
qDebug()<<"soldier:翻开棋子";
chess.SetState(chess::LIVE);
player.SetSelectState(player::SELECT_WAIT);
this->ConvertPlayer();
break;
case chess::DEAD:
if(this->IsCanMove(row,col,player.GetSolider().row,player.GetSolider().col)){
this->MoveChess(row,col,player.GetSolider().row,player.GetSolider().col);
player.SetSelectState(player::SELECT_WAIT);
this->ConvertPlayer();
}else{
qDebug()<<"soldier:移动棋子距离不合法!";
player.SetSelectState(player::SELECT_SOLDIER);
}
break;
default:
qDebug()<<"soldier:错误";
break;
}
}
void judge::AttackRuler(player &player,chess &chess,int row,int col){
if(this->IsVaildAttackLen(player.GetSolider().row,player.GetSolider().col,row,col)){
qDebug()<<"attack:攻击长度合法";
if(IsVaildEetChess(player.GetSolider().row
,player.GetSolider().col,row,col))
{
this->m_chesshander->GetChess(row,col).SetState(chess::DEAD);
this->m_chesshander->MoveChess(player.GetSolider().row
,player.GetSolider().col,row,col);
qDebug()<<"attack:攻击合法";
player.SetSelectState(player::SELECT_WAIT);
this->ConvertPlayer();
}else{
qDebug()<<"attack:攻击非法";
player.SetSelectState(player::SELECT_SOLDIER);
}
}else{
qDebug()<<"attack:攻击长度非法";
player.SetSelectState(player::SELECT_SOLDIER);
}
}
void judge::SelectWaitRuler(player &player,chess &chess,int row,int col){
switch (chess.GetState()) {
case chess::LIVE:
if(this->IsSoliderChess(player,chess)){
qDebug()<<"wait:选择自己的棋子";
player.SetSolider(row,col);
player.SetSelectState(player::SELECT_SOLDIER);
}else{
qDebug()<<"wait:不可选择";
}
break;
case chess::DOWN:
qDebug()<<"wait:翻开棋子";
chess.SetState(chess::LIVE);
player.SetSelectState(player::SELECT_WAIT);
this->ConvertPlayer();
break;
default:
qDebug()<<"wait:点击空位";
break;
}
}
void judge::ConvertPlayer(){
this->m_currentplayer==judge::RED?this->m_currentplayer=judge::BLACK:this->m_currentplayer=judge::RED;
player player=this->GetCurrentPlayer();
switch (player.GetType()) {
case player::ROBET:
//do some thing
break;
case player::PLAYER:
break;
default:
break;
}
}
bool judge::IsCanMove(int row,int col,int to_row,int to_col){
int l=0;
l=(row-to_row)*(row-to_row);
l+=(col-to_col)*(col-to_col);
qDebug()<<l;
qDebug()<<(l==1?true:false);
return l==1?true:false;
}
bool judge::IsVaildAttackLen(int solider_row,int solider_col,int target_row,int target_col){
chess &solider_chess=this->m_chesshander->GetChess(solider_row,solider_col);
int l=0;
l=(solider_row-target_row)*(solider_row-target_row);
l+=(solider_col-target_col)*(solider_col-target_col);
if(solider_chess.GetName()==chess::PAO){//炮的攻击规则
if(l<2?true:false)
return false;
int orient=0,n=0,len=0,flag=0,x=0,y=0;
if((solider_row-target_row)==0){ //行方向
target_col-solider_col>0?orient=1:orient=-1;
n=solider_col+orient;
len=target_col;
flag=0;
}else{//列方向
target_row-solider_row>0?orient=1:orient=-1;
n=solider_row+orient;
len=target_row;
flag=1;
}
while(n!=len){
if(flag==0){
x=solider_row;
y=n;
}else{
x=n;
y=solider_col;
}
chess& slect_temp=this->m_chesshander->GetChess(x,y);
if(slect_temp.GetState()!=chess::DEAD)
break;
n=n+orient;
}
if(n==len) //没有炮架
return false;
//到目标中间是否有阻挡
n=n+orient;
while(n!=len){
if(flag==0){
x=solider_row;
y=n;
}else{
x=n;
y=solider_col;
}
chess& slect_temp1=this->m_chesshander->GetChess(x,y);
if(slect_temp1.GetState()!=chess::DEAD)
return false;
n=n+ori
评论0