Thread: Random Checkerboard Help

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    39

    Random Checkerboard Help

    have a C++ book I am currently looking at and trying to make some modifications...

    Here is the code I modified from C++ text I have:
    Have a checkerboard of random 0s and 1s and check to see if of the same number is found vertically, horizontally or diagonals...

    diagonals apparently do not work..
    Runs:
    Code:
    11101001
    01100101
    11110110
    10000011
    11111111
    11001010
    01001100
    11001001
    All 1's on row 4
    No same numbers on the same column
    No same numbers on the same diagonal
    No same numbers on the same subdiagonal
    
    11011011
    00000001
    00000011
    11110001
    11100111
    10000001
    10110101
    01001101
    All 1's on column 7
    All 1's on sub-diagonal
    No same numbers on the same row
    No same numbers on the same diagonal
    The above is wrong...

    Here is the code...I cant see the problem...thanks for help

    Code:
    #include <iostream>
    #include <ctime> // for time function
    #include <cstdlib> // for rand and srand functions
    using namespace std;
    
    int main()
    {
      srand(time(0));
      int board[8][8];
    
      bool isSameOnARow = false, isSameOnAColumn = false,
           isSameOnADiagonal = false, isSameOnASubdiagonal = false;
    
      for (int i = 0; i < 8; i++)
      {
        for (int j = 0; j < 8; j++)
        {
          board[i] [j] = rand() % 2;
          cout << board[i] [j];
        }
    
        cout << endl;
      }
    
      // Check rows
      for (int i = 0; i < 8; i++)
      {
        bool same = true;
        for (int j = 1; j < 8; j++)
        {
          if (board[i][0] != board[i] [j])
          {
            same = false; break;
          }
        }
    
        if (same) {
          cout << "All " << board[i] [0] << "'s on row " << i << endl;
          isSameOnARow = true;
        }
      }
    
      // Check columns
      for (int j = 0; j < 8; j++)
      {
        bool same = true;
        for (int i = 1; i < 8; i++)
        {
          if (board[0] [j] != board[i] [j])
          {
            same = false; break;
          }
    
        }
        if (same) {
          cout << "All " << board[0] [j] << "'s on column " << j << endl;
          isSameOnAColumn = true;
        }
      }
    
      // Check major diagonal
      bool same = true;
      for (int i = 1; i < 8; i++)
      {
        if (board[0] [0] == board[i] [i])
        {
          same = false; break;
        }
      }
    
      if (same) {
        cout << "All " << board[0] [0] << "'s on major diagonal" << endl;
        isSameOnADiagonal = true;
      }
    
      // Check subdiagonal
      same = true;
      for (int i = 1; i < 8; i++)
      {
        if (board[0] [7] == board[i] [7 - i])
        {
          same = false; break;
        }
      }
    
      if (same) {
        cout << "All " << board[0][0] << "'s on sub-diagonal" << endl;
        isSameOnASubdiagonal = true;
      }
    
      if (!isSameOnARow) {
        cout << "No same numbers on the same row" << endl;
      }
    
      if (!isSameOnAColumn) {
        cout << "No same numbers on the same column" << endl;
      }
    
      if (!isSameOnADiagonal) {
        cout << "No same numbers on the same diagonal" << endl;
      }
    
      if (!isSameOnASubdiagonal) {
        cout << "No same numbers on the same subdiagonal" << endl;
      }
    /* Scaffolding code for testing purposes */
    cin.ignore(256, '\n');
    cout << "Press ENTER to continue..." << endl;
    cin.get();
    /* End Scaffolding */ 
      return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Compare your if() statement with one which works, with one that doesn't.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM