//Four In a Row

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <ctime>

using namespace std;

// global constants
const char X = 'X';
const char O = 'O';
const char EMPTY = ' ';
const char TIE = 'T';
const char NO_ONE = 'N';
const int WINNING_ROWS[69][4] = { 
                                     {0, 6, 12, 18},
                                     {1, 7, 13, 19},
                                     {2, 8, 14, 20},
                                     {3, 9, 15, 21},
                                     {4, 10, 16, 22},
                                     {5, 11, 17, 23},
                                     {6, 12 ,18, 24},
                                     {7, 13, 19, 25},
                                     {8, 14, 20, 26},
                                     {9, 15, 21, 27},
                                     {10, 16, 22, 28}, 
                                     {11, 17, 23, 29},
                                     {12, 18, 24, 30},
                                     {13, 19, 25, 31},
                                     {14, 20, 26, 32},
                                     {15, 21, 27, 33},
                                     {16, 22, 28, 34},
                                     {17, 23, 29, 35},
                                     {18, 24, 30, 36},
                                     {19, 25, 31, 37},
                                     {20, 26, 32, 38},
                                     {21, 27, 33, 39},
                                     {22, 28, 34, 40},
                                     {23, 29, 35, 41},
                                     {0, 1, 2, 3},
                                     {1, 2, 3, 4},
                                     {2, 3, 4, 5},
                                     {6, 7, 8, 9},
                                     {7, 8, 9, 10},
                                     {8, 9, 10, 11},
                                     {12, 13, 14, 15},
                                     {13, 14, 15, 16},
                                     {14, 15, 16, 17},
                                     {18, 19, 20, 21},
                                     {19, 20, 21, 22},
                                     {20, 21, 22, 23},
                                     {24, 25, 26, 27},
                                     {25, 26, 27, 28},
                                     {26, 27, 28, 29},
                                     {30, 31, 32, 33},
                                     {31, 32, 33, 34},
                                     {32, 33, 34, 35},
                                     {36, 37, 38, 39},
                                     {37, 38, 39, 40},
                                     {38, 39, 40, 41},
                                     {0, 7, 14, 21},
                                     {1, 8, 15, 22},
                                     {2, 9, 16, 23},
                                     {6, 13, 20, 27},
                                     {7, 14, 21, 28},
                                     {8, 15, 22, 29},
                                     {12, 19, 26, 33},
                                     {13, 20, 27, 34},
                                     {14, 21, 28, 35},
                                     {18, 25, 32, 39},
                                     {19, 26, 33, 40},
                                     {20, 27, 34, 41},
                                     {3, 8, 13, 18},
                                     {4, 9, 14, 19},
                                     {5, 10, 15, 20},
                                     {9, 14, 19, 24},
                                     {10, 15, 20, 25},
                                     {22, 27, 32, 37},
                                     {15, 20, 25, 30},
                                     {16, 21, 26, 31},
                                     {17, 22, 27, 32},
                                     {21, 26, 31, 36},
                                     {22, 27, 32, 37},
                                     {23, 28, 33, 38},
                                     };
    const int TOTAL_ROWS = 69;

// function prototypes
char winner(const vector<char>& allColumns); 


int main()
{
    int column; char playerTurn; char playerPiece;  char computerPiece; int row; 

    vector<char> column1(6, EMPTY);
    vector<char> column2(6, EMPTY);
    vector<char> column3(6, EMPTY);
    vector<char> column4(6, EMPTY);
    vector<char> column5(6, EMPTY);
    vector<char> column6(6, EMPTY);
    vector<char> column7(6, EMPTY);
    vector<char> allColumns(42, EMPTY);
   
         
    {
cout << "Welcome to Four In a Row\n\n";
    cout << "The purpose of this game is to prove that I'm smarter than you.\n";
    cout << "Choose a column by entering a number, 1 - 7. See illustration.\n\n";
    
    cout << "         1   2   3   4   5   6   7 \n";   
    cout << "       -----------------------------\n";
    cout << "       |   |   |   |   |   |   |   |\n";
    cout << "       -----------------------------\n";
    cout << "       |   |   |   |   |   |   |   |\n";
    cout << "       -----------------------------\n";
    cout << "       |   |   |   |   |   |   |   |\n";
    cout << "       -----------------------------\n";
    cout << "       |   |   |   |   |   |   |   |\n";
    cout << "       -----------------------------\n";
    cout << "       |   |   |   |   |   |   |   |\n";
    cout << "       -----------------------------\n";
    cout << "       |   |   |   |   |   |   |   |\n";
    cout << "       -----------------------------\n\n";

    cout << "Prepare to lose. Ha ha ha.\n";
}

    do
    {
        cout << "\nI suggest you go first, cause you're gonna fucking lose. (y/n): ";
        cin >> playerTurn;
        cout << "\n";
        if (playerTurn != 'y' && playerTurn != 'n')
        {cout << "\nYyeeeeeessss oooorr Nnoooooo??? Don't you understand plain English???";}
    } while (playerTurn != 'y' && playerTurn != 'n');
    
    do
    {
        cout << "\nSo, do you want to be X or O ??? (X/O)(uppercase): ";
        cin >> playerPiece;
        if (playerPiece != 'X' && playerPiece != 'O')
        {cout << "\nWhat the fuck??? X or O. Bone head.";}
    } while (playerPiece != 'X' && playerPiece != 'O');

    if (playerPiece == 'X')
    {computerPiece = 'O';}
    if (playerPiece == 'O')
    {computerPiece = 'X';}
               
    cout << "\n\n"  <<   "1   2   3   4   5   6   7 \n";   
    cout << "\n" <<  " -----------------------------\n";
    cout << "\n" <<  "  | "<< column1[5]<<" | "<<column2[5]<<" | "<<column3[5]<<" | "<<column4[5]<<" | "<<column5[5]<<" | "<<column6[5]<<" | "<<column7[5]<<" | \n" ;
    cout << "\n" <<  " -----------------------------\n";
    cout << "\n" <<  "  | "<< column1[4]<<" | "<<column2[4]<<" | "<<column3[4]<<" | "<<column4[4]<<" | "<<column5[4]<<" | "<<column6[4]<<" | "<<column7[4]<<" | \n";
    cout << "\n" <<  " -----------------------------\n";
    cout << "\n" <<  "  | "<< column1[3]<<" | "<<column2[3]<<" | "<<column3[3]<<" | "<<column4[3]<<" | "<<column5[3]<<" | "<<column6[3]<<" | "<<column7[3]<<" | \n";
    cout << "\n" <<  " -----------------------------\n";
    cout << "\n" <<  "  | "<< column1[2]<<" | "<<column2[2]<<" | "<<column3[2]<<" | "<<column4[2]<<" | "<<column5[2]<<" | "<<column6[2]<<" | "<<column7[2]<<" | \n";
    cout << "\n" <<  " -----------------------------\n";
    cout << "\n" <<  "  | "<< column1[1]<<" | "<<column2[1]<<" | "<<column3[1]<<" | "<<column4[1]<<" | "<<column5[1]<<" | "<<column6[1]<<" | "<<column7[1]<<" | \n";
    cout << "\n" <<  " -----------------------------\n";
    cout << "\n" <<  "  | "<< column1[0]<<" | "<<column2[0]<<" | "<<column3[0]<<" | "<<column4[0]<<" | "<<column5[0]<<" | "<<column6[0]<<" | "<<column7[0]<<" | \n";
    cout << "\n" <<  " -----------------------------\n\n";

    while (winner(allColumns) == NO_ONE)
    {
        if (playerTurn == 'y')
        {
            do
            {cout << "\nChoose a column. (1-7):";
            cin >> column;
            if (column > 7 || column < 1)
            {cout << "\nThere's no " << column << "Why are you so stupid???";} 
        } while (column > 7 || column < 1);
         
         
        {if (column == 1 && column1[5] == EMPTY)
        {
        for (row = 0 ; column1[row]!= EMPTY; ++row){}
        column1[row] = playerPiece; }
        
        if (column == 2 && column2[5] == EMPTY)
        {
        for (row = 0 ; column2[row]!= EMPTY; ++row){}
        column2[row] = playerPiece; }
        
        if (column == 3 && column3[5] == EMPTY)
        {
        for (row = 0 ; column3[row]!= EMPTY; ++row){}
        column3[row] = playerPiece; }
        
        if (column == 4 && column4[5] == EMPTY)
        {
        for (row = 0 ; column4[row]!= EMPTY; ++row){}
        column4[row] = playerPiece; }
        
        if (column == 5 && column5[5] == EMPTY)
        {
        for (row = 0 ; column5[row]!= EMPTY; ++row){}
        column5[row] = playerPiece; }
        
        if (column == 6 && column6[5] == EMPTY)
        {
        for (row = 0 ; column6[row]!= EMPTY; ++row){}
        column6[row] = playerPiece; }
        
        if (column == 7 && column7[5] == EMPTY)
        {
        for (row = 0 ; column7[row]!= EMPTY; ++row){}
        column7[row] = playerPiece; }
        else
        {cout << "\n\nThat column is already filled. Can't you see??? Idiot."; column = 0;}
        }
        
        allColumns[0] = column1[0];
    allColumns[1] = column1[1];
    allColumns[2] = column1[2];
    allColumns[3] = column1[3];
    allColumns[4] = column1[4];
    allColumns[5] = column1[5];
    allColumns[6] = column2[0];
    allColumns[7] = column2[1];
    allColumns[8] = column2[2];
    allColumns[9] = column2[3];
    allColumns[10] = column2[4];
    allColumns[11] = column2[5];
    allColumns[12] = column3[0];
    allColumns[13] = column3[1];
    allColumns[14] = column3[2];
    allColumns[15] = column3[3];
    allColumns[16] = column3[4];
    allColumns[17] = column3[5];
    allColumns[18] = column4[0];
    allColumns[19] = column4[1];
    allColumns[20] = column4[2];
    allColumns[21] = column4[3];
    allColumns[22] = column4[4];
    allColumns[23] = column4[5];
    allColumns[24] = column5[0];
    allColumns[25] = column5[1];
    allColumns[26] = column5[2];
    allColumns[27] = column5[3];
    allColumns[28] = column5[4];
    allColumns[29] = column5[5];
    allColumns[30] = column6[0];
    allColumns[31] = column6[1];
    allColumns[32] = column6[2];
    allColumns[33] = column6[3];
    allColumns[34] = column6[4];
    allColumns[35] = column6[5];
    allColumns[36] = column7[0];
    allColumns[37] = column7[1];
    allColumns[38] = column7[2];
    allColumns[39] = column7[3];
    allColumns[40] = column7[4];
    allColumns[41] = column7[5];}
        
        
        
        else
        { char changed = 'n';
            while (changed == 'n')
            {srand(time(0));
        column = rand() % 7 + 1;
        char changed;
         
        if (column == 1 && column1[5] == EMPTY)
        {
        for (row = 0 ; column1[row]!= EMPTY; ++row){}
        column1[row] = computerPiece;
        changed = 'y'; }
        
        if (column == 2&& column2[5] == EMPTY)
        {
        for (row = 0 ; column2[row]!= EMPTY; ++row){}
        column2[row] = computerPiece;
        changed = 'y'; }
        
        if (column == 3&& column3[5] == EMPTY)
        {
        for (row = 0 ; column3[row]!= EMPTY; ++row){}
        column3[row] = computerPiece;
        changed = 'y'; }
        
        if (column == 4&& column4[5] == EMPTY)
        {
        for (row = 0 ; column4[row]!= EMPTY; ++row){}
        column4[row] = computerPiece; 
        changed = 'y';}
        
        if (column == 5&& column5[5] == EMPTY)
        {
        for (row = 0 ; column5[row]!= EMPTY; ++row){}
        column5[row] = computerPiece; 
        changed = 'y';}
        
        if (column == 6&& column6[5] == EMPTY)
        {
        for (row = 0 ; column6[row]!= EMPTY; ++row){}
        column6[row] = computerPiece; 
        changed = 'y';}
        
        if (column == 7&& column6[5] == EMPTY)
        {
        for (row = 0 ; column7[row]!= EMPTY; ++row){}
        column7[row] = computerPiece; 
        changed = 'y';}
        allColumns[0] = column1[0];
    allColumns[1] = column1[1];
    allColumns[2] = column1[2];
    allColumns[3] = column1[3];
    allColumns[4] = column1[4];
    allColumns[5] = column1[5];
    allColumns[6] = column2[0];
    allColumns[7] = column2[1];
    allColumns[8] = column2[2];
    allColumns[9] = column2[3];
    allColumns[10] = column2[4];
    allColumns[11] = column2[5];
    allColumns[12] = column3[0];
    allColumns[13] = column3[1];
    allColumns[14] = column3[2];
    allColumns[15] = column3[3];
    allColumns[16] = column3[4];
    allColumns[17] = column3[5];
    allColumns[18] = column4[0];
    allColumns[19] = column4[1];
    allColumns[20] = column4[2];
    allColumns[21] = column4[3];
    allColumns[22] = column4[4];
    allColumns[23] = column4[5];
    allColumns[24] = column5[0];
    allColumns[25] = column5[1];
    allColumns[26] = column5[2];
    allColumns[27] = column5[3];
    allColumns[28] = column5[4];
    allColumns[29] = column5[5];
    allColumns[30] = column6[0];
    allColumns[31] = column6[1];
    allColumns[32] = column6[2];
    allColumns[33] = column6[3];
    allColumns[34] = column6[4];
    allColumns[35] = column6[5];
    allColumns[36] = column7[0];
    allColumns[37] = column7[1];
    allColumns[38] = column7[2];
    allColumns[39] = column7[3];
    allColumns[40] = column7[4];
    allColumns[41] = column7[5];}
        
        
        cout << "\n\nI shall take the " << column <<"th column."; 
        }
        
        if (playerTurn == 'y')
        {playerTurn == 'n';}
        if (playerTurn == 'n')
        {playerTurn == 'y';}
        
    cout << "\n\n"  <<   "1   2   3   4   5   6   7 \n";   
    cout << "\n" <<  "-----------------------------\n";
    cout << "\n" <<  " | "<< column1[5]<<" | "<<column2[5]<<" | "<<column3[5]<<" | "<<column4[5]<<" | "<<column5[5]<<" | "<<column6[5]<<" | "<<column7[5]<<" | \n" ;
    cout << "\n" <<  "-----------------------------\n";
    cout << "\n" <<  " | "<< column1[4]<<" | "<<column2[4]<<" | "<<column3[4]<<" | "<<column4[4]<<" | "<<column5[4]<<" | "<<column6[4]<<" | "<<column7[4]<<" | \n";
    cout << "\n" <<  "-----------------------------\n";
    cout << "\n" <<  " | "<< column1[3]<<" | "<<column2[3]<<" | "<<column3[3]<<" | "<<column4[3]<<" | "<<column5[3]<<" | "<<column6[3]<<" | "<<column7[3]<<" | \n";
    cout << "\n" <<  "-----------------------------\n";
    cout << "\n" <<  " | "<< column1[2]<<" | "<<column2[2]<<" | "<<column3[2]<<" | "<<column4[2]<<" | "<<column5[2]<<" | "<<column6[2]<<" | "<<column7[2]<<" | \n";
    cout << "\n" <<  "-----------------------------\n";
    cout << "\n" <<  " | "<< column1[1]<<" | "<<column2[1]<<" | "<<column3[1]<<" | "<<column4[1]<<" | "<<column5[1]<<" | "<<column6[1]<<" | "<<column7[1]<<" | \n";
    cout << "\n" <<  "-----------------------------\n";
    cout << "\n" <<  " | "<< column1[0]<<" | "<<column2[0]<<" | "<<column3[0]<<" | "<<column4[0]<<" | "<<column5[0]<<" | "<<column6[0]<<" | "<<column7[0]<<" | \n";
    cout << "\n" <<  "-----------------------------\n\n";
    }
    
    if (winner(allColumns) == computerPiece)
    {
        cout << winner << "'s won!\n";
        cout << "That was so easy. I've never met someone so stupid. What a loser.";
    }

    if (winner(allColumns) == computerPiece)
    {
        cout << winner << "'s won!\n";
        cout << "Huh. You wouldn't have won if I haven't let you.";
    }

    else
    {
        cout << "It's a tie.\n";
        cout << "Huh. You think you're so good??? I'll beat you next time.";
    }
     system("pause");
    return 0;
}
// functions

char winner(const vector<char>& allColumns)
{
      
  
    // if any winning row has three values that are the same (and not EMPTY),
    // then we have a winner
    for(int row = 0; row < TOTAL_ROWS; ++row)
    {
        if ( (allColumns[WINNING_ROWS[row][0]] != EMPTY) &&
             (allColumns[WINNING_ROWS[row][0]] == allColumns[WINNING_ROWS[row][1]]) &&
             (allColumns[WINNING_ROWS[row][1]] == allColumns[WINNING_ROWS[row][2]]) &&
             (allColumns[WINNING_ROWS[row][2]] == allColumns[WINNING_ROWS[row][3]]))
        {
            return allColumns[WINNING_ROWS[row][0]];
        }
    }

    // since nobody has won, check for a tie (no empty squares left)
    if (count(allColumns.begin(), allColumns.end(), EMPTY) == 0)
        return TIE;

    // since nobody has won and it isn't a tie, the game ain't over
    return NO_ONE;
}

when I compiled it, it said:

[Warning] the address of `char winner(const std::vector<char, std::allocator<char> >&)', will always evaluate as `true'

can you please help.
Thank You very much

Recommended Answers

All 6 Replies

I'm assuming that warning came with a line number?

> will always evaluate as `true'
It means that if you've done something like

if ( anExpressionWhichWarnsOfAlwaysBeingTrue ) {
} else {
  // do something important
}

Then it's telling you that something important isn't going to happen.

Oh, and split main () up into more functions.

I'm assuming that warning came with a line number?

> will always evaluate as `true'
It means that if you've done something like

if ( anExpressionWhichWarnsOfAlwaysBeingTrue ) {
} else {
  // do something important
}

Then it's telling you that something important isn't going to happen.

Oh, and split main () up into more functions.

Yes the warning came with a line number.
383 and 389

cout << winner << "'s won!\n";
[Warning] the address of `char winner(const std::vector<char, std::allocator<char> >&)', will always evaluate as `true'

what do you mean by "split main () up into more functions."

> cout << winner << "'s won!\n";
If you were hoping to call this function, then you need cout << winner(someParameter) << "'s won!\n"; > what do you mean by "split main () up into more functions."
I mean, it's 300 or so lines long and it just waders from one aspect of the problem to the next.
My personal rule of thumb is that if you can't see the start { and end } of the whole screen at once, then it's time to think about splitting the function into something more manageable.

In my book it said if you use function, it would make the program runs slower isn't it?

Thank You for your reply. Thank you for your suggestions.

In my book it said if you use function, it would make the program runs slower isn't it?

That's not something you have to worry about. If you were using a 15 year old PC and your program was x-times bigger, then it would become an issue.

> cout << winner << "'s won!\n";
If you were hoping to call this function, then you need cout << winner(someParameter) << "'s won!\n"; > what do you mean by "split main () up into more functions."
I mean, it's 300 or so lines long and it just waders from one aspect of the problem to the next.
My personal rule of thumb is that if you can't see the start { and end } of the whole screen at once, then it's time to think about splitting the function into something more manageable.

i still doesn't get it work.

when i play it, it said the row is already fill.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.