说句闲话:研究鸭棋的最好方法是
查看原帖
说句闲话:研究鸭棋的最好方法是
216011
ForeverYurippe楼主2020/11/12 07:53
A了这道题

祝你们成功(滑稽

咳咳,说回正题,原来玩具看起来只能在linux上运行,所以就跟源代码搞了个c++的版本,写的很丑,见谅。

#include<bits/stdc++.h>
#include<conio.h>
#include "windows.h"
using namespace std;
int Check_ca(){};
int q;
int f = 1,Go,C;//判断红/黑方走子 游戏结束 选了哪个棋子种类
string name[10] = {"","captain","guard","elephant","horse","car","duck","soldier"};
string name2[10] = {"","王","士","象","马","车","鸭","兵"};
string col[2] = {"blue","red"};
int Ca_x[2] = {9,0},Ca_y[2] = {4,4}; 
int captain[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
int guard[4][2] = {{1,-1},{1,1},{-1,1},{-1,-1}};
int elephant[4][2] = {{2,2},{2,-2},{-2,2},{-2,-2}};
int horse[8][2] = {{2,1},{-2,1},{2,-1},{-2,-1},{1,2},{-1,2},{1,-2},{-1,-2}};
int duck[8][2] = {{3,2},{-3,2},{3,-2},{-3,-2},{2,3},{-2,3},{2,-3},{-2,-3}};
int solider[8][2] = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
//所有棋子移动方式(车除外)
int Felephant[4][2] = {{1,1},{1,-1},{-1,1},{-1,-1}};
int Fhorse[8][2] = {{1,0},{-1,0},{1,0},{-1,0},{0,1},{0,1},{0,-1},{0,-1}};
int Fduck1[8][2] = {{2,1},{-2,1},{2,-1},{-2,-1},{1,2},{-1,2},{1,-2},{-1,-2}};
int Fduck2[8][2] = {{1,0},{-1,0},{1,0},{-1,0},{0,1},{0,1},{0,-1},{0,-1}};
//判断象,马,鸭行动是否可行
struct MAP{
    int color,c;//属于哪一方 棋子种类
    MAP(int color = 0,int c = 0){this->color = color;this->c = c;}
};
ostream& operator << (ostream &out,MAP a){
	out<<"("<<a.c<<","<<a.color<<")";
	return out;
}
//重载输出
MAP a[15][15] = {{MAP(1,5),MAP(1,4),MAP(1,3),MAP(1,2),MAP(1,1),MAP(1,2),MAP(1,3),MAP(1,4),MAP(1,5)},{},{MAP(1,6),0,0,0,0,0,0,0,MAP(1,6)},{MAP(1,7),0,MAP(1,7),0,MAP(1,7),0,MAP(1,7),0,MAP(1,7)},{},{},{MAP(0,7),0,MAP(0,7),0,MAP(0,7),0,MAP(0,7),0,MAP(0,7)},{MAP(0,6),0,0,0,0,0,0,0,MAP(0,6)},{},{MAP(0,5),MAP(0,4),MAP(0,3),MAP(0,2),MAP(0,1),MAP(0,2),MAP(0,3),MAP(0,4),MAP(0,5)}};
//初始化地图
void Cant_C(){//输出无法行动
    cout<<"无效移动,请重新选择!"<<endl;
    return;
}
int Check_move(int xs,int ys,int nx,int ny){
    // if(a[xs][ys].c == 1 || a[xs][ys].c == 2 || a[xs][ys].c == 5 || a[xs][ys].c == 7)return - 1;
    if(a[xs][ys].c == 1){
        C = 1;
        for(int i = 0; i < 4; i++){
            int xx = xs + captain[i][0];
            int yy = ys + captain[i][1];
            if(xx == nx && yy == ny && xx < 10 && yy < 9 && xx >= 0 && yy >= 0){
                if(a[xx][yy].c == 0 || a[xx][yy].color != f)return 0;
                return 1;
            }
        }
        return 1;
    }else if(a[xs][ys].c == 2){
        C = 2;
        for(int i = 0; i < 4; i++){
            int xx = xs + guard[i][0];
            int yy = ys + guard[i][1];
            if(xx == nx && yy == ny && xx < 10 && yy < 9 && xx >= 0 && yy >= 0){
               if(a[xx][yy].c == 0 || a[xx][yy].color != f)return 0;
                return 1;
            }
        }
        return 1;
    }else if(a[xs][ys].c == 7){
        C = 7;
        for(int i = 0; i < 8; i++){
            int xx = xs + solider[i][0];
            int yy = ys + solider[i][1];
            if(xx == nx && yy == ny && xx < 10 && yy < 9 && xx >= 0 && yy >= 0){
               if(a[xx][yy].c == 0 || a[xx][yy].color != f)return 0;
                return 1;
            }
        }
        return 1;
    }else if(a[xs][ys].c == 5){
        C = 5;
        if(nx == xs){
//        	cout<<ys<<" "<<ny<<"-";
        	if(a[nx][ny].c != 0 && a[nx][ny].color == f)return 1;
			if(ys < ny){
	            for(int i = ys + 1; i < ny; i++)
	                if(a[xs][i].c != 0)return 1;
        	}else{
				for(int i = ny + 1; i < ys; i++){
                	if(a[xs][i].c != 0)return 1;
				}
			}	
		}else if(ny == ys){
			if(a[nx][ny].c != 0 && a[nx][ny].color == f)return 1;
			if(xs < nx){
				for(int i = xs + 1; i < nx; i++)
	                if(a[i][ys].c != 0)return 1;
        	}else{
				for(int i = nx + 1; i < xs; i++)
                	if(a[i][ys].c != 0)return 1;
			}
		}else{return 1;}
        return 0;
    }else if(a[xs][ys].c == 3){
        C = 3;
        for(int i = 0; i < 4; i++){
            int xx = xs + elephant[i][0];
            int yy = ys + elephant[i][1];
            // cout<<endl<<xx<<" "<<yy; 
            if(xx == nx && yy == ny && xx < 10 && yy < 9 && xx >= 0 && yy >= 0){
                if(a[xs + Felephant[i][0]][ys + Felephant[i][1]].c == 0)return 0;
                return 1;
            }
        }
        return 1;
    }else if(a[xs][ys].c == 4){
        C = 4;
        for(int i = 0; i < 8; i++){
            int xx = xs + horse[i][0];
            int yy = ys + horse[i][1];
            if(xx == nx && yy == ny && xx < 10 && yy < 9 && xx >= 0 && yy >= 0){
                if(a[xs + Fhorse[i][0]][ys + Fhorse[i][1]].c == 0)return 0;
                return 1;
            }
        }
        return 1;
    }else{
        C = 6;
        for(int i = 0; i < 8; i++){
            int xx = xs + duck[i][0];
            int yy = ys + duck[i][1];
            if(xx == nx && yy == ny && xx < 10 && yy < 9 && xx >= 0 && yy >= 0){
                if(a[xs + Fduck1[i][0]][ys + Fduck1[i][1]].c == 0 && a[xs + Fduck2[i][0]][ys + Fduck2[i][1]].c == 0)return 0;
                return 1;
            }
        }
        return 1;
    }
    return 1;
}
//检查能否移动
int Check_ca(int xs,int ys,int nx,int ny,int f){
    if(a[xs][ys].c != 1)return 0;
//    cout<<1;
    for(int i = ny - 1; i >= 0; i--){
        if(a[nx][i].color != f && a[nx][i].c == 5)return 1;
    	if(a[nx][i].c != 0)break;
	}
	for(int i = ny + 1; i < 9; i++){
        if(a[nx][i].color != f && a[nx][i].c == 5)return 1;
    	if(a[nx][i].c != 0)break;
	}
//    cout<<1;
    for(int i = nx - 1; i >= 0; i--){
		if(a[i][ny].color != f && a[i][ny].c == 5)return 1;
		if(a[i][ny].c != 0)break;
	}
	for(int i = nx + 1; i < 10; i++){
		if(a[i][ny].color != f && a[i][ny].c == 5)return 1;
		if(a[i][ny].c != 0)break;
	}
//	cout<<2;
	for(int i = 0; i < 8; i++){
        int xx = nx + solider[i][0];
        int yy = ny + solider[i][1];
        if(a[xx][yy].color != f && a[xx][yy].c == 7)return 1;
    }
//    cout<<3;
    for(int i = 0; i < 4; i++){//
        int xx = nx + captain[i][0];
        int yy = ny + captain[i][1];
        if(a[xx][yy].color != f && a[xx][yy].c == 1)return 1;
        xx = nx + guard[i][0];
        yy = ny + guard[i][1];
//        cout<<xx<<" "<<yy<<" "<<f<<endl;
        if(a[xx][yy].color != f && a[xx][yy].c == 2)return 1;
		xx = nx + elephant[i][0];
        yy = ny + elephant[i][1];
        if(a[xx][yy].color != f && a[xx][yy].c == 3)
            if(a[xx - Felephant[i][0]][yy - Felephant[i][1]].c == 0)return 1;
	}
//	cout<<4;
    for(int i = 0; i < 8; i++){
        int xx = nx + horse[i][0];
        int yy = ny + horse[i][1];
        if(a[xx][yy].color != f && a[xx][yy].c == 4)
            if(a[xx - Fhorse[i][0]][yy - Fhorse[i][1]].c == 0)return 1;
        xx = nx + duck[i][0];
        yy = ny + duck[i][1];
        if(a[xx][yy].color != f && a[xx][yy].c == 6)
            if(a[xx - Fduck1[i][0]][yy - Fduck1[i][1]].c == 0 && a[xx - Fduck2[i][0]][yy - Fduck2[i][1]].c == 0)return 1; 
    }
//    cout<<5;
    return 0;
}
//检查是否被将
int Check(int f){
	
		int x = Ca_x[f],y = Ca_y[f];
		if(Check_ca(x,y,x,y,f))return 1;
	
	return 0;
}
int main(){
//    cin>>q;
    int xs,ys,nx,ny;
    
    for(;1;){
//    	system("CLS");
    	xs = ys = nx = ny = 0;
    	
        while(1){
        cout<<"现轮到";
        if(f)cout<<"红";
        else cout<<"蓝";
        cout<<"方走子,w,a,s,d上下左右移动,按Enter键选选取棋子。"<<endl;
            for(int i = 9; i >= 0; i--){
                for(int j = 0; j < 9; j++){
                    if(a[i][j].color == 1)
                    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
                    if(a[i][j].color == 0)
                    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
                    if(i == xs && j == ys)
                    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |
		FOREGROUND_BLUE);
                    if(a[i][j].c != 0)cout<<name2[a[i][j].c]<<" ";
                    else{
                    	if(i == xs && j == ys)cout<<"00 ";
                        else{
							SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |
		FOREGROUND_GREEN | FOREGROUND_BLUE);
                        	cout<<"00 ";
                    	}
					}
                }
                cout<<endl;
            }
            char sds = getch();
            if((int)sds == 119)xs++;
            else if((int)sds == 97)ys--;
            else if((int)sds == 115)xs--;
            else if((int)sds == 100)ys++;
            else if((int)sds == 13)break;
            if(xs < 0)xs = 9;
            if(ys < 0)ys = 8;
            if(xs >= 10)xs = 0;
            if(ys >= 9)ys = 0;
            system("CLS");
        }
        nx = xs;
        ny = ys;
        system("CLS");
            while(1){
            cout<<"选取落子点,w,a,s,d上下左右移动,按Enter键选择。"<<endl;
                for(int i = 9; i >= 0; i--){
                    for(int j = 0; j < 9; j++){
                        if(a[i][j].color == 1)
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
                        if(a[i][j].color == 0)
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
                        if(i == xs && j == ys)
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |
		FOREGROUND_BLUE);
                        if(i == nx && j == ny)
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
                        if(a[i][j].c != 0)cout<<name2[a[i][j].c]<<" ";
                        else{
                        	if((i == xs && j == ys) || (i == nx && j == ny))cout<<"00 ";
                        	else{
	                            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |
		FOREGROUND_GREEN | FOREGROUND_BLUE);
	                            cout<<"00 ";
                        	}
						}
                    }
                    cout<<endl;
                }
                char sds = getch();
                if((int)sds == 119)nx++;
                else if((int)sds == 97)ny--;
                else if((int)sds == 115)nx--;
                else if((int)sds == 100)ny++;
                else if((int)sds == 13)break;
                if(nx < 0)nx = 9;
                if(ny < 0)ny = 8;
                if(nx >= 10)nx = 0;
                if(ny >= 9)ny = 0;
                system("CLS");
            }
        // cin>>xs>>ys>>nx>>ny;
        if(nx == xs && ny == ys){Cant_C();continue;}
		if(nx >= 10 || ny >= 9 || nx < 0 || ny < 0 || xs < 0 || ys < 0 || xs >= 10 || ys >= 9){Cant_C();continue;}
        if(a[nx][ny].c != 0 && a[nx][ny].color == f){Cant_C();continue;}
        if(a[xs][ys].c == 0){Cant_C();continue;}//判断是否有棋子
        if(a[xs][ys].color != f && a[xs][ys].c != 0){Cant_C();continue;}//判断是否为对方棋子
        int cm = Check_move(xs,ys,nx,ny),cc = Check_ca(xs,ys,nx,ny,f);
        if(cm){Cant_C();continue;}
        // cout<<col[f]<<" "<<name[C]<<";";
        // if(a[nx][ny].c == 0)cout<<"NA"<<";";
        // else cout<<col[!f]<<" "<<name[a[nx][ny].c]<<";";
        system("CLS");
        swap(a[nx][ny].c,a[xs][ys].c);
        swap(a[nx][ny].color,a[xs][ys].color);
        if(cc || Check(f)){cout<<"将军无法移动!请重新选择。"<<endl;continue;}
         if(a[nx][ny].c == 1)Go = 1;
         if(C == 1)Ca_x[f] = nx,Ca_y[f] = ny;
//         a[nx][ny].c = a[xs][ys].c;a[nx][ny].color = a[xs][ys].color;
         a[xs][ys].c = a[xs][ys].color = 0;
        if(Go){if(f)cout<<"红";else cout<<"蓝";cout<<"方获胜!";return 0;}
        // int Cc = Check_ca(nx,ny,nx,ny,f);
		f = !f;
		// if(Check())cout<<"yes;";
		// else cout<<"no;";
        // if(Go)cout<<"yes"<<endl;
        // else cout<<"no"<<endl;
//        if(i == 345 || i == 344)
//        for(int j = 10; j >= 0; j--){
//			for(int k = 0; k < 9; k++){
//				if(a[j][k].c != 0){
//					if((j == Ca_x[f] && k == Ca_y[f]) || (j == Ca_x[!f] && k == Ca_y[!f]))SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
//					cout<<name2[a[j][k].c]<<a[j][k].color<<" ";
//					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |
//		FOREGROUND_GREEN | FOREGROUND_BLUE);
//				}else{
//					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |
//		FOREGROUND_GREEN | FOREGROUND_BLUE);
//					cout<<"000 ";
//				}
//			}
//			cout<<endl;
//		}
    }
    return 0;
}
2020/11/12 07:53
加载中...