C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 300,497 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,854 people online right now. Registration is fast and FREE... Join Now!




Tic Tac Toe

 

Tic Tac Toe, winner, looser, or tie: problems concerning 2D array

cbc123

10 Jan, 2009 - 12:42 PM
Post #1

New D.I.C Head
*

Joined: 10 Jan, 2009
Posts: 30


My Contributions
I having trouble with finding out the winner, looser or tie. I did some if statments,but none worked. I'd appreciate any tips, thanks, kyle.
CODE
#include <iostream>
#include <iomanip>
using namespace std;

const int ROW = 3;
const int COL = 3;

void showGrid(char grid[][COL], int);


int main()
{ char grid[ROW][COL] =    {{'*', '*', '*'},
                            {'*', '*', '*'},
                            {'*', '*', '*'}};
    int rows, cols;
    int el = 1;
    
cout << " TIC TAC TOE" << endl;


do {
showGrid(grid, ROW);

cout << "\nPlayer X, Enter the row and column:";
cin >> rows >> cols;
if (grid[rows-1][cols-1] != '*')
{cout << "\nThis space is taken... Enter the row and column:\n";
cin >> rows >> cols; }
else
grid[rows-1][cols-1] = 'X';
showGrid(grid, ROW);

cout << "\nPlayer O, Enter the row and column:";
cin >> rows >> cols;
if (grid[rows-1][cols-1] != '*')
{cout << "This space is taken... Enter the row and column:\n";
cin >> rows >> cols; }
else
grid[rows-1][cols-1] = 'O';


/*if(grid[0][0] =='X'&& grid[0][1]=='X' && grid[0][2]=='X')
{         cout << "Player X wins this game.";    
                  

}*/


}
while(el = 1);


showGrid(grid, ROW);

return 0;

}


void showGrid(char array[][COL], int numRows)
{
    for (int row = 0; row < numRows; row++)
    {   for (int col = 0; col < COL; col++)
        {
            cout << setw(2) << array[row][col] << " ";
        }
        cout << endl;
    }
}


User is offlineProfile CardPM
+Quote Post


Psionics

RE: Tic Tac Toe

10 Jan, 2009 - 01:22 PM
Post #2

D.I.C Head
Group Icon

Joined: 6 Sep, 2008
Posts: 158



Thanked: 11 times
Dream Kudos: 200
My Contributions
Well there's a couple ways to check the winner, but typically you'll want to check horizontal wins, vertical wins, and then the two cross wins:

cpp

// check horizontal
for (int i = 0; i < 3; ++i)
{
if (theBoard[i][i] != EMPTY)
if (theBoard[i][0] == theBoard[i][1] && theBoard[i][1] == theBoard[i][2])
return true;
}

// check vertical
for (int i = 0; i < 3; ++i)
{
if (theBoard[i][i] != EMPTY)
if (theBoard[0][i] == theBoard[1][i] && theBoard[1][i] == theBoard[2][i])
return true;
}

// check right cross
if (theBoard[0][0] != EMPTY)
{
if (theBoard[0][0] == theBoard[1][1] && theBoard[1][1] == theBoard[2][2])
return true;
}

// check left cross
if (theBoard[0][2] != EMPTY)
{
if (theBoard[0][2] == theBoard[1][1] && theBoard[1][1] == theBoard[2][0])
return true;
}


Obviously EMPTY would be a #define somewhere, or whatever you have in your array
User is offlineProfile CardPM
+Quote Post

Bench

RE: Tic Tac Toe

10 Jan, 2009 - 01:30 PM
Post #3

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 847



Thanked: 64 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
Instead of repeating a message I posted a while ago, here's a link to a thread on the same topic

http://www.dreamincode.net/forums/index.ph...=15&t=41074
User is online!Profile CardPM
+Quote Post

Martyr2

RE: Tic Tac Toe

10 Jan, 2009 - 01:32 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,246



Thanked: 820 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
well the best way to think of this is that you are going to need some loops. A loop which checks a row inside a loop which loops through each row. Same with columns. Then you just need to check diagonals.

Each of these winning checks should be in its own function. Make three functions called "checkRows()" which will be used to check each row for three of a kind. Another called "checkColumns()" which will then check each column. Last "checkDiagonals()" to check the diagonals.

Here is how you might check your rows...

cpp

// Returns true if a win, false if not. playerChar is either 'X' or 'O'
bool checkRows(char grid[][3], char playerChar) {
// Loop through each row
for (int i = 0; i < 3; i++) {
// Count will keep track player chars in row
int count = 0;

// Loop through each column checking if char
// If it is, increment count
for (int j = 0; j < 3; j++) {
if (grid[i][j] == playerChar) { count++; }
}

// If we have 3, it means they have the row so they win.
if (count == 3) { return true; }
}

// Nope, no win for rows
return false;
}


So that will give you the general idea of how you can check for horizontal row wins for a player. You would pass it the grid after each player's turn and that player's char and it will check rows. So for checking for a win you will call all three checks: horizontal, vertical and diagonals.

This should get you started. Enjoy!

"At DIC we be tic tac toe winning code ninjas... in our version of war games, joshua always wins because we coded it!" decap.gif
User is offlineProfile CardPM
+Quote Post

cbc123

RE: Tic Tac Toe

10 Jan, 2009 - 03:51 PM
Post #5

New D.I.C Head
*

Joined: 10 Jan, 2009
Posts: 30


My Contributions
I've been at this project since early this morning and am starting to get eye bags, so please bear with me. I'm having trouble calling the the checkrows function it's self and am confused about where to call it. I don't have a lot of experience with bool types, so I've been gusing and checking. Heres what I got.
CODE
bool checkRows(char grid[][3], char);// checkrows protoype

//call checkrows to test for winner
if( checkRows(grid, ROW))
  cout <<"x is winner";
else
  cout <<"o is winner";


bool checkRows(char grid[][3], char playerChar)

User is offlineProfile CardPM
+Quote Post

Hyper

RE: Tic Tac Toe

10 Jan, 2009 - 04:01 PM
Post #6

Banned
*****

Joined: 15 Oct, 2008
Posts: 2,129



Thanked: 97 times
Dream Kudos: 425
My Contributions
Boolean data-type returns one of two things:
true and false

To call a function (regardless of being type int, void, char, bool, etc), you call it by name, EG: checkRows();
You can also pass it parameters, eg: checkRows(grid[][3], playerChar);

This post has been edited by Hyper: 10 Jan, 2009 - 04:02 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 11/8/09 04:55AM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month