2017-01-13 8 views
6

तो मैं एक शतरंज खेल बना रहा हूं, हालांकि मुझे बिशप टुकड़ा सही ढंग से नहीं चल रहा है।सी ++ - शतरंज बिशप MoveCode त्रुटि?

string board[8][8] = { 

{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"}, 
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"}, 
{"_" , "_", "_" , "_" ,"_", "B" , "_" , "_"}, 
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"}, 
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"}, 
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"}, 
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"}, 
{"_" , "_", "_" , "_" ,"_", "_" , "_" , "_"} }; 

यहाँ बोर्ड आकर्षित करने के लिए ड्रा समारोह है:

यह मेरी बिसात है।

void Draw() 
{ 
for(int i = 0; i < 8; i++) 
{ 
    for(int j = 0; j < 8; j++) 
     std::cout << board[ i ][ j ] << ' '; 

    std::cout << '\n'; 
} 
cout<<"\n"; 
} 

बिशप मूवमेंट कोड अब तक।

if (board[x][y] == "B") 
{  //Highlight the users chosen piece 
    board[x][y] = "\033[0;31mB\033[0m"; 
    //Now showing available moves the chosen bishop can move to 
    for(int counter=1; counter <=7; counter++) { 
     if(board[x+counter][y+counter] == "_") { //if there is an empty space, then place X to show peice can move there 
      board[x+counter][y+counter] = "X"; 
     } 
     else { //if cannot move their ,then break 
      break; 
     } 
    } 
} 

यहां मेरी समस्या है। यह एक्स स्पेस दिखाता है कि टुकड़ा बोर्ड पर कुछ स्थानों पर उपयोगकर्ता को स्थानांतरित करने में सक्षम है। हालांकि जब टुकड़ा सरणी के कुछ स्थानों में होता है जैसे वह बोर्ड कोड में होता है। यह Xs को चित्रित करने के बजाय बोर्ड के विभिन्न पक्षों पर एक्स को ओवरलैप करता है और दिखाता है जब उनका कोई "_" उपलब्ध नहीं होता है।

+0

क्या आपने डीबगर के साथ अपने कोड से कदम उठाने का प्रयास किया था? –

+2

आपके पास बोर्ड के सूचकांक [एक्स + काउंटर] [वाई + काउंटर] में ओवर-एंडफ्लो है। – PanicSheep

+0

@ PanicSheep की टिप्पणी बिशप के अलावा पीछे की ओर भी चलता है। – bansi

उत्तर

7

आपको यह जांचना होगा कि x + counter और y + counter बोर्ड के भीतर आते हैं या नहीं।

if (board[x][y] == "B") 
{  //Highlight the users chosen piece 
    board[x][y] = "\033[0;31mB\033[0m"; 
    //Now showing available moves the chosen bishop can move to 
    for(int counter = 1; (x + counter) <= 7 && (y + counter) <= 7; counter++){ 
     if(board[x + counter][y + counter] == "_") { 
      //if there is an empty space, then place X to show peice can move there 
      board[x + counter][y + counter] = "X"; 
     } 
     else { //if cannot move their ,then break 
      break; 
     } 
    } 
} 

बेशक, यह केवल एक दिशा को चिह्नित करता है, जबकि बिशप वास्तव में चार दिशाओं में स्थानांतरित हो सकता है।

इसके अलावा, यह जांच नहीं करता है कि पथ के बीच कोई टुकड़ा है या नहीं।

सभी चार दिशाओं पर विचार करने के लिए, आप एक दिशा मैट्रिक्स बना सकते हैं, जो प्रत्येक दिशा के लिए एक्स और वाई में परिवर्तन को संग्रहीत करता है।

// 4 directions in which bishop can move 
int dx[4] = {-1, -1, 1, 1}; 
int dy[4] = {-1, 1, -1, 1}; 


if (board[x][y] == "B") 
{ 
    // for each direction 
    for(int dir = 0; dir < 4; dir++) { 

     // the bishop can move to a maximum of 7 squares 
     for(int counter = 1; counter < 8; counter++) { 

      // calculate where the bishop will be 
      // after moving "counter" number of squares 
      int new_x, new_y; 
      new_x = x + dx[dir] * counter; 
      new_y = y + dy[dir] * counter; 

      // check if the square lies within the board 
      if(new_x >= 0 && new_x < 8 && new_y >= 0 && new_y < 8) { 

       // if there is an empty space, then place X to show peice can move there 
       if(board[cur_x][cur_y] == "_") { 
        board[cur_x][cur_y] = "X"; 
       } 

       // if there is any other piece in between, the bishop can't move further 
       else { 
        break; 
       } 
      } 

      // break if the square is outside the board 
      else { 
       break; 
      } 
     } 
    } 
} 
+0

प्रतिक्रिया के लिए धन्यवाद। मैंने आपकी प्रतिक्रिया से बहुत कुछ सीखा है। –

+1

महान उत्तर! ;) '++' – Inian

+0

धन्यवाद @ इयनियन :) –

संबंधित मुद्दे