❔ For loop is being skipped after a do while loop.

Hello, Im trying to build a simple connect four game in console and I've come across an issue where my for loop is being skipped but the if statement is being reached.
No description
33 Replies
Just Some Bread
Just Some BreadOP13mo ago
Both of the for loops are being skipped after the do while at the beginning of the game loop
ZacharyPatten
ZacharyPatten13mo ago
please share your code rather than a screenshot of your code even better if you can upload to github that would be nice $code
MODiX
MODiX13mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Just Some Bread
Just Some BreadOP13mo ago
Oh right sorry about that.
using System;

char[,] grid = new char[7,6];
grid[0,0] = 'R';
int boardWidth = 7;
int boardHeight = 6;
int currentPlayer = 0;

string[] playerNames = new string[2];
Console.WriteLine("Enter player 1 name: ");
playerNames[0] = Console.ReadLine();
Console.WriteLine("Enter player 2 name: ");
playerNames[1] = Console.ReadLine();


//gameloop
do{
int columnSelection;
Console.WriteLine("Player "+playerNames[currentPlayer]+" enter your column selection between 1 and 7: ");

do{
Console.WriteLine("Enter your column selection between 1 and 7: ");
columnSelection = int.Parse(Console.ReadLine())-1;

if((columnSelection < boardWidth && columnSelection >= 0) && grid[0, columnSelection]!= '\0'){
Console.WriteLine("There is no space in that column.");
columnSelection = -1;
}
}while(columnSelection<0 || columnSelection >= boardWidth);

int rowSelection = boardHeight -1;
for(; rowSelection<=0; rowSelection--){
if(grid[rowSelection, columnSelection] == '\0'){
grid[rowSelection, columnSelection] = 'R';
}
else{
grid[rowSelection,columnSelection] = 'Y';
}
break;
}

for(int c = 0; c>=boardWidth-1; c++){
for(int r = 0; r>=boardHeight-1; r++){
if(grid[r,c] == 'R'){
Console.Write("| R ");
}else if(grid[r,c] == 'Y'){
Console.Write("| Y ");
}else{
Console.Write("| ");
}
}
Console.Write('|');
Console.WriteLine();
}
if(currentPlayer == 0){
currentPlayer++;
}
else{
currentPlayer--;
}
}while(true);
using System;

char[,] grid = new char[7,6];
grid[0,0] = 'R';
int boardWidth = 7;
int boardHeight = 6;
int currentPlayer = 0;

string[] playerNames = new string[2];
Console.WriteLine("Enter player 1 name: ");
playerNames[0] = Console.ReadLine();
Console.WriteLine("Enter player 2 name: ");
playerNames[1] = Console.ReadLine();


//gameloop
do{
int columnSelection;
Console.WriteLine("Player "+playerNames[currentPlayer]+" enter your column selection between 1 and 7: ");

do{
Console.WriteLine("Enter your column selection between 1 and 7: ");
columnSelection = int.Parse(Console.ReadLine())-1;

if((columnSelection < boardWidth && columnSelection >= 0) && grid[0, columnSelection]!= '\0'){
Console.WriteLine("There is no space in that column.");
columnSelection = -1;
}
}while(columnSelection<0 || columnSelection >= boardWidth);

int rowSelection = boardHeight -1;
for(; rowSelection<=0; rowSelection--){
if(grid[rowSelection, columnSelection] == '\0'){
grid[rowSelection, columnSelection] = 'R';
}
else{
grid[rowSelection,columnSelection] = 'Y';
}
break;
}

for(int c = 0; c>=boardWidth-1; c++){
for(int r = 0; r>=boardHeight-1; r++){
if(grid[r,c] == 'R'){
Console.Write("| R ");
}else if(grid[r,c] == 'Y'){
Console.Write("| Y ");
}else{
Console.Write("| ");
}
}
Console.Write('|');
Console.WriteLine();
}
if(currentPlayer == 0){
currentPlayer++;
}
else{
currentPlayer--;
}
}while(true);
mtreit
mtreit13mo ago
Step through the code in a debugger Useful skill to learn Also that code formatting is something else 😄
Just Some Bread
Just Some BreadOP13mo ago
Yeah I've been doing that but it still just skips the code without any reason.
mtreit
mtreit13mo ago
"without any reason" is going to turn out to be an incorrect statement....
Just Some Bread
Just Some BreadOP13mo ago
Not entirely sure why it ended up like that ill be honest haha Yeah maybe I should rephrase to "I have yet to find the reason its doing that and I might just be blind."
mtreit
mtreit13mo ago
Do you have a build warning that says "unreachable code detected" ? (That's a rhetorical question)
mtreit
mtreit13mo ago
No description
Just Some Bread
Just Some BreadOP13mo ago
Yes. haha I put a print statement jsut after the do while loop in the game loop and that doesnt seem to output to the console.
ZacharyPatten
ZacharyPatten13mo ago
it still just skips the code without any reason
You must be confused about what is happening. what code do you believe is being skipped for no reason?
mtreit
mtreit13mo ago
I always run with warnings as errors enabled so for me this code wouldn't even build.
Just Some Bread
Just Some BreadOP13mo ago
The 2 for loops that are after the do while inside the game loop seems be getting skipped and im not sure why.
ZacharyPatten
ZacharyPatten13mo ago
No description
Just Some Bread
Just Some BreadOP13mo ago
Nevermind the print statements work i just forgot semi colons haha
ZacharyPatten
ZacharyPatten13mo ago
if you put a break point you will see why rowSelection is 5, which is greater than 0
mtreit
mtreit13mo ago
for (; rowSelection <= 0; rowSelection--)
{
}
for (; rowSelection <= 0; rowSelection--)
{
}
rowSelection will never be less than or equal to zero so this loop will never execute. Is that supposed to be >=
Just Some Bread
Just Some BreadOP13mo ago
Then question is why is the second loop being skipped? Or is that being caused because the first one wont be executed
ZacharyPatten
ZacharyPatten13mo ago
nothing is being "skipped". code will do exactly what you tell it to. you have told your code not to run the loop if rowSelection is greater than 0, which it is
mtreit
mtreit13mo ago
for (int c = 0; c >= boardWidth - 1; c++)
for (int c = 0; c >= boardWidth - 1; c++)
This will also never execute.
Just Some Bread
Just Some BreadOP13mo ago
Ah yes, I see what you mean. Thank you very much!
mtreit
mtreit13mo ago
Because this says "run this loop while c is greater than or equal to boardWidth - 1", but on the first iteration of the loop c is zero so this loop condition is never satisfied.
ZacharyPatten
ZacharyPatten13mo ago
also, I have a version of connect 4 on GitHub if you would like to see it. not trying to discourage you from coding your own, but if you want to see another person's code just ask and I can link it
Just Some Bread
Just Some BreadOP13mo ago
Oh right so do I need to change the condition for it to be less than the boardWidth-1 so it satisifies it? That would be helpful thank you! Ill try and keep coding it myself but it would be nice to see how other people have gone about it.
ZacharyPatten
ZacharyPatten13mo ago
https://github.com/dotnet/dotnet-console-games it is one of the games in here 🙂 you can also play it online so you don't have to download and compile it if you don't want to
mtreit
mtreit13mo ago
What do you think? 🙂 It might help to remind yourself that a for loop is just syntactic sugar to make writing a while loop a bit less verbose.
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}
This basically translates to the following:
int i = 0;

while (i < 10)
{
Console.WriteLine(i);
i++;
}
int i = 0;

while (i < 10)
{
Console.WriteLine(i);
i++;
}
Just Some Bread
Just Some BreadOP13mo ago
Haha if only i could understand the code, I'm pretty new to this so its needing a bit of practice before i can understand. Either way thank you very much the both of you! Oh right i see, so the condition in the middle needs to be satisfied for the for loop to continue
ZacharyPatten
ZacharyPatten13mo ago
if you do look at it and have questiosn feel free to ping me
Just Some Bread
Just Some BreadOP13mo ago
That makes sense why the for loops seemed as if they were being skipped when they just werent being satisfied at all.
mtreit
mtreit13mo ago
Yes, in a for loop and in a while loop the loop never executes if the condition is not satisfied. In a do-while loop it always executes at least once because the condition is checked at the end of the loop instead of the beginning.
Just Some Bread
Just Some BreadOP13mo ago
Thank you! Oh right now I understand, thank you very much for clarifying. That makes sense why a game loop uses a do while rather than just a while as well.
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server