Creating A Chessboard Program in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • punkybrewster
    New Member
    • Oct 2006
    • 11

    Creating A Chessboard Program in C++

    I have the idea to create the program but I need help in finishing it. My task is to write a program that prints out a chessboard using only asterisks. I have to use multiple nested loops with nested conditional statements inside. The output should look like a chessboard as follows: The pattern is not coming out as I want but I guess y'all know what a chessboard look like

    * * * * *
    * * * * *
    * * * * *



    * * * * *
    * * * * *
    * * * * *


    int main()
    {
    for (int rowloop = 0; rowloop < 4; rowloop++)
    {
    for (int row = 0; row < 3; row++)
    {
    for (int column = 0; column < 5; column++)
    cout << "*";
    cout << endl;
    }
    cout << endl << endl;
    }
    return 0;
    }

    I want to have a variable for the box that way when I set up the row and column if it is odd it starts doing the pattern from the first location and if not it indent in like the second row in the example and print the pattern. I appreciate any help that I can get.
  • vninja
    New Member
    • Oct 2006
    • 40

    #2
    i tried approaching this challenge a different way. first off i made this program write one line of the whole board x number times then made it rpt a couple of time (pardon the poor writing but its 5 in the morning and i'm dead tired (working) trying to get things to makes sense)

    here is my program for creating the board. personally i would have used arrays and functions but this is taking off of what you had originally.



    #include<iostre am>
    using namespace std;

    int main()
    {
    int count = 0; // number for changing which row gets printed to screen in what
    // pattern, change this to one to invert the pattern
    for(int row = 0;row<6;row++) // number of rows in a chess board (might want to
    { // change that 6 to whatever you need)
    if(count == 0)
    {
    for(int sqr=0;sqr<4;sqr ++) // square size row wise how many * will be under
    { // each other
    for(int col=0;col<6;col ++)// <-- number of collumns in chess board divided by
    { // 2 (dark and light)
    cout << " *****"; //<-- the output. if your square are going to be
    } // squares make sure the num of * and spaces are
    cout<<endl; // equivilant to the int number of variable sqr ^ above
    }
    }
    else // when count switches this changes the output to the following
    {
    for(int sqr=0;sqr<4;sqr ++)
    {
    for(int col=0;col<6;col ++)
    {
    cout << "***** ";
    }
    cout<<endl;
    }
    }
    count++;
    if(count == 2)
    count = 0; // keeps count 0 or 1
    }
    system("pause") ;

    return 0;
    }

    sorry if this is comments are kind of long but i don't know what level of C++ you understand.

    Comment

    • vninja
      New Member
      • Oct 2006
      • 40

      #3
      here is my output:

      ***** ***** ***** ***** ***** *****
      ***** ***** ***** ***** ***** *****
      ***** ***** ***** ***** ***** *****
      ***** ***** ***** ***** ***** *****
      ***** ***** ***** ***** ***** *****
      ***** ***** ***** ***** ***** *****
      ***** ***** ***** ***** ***** *****
      ***** ***** ***** ***** ***** *****
      ***** ***** ***** ***** ***** *****
      ***** ***** ***** ***** ***** *****
      ***** ***** ***** ***** ***** *****
      ***** ***** ***** ***** ***** *****
      ***** ***** ***** ***** ***** *****
      ***** ***** ***** ***** ***** *****
      ***** ***** ***** ***** ***** *****
      ***** ***** ***** ***** ***** *****
      ***** ***** ***** ***** ***** *****
      ***** ***** ***** ***** ***** *****
      ***** ***** ***** ***** ***** *****
      ***** ***** ***** ***** ***** *****
      ***** ***** ***** ***** ***** *****
      ***** ***** ***** ***** ***** *****
      ***** ***** ***** ***** ***** *****
      ***** ***** ***** ***** ***** *****

      Comment

      • vninja
        New Member
        • Oct 2006
        • 40

        #4
        Here is my output (let me try again)
        ***** *****
        ***** *****
        ***** *****
        ***** *****
        ***** *****
        ***** *****
        ***** *****
        ***** *****
        ***** *****
        ***** *****
        ***** *****
        ***** *****
        looks good on my post and on my screen but don't know if posting will cause formatting errors

        Comment

        • punkybrewster
          New Member
          • Oct 2006
          • 11

          #5
          Thank you so much for your response. I will try and run the program again tonight to see what happen and let you know.

          Comment

          Working...