0
I am developing the game of the rooster / old game and in this game I have to check every new move or iteration if there is any winner. I started by trying to do this for a static matrix predefined by me and I also started by doing the check first by deleting from the two possible diagonals. The code is as follows:
UPDATE
int main()
{
int VEC[3][3] = {
{1,1,1},
{0,1,0},
{1,0,1}
};
int m = 3;
int n = 3;
int i = 0,j = 0,count = 1,total = 0;
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
if(VEC[i][j] == VEC[i][j+1])
{
count++;
if(count == 3)
{
printf("GANHOU!\n");
break;
}
}
else
{
count = 0;
break;
}
}
}
}
In this case that I presented he tells me that there is a winner and there really is a winning key in the last row of the matrix. But I have some doubts about this code. First of all because I think in all rows I left the matrix by checking the position j + 1. And I don’t know to what extent this is quite wrong or not. Then I tried to control this situation by adding i <= 3 and j <= 3 but I always need to check if the next position to which I am is equal to increase the count of equal values to know if I found 3 values that give the winning key.
NEW INFORMATION
I’ve already arranged to find the winning key, but if there are two keys, he’ll give me two winners. How can I finish the program when the winning condition is found? Finish in full since only 1 player can win.
This part I have to do yet but I have an idea how to do it, but I think I managed to get the winner right but I needed to break every cycle when I found the winner. At this point he even finds the winner in the first row of the matrix continues to traverse the matrix when I intended it to stop because we can only have one winner in the game. Any tips? I thought "break" would work but apparently not!
– João
Makes a function that contains a return 1 or 0. 1 being the player and 0 the second player. Get in the main this value makes the conditions and puts a break inside the loop. I’ll give you a hint of how I did it.. I created a do while and called the functions in by passing the matrix as parameter. If there is a winner it for the loop. If it is draw it Zera the matrix and starts a new game. I made it as simple as possible
– Igor Vargas