Tic Tac Toe Winner

Afternoon Gents, I've been using this website for reference ever since I began C++ 4 months back. While I was coding for a tic tac toe game I ran into somewhat of a redundancy problem that hopefully you will be able to assist me with.

I've set up a game using an array that the "users" input the X's and O's. I run into problems when creating the METHOD of verifying who won. My current level of knowledge is using Loops, Array, and Functions.

The code so far:

#include <iostream>
using namespace std;


const int COLUMN_SIZE = 3, ROW_SIZE = 3;
char letter1 = 'X', letter2 = 'O';
int winner = 0;

void printArray( char[ ][ COLUMN_SIZE ] );
void player1( char[ ][ COLUMN_SIZE ] );
void player2( char[ ][ COLUMN_SIZE ] );


int main ( )
{
char ticTac[ ROW_SIZE ][ COLUMN_SIZE ]= {
{ 4, 4, 4 },
{ 4, 4, 4 },
{ 4, 4, 4 } };

while ( winner != 1 )
{
printArray( ticTac );
cout << endl;

player1( ticTac );
cout << endl;


printArray( ticTac );
cout << endl;

player2( ticTac );
cout << endl;
}
}

void printArray ( char ticTac[][ COLUMN_SIZE ] )
{
cout << " -----------" << endl;
for ( int x = 0; x < ROW_SIZE; x++ )
{
cout << "| ";
for ( int y = 0; y < COLUMN_SIZE; y++ )
{
cout << ticTac[ x ][ y ] << " | ";
}
cout << endl;
}
cout << " -----------" << endl;
}

void player1( char ticTac[ ][ COLUMN_SIZE ] )
{
int row, column;
cout << "Player1. Place your X. Row/Column \n";
cin >> row >> column;

row -= 1;
column -= 1;

ticTac [ row ][ column ] = letter1;
}

void player2( char ticTac[ ][ COLUMN_SIZE ])
{
int row, column;
cout << "Player2. Place your O. Row/Column \n";
cin >> row >> column;

row -= 1;
column -= 1;

ticTac [ row ][ column ] = letter2;
}


/*Player1. Place your X. Row/Column
1
1

-----------
| X | ♦ | ♦ |
| ♦ | ♦ | ♦ |
| ♦ | ♦ | ♦ |
-----------

Player2. Place your O. Row/Column
2
3

-----------
| X | ♦ | ♦ |
| ♦ | ♦ | O |
| ♦ | ♦ | ♦ |
-----------

Player1. Place your X. Row/Column
1
2

-----------
| X | X | ♦ |
| ♦ | ♦ | O |
| ♦ | ♦ | ♦ |
-----------

Player2. Place your O. Row/Column*/


END CODE.



Is it absolutely necessary to use a combination of loops and if statements, to add up a line of indices to match the literal value ( X or O ) * 3 to find the winner?

for example,

//in a player function
int bool isWinner = ( ticTac[ 0 ] [ 1 ] + ticTac[ 0 ] [ 2 ] + ticTac[ 0 ] [ 3 ]
== 264 //for 'X * 3'
|| ticTac [ 0 ] [ 1 ] + ticTac[ 1 ][ 1 ] ...


Or is there a less repetitive verification?
( ticTac[0][1] == ticTac[0][2] ) && ( ticTac[0][2] == ticTac[0][3] ) && ( ticTac[0][3] != 4 )

is probably the most efficient way. if the expression is true, then the player whose token
is in any of the three positions is the winner.

You have to repeat the above checks for all 8 ways to win.

Alternatively, I would put all the index triples in an array and walk the array, checking each one.
Then you only have to write the above line once instead of 8 times.
Thanks for the help. I'll use your code alongside loops to check for horizontal/vertical hits.

Then I suppose ( ticTac[0] [ 2 ] == ticTac[ 1 ][ 1 ] ) && ( ticTac[ 1 ] [ 1 ] == ticTac [ 2 ] [ 0 ] )
&& ticTac[ 0 ] [ 2 ] != 4 ) // & the opposite for the other hit.

Again, thanks.
Topic archived. No new replies allowed.