New to this board...
I 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:
Check to see if five of the same letter is found vertically, horizontally or diagonals...and report...
Code:
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main()
{
char board[5] [5];
srand(time(0));
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
board[i] [j] = static_cast<char>('a' + rand() % ('z' - 'a' + 1));
cout << setw (3) << board[i] [j];
}
cout << endl;
}
// Check rows
for (int i = 0; i < 5; i++)
{
bool same = true;
for (int j = 1; j < 5; j++)
{
if (board[i] [0] != board[i] [j])
{
same = false; break;
}
}
if (same) cout << "All " << board[i] [0] << "'s on row " << i << endl;
}
// Check columns
for (int j = 0; j < 5; j++)
{
bool same = true;
for (int i = 1; i < 5; i++)
{
if (board[0] [j] != board[i] [j])
{
same = false; break;
}
}
if (same) cout << "All " << board[0] [j] << "'s on column " << j << endl;
}
// Check major diagonal
bool same = true;
for (int i = 1; i < 5; i++)
{
if (board[0] [0] == board[i] [i])
{
same = false; break;
}
}
if (same) cout << "All " << board[0] [0] << "'s on major diagonal" << endl;
// Check subdiagonal
same = true;
for (int i = 1; i < 5; i++)
{
if (board[0] [4] == board[i] [4 - i])
{
same = false; break;
}
}
if (same) cout << "All " << board[0] [0] << "'s on subdiagonal" << endl;
/* Scaffolding code for testing purposes */
cin.ignore(256, '\n');
cout << "Press ENTER to continue..." << endl;
cin.get();
/* End Scaffolding */
return 0;
}
It does random place value into a 5 by 5 array however when I check it is wrong:
Any insight would be appreciated...Thanks
2 Sample Runs:
m q h o q
n k t f d
l n d m q
e y r n n
v v h l k
All m's on major diagonal
All m's on subdiagonal
w g j m g
h a k u s
i v b v i
b e s i r
d i q a i
All w's on major diagonal
All w's on subdiagonal