程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> uva 10196 check the check

uva 10196 check the check

編輯:C++入門知識

[cpp] 
#include<iostream>  
#include<string.h>  
#include<stdlib.h>  
#include<stdio.h>  
using namespace std; 
int flagData[9][9],data[9][9]; 
char image[9][9]; 
int blackKingX, blackKingY, whiteKingX, whiteKingY; 
int p_dx[]={1,1},p_dy[]={1,-1},P_dx[]={-1,-1},P_dy[]={1,-1}; 
int R_r_dx[]={0,0,1,-1 }, R_r_dy[]={1,-1,0,0 }; 
int B_b_dx[]={1,-1,1,-1}, B_b_dy[]={1,-1,-1,1}; 
int Q_q_dx[]={1,-1,1,-1,0,0,1,-1 }, Q_q_dy[]={1,-1,-1,1,1,-1,0,0 }; 
int K_k_dx[]={1,-1,1,-1,0,0,1,-1 }, K_k_dy[]={1,-1,-1,1,1,-1,0,0 }; 
int N_n_dx[]={-1,-2,-2,-1,1,2,2,1}, N_n_dy[]={-2,-1,1,2,2,1,-1,-2}; 
int checkTheNext(int x, int y) 

    if (x >= 0 && x < 8 && y >= 0 && y < 8) 
    { 
        if (flagData[x][y] == 1) 
            return 2; 
        else return 1; 
    } 
    else return 0; 

void p(int x, int y) 

    for (int i = 0; i < 2; i++) 
    { 
        int newx = x + p_dx[i]; 
        int newy = y + p_dy[i]; 
        if (checkTheNext(newx, newy)) 
        { 
            if (data[newx][newy] == 0 || data[newx][newy] == 1) 
                data[newx][newy] = 1; 
            else data[newx][newy] = 3; 
        } 
    } 

int P(int x, int y) 

    for (int i = 0; i < 2; i++) 
    { 
        int newx = x + P_dx[i]; 
        int newy = y + P_dy[i]; 
        if (checkTheNext(newx, newy)) 
        { 
            if (data[newx][newy] == 0 || data[newx][newy] == 2) 
                data[newx][newy] = 2; 
            else data[newx][newy] = 3; 
        } 
    } 

void R_r(int x, int y, int type) 

    for (int i = 0; i < 4; i++) 
    { 
        int temp; 
        int newx = x + R_r_dx[i]; 
        int newy = y + R_r_dy[i]; 
        temp = checkTheNext(newx, newy); 
        while (temp) 
        { 
            if (data[newx][newy] == 0 || data[newx][newy] == type) 
                data[newx][newy] = type; 
            else data[newx][newy] = 3; 
            if (temp == 2) break; 
            newx += R_r_dx[i]; 
            newy += R_r_dy[i]; 
            temp = checkTheNext(newx , newy); 
        } 
    } 

void B_b(int x, int y, int type) 

    for (int i = 0; i < 4; i++) 
    { 
        int temp; 
        int newx = x + B_b_dx[i]; 
        int newy = y + B_b_dy[i]; 
        temp = checkTheNext(newx, newy); 
        while (temp) 
        { 
            if (data[newx][newy] == 0 || data[newx][newy] == type) 
                data[newx][newy] = type; 
            else data[newx][newy] = 3; 
            if (temp == 2) break; 
            newx += B_b_dx[i]; 
            newy += B_b_dy[i]; 
            temp = checkTheNext(newx , newy); 
        } 
    } 

void Q_q(int x, int y, int type) 

    for (int i = 0; i < 8; i++) 
    { 
        int temp; 
        int newx = x + Q_q_dx[i]; 
        int newy = y + Q_q_dy[i]; 
        temp = checkTheNext(newx, newy); 
        while (temp) 
        { 
            if (data[newx][newy] == 0 || data[newx][newy] == type) 
                data[newx][newy] = type; 
            else data[newx][newy] = 3; 
            if (temp == 2) break; 
            newx += Q_q_dx[i]; 
            newy += Q_q_dy[i]; 
            temp = checkTheNext(newx , newy); 
        } 
    } 

void K_k(int x, int y, int type) 

    for (int i = 0; i < 8; i++) 
    { 
        int newx = x + K_k_dx[i]; 
        int newy = y + K_k_dy[i]; 
        if (checkTheNext(newx, newy)) 
        { 
            if (data[newx][newy] == 0 || data[newx][newy] == type) 
                data[newx][newy] = type; 
            else data[newx][newy] = 3; 
        } 
    } 

void N_n(int x, int y, int type) 

    for (int i = 0; i < 8; i++) 
    { 
        int newx = x + N_n_dx[i]; 
        int newy = y + N_n_dy[i]; 
        if (checkTheNext(newx, newy)) 
        { 
            if (data[newx][newy] == 0 || data[newx][newy] == type) 
                data[newx][newy] = type; 
            else data[newx][newy] = 3; 
        } 
    } 

int main() 

    int n = 0,flag = 1,i,j; 
    string str; 
    while (flag) 
    { 
        char temp; 
        int point_size = 0; 
        memset(data, 0, sizeof(data)); 
        memset(flagData, 0, sizeof(flagData)); 
        for (i = 0; i < 64; i++) 
        { 
            cin >> temp; 
            int x = i / 8; 
            int y = i % 8; 
            image[x][y] = temp; 
            if (temp != '.') flagData[x][y] = 1; 
            else point_size++; 
            if (temp == 'k') 
            { 
                blackKingX = x; 
                blackKingY = y; 
            } 
            if (temp == 'K') 
            { 
                whiteKingX = x; 
                whiteKingY = y; 
            } 
        } 
        if (point_size == 64) break; 
        else 
        { 
            for (i = 0; i < 8; i++) 
                for (j = 0; j < 8; j++) 
                    switch (image[i][j]) 
                    { 
                    case '.': 
                        break; 
                    case 'p': 
                        p(i, j); 
                        break; 
                    case 'P': 
                        P(i, j); 
                        break; 
                    case 'r': 
                        R_r(i, j, 1); 
                        break; 
                    case 'R': 
                        R_r(i, j, 2); 
                        break; 
                    case 'b': 
                        B_b(i, j, 1); 
                        break; 
                    case 'B': 
                        B_b(i, j, 2); 
                        break; 
                    case 'q': 
                        Q_q(i, j, 1); 
                        break; 
                    case 'Q': 
                        Q_q(i, j, 2); 
                        break; 
                    case 'k': 
                        K_k(i, j, 1); 
                        break; 
                    case 'K': 
                        K_k(i, j, 2); 
                        break; 
                    case 'n': 
                        N_n(i, j, 1); 
                        break; 
                    case 'N': 
                        N_n(i, j, 2); 
                        break; 
                    } 
            cout << "Game #" << ++n << ": "; 
            if (data[whiteKingX][whiteKingY] == 1 || data[whiteKingX][whiteKingY] == 3) 
                cout << "white king is in check." << endl; 
            else if (data[blackKingX][blackKingY] == 2 || data[blackKingX][blackKingY] == 3) 
                cout << "black king is in check." << endl; 
            else 
                cout << "no king is in check." << endl; 
        } 
    } 
    return 0; 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved