Tic Tac Toe

winner, looser, or tie: problems concerning 2D array

Page 1 of 1

5 Replies - 2379 Views - Last Post: 10 January 2009 - 05:01 PM Rate Topic: -----

#1 cbc123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 10-January 09

Tic Tac Toe

Post icon  Posted 10 January 2009 - 01:42 PM

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.
#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;
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Tic Tac Toe

#2 Psionics  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 158
  • Joined: 06-September 08

Re: Tic Tac Toe

Posted 10 January 2009 - 02:22 PM

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:

	// 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
Was This Post Helpful? 0
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Tic Tac Toe

Posted 10 January 2009 - 02:30 PM

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

http://www.dreaminco...h...=15&t=41074
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5074
  • View blog
  • Posts: 13,697
  • Joined: 18-April 07

Re: Tic Tac Toe

Posted 10 January 2009 - 02:32 PM

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...

// 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!" :snap:
Was This Post Helpful? 0
  • +
  • -

#5 cbc123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 10-January 09

Re: Tic Tac Toe

Posted 10 January 2009 - 04:51 PM

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.
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)

Was This Post Helpful? 0
  • +
  • -

#6 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Tic Tac Toe

Posted 10 January 2009 - 05:01 PM

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 January 2009 - 05:02 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1