连接四个获胜组合检查适用于大多数情况,但对某些情况不起作用

时间:2017-11-26 06:10:41

标签: c++ debugging multidimensional-array stack 2d-games

所以,我基本上记录了我所做的所有测试用例。幸运的是,他们中的大多数都工作(尤其是垂直赢取支票),但有时它表示,即使没有四个连接件,也有胜利者。

(这是我为个人项目所做的事情,而不是学校作业。)

#include <cstdio>
#include <cstring>
#include <cstdlib>
const int stSize = 6;
const int width = 7;

class myStack // class for myStack. Each column is a stack.
{
 private:
     int boardArr[width][stSize];
     int inUseA;
     int inUseB;
     int inUseC;
     int inUseD;
     int inUseE;
     int inUseF;
     int inUseG;
     int moveNumber;
     int slotsLeft;
     //char player; 
 public:
 myStack ()
 {
     for(int p = 0; p<width;p++) {
         for(int c = 0; c<stSize;c++){
         boardArr[p][c] = 'O';
         }
     }
     inUseA = -1;
     inUseB = -1;
     inUseC = -1;
     inUseD = -1;
     inUseE = -1;
     inUseF = -1;
     inUseG = -1;
     moveNumber = 0;
     slotsLeft = 42;
 }

 int getstSize() {
     return stSize;
 }

 int getMoveNumber() {
     return moveNumber;
 }

 int getSlotsLeft() {
     return slotsLeft;
 }

void showBoard() {
    printf("  =============================\n");
    printf("1:| %c | %c | %c | %c | %c | %c | %c |\n",boardArr[0][5],boardArr[1][5],boardArr[2][5],boardArr[3][5], boardArr[4][5],boardArr[5][5],boardArr[6][5]);
    printf("2:| %c | %c | %c | %c | %c | %c | %c |\n",boardArr[0][4],boardArr[1][4],boardArr[2][4],boardArr[3][4], boardArr[4][4],boardArr[5][4],boardArr[6][4]);
    printf("3:| %c | %c | %c | %c | %c | %c | %c |\n",boardArr[0][3],boardArr[1][3],boardArr[2][3],boardArr[3][3], boardArr[4][3],boardArr[5][3],boardArr[6][3]);
    printf("4:| %c | %c | %c | %c | %c | %c | %c |\n",boardArr[0][2],boardArr[1][2],boardArr[2][2],boardArr[3][2], boardArr[4][2],boardArr[5][2],boardArr[6][2]);
    printf("5:| %c | %c | %c | %c | %c | %c | %c |\n",boardArr[0][1],boardArr[1][1],boardArr[2][1],boardArr[3][1], boardArr[4][1],boardArr[5][1],boardArr[6][1]);
    printf("6:| %c | %c | %c | %c | %c | %c | %c |\n",boardArr[0][0],boardArr[1][0],boardArr[2][0],boardArr[3][0], boardArr[4][0],boardArr[5][0],boardArr[6][0]);
    printf("  =============================\n"); // FORMAT: ([WIDTH][HEIGHT])
    printf("    A   B   C   D   E   F   G   \n");
    }

void pushA() {
    char data;
    if (moveNumber % 2 != 0) {
        data = '@';
    }
    else {
        data = '#';
    }
 if (inUseA == stSize-1) {
     printf("Error. Stack's full.\n");
     return;
 }
 else {
     inUseA = inUseA +1;
     boardArr[0][inUseA] = data;
     slotsLeft--;
     moveNumber++;
 }

}
void pushB() {
    char data;
    if (moveNumber % 2 != 0) {
        data = '@';
    }
    else {
        data = '#';
    }
    if (inUseB == stSize-1) {
        printf("Error. Stack's full.\n");
        return;
    }
    else {
        inUseB = inUseB +1;
        boardArr[1][inUseB] = data;
        slotsLeft--;
        moveNumber++;
    }
   }
void pushC() {
    char data;
    if (moveNumber % 2 != 0) {
        data = '@';
    }
    else {
        data = '#';
    }
    if (inUseC == stSize-1) {
        printf("Error. Stack's full.\n");
        return;
    }
    else {
        inUseC = inUseC +1;
        boardArr[2][inUseC] = data;
        slotsLeft--;
        moveNumber++;
    }
}
void pushD() {
    char data;
    if (moveNumber % 2 != 0) {
        data = '@';
    }
    else {
        data = '#';
    }
    if (inUseD == stSize-1) {
        printf("Error. Stack's full.\n");
        return;
    }
    else {
        inUseD = inUseD +1;
        boardArr[3][inUseD] = data;
        slotsLeft--;
        moveNumber++;
    }
}

void pushE() {
    char data;
    if (moveNumber % 2 != 0) {
        data = '@';
    }
    else {
        data = '#';
    }
    if (inUseE == stSize-1) {
        printf("Error. Stack's full.\n");
        return;
    }
    else {
        inUseE = inUseE +1;
        boardArr[4][inUseE] = data;
        slotsLeft--;
        moveNumber++;
    }
}

void pushF() {
    char data;
    if (moveNumber % 2 != 0) {
        data = '@';
    }
    else {
        data = '#';
    }
    if (inUseF == stSize-1) {
        printf("Error. Stack's full.\n");
        return;
    }
    else {
        inUseF = inUseF +1;
        boardArr[5][inUseF] = data;
        slotsLeft--;
        moveNumber++;
    }
}

void pushG() {
    char data;
    if (moveNumber % 2 != 0) {
        data = '@';
    }
    else {
        data = '#';
    }
    if (inUseG == stSize-1) {
        printf("Error. Stack's full.\n");
        return;
    }
    else {
        inUseG = inUseG +1;
        boardArr[6][inUseG] = data;
        slotsLeft--;
        moveNumber++;
    }
}

int HorizontalWin() {
    int horizCounter1 = 0;
    int horizCounter2 = 0;

    for(int n = 0; n<stSize;n++) {
        for(int m = 0; m<width;m++) {
            if(boardArr[n][m] == '@') {
                horizCounter1++;
                horizCounter2 = 0;
            }
            else if(boardArr[n][m] == '#') {
                horizCounter2++;
                horizCounter1 = 0;
            }
        }
        if(horizCounter1 == 5 || horizCounter2 == 5) {
            printf("Winner!\n");
            return 1;
        }
    }
    return 0;
}

int VerticalWin () {
    int vertCounter1 = 0;
    int vertCounter2 = 0;

    for(int x = 0; x<width;x++) {
        for(int y = 0; y<stSize;y++) {
            if(boardArr[x][y] == '@') {
                vertCounter1++;
        vertCounter2 = 0;
            }
            else if(boardArr[x][y] == '#') {
                vertCounter2++;
        vertCounter1 = 0;
            }
        }
        if(vertCounter1 == 4 || vertCounter2 == 4) {
            printf("Winner!\n");
            return 1;
        }
    }
    return 0;
}

int diagonalWin() { // Don't mind this, i haven't started making this yet
    return 0;
}

void processCommandLoop (FILE* inFile)
{
 char  buffer[300];
 char* input;

 input = fgets ( buffer, 300, inFile );   // get a line of input

 // loop until all lines are read from the input
 while (input != NULL)
 {
   // process each line of input using the strtok functions 
   char* command;
   command = strtok (input , " \n\t");

   printf ("*%s*\n", command);

   if ( command == NULL ) {
     printf ("Blank Line\n"); 
   }

   else if ( strcmp (command, "q") == 0){ 
     exit(1); 
   }

   else if ( strcmp (command, "?") == 0){ 
     showCommands();
   }

   else if ( strcmp (command, "a") == 0){ 
     pushA();
     showBoard();
     if (VerticalWin() == 1 || HorizontalWin() == 1) {
         break;
     }
   }
   else if ( strcmp (command, "b") == 0){ 
     pushB();
     showBoard();
     if (VerticalWin() == 1 || HorizontalWin() == 1) {
         break;
     }
   }
   else if ( strcmp (command, "c") == 0){ 
     pushC();
     showBoard();
     if (VerticalWin() == 1|| HorizontalWin() == 1) {
         break;
     }
   }
   else if ( strcmp (command, "d") == 0){ 
     pushD();
     showBoard();
     if (VerticalWin() == 1|| HorizontalWin() == 1) {
         break;
     }
   }
   else if ( strcmp (command, "e") == 0){ 
     pushE();
     showBoard();
     if (VerticalWin() == 1|| HorizontalWin() == 1) {
         break;
     }
   }
   else if ( strcmp (command, "f") == 0){ 
     pushF();
     showBoard();
     if (VerticalWin() == 1|| HorizontalWin() == 1) {
         break;
     }
   }
   else if ( strcmp (command, "g") == 0){ 
     pushG();
     showBoard();
     if (VerticalWin() == 1|| HorizontalWin() == 1) {
         break;
     }
   }
   else {
     printf ("Invalid input: %s\n", command);
   }
   input = fgets ( buffer, 300, inFile );   // get the next line of input

 }
}


void showCommands()
{
  printf ("The commands for this game:\n");
  printf("At any point, you may quit the game by entering 'q'");
  printf ("  q \n");
  printf ("  ? \n");
  printf ("  a \n");
  printf ("  b  \n");
  printf ("  c \n");
  printf ("  d \n");
  printf ("  e \n");
  printf ("  f \n");
  printf ("  g \n");
  printf("Options a-g mean that you can push a piece into the indicated stack\n");
}

};


 int main()
 {
   FILE* inFile = stdin;

   myStack test;
   test.showCommands();
   test.showBoard();
   test.processCommandLoop(inFile);
   printf("Quitting..\n");
   return 1;
 }

所以,是的,我知道我没有有效地编写程序(特别是考虑到我有很多推送函数xD,但我主要担心的是我的算法来确定谁赢了。我的垂直案例适用于大多数情况下,但我的横向几乎没有。

要了解我的意思,请查看我的测试用例:

  =============================
1:| O | O | O | O | O | O | O |
2:| # | O | O | O | O | O | O |
3:| # | @ | O | O | O | O | O |
4:| # | @ | O | O | O | O | O |
5:| # | @ | O | O | O | O | O |
6:| @ | # | O | O | O | O | O |
  =============================
    A   B   C   D   E   F   G
Winner!
--
  =============================
1:| @ | O | O | O | O | O | O |
2:| @ | O | O | O | O | O | O |
3:| @ | O | O | O | O | O | O |
4:| @ | @ | # | O | O | O | O |
5:| # | # | # | O | O | O | O |
6:| @ | # | # | O | O | O | O |
  =============================
    A   B   C   D   E   F   G
Winner!
  =============================
1:| O | O | O | O | O | O | O |
2:| O | O | O | O | O | O | O |
3:| # | O | O | O | O | O | O |
4:| # | O | O | O | O | O | O |
5:| # | O | @ | O | O | O | O |
6:| # | @ | @ | O | O | O | O |
  =============================
    A   B   C   D   E   F   G
Winner!
---------------------------------A

  =============================
1:| O | O | O | O | O | O | O |
2:| O | O | O | O | O | O | O |
3:| O | @ | O | O | O | O | O |
4:| O | @ | O | O | O | O | O |
5:| # | @ | # | O | O | O | O |
6:| # | @ | # | O | O | O | O |
  =============================
    A   B   C   D   E   F   G

Winner!
  =============================
1:| O | O | O | O | O | O | O |
2:| O | # | O | O | O | O | O |
3:| O | # | O | O | O | O | O |
4:| @ | # | O | O | O | O | O |
5:| @ | # | O | O | O | O | O |
6:| # | @ | @ | O | O | O | O |
  =============================
    A   B   C   D   E   F   G
Winner!
  =============================
1:| @ | # | O | O | O | O | O |
2:| @ | # | O | O | O | O | O |
3:| @ | # | O | O | O | O | O |
4:| # | # | @ | O | O | O | O |
5:| # | @ | @ | O | O | O | O |
6:| # | @ | # | O | O | O | O |
  =============================
    A   B   C   D   E   F   G

赢家!     ------------------------------------乙

  =============================
1:| O | O | O | O | O | O | O |
2:| O | O | O | O | O | O | O |
3:| O | O | @ | O | O | O | O |
4:| O | O | @ | O | O | O | O |
5:| O | O | @ | O | # | O | O |
6:| # | O | @ | O | # | O | # |
  =============================
    A   B   C   D   E   F   G
Winner!
  =============================
1:| O | O | O | O | O | O | O |
2:| O | O | # | O | O | O | O |
3:| O | O | # | O | O | O | O |
4:| O | O | # | O | O | O | O |
5:| @ | O | # | @ | O | O | O |
6:| @ | O | @ | # | O | O | O |
  =============================
    A   B   C   D   E   F   G
Winner!

  =============================
1:| O | O | @ | O | O | O | O |
2:| O | O | @ | O | O | O | O |
3:| O | O | @ | # | O | O | O |
4:| O | O | @ | # | O | O | O |
5:| O | O | # | # | O | O | O |
6:| O | O | # | @ | O | O | O |
  =============================
    A   B   C   D   E   F   G

Winner!
  =============================
1:| O | O | O | O | O | O | O |
2:| O | O | O | O | O | O | O |
3:| O | O | O | O | O | O | O |
4:| O | O | @ | O | O | O | O |
5:| # | O | @ | O | O | O | O |
6:| # | O | @ | @ | O | # | # |
  =============================
    A   B   C   D   E   F   G
Winner!^^ shouldnt be the case.. this implies that even adjacent pieces are counted for win status.

------------------------------------C
  =============================
1:| O | O | O | O | O | O | O |
2:| O | O | O | O | O | O | O |
3:| O | O | O | # | O | O | O |
4:| O | O | @ | # | O | O | O |
5:| O | O | @ | # | O | O | O |
6:| O | O | @ | # | O | O | O |
  =============================
    A   B   C   D   E   F   G
Winner!
  =============================
1:| O | O | O | @ | O | O | O |
2:| O | O | O | @ | O | O | O |
3:| O | O | O | @ | O | O | O |
4:| O | O | O | @ | O | O | O |
5:| # | O | @ | # | O | O | O |
6:| # | # | @ | # | O | O | # |
  =============================
    A   B   C   D   E   F   G
Winner!
  =============================
1:| O | O | O | O | O | O | O |
2:| O | O | O | @ | O | O | O |
3:| O | O | O | @ | O | O | O |
4:| # | O | O | @ | O | O | O |
5:| # | O | O | @ | O | O | O |
6:| # | O | O | # | O | O | O |
  =============================
    A   B   C   D   E   F   G
Winner!
------------------------------------D

  =============================
1:| O | O | O | O | O | O | O |
2:| O | O | O | O | O | O | O |
3:| O | O | O | O | # | O | O |
4:| O | O | O | O | # | O | O |
5:| @ | O | O | O | # | O | O |
6:| @ | O | O | O | # | O | @ |
  =============================
    A   B   C   D   E   F   G
Winner!
  =============================
1:| O | O | O | O | O | O | O |
2:| O | O | O | O | @ | O | O |
3:| O | O | O | O | @ | O | O |
4:| # | O | O | O | @ | O | O |
5:| # | O | O | O | @ | O | O |
6:| # | O | O | O | # | O | O |
  =============================
    A   B   C   D   E   F   G
Winner!

  =============================
1:| O | O | O | O | @ | O | O |
2:| O | O | O | O | @ | O | O |
3:| O | O | O | O | @ | O | O |
4:| O | O | O | O | @ | O | O |
5:| # | O | O | # | # | O | O |
6:| @ | # | O | @ | # | O | # |
  =============================
    A   B   C   D   E   F   G
Winner!
------------------------------------E

  =============================
1:| O | O | O | O | O | O | O |
2:| O | O | O | O | O | O | O |
3:| O | O | O | O | O | # | O |
4:| O | O | O | O | O | # | O |
5:| @ | O | O | O | O | # | O |
6:| @ | @ | O | O | O | # | O |
  =============================
    A   B   C   D   E   F   G
Winner!

  =============================
1:| O | O | O | O | O | O | O |
2:| O | O | O | O | O | @ | O |
3:| O | O | O | O | O | @ | O |
4:| O | O | O | O | O | @ | O |
5:| # | O | O | O | O | @ | O |
6:| # | # | O | O | O | # | O |
  =============================
    A   B   C   D   E   F   G
Winner!


  =============================
1:| O | O | O | O | O | @ | O |
2:| O | O | O | O | O | @ | O |
3:| O | O | O | O | O | @ | O |
4:| O | O | O | O | O | @ | O |
5:| O | O | O | O | O | # | O |
6:| @ | # | # | # | O | # | O |
  =============================
    A   B   C   D   E   F   G
Winner!

------------------------------------F
  =============================
1:| O | O | O | O | O | O | O |
2:| O | O | O | O | O | O | O |
3:| O | O | O | O | O | O | # |
4:| O | O | O | O | O | O | # |
5:| @ | O | O | O | O | O | # |
6:| @ | O | @ | O | O | O | # |
  =============================
    A   B   C   D   E   F   G
Winner!
  =============================
1:| O | O | O | O | O | O | @ |
2:| O | O | O | O | O | O | @ |
3:| O | O | O | O | O | O | @ |
4:| # | O | # | O | O | O | @ |
5:| @ | O | # | O | O | O | # |
6:| @ | # | @ | O | # | O | # |
  =============================
    A   B   C   D   E   F   G
Winner!

  =============================
1:| O | O | O | O | O | O | O |
2:| O | O | O | O | O | O | # |
3:| O | O | O | O | O | O | # |
4:| @ | O | O | O | O | O | # |
5:| @ | O | O | O | @ | O | # |
6:| # | @ | O | O | # | O | @ |
  =============================
    A   B   C   D   E   F   G
Winner!

>>>pay special attention to this error
  =============================
1:| O | O | O | O | O | O | O |
2:| O | O | O | O | O | O | O |
3:| O | O | O | O | O | O | @ |
4:| # | O | O | O | O | O | @ |
5:| # | O | O | O | O | O | @ |
6:| # | O | # | @ | O | O | # |
  =============================
    A   B   C   D   E   F   G
Winner! < shouldnt be the case
------------------------------------G

horizontal error:
  =============================
1:| O | O | O | O | O | O | O |
2:| O | O | O | O | O | O | O |
3:| O | O | O | O | O | O | O |
4:| O | O | O | O | O | O | O |
5:| O | O | O | O | @ | O | O |
6:| # | # | # | O | @ | O | O |
  =============================
    A   B   C   D   E   F   G
Winner!< shouldnt be the case

正如您所看到的,大多数垂直案例确实有效,但我发现这里和那里有一些错误。

另外,我的横向检查工作正常。

那么,CS人员,你认为我需要在哪里调整算法? 当vertcounter2递增时,我将verticalCounter1 = 0的原因是,如果蓝色部分是X列上的红色部分的顶部,那么这将影响红色部分的计数器,以便红色部分必须再次达到4。

我尝试用所有这些情况手动调试这个,但我不知道如何使用GDB这样的情况。我知道如何将它用于分段错误和内存泄漏,但我是CPP的新手,所以如果我在这里搞砸了,我会道歉。我尽我所能。

1 个答案:

答案 0 :(得分:0)

我看到了几个问题:

  1. 当您点击空格时,您没有将计数器重置为0。如果遇到没有任何内容的位置,则两个计数器都应重置。这不会导致垂直情况出现问题,因为占用空间之间没有空格,但是对于其他获胜类型来说这是一个问题
  2. 当您移动到新的行/列/对角线时,您没有重置计数器。例如,在Horizo​​ntalWin中,当您完成第一行的检查时,您应该在移动到第二行之前重置计数器,因为所有四个部分都需要在同一行中。